diff options
| author | Drew DeVault <sir@cmpwn.com> | 2021-12-12 13:56:23 +0100 |
|---|---|---|
| committer | Drew DeVault <sir@cmpwn.com> | 2021-12-12 13:56:23 +0100 |
| commit | eae63b218494d3aa077a9ea196abc4c5096e016b (patch) | |
| tree | b2bdd4c73685879aacecd1adc3ca962741267fee | |
| parent | sdl2::mixer: implement sample loading (diff) | |
| download | hare-chip8-eae63b218494d3aa077a9ea196abc4c5096e016b.tar.gz | |
sdl2::mixer: implement channel playback
| -rw-r--r-- | cmd/demo/main.ha | 1 | ||||
| -rw-r--r-- | sdl2/mixer/channels.ha | 20 | ||||
| -rw-r--r-- | sdl2/mixer/samples.ha | 6 |
3 files changed, 27 insertions, 0 deletions
diff --git a/cmd/demo/main.ha b/cmd/demo/main.ha index a5e9e68..e97c433 100644 --- a/cmd/demo/main.ha +++ b/cmd/demo/main.ha @@ -74,6 +74,7 @@ fn run() (void | fs::error | sdl2::error) = { let sample = mixer::load_file("sample.ogg")?; defer mixer::free_chunk(sample); + mixer::play_channel(0, sample, -1)?; let state = state { window = win, diff --git a/sdl2/mixer/channels.ha b/sdl2/mixer/channels.ha new file mode 100644 index 0000000..4bf4820 --- /dev/null +++ b/sdl2/mixer/channels.ha @@ -0,0 +1,20 @@ +use sdl2; + +@symbol("Mix_PlayChannelTimed") fn _play_channel_timed( + channel: int, + sample: *chunk, + loops: int, + ticks: int, +) int; + +// Play chunk on channel, or if channel is -1, pick the first free unreserved +// channel. The sample will play for loops+1 number of times, unless stopped by +// halt, or fade out, or setting a new expiration time of less time than it +// would have originally taken to play the loops, or closing the mixer. +export fn play_channel( + channel: int, + sample: *chunk, + loops: int, +) (void | sdl2::error) = { + return sdl2::wrapvoid(_play_channel_timed(channel, sample, loops, -1)); +}; diff --git a/sdl2/mixer/samples.ha b/sdl2/mixer/samples.ha index 49f1e1e..aaf1ad1 100644 --- a/sdl2/mixer/samples.ha +++ b/sdl2/mixer/samples.ha @@ -31,3 +31,9 @@ export fn load_file(src: str) (*chunk | fs::error | sdl2::error) = { // chunk after this without loading a new sample to it. Note: It's a bad idea to // free a chunk that is still being played... export @symbol("Mix_FreeChunk") fn free_chunk(chunk: *chunk) void; + +// Maximum volume for a chunk. +export def MAX_VOLUME: int = 128; // XXX: SDL_mixer derives this from SDL_MIX_MAXVOLUME + +// Sets the chunk volume as specified, returning the previous value. +export @symbol("Mix_VolumeChunk") fn volume_chunk(chunk: *chunk, volume: int) int; |
