|author||David Thompson <firstname.lastname@example.org>||2017-01-24 21:26:10 -0500|
|committer||David Thompson <email@example.com>||2017-01-24 21:26:10 -0500|
Add audio support.
* chickadee/audio.scm: New file. * Makefile.am (SOURCES): Add it. * chickadee.scm (run-game): Initialize audio. * doc/api.texi ("Audio"): Document it.
Diffstat (limited to 'chickadee')
1 files changed, 144 insertions, 0 deletions
diff --git a/chickadee/audio.scm b/chickadee/audio.scm
new file mode 100644
@@ -0,0 +1,144 @@
+;;; Chickadee Game Toolkit
+;;; Copyright © 2017 David Thompson <firstname.lastname@example.org>
+;;; Chickadee is free software: you can redistribute it and/or modify
+;;; it under the terms of the GNU General Public License as published
+;;; by the Free Software Foundation, either version 3 of the License,
+;;; or (at your option) any later version.
+;;; Chickadee is distributed in the hope that it will be useful, but
+;;; WITHOUT ANY WARRANTY; without even the implied warranty of
+;;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
+;;; General Public License for more details.
+;;; You should have received a copy of the GNU General Public License
+;;; along with this program. If not, see
+;; Sound effects and music.
+(define-module (chickadee audio)
+ #:use-module (ice-9 format)
+ #:use-module (srfi srfi-9)
+ #:use-module (srfi srfi-9 gnu)
+ #:use-module ((sdl2 mixer) #:prefix sdl2:)
+ #:export (load-sample
+ ;; The SDL mixer will throw an exception if it cannot initialize a
+ ;; particular audio format. We don't want this to be fatal, so we
+ ;; ignore it.
+ (false-if-exception (sdl2:mixer-init))
+;; Used to wrap SDL audio functions whose return values should be
+(define-syntax-rule (ignore-value body ...)
+ body ...
+;; Wrapper over SDL audio objects.
+ (make-sample audio)
+ (audio sample-audio))
+(define (display-sample sample port)
+ (format port "#<sample ~x>" (object-address sample)))
+(set-record-type-printer! <sample> display-sample)
+(define (load-sample file)
+ "Load audio sample from FILE."
+ (let ((audio (sdl2:load-chunk file)))
+ (if audio
+ (make-sample audio)
+ (error "cannot load audio sample:" file))))
+(define (set-sample-volume! volume)
+ "Set the volume that all samples are played at to VOLUME, an integer
+value between 0 and 128."
+ (ignore-value (sdl2:set-channel-volume! #f volume)))
+(define (play-sample sample)
+ "Play SAMPLE."
+ ;; An exception will be thrown if too many samples are being played
+ ;; at once, but it can be safely ignored.
+ (sdl2:play-chunk! (sample-audio sample)))))
+;; Wrapper over SDL music objects.
+ (make-music audio)
+ (audio music-audio))
+(define (display-music music port)
+ (format port "#<music ~x>" (object-address music)))
+(set-record-type-printer! <music> display-music)
+(define (load-music file)
+ "Load music from FILE."
+ (make-music (sdl2:load-music file)))
+ "Return the volume level for music, an integer value between 0 and
+(define (set-music-volume! volume)
+ "Set the volume that music is played at to VOLUME, an integer value
+between 0 and 128."
+ (ignore-value (sdl2:set-music-volume! volume)))
+(define* (play-music music #:key loop?)
+ "Play MUSIC. If LOOP?, play it over and over and over and over
+ (sdl2:play-music! (music-audio music) (if loop? #f 1)))
+ "Pause the current music track."
+ "Resume the current music track."
+ "Restart the current music track from the beginning."
+ "Stop playing the current music track."
+ "Return #t if music is currently playing."
+ "Return #t if music is currently paused."