mirror of
https://github.com/carp-lang/Carp.git
synced 2024-11-04 01:25:04 +03:00
Can play sound effect but music is not working yet.
This commit is contained in:
parent
e2aa4792a4
commit
2253b890fa
@ -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)))
|
||||
|
@ -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)))
|
||||
|
||||
)
|
||||
|
@ -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)))
|
||||
|
BIN
resources/fx1.wav
Normal file
BIN
resources/fx1.wav
Normal file
Binary file not shown.
BIN
resources/song.aif
Normal file
BIN
resources/song.aif
Normal file
Binary file not shown.
Loading…
Reference in New Issue
Block a user