aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorDrew DeVault <sir@cmpwn.com>2021-12-12 12:40:47 +0100
committerDrew DeVault <sir@cmpwn.com>2021-12-12 13:04:42 +0100
commite5ad22498ca893e96557486a808213b05601e25f (patch)
tree8929b40773af11371cb961379daa8cb3be22b8b2
parentAdd blend modes (diff)
downloadhare-chip8-e5ad22498ca893e96557486a808213b05601e25f.tar.gz
sdl2::mixer: initial commit
-rw-r--r--Makefile2
-rw-r--r--cmd/demo/main.ha7
-rw-r--r--sdl2/audio.ha31
-rw-r--r--sdl2/mixer/general.ha52
4 files changed, 91 insertions, 1 deletions
diff --git a/Makefile b/Makefile
index afbbfd2..c6cdb28 100644
--- a/Makefile
+++ b/Makefile
@@ -1,7 +1,7 @@
.POSIX:
.SUFFIXES:
-LIBS=-lc -lSDL2_image -lSDL2
+LIBS=-lc -lSDL2_image -lSDL2_mixer -lSDL2
demo:
hare build $(LIBS) cmd/demo
diff --git a/cmd/demo/main.ha b/cmd/demo/main.ha
index 081134a..73a5517 100644
--- a/cmd/demo/main.ha
+++ b/cmd/demo/main.ha
@@ -2,6 +2,7 @@ use fmt;
use sdl2;
use sdl2::{ controller_axis, event_type, renderer_flags, window_flags };
use sdl2::image;
+use sdl2::mixer;
use strings;
type object = struct {
@@ -34,6 +35,12 @@ fn run() (void | sdl2::error) = {
image::init(image::init_flags::PNG | image::init_flags::JPG);
defer image::quit();
+ mixer::init(mixer::init_flags::OGG)!;
+ defer mixer::quit();
+ mixer::open_audio(mixer::DEFAULT_FREQUENCY, mixer::DEFAULT_FORMAT,
+ mixer::DEFAULT_CHANNELS, 1024)!;
+ defer mixer::close_audio();
+
const win = sdl2::create_window("Hare SDL2 demo",
sdl2::WINDOWPOS_UNDEFINED, sdl2::WINDOWPOS_UNDEFINED,
640, 480, window_flags::NONE)?;
diff --git a/sdl2/audio.ha b/sdl2/audio.ha
new file mode 100644
index 0000000..42c4b95
--- /dev/null
+++ b/sdl2/audio.ha
@@ -0,0 +1,31 @@
+// Audio format flags.
+//
+// Current representation (unspecified bits are always zero):
+//
+// ++-----------------------sample is signed if set
+// ||
+// || ++-----------sample is bigendian if set
+// || ||
+// || || ++---sample is float if set
+// || || ||
+// || || || +---sample bit size---+
+// || || || | |
+// 15 14 13 12 11 10 09 08 07 06 05 04 03 02 01 00
+export type audio_format = u16;
+
+// Unsigned 8-bit samples
+export def AUDIO_U8: audio_format = 0x0008;
+// Signed 8-bit samples
+export def AUDIO_S8: audio_format = 0x8008;
+// Unsigned 16-bit samples, little-endian
+export def AUDIO_U16LSB: audio_format = 0x0010;
+// Signed 16-bit samples, little-endian
+export def AUDIO_S16LSB: audio_format = 0x8010;
+// Unsigned 16-bit samples, big-endian
+export def AUDIO_U16MSB: audio_format = 0x1010;
+// Signed 16-bit samples, big-endian
+export def AUDIO_S16MSB: audio_format = 0x9010;
+// Unsigned 16-bit samples
+export def AUDIO_U16: audio_format = AUDIO_U16LSB;
+// Signed 16-bit samples
+export def AUDIO_S16: audio_format = AUDIO_S16LSB;
diff --git a/sdl2/mixer/general.ha b/sdl2/mixer/general.ha
new file mode 100644
index 0000000..b87ac4e
--- /dev/null
+++ b/sdl2/mixer/general.ha
@@ -0,0 +1,52 @@
+use sdl2;
+
+// Flags for [[init]].
+export type init_flags = enum {
+ FLAC = 0x00000001,
+ MOD = 0x00000002,
+ MP3 = 0x00000008,
+ OGG = 0x00000010,
+ MID = 0x00000020,
+ OPUS = 0x00000040
+};
+
+// The default mixer has 8 simultaneous mixing channels
+export def CHANNELS: int = 8;
+
+// Good default frequency for a PC soundcard
+export def DEFAULT_FREQUENCY: int = 22050;
+
+// Good default channels for a PC soundcard
+export def DEFAULT_CHANNELS: int = 2;
+
+// XXX: This should choose MSB on a big-endian system:
+
+// Good default format for a PC soundcard
+export def DEFAULT_FORMAT: sdl2::audio_format = sdl2::AUDIO_S16LSB;
+
+@symbol("Mix_Init") fn _init(flags: int) int;
+
+// Loads dynamic libraries and prepares them for use. Flags should be
+// one or more flags from [[init_flags]] OR'd together.
+export fn init(flags: init_flags) (void | sdl2::error) = {
+ return sdl2::wrapvoid(_init(flags));
+};
+
+// Unloads libraries loaded with [[init]].
+export @symbol("Mix_Quit") fn quit() void;
+
+@symbol("Mix_OpenAudio") fn _openaudio(frequency: int,
+ format: u16, channels: int, chunksize: int) int;
+
+// Open the mixer with a certain audio format
+export fn open_audio(
+ frequency: int,
+ format: sdl2::audio_format,
+ channels: int,
+ chunksize: int,
+) (void | sdl2::error) = {
+ return sdl2::wrapvoid(_openaudio(frequency, format, channels, chunksize));
+};
+
+// Close the mixer, halting all playing audio
+export @symbol("Mix_CloseAudio") fn close_audio() void;