diff --git a/core/Interfaces.carp b/core/Interfaces.carp index 52cbca34..c00b05da 100644 --- a/core/Interfaces.carp +++ b/core/Interfaces.carp @@ -55,3 +55,9 @@ (if (< a b) a b)) (defn id [x] x) + +(defn null? [p] + (Pointer.eq NULL (the (Ptr t) p))) + +(defn not-null? [p] + (not (null? p))) diff --git a/core/SDL_mixer.carp b/core/SDL_mixer.carp index 508ad0be..674d97f6 100644 --- a/core/SDL_mixer.carp +++ b/core/SDL_mixer.carp @@ -7,13 +7,65 @@ (defmodule Mixer - (register default-format - Int - "MIX_DEFAULT_FORMAT") - - ;; freq format channels chunksize + ;; Setup (register open-audio - (Fn [Int Int Int Int] ()) + ;; freq format channels chunksize + (Fn [Int Int Int Int] Int) "Mix_OpenAudio") + (register default-format Int "MIX_DEFAULT_FORMAT") + + (register ogg-support Int "MIX_INIT_OGG") + (register mp3-support Int "MIX_INIT_MP3") + (register mod-support Int "MIX_INIT_MOD") + (register flac-support Int "MIX_INIT_FLAC") + + (register init (Fn [Int] Int) "Mix_Init") + (register quit (Fn [Int] Int) "Mix_Quit") + + ;; Loading + (register load-wav + (Fn [(Ptr Char)] (Ptr Mix_Chunk)) + "Mix_LoadWAV") + + (register load-music + (Fn [(Ptr Char)] (Ptr Mix_Music)) + "Mix_LoadMUS") + + ;; Playing samples (Mix_Chunk:s) + (register play-channel + ;; args: channel chunk loops + ;; ret: channel + (Fn [Int (Ptr Mix_Chunk) Int] Int) + "Mix_PlayChannel") + + (register channel-playing? + (Fn [Int] Bool) + "Mix_Playing") + + ;; Music + (register nr-of-music-decoders + (Fn [] Int) + "Mix_GetNumMusicDecoders") + + ;; This function seems flakey, returns NULL? + (register get-music-decoder + (Fn [Int] (Ptr Char)) + "Mix_GetMusicDecoder") + + (register play-music + ;; args: music loops + ;; ret: ok-code + (Fn [(Ptr Mix_Music) Int] Int) + "Mix_PlayMusic") + + ;; Helpers + (defn ok? [error-code] + (= 0 error-code)) + + (def any-free-channel -1) + + (defn valid-channel? [ch] + (not (= ch -1))) + ) diff --git a/examples/sounds.carp b/examples/sounds.carp index 65d3cd80..62af0c4c 100644 --- a/examples/sounds.carp +++ b/examples/sounds.carp @@ -1,12 +1,37 @@ (load "SDL.carp") (load "SDL_mixer.carp") -(defn draw [app rend state] - (SDL.bg rend &(SDL.rgb 255 0 0))) +(def fx1 (the (Ptr Mix_Chunk) NULL)) + +(defn play-sound-fx1 [] + (ignore (Mixer.play-channel Mixer.any-free-channel fx1 0))) + +(defn event-handler [app] + (let [event (SDL.Event.init)] + (while (SDL.Event.poll (address event)) + (let [et (SDL.Event.type &event)] + (cond (= et SDL.Event.quit) + (SDLApp.stop app) + (= et SDL.Event.key-down) + (let [key (SDL.Event.keycode &event)] + (case key + SDL.key-escape (SDLApp.stop app) + SDL.key-return (play-sound-fx1) + ())) + ()))))) (defn main [] (let [app (SDLApp.create "Sound Effects with SDL_mixer" 400 300) rend @(SDLApp.renderer &app)] (do - (SDLApp.run-with-callbacks &app SDLApp.default-event-handler id draw 0) + (if (Mixer.ok? (Mixer.open-audio 22050 Mixer.default-format 2 4096)) + (println* "Audio initialized.") + (println* "Failed to initialize audio.")) + (set! fx1 (Mixer.load-wav (cstr "resources/fx1.wav"))) + (assert (not-null? fx1)) + (let-do [n (Mixer.nr-of-music-decoders)] + (println* "Nr of music decoders: " n)) + (let-do [music (Mixer.load-music (cstr "resources/song.aif"))] + (println* "Music null? " (null? music))) + (SDLApp.run-with-callbacks &app event-handler id SDLApp.default-draw 0) 0))) diff --git a/resources/fx1.wav b/resources/fx1.wav new file mode 100644 index 00000000..2867ba12 Binary files /dev/null and b/resources/fx1.wav differ diff --git a/resources/song.aif b/resources/song.aif new file mode 100644 index 00000000..b135b3ba Binary files /dev/null and b/resources/song.aif differ