1 /* 2 * Copyright (C) 2015-2017, by Laszlo Szeremi under the Boost license. 3 * 4 * Pixel Perfect Engine, sound module 5 */ 6 7 module pixelperfectengine.sound.sound; 8 9 import bindbc.sdl.mixer; 10 import std.conv; 11 public import pixelperfectengine.system.exc; 12 13 /** 14 * Implements a simple way for audio playback, through the use of SDL2_mixer. 15 * 16 * It's simple to use, but very limited and will be deprecated. `PixelPerfect.audio` will implement a system for 17 * plugin-based audio playback, 18 * with MIDI 2.0 support. 19 * 20 * Only one instance should be used. 21 */ 22 public class SoundStream { 23 /** 24 * The sound samples stored within this class. 25 */ 26 public Mix_Chunk*[int] soundBank; 27 /** 28 * Default CTOR. 29 * 30 * Opens SDL_Mixer, or throws an AudioInitializationException if it fails. 31 */ 32 public this(){ 33 loadSDLMixer(); 34 if(Mix_OpenAudio(44_100, MIX_DEFAULT_FORMAT, 2, 2048) < 0){ 35 //string msg = Mix_GetError(); 36 throw new AudioInitializationException(to!string(Mix_GetError()) , __FILE__, __LINE__, null); 37 } 38 } 39 public ~this(){ 40 foreach(foo ; soundBank){ 41 Mix_FreeChunk(foo); 42 } 43 } 44 /** 45 * Plays a sound sample from the bank at the specified channel. Can be looped. 46 */ 47 public void play(int number, int channel = -1, int loops = 0){ 48 Mix_PlayChannel(channel, soundBank[number], loops); 49 } 50 /** 51 * Halts the playback on the specified channel. 52 */ 53 public void halt(int channel){ 54 Mix_HaltChannel(channel); 55 56 } 57 58 }