From d069601d7bdf027b7e2e29deab339abe696ba657 Mon Sep 17 00:00:00 2001 From: Ricardo Wurmus Date: Wed, 14 Nov 2018 17:06:13 +0100 Subject: Replace (chickadee audio). --- Makefile.am | 1 + engine/audio.scm | 148 +++++++++++++++++++++++++++++++++++++++++++++++++++++++ engine/scene.scm | 2 +- scenes/death.scm | 2 +- scenes/game.scm | 2 +- scenes/intro.scm | 2 +- 6 files changed, 153 insertions(+), 4 deletions(-) create mode 100644 engine/audio.scm diff --git a/Makefile.am b/Makefile.am index fb4eddf..f7c3636 100644 --- a/Makefile.am +++ b/Makefile.am @@ -49,6 +49,7 @@ SOURCES = \ characters/reaper.scm \ engine/inotify.scm \ engine/assets.scm \ + engine/audio.scm \ engine/node.scm \ engine/node-2d.scm \ engine/scene.scm \ diff --git a/engine/audio.scm b/engine/audio.scm new file mode 100644 index 0000000..3a6b1a8 --- /dev/null +++ b/engine/audio.scm @@ -0,0 +1,148 @@ +;;; Chickadee Game Toolkit +;;; Copyright © 2017 David Thompson +;;; Copyright © 2018 Ricardo Wurmus +;;; +;;; 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 +;;; . + +;;; Commentary: +;; +;; Sound effects and music. +;; +;;; Code: + +(define-module (engine 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 + sample? + sample-audio + set-sample-volume! + play-sample + load-music + music? + music-audio + music-volume + play-music + set-music-volume! + pause-music + resume-music + rewind-music + stop-music + music-paused? + music-playing?)) + +(define (enable-audio) + ;; 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)) + (sdl2:open-audio)) + +;; Used to wrap SDL audio functions whose return values should be +;; ignored. +(define-syntax-rule (ignore-value body ...) + (begin + body ... + *unspecified*)) + +;; Wrapper over SDL audio objects. +(define-record-type + (make-sample audio) + sample? + (audio sample-audio)) + +(define (display-sample sample port) + (format port "#" (object-address sample))) + +(set-record-type-printer! 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 #:optional (channel #f)) + "Set the volume that all samples are played at to VOLUME, a floating +point value between 0 and 1." + (ignore-value + (sdl2:set-channel-volume! channel (inexact->exact (round (* volume 128.0)))))) + +(define* (play-sample sample #:optional (channel #f)) + "Play SAMPLE." + (ignore-value + ;; An exception will be thrown if too many samples are being played + ;; at once, but it can be safely ignored. + (false-if-exception + (sdl2:play-chunk! (sample-audio sample))))) + +;; Wrapper over SDL music objects. +(define-record-type + (make-music audio) + music? + (audio music-audio)) + +(define (display-music music port) + (format port "#" (object-address music))) + +(set-record-type-printer! display-music) + +(define (load-music file) + "Load music from FILE." + (make-music (sdl2:load-music file))) + +(define (music-volume) + "Return the volume level for music, a floating point value between +0 and 1." + (/ (sdl2:music-volume) 128.0)) + +(define (set-music-volume! volume) + "Set the volume that music is played at to VOLUME, a floating point +value between 0 and 1." + (ignore-value + (sdl2:set-music-volume! + (inexact->exact (round (* volume 128.0)))))) + +(define* (play-music music #:key loop?) + "Play MUSIC. If LOOP?, play it over and over and over and over +and..." + (sdl2:play-music! (music-audio music) (if loop? #f 1))) + +(define (pause-music) + "Pause the current music track." + (sdl2:pause-music!)) + +(define (resume-music) + "Resume the current music track." + (sdl2:resume-music!)) + +(define (rewind-music) + "Restart the current music track from the beginning." + (sdl2:rewind-music!)) + +(define (stop-music) + "Stop playing the current music track." + (sdl2:stop-music!)) + +(define (music-playing?) + "Return #t if music is currently playing." + (sdl2:music-playing?)) + +(define (music-paused?) + "Return #t if music is currently paused." + (sdl2:music-paused?)) diff --git a/engine/scene.scm b/engine/scene.scm index b47a30e..ecb8f0d 100644 --- a/engine/scene.scm +++ b/engine/scene.scm @@ -16,7 +16,7 @@ ;;; . (define-module (engine scene) - #:use-module (chickadee audio) + #:use-module (engine audio) #:use-module (engine node-2d) #:use-module (oop goops) #:export ( diff --git a/scenes/death.scm b/scenes/death.scm index 8f7048a..301db14 100644 --- a/scenes/death.scm +++ b/scenes/death.scm @@ -19,7 +19,6 @@ #:use-module (srfi srfi-1) #:use-module (ice-9 match) #:use-module (chickadee) - #:use-module (chickadee audio) #:use-module (chickadee math rect) #:use-module (chickadee math vector) #:use-module (chickadee render color) @@ -29,6 +28,7 @@ #:use-module (chickadee render tiled) #:use-module (chickadee scripting) #:use-module (engine assets) + #:use-module (engine audio) #:use-module (engine node) #:use-module (engine node-2d) #:use-module (engine scene) diff --git a/scenes/game.scm b/scenes/game.scm index f896cd8..aaabd11 100644 --- a/scenes/game.scm +++ b/scenes/game.scm @@ -17,7 +17,6 @@ ;;; . (define-module (scenes game) - #:use-module (chickadee audio) #:use-module (chickadee input keyboard) #:use-module (chickadee math easings) #:use-module (chickadee math rect) @@ -31,6 +30,7 @@ #:use-module (srfi srfi-26) #:use-module (ice-9 match) #:use-module (engine assets) + #:use-module (engine audio) #:use-module (engine node) #:use-module (engine node-2d) #:use-module (engine scene) diff --git a/scenes/intro.scm b/scenes/intro.scm index 5b80b36..b0a7972 100644 --- a/scenes/intro.scm +++ b/scenes/intro.scm @@ -16,7 +16,6 @@ ;;; . (define-module (scenes intro) - #:use-module (chickadee audio) #:use-module (chickadee math rect) #:use-module (chickadee math vector) #:use-module (chickadee render color) @@ -24,6 +23,7 @@ #:use-module (chickadee render texture) #:use-module (chickadee scripting) #:use-module (engine assets) + #:use-module (engine audio) #:use-module (engine node) #:use-module (engine node-2d) #:use-module (engine scene) -- cgit v1.2.3