1 /* 2 DSFML - The Simple and Fast Multimedia Library for D 3 4 Copyright (c) 2013 - 2015 Jeremy DeHaan (dehaan.jeremiah@gmail.com) 5 6 This software is provided 'as-is', without any express or implied warranty. 7 In no event will the authors be held liable for any damages arising from the use of this software. 8 9 Permission is granted to anyone to use this software for any purpose, including commercial applications, 10 and to alter it and redistribute it freely, subject to the following restrictions: 11 12 1. The origin of this software must not be misrepresented; you must not claim that you wrote the original software. 13 If you use this software in a product, an acknowledgment in the product documentation would be appreciated but is not required. 14 15 2. Altered source versions must be plainly marked as such, and must not be misrepresented as being the original software. 16 17 3. This notice may not be removed or altered from any source distribution 18 */ 19 20 module dsfml.audio.music; 21 22 import dsfml.system.mutex; 23 import dsfml.system.time; 24 import dsfml.system.inputstream; 25 26 import dsfml.audio.soundstream; 27 28 29 /++ 30 + Streamed music played from an audio file. 31 + 32 + Musics are sounds that are streamed rather than completely loaded in memory. 33 + 34 + This is especially useful for compressed musics that usually take hundreds of MB when they are uncompressed: by streaming it instead of loading it entirely, you avoid saturating the memory and have almost no loading delay. 35 + 36 + Apart from that, a Music has almost the same features as the SoundBuffer / Sound pair: you can play/pause/stop it, request its parameters (channels, sample rate), change the way it is played (pitch, volume, 3D position, ...), etc. 37 + 38 + As a sound stream, a music is played in its own thread in order not to block the rest of the program. This means that you can leave the music alone after calling play(), it will manage itself very well. 39 + 40 + See_Also: http://sfml-dev.org/documentation/2.0/classsf_1_1Music.php#details 41 + Authors: Laurent Gomila, Jeremy DeHaan 42 +/ 43 class Music : SoundStream 44 { 45 import dsfml.audio.soundfile; 46 47 private 48 { 49 SoundFile m_file; 50 Time m_duration; 51 short[] m_samples; 52 Mutex m_mutex; 53 } 54 55 this() 56 { 57 m_file.create(); 58 59 m_mutex = new Mutex(); 60 61 super(); 62 } 63 64 /** 65 * Open a music from an audio file. 66 * 67 * This function doesn't start playing the music (call play() to do so). 68 * 69 * Here is a complete list of all the supported audio formats: ogg, wav, flac, aiff, au, raw, paf, svx, nist, voc, ircam, w64, mat4, mat5 pvf, htk, sds, avr, sd2, caf, wve, mpc2k, rf64. 70 * 71 * Params: 72 * filename = Path of the music file to open. 73 * Returns: True if loading succeeded, false if it failed. 74 */ 75 bool openFromFile(string filename) 76 { 77 //stop music if already playing 78 stop(); 79 80 if(!m_file.openReadFromFile(filename)) 81 { 82 return false; 83 } 84 85 initialize(); 86 87 return true; 88 } 89 90 /** 91 * Open a music from an audio file in memory. 92 * 93 * This function doesn't start playing the music (call play() to do so). 94 * 95 * Here is a complete list of all the supported audio formats: ogg, wav, flac, aiff, au, raw, paf, svx, nist, voc, ircam, w64, mat4, mat5 pvf, htk, sds, avr, sd2, caf, wve, mpc2k, rf64. 96 * 97 * Since the music is not loaded completely but rather streamed continuously, the data must remain available as long as the music is playing (ie. you can't deallocate it right after calling this function). 98 * 99 * Params: 100 * data = The array of data 101 * Returns: True if loading succeeded, false if it failed. 102 */ 103 bool openFromMemory(const(void)[] data) 104 { 105 stop(); 106 107 if(!m_file.openReadFromMemory(data)) 108 { 109 return false; 110 } 111 112 initialize(); 113 return true; 114 } 115 116 /** 117 * Open a music from an audio file in memory. 118 * 119 * This function doesn't start playing the music (call play() to do so). 120 * 121 * Here is a complete list of all the supported audio formats: ogg, wav, flac, aiff, au, raw, paf, svx, nist, voc, ircam, w64, mat4, mat5 pvf, htk, sds, avr, sd2, caf, wve, mpc2k, rf64. 122 * 123 * Since the music is not loaded completely but rather streamed continuously, the stream must remain available as long as the music is playing (ie. you can't deallocate it right after calling this function). 124 * 125 * Params: 126 * stream = Source stream to read from 127 * Returns: True if loading succeeded, false if it failed. 128 */ 129 bool openFromStream(InputStream stream) 130 { 131 stop(); 132 133 if(!m_file.openReadFromStream(stream)) 134 { 135 return false; 136 } 137 138 initialize(); 139 140 return true; 141 } 142 143 ~this() 144 { 145 import dsfml.system.config; 146 mixin(destructorOutput); 147 stop(); 148 } 149 150 /// Get the total duration of the music. 151 /// Returns: Music duration 152 Time getDuration() 153 { 154 return m_duration; 155 } 156 157 protected 158 { 159 /** 160 * Request a new chunk of audio samples from the stream source. 161 * 162 * This function fills the chunk from the next samples to read from the audio file. 163 * 164 * Params: 165 * samples = Array of samples to fill 166 * 167 * Returns: True to continue playback, false to stop. 168 */ 169 override bool onGetData(ref const(short)[] samples) 170 { 171 import dsfml.system.lock; 172 173 Lock lock = Lock(m_mutex); 174 175 auto length = cast(size_t)m_file.read(m_samples); 176 samples = m_samples[0..length]; 177 178 return (samples.length == m_samples.length); 179 } 180 /** 181 * Change the current playing position in the stream source. 182 * 183 * Params: 184 * timeOffset = New playing position, from the beginning of the music 185 * 186 * Implements SoundStream. 187 */ 188 override void onSeek(Time timeOffset) 189 { 190 import dsfml.system.lock; 191 192 Lock lock =Lock(m_mutex); 193 194 m_file.seek(timeOffset.asMicroseconds()); 195 } 196 } 197 198 private 199 { 200 /** 201 * Define the audio stream parameters. 202 * 203 * This function must be called by derived classes as soon as they know the audio settings of the stream to play. Any attempt to manipulate the stream (play(), ...) before calling this function will fail. 204 * 205 * It can be called multiple times if the settings of the audio stream change, but only when the stream is stopped. 206 * 207 * Params: 208 * channelCount = Number of channels of the stream 209 * sampleRate = Sample rate, in samples per second 210 */ 211 void initialize() 212 { 213 size_t sampleCount = cast(size_t)m_file.getSampleCount(); 214 215 uint channelCount = m_file.getChannelCount(); 216 217 uint sampleRate = m_file.getSampleRate(); 218 219 // Compute the music duration 220 m_duration = seconds(cast(float)(sampleCount) / sampleRate / channelCount); 221 222 // Resize the internal buffer so that it can contain 1 second of audio samples 223 m_samples.length = sampleRate * channelCount; 224 225 // Initialize the stream 226 super.initialize(channelCount, sampleRate); 227 228 } 229 230 } 231 } 232 233 unittest 234 { 235 version(DSFML_Unittest_Audio) 236 { 237 import std.stdio; 238 import dsfml.system.clock; 239 240 writeln("Unit test for Music Class"); 241 242 auto music = new Music(); 243 244 //TODO: update this for a real unit test users can run themselves. 245 if(!music.openFromFile("res/TestMusic.ogg")) 246 { 247 return; 248 } 249 250 auto clock = new Clock(); 251 252 writeln("Playing music for 5 seconds"); 253 254 music.play(); 255 while(clock.getElapsedTime().asSeconds() < 5) 256 { 257 //playing music in seoarate thread while main thread is stuck here 258 } 259 260 music.stop(); 261 262 263 264 265 writeln(); 266 } 267 } 268 269