Some variables renamed

This commit is contained in:
Ray 2019-07-24 22:37:24 +02:00
parent 8730f22371
commit ae95111006
2 changed files with 215 additions and 253 deletions

View File

@ -236,16 +236,16 @@ static void MixAudioFrames(float *framesOut, const float *framesIn, ma_uint32 fr
// AudioBuffer management functions declaration // AudioBuffer management functions declaration
// NOTE: Those functions are not exposed by raylib... for the moment // NOTE: Those functions are not exposed by raylib... for the moment
AudioBuffer *InitAudioBuffer(ma_format format, ma_uint32 channels, ma_uint32 sampleRate, ma_uint32 bufferSizeInFrames, int usage); AudioBuffer *InitAudioBuffer(ma_format format, ma_uint32 channels, ma_uint32 sampleRate, ma_uint32 bufferSizeInFrames, int usage);
void CloseAudioBuffer(AudioBuffer *audioBuffer); void CloseAudioBuffer(AudioBuffer *buffer);
bool IsAudioBufferPlaying(AudioBuffer *audioBuffer); bool IsAudioBufferPlaying(AudioBuffer *buffer);
void PlayAudioBuffer(AudioBuffer *audioBuffer); void PlayAudioBuffer(AudioBuffer *buffer);
void StopAudioBuffer(AudioBuffer *audioBuffer); void StopAudioBuffer(AudioBuffer *buffer);
void PauseAudioBuffer(AudioBuffer *audioBuffer); void PauseAudioBuffer(AudioBuffer *buffer);
void ResumeAudioBuffer(AudioBuffer *audioBuffer); void ResumeAudioBuffer(AudioBuffer *buffer);
void SetAudioBufferVolume(AudioBuffer *audioBuffer, float volume); void SetAudioBufferVolume(AudioBuffer *buffer, float volume);
void SetAudioBufferPitch(AudioBuffer *audioBuffer, float pitch); void SetAudioBufferPitch(AudioBuffer *buffer, float pitch);
void TrackAudioBuffer(AudioBuffer *audioBuffer); void TrackAudioBuffer(AudioBuffer *buffer);
void UntrackAudioBuffer(AudioBuffer *audioBuffer); void UntrackAudioBuffer(AudioBuffer *buffer);
//---------------------------------------------------------------------------------- //----------------------------------------------------------------------------------
// Multi channel playback globals // Multi channel playback globals
@ -644,151 +644,127 @@ AudioBuffer *InitAudioBuffer(ma_format format, ma_uint32 channels, ma_uint32 sam
} }
// Delete an audio buffer // Delete an audio buffer
void CloseAudioBuffer(AudioBuffer *audioBuffer) void CloseAudioBuffer(AudioBuffer *buffer)
{ {
if (audioBuffer == NULL) if (buffer != NULL)
{ {
TraceLog(LOG_ERROR, "CloseAudioBuffer() : No audio buffer"); UntrackAudioBuffer(buffer);
return; RL_FREE(buffer->buffer);
RL_FREE(buffer);
} }
else TraceLog(LOG_ERROR, "CloseAudioBuffer() : No audio buffer");
UntrackAudioBuffer(audioBuffer);
RL_FREE(audioBuffer->buffer);
RL_FREE(audioBuffer);
} }
// Check if an audio buffer is playing // Check if an audio buffer is playing
bool IsAudioBufferPlaying(AudioBuffer *audioBuffer) bool IsAudioBufferPlaying(AudioBuffer *buffer)
{ {
if (audioBuffer == NULL) bool result = false;
{
TraceLog(LOG_ERROR, "IsAudioBufferPlaying() : No audio buffer"); if (buffer != NULL) result = (buffer->playing && !buffer->paused);
return false; else TraceLog(LOG_ERROR, "IsAudioBufferPlaying() : No audio buffer");
}
return audioBuffer->playing && !audioBuffer->paused; return result;
} }
// Play an audio buffer // Play an audio buffer
// NOTE: Buffer is restarted to the start. // NOTE: Buffer is restarted to the start.
// Use PauseAudioBuffer() and ResumeAudioBuffer() if the playback position should be maintained. // Use PauseAudioBuffer() and ResumeAudioBuffer() if the playback position should be maintained.
void PlayAudioBuffer(AudioBuffer *audioBuffer) void PlayAudioBuffer(AudioBuffer *buffer)
{ {
if (audioBuffer == NULL) if (buffer != NULL)
{ {
TraceLog(LOG_ERROR, "PlayAudioBuffer() : No audio buffer"); buffer->playing = true;
return; buffer->paused = false;
buffer->frameCursorPos = 0;
} }
else TraceLog(LOG_ERROR, "PlayAudioBuffer() : No audio buffer");
audioBuffer->playing = true;
audioBuffer->paused = false;
audioBuffer->frameCursorPos = 0;
} }
// Stop an audio buffer // Stop an audio buffer
void StopAudioBuffer(AudioBuffer *audioBuffer) void StopAudioBuffer(AudioBuffer *buffer)
{ {
if (audioBuffer == NULL) if (buffer != NULL)
{ {
TraceLog(LOG_ERROR, "StopAudioBuffer() : No audio buffer"); if (IsAudioBufferPlaying(buffer))
return; {
buffer->playing = false;
buffer->paused = false;
buffer->frameCursorPos = 0;
buffer->isSubBufferProcessed[0] = true;
buffer->isSubBufferProcessed[1] = true;
}
} }
else TraceLog(LOG_ERROR, "StopAudioBuffer() : No audio buffer");
// Don't do anything if the audio buffer is already stopped.
if (!IsAudioBufferPlaying(audioBuffer)) return;
audioBuffer->playing = false;
audioBuffer->paused = false;
audioBuffer->frameCursorPos = 0;
audioBuffer->isSubBufferProcessed[0] = true;
audioBuffer->isSubBufferProcessed[1] = true;
} }
// Pause an audio buffer // Pause an audio buffer
void PauseAudioBuffer(AudioBuffer *audioBuffer) void PauseAudioBuffer(AudioBuffer *buffer)
{ {
if (audioBuffer == NULL) if (buffer != NULL) buffer->paused = true;
{ else TraceLog(LOG_ERROR, "PauseAudioBuffer() : No audio buffer");
TraceLog(LOG_ERROR, "PauseAudioBuffer() : No audio buffer");
return;
}
audioBuffer->paused = true;
} }
// Resume an audio buffer // Resume an audio buffer
void ResumeAudioBuffer(AudioBuffer *audioBuffer) void ResumeAudioBuffer(AudioBuffer *buffer)
{ {
if (audioBuffer == NULL) if (buffer != NULL) buffer->paused = false;
{ else TraceLog(LOG_ERROR, "ResumeAudioBuffer() : No audio buffer");
TraceLog(LOG_ERROR, "ResumeAudioBuffer() : No audio buffer");
return;
}
audioBuffer->paused = false;
} }
// Set volume for an audio buffer // Set volume for an audio buffer
void SetAudioBufferVolume(AudioBuffer *audioBuffer, float volume) void SetAudioBufferVolume(AudioBuffer *buffer, float volume)
{ {
if (audioBuffer == NULL) if (buffer != NULL) buffer->volume = volume;
{ else TraceLog(LOG_WARNING, "SetAudioBufferVolume() : No audio buffer");
TraceLog(LOG_WARNING, "SetAudioBufferVolume() : No audio buffer");
return;
}
audioBuffer->volume = volume;
} }
// Set pitch for an audio buffer // Set pitch for an audio buffer
void SetAudioBufferPitch(AudioBuffer *audioBuffer, float pitch) void SetAudioBufferPitch(AudioBuffer *buffer, float pitch)
{ {
if (audioBuffer == NULL) if (buffer != NULL)
{ {
TraceLog(LOG_WARNING, "SetAudioBufferPitch() : No audio buffer"); float pitchMul = pitch/buffer->pitch;
return;
// Pitching is just an adjustment of the sample rate. Note that this changes the duration of the sound - higher pitches
// will make the sound faster; lower pitches make it slower.
ma_uint32 newOutputSampleRate = (ma_uint32)((float)buffer->dsp.src.config.sampleRateOut/pitchMul);
buffer->pitch *= (float)buffer->dsp.src.config.sampleRateOut/newOutputSampleRate;
ma_pcm_converter_set_output_sample_rate(&buffer->dsp, newOutputSampleRate);
} }
else TraceLog(LOG_WARNING, "SetAudioBufferPitch() : No audio buffer");
float pitchMul = pitch/audioBuffer->pitch;
// Pitching is just an adjustment of the sample rate. Note that this changes the duration of the sound - higher pitches
// will make the sound faster; lower pitches make it slower.
ma_uint32 newOutputSampleRate = (ma_uint32)((float)audioBuffer->dsp.src.config.sampleRateOut / pitchMul);
audioBuffer->pitch *= (float)audioBuffer->dsp.src.config.sampleRateOut / newOutputSampleRate;
ma_pcm_converter_set_output_sample_rate(&audioBuffer->dsp, newOutputSampleRate);
} }
// Track audio buffer to linked list next position // Track audio buffer to linked list next position
void TrackAudioBuffer(AudioBuffer *audioBuffer) void TrackAudioBuffer(AudioBuffer *buffer)
{ {
ma_mutex_lock(&audioLock); ma_mutex_lock(&audioLock);
{ {
if (firstAudioBuffer == NULL) firstAudioBuffer = audioBuffer; if (firstAudioBuffer == NULL) firstAudioBuffer = buffer;
else else
{ {
lastAudioBuffer->next = audioBuffer; lastAudioBuffer->next = buffer;
audioBuffer->prev = lastAudioBuffer; buffer->prev = lastAudioBuffer;
} }
lastAudioBuffer = audioBuffer; lastAudioBuffer = buffer;
} }
ma_mutex_unlock(&audioLock); ma_mutex_unlock(&audioLock);
} }
// Untrack audio buffer from linked list // Untrack audio buffer from linked list
void UntrackAudioBuffer(AudioBuffer *audioBuffer) void UntrackAudioBuffer(AudioBuffer *buffer)
{ {
ma_mutex_lock(&audioLock); ma_mutex_lock(&audioLock);
{ {
if (audioBuffer->prev == NULL) firstAudioBuffer = audioBuffer->next; if (buffer->prev == NULL) firstAudioBuffer = buffer->next;
else audioBuffer->prev->next = audioBuffer->next; else buffer->prev->next = buffer->next;
if (audioBuffer->next == NULL) lastAudioBuffer = audioBuffer->prev; if (buffer->next == NULL) lastAudioBuffer = buffer->prev;
else audioBuffer->next->prev = audioBuffer->prev; else buffer->next->prev = buffer->prev;
audioBuffer->prev = NULL; buffer->prev = NULL;
audioBuffer->next = NULL; buffer->next = NULL;
} }
ma_mutex_unlock(&audioLock); ma_mutex_unlock(&audioLock);
} }
@ -802,10 +778,9 @@ Wave LoadWave(const char *fileName)
{ {
Wave wave = { 0 }; Wave wave = { 0 };
if (false) { }
#if defined(SUPPORT_FILEFORMAT_WAV) #if defined(SUPPORT_FILEFORMAT_WAV)
if (IsFileExtension(fileName, ".wav")) wave = LoadWAV(fileName); else if (IsFileExtension(fileName, ".wav")) wave = LoadWAV(fileName);
#else
if (false) {}
#endif #endif
#if defined(SUPPORT_FILEFORMAT_OGG) #if defined(SUPPORT_FILEFORMAT_OGG)
else if (IsFileExtension(fileName, ".ogg")) wave = LoadOGG(fileName); else if (IsFileExtension(fileName, ".ogg")) wave = LoadOGG(fileName);
@ -821,25 +796,6 @@ Wave LoadWave(const char *fileName)
return wave; return wave;
} }
// Load wave data from raw array data
Wave LoadWaveEx(void *data, int sampleCount, int sampleRate, int sampleSize, int channels)
{
Wave wave;
wave.data = data;
wave.sampleCount = sampleCount;
wave.sampleRate = sampleRate;
wave.sampleSize = sampleSize;
wave.channels = channels;
// NOTE: Copy wave data to work with, user is responsible of input data to free
Wave cwave = WaveCopy(wave);
WaveFormat(&cwave, sampleRate, sampleSize, channels);
return cwave;
}
// Load sound from file // Load sound from file
// NOTE: The entire file is loaded to memory to be played (no-streaming) // NOTE: The entire file is loaded to memory to be played (no-streaming)
Sound LoadSound(const char *fileName) Sound LoadSound(const char *fileName)
@ -903,7 +859,7 @@ void UnloadWave(Wave wave)
// Unload sound // Unload sound
void UnloadSound(Sound sound) void UnloadSound(Sound sound)
{ {
CloseAudioBuffer((AudioBuffer *)sound.stream.buffer); CloseAudioBuffer(sound.stream.buffer);
TraceLog(LOG_INFO, "Unloaded sound data from RAM"); TraceLog(LOG_INFO, "Unloaded sound data from RAM");
} }
@ -911,7 +867,7 @@ void UnloadSound(Sound sound)
// Update sound buffer with new data // Update sound buffer with new data
void UpdateSound(Sound sound, const void *data, int samplesCount) void UpdateSound(Sound sound, const void *data, int samplesCount)
{ {
AudioBuffer *audioBuffer = (AudioBuffer *)sound.stream.buffer; AudioBuffer *audioBuffer = sound.stream.buffer;
if (audioBuffer == NULL) if (audioBuffer == NULL)
{ {
@ -930,10 +886,9 @@ void ExportWave(Wave wave, const char *fileName)
{ {
bool success = false; bool success = false;
if (false) { }
#if defined(SUPPORT_FILEFORMAT_WAV) #if defined(SUPPORT_FILEFORMAT_WAV)
if (IsFileExtension(fileName, ".wav")) success = SaveWAV(wave, fileName); else if (IsFileExtension(fileName, ".wav")) success = SaveWAV(wave, fileName);
#else
if (false) {}
#endif #endif
else if (IsFileExtension(fileName, ".raw")) else if (IsFileExtension(fileName, ".raw"))
{ {
@ -994,7 +949,7 @@ void ExportWaveAsCode(Wave wave, const char *fileName)
// Play a sound // Play a sound
void PlaySound(Sound sound) void PlaySound(Sound sound)
{ {
PlayAudioBuffer((AudioBuffer *)sound.stream.buffer); PlayAudioBuffer(sound.stream.buffer);
} }
// Play a sound in the multichannel buffer pool // Play a sound in the multichannel buffer pool
@ -1046,17 +1001,16 @@ void PlaySoundMulti(Sound sound)
audioBufferPoolChannels[index] = audioBufferPoolCounter; audioBufferPoolChannels[index] = audioBufferPoolCounter;
audioBufferPoolCounter++; audioBufferPoolCounter++;
audioBufferPool[index]->volume = ((AudioBuffer*)sound.stream.buffer)->volume; audioBufferPool[index]->volume = sound.stream.buffer->volume;
audioBufferPool[index]->pitch = ((AudioBuffer*)sound.stream.buffer)->pitch; audioBufferPool[index]->pitch = sound.stream.buffer->pitch;
audioBufferPool[index]->looping = ((AudioBuffer*)sound.stream.buffer)->looping; audioBufferPool[index]->looping = sound.stream.buffer->looping;
audioBufferPool[index]->usage = ((AudioBuffer*)sound.stream.buffer)->usage; audioBufferPool[index]->usage = sound.stream.buffer->usage;
audioBufferPool[index]->isSubBufferProcessed[0] = false; audioBufferPool[index]->isSubBufferProcessed[0] = false;
audioBufferPool[index]->isSubBufferProcessed[1] = false; audioBufferPool[index]->isSubBufferProcessed[1] = false;
audioBufferPool[index]->bufferSizeInFrames = ((AudioBuffer*)sound.stream.buffer)->bufferSizeInFrames; audioBufferPool[index]->bufferSizeInFrames = sound.stream.buffer->bufferSizeInFrames;
audioBufferPool[index]->buffer = ((AudioBuffer*)sound.stream.buffer)->buffer; audioBufferPool[index]->buffer = sound.stream.buffer->buffer;
PlayAudioBuffer(audioBufferPool[index]); PlayAudioBuffer(audioBufferPool[index]);
} }
// Stop any sound played with PlaySoundMulti() // Stop any sound played with PlaySoundMulti()
@ -1081,37 +1035,37 @@ int GetSoundsPlaying(void)
// Pause a sound // Pause a sound
void PauseSound(Sound sound) void PauseSound(Sound sound)
{ {
PauseAudioBuffer((AudioBuffer *)sound.stream.buffer); PauseAudioBuffer(sound.stream.buffer);
} }
// Resume a paused sound // Resume a paused sound
void ResumeSound(Sound sound) void ResumeSound(Sound sound)
{ {
ResumeAudioBuffer((AudioBuffer *)sound.stream.buffer); ResumeAudioBuffer(sound.stream.buffer);
} }
// Stop reproducing a sound // Stop reproducing a sound
void StopSound(Sound sound) void StopSound(Sound sound)
{ {
StopAudioBuffer((AudioBuffer *)sound.stream.buffer); StopAudioBuffer(sound.stream.buffer);
} }
// Check if a sound is playing // Check if a sound is playing
bool IsSoundPlaying(Sound sound) bool IsSoundPlaying(Sound sound)
{ {
return IsAudioBufferPlaying((AudioBuffer *)sound.stream.buffer); return IsAudioBufferPlaying(sound.stream.buffer);
} }
// Set volume for a sound // Set volume for a sound
void SetSoundVolume(Sound sound, float volume) void SetSoundVolume(Sound sound, float volume)
{ {
SetAudioBufferVolume((AudioBuffer *)sound.stream.buffer, volume); SetAudioBufferVolume(sound.stream.buffer, volume);
} }
// Set pitch for a sound // Set pitch for a sound
void SetSoundPitch(Sound sound, float pitch) void SetSoundPitch(Sound sound, float pitch)
{ {
SetAudioBufferPitch((AudioBuffer *)sound.stream.buffer, pitch); SetAudioBufferPitch(sound.stream.buffer, pitch);
} }
// Convert wave data to desired format // Convert wave data to desired format
@ -1213,25 +1167,26 @@ float *GetWaveData(Wave wave)
Music LoadMusicStream(const char *fileName) Music LoadMusicStream(const char *fileName)
{ {
Music music = (MusicStream *)RL_MALLOC(sizeof(MusicStream)); Music music = (MusicStream *)RL_MALLOC(sizeof(MusicStream));
bool musicLoaded = true; bool musicLoaded = false;
if (false) { }
#if defined(SUPPORT_FILEFORMAT_OGG) #if defined(SUPPORT_FILEFORMAT_OGG)
if (IsFileExtension(fileName, ".ogg")) else if (IsFileExtension(fileName, ".ogg"))
{ {
// Open ogg audio stream // Open ogg audio stream
music->ctxData = stb_vorbis_open_filename(fileName, NULL, NULL); music->ctxData = stb_vorbis_open_filename(fileName, NULL, NULL);
if (music->ctxData == NULL) musicLoaded = false; if (music->ctxData != NULL)
else
{ {
music->ctxType = MUSIC_AUDIO_OGG;
stb_vorbis_info info = stb_vorbis_get_info((stb_vorbis *)music->ctxData); // Get Ogg file info stb_vorbis_info info = stb_vorbis_get_info((stb_vorbis *)music->ctxData); // Get Ogg file info
// OGG bit rate defaults to 16 bit, it's enough for compressed format // OGG bit rate defaults to 16 bit, it's enough for compressed format
music->stream = InitAudioStream(info.sample_rate, 16, info.channels); music->stream = InitAudioStream(info.sample_rate, 16, info.channels);
music->sampleCount = (unsigned int)stb_vorbis_stream_length_in_samples((stb_vorbis *)music->ctxData)*info.channels; music->sampleCount = (unsigned int)stb_vorbis_stream_length_in_samples((stb_vorbis *)music->ctxData)*info.channels;
music->sampleLeft = music->sampleCount; music->sampleLeft = music->sampleCount;
music->ctxType = MUSIC_AUDIO_OGG; music->loopCount = 0; // Infinite loop by default
music->loopCount = 0; // Infinite loop by default musicLoaded = true;
TraceLog(LOG_DEBUG, "[%s] OGG total samples: %i", fileName, music->sampleCount); TraceLog(LOG_DEBUG, "[%s] OGG total samples: %i", fileName, music->sampleCount);
TraceLog(LOG_DEBUG, "[%s] OGG sample rate: %i", fileName, info.sample_rate); TraceLog(LOG_DEBUG, "[%s] OGG sample rate: %i", fileName, info.sample_rate);
@ -1239,24 +1194,22 @@ Music LoadMusicStream(const char *fileName)
TraceLog(LOG_DEBUG, "[%s] OGG memory required: %i", fileName, info.temp_memory_required); TraceLog(LOG_DEBUG, "[%s] OGG memory required: %i", fileName, info.temp_memory_required);
} }
} }
#else
if (false) {}
#endif #endif
#if defined(SUPPORT_FILEFORMAT_FLAC) #if defined(SUPPORT_FILEFORMAT_FLAC)
else if (IsFileExtension(fileName, ".flac")) else if (IsFileExtension(fileName, ".flac"))
{ {
music->ctxData = drflac_open_file(fileName); music->ctxData = drflac_open_file(fileName);
if (music->ctxData == NULL) musicLoaded = false; if (music->ctxData != NULL)
else
{ {
music->ctxType = MUSIC_AUDIO_FLAC;
drflac *ctxFlac = (drflac *)music->ctxData; drflac *ctxFlac = (drflac *)music->ctxData;
music->stream = InitAudioStream(ctxFlac->sampleRate, ctxFlac->bitsPerSample, ctxFlac->channels); music->stream = InitAudioStream(ctxFlac->sampleRate, ctxFlac->bitsPerSample, ctxFlac->channels);
music->sampleCount = (unsigned int)ctxFlac->totalSampleCount; music->sampleCount = (unsigned int)ctxFlac->totalSampleCount;
music->sampleLeft = music->sampleCount; music->sampleLeft = music->sampleCount;
music->ctxType = MUSIC_AUDIO_FLAC; music->loopCount = 0; // Infinite loop by default
music->loopCount = 0; // Infinite loop by default musicLoaded = true;
TraceLog(LOG_DEBUG, "[%s] FLAC total samples: %i", fileName, music->sampleCount); TraceLog(LOG_DEBUG, "[%s] FLAC total samples: %i", fileName, music->sampleCount);
TraceLog(LOG_DEBUG, "[%s] FLAC sample rate: %i", fileName, ctxFlac->sampleRate); TraceLog(LOG_DEBUG, "[%s] FLAC sample rate: %i", fileName, ctxFlac->sampleRate);
@ -1273,19 +1226,19 @@ Music LoadMusicStream(const char *fileName)
int result = drmp3_init_file(ctxMp3, fileName, NULL); int result = drmp3_init_file(ctxMp3, fileName, NULL);
if (!result) musicLoaded = false; if (result > 0)
else
{ {
TraceLog(LOG_INFO, "[%s] MP3 sample rate: %i", fileName, ctxMp3->sampleRate); music->ctxType = MUSIC_AUDIO_MP3;
TraceLog(LOG_INFO, "[%s] MP3 bits per sample: %i", fileName, 32);
TraceLog(LOG_INFO, "[%s] MP3 channels: %i", fileName, ctxMp3->channels);
music->stream = InitAudioStream(ctxMp3->sampleRate, 32, ctxMp3->channels); music->stream = InitAudioStream(ctxMp3->sampleRate, 32, ctxMp3->channels);
music->sampleCount = drmp3_get_pcm_frame_count(ctxMp3)*ctxMp3->channels; music->sampleCount = drmp3_get_pcm_frame_count(ctxMp3)*ctxMp3->channels;
music->sampleLeft = music->sampleCount; music->sampleLeft = music->sampleCount;
music->ctxType = MUSIC_AUDIO_MP3; music->loopCount = 0; // Infinite loop by default
music->loopCount = 0; // Infinite loop by default musicLoaded = true;
TraceLog(LOG_INFO, "[%s] MP3 sample rate: %i", fileName, ctxMp3->sampleRate);
TraceLog(LOG_INFO, "[%s] MP3 bits per sample: %i", fileName, 32);
TraceLog(LOG_INFO, "[%s] MP3 channels: %i", fileName, ctxMp3->channels);
TraceLog(LOG_INFO, "[%s] MP3 total samples: %i", fileName, music->sampleCount); TraceLog(LOG_INFO, "[%s] MP3 total samples: %i", fileName, music->sampleCount);
} }
} }
@ -1297,23 +1250,23 @@ Music LoadMusicStream(const char *fileName)
int result = jar_xm_create_context_from_file(&ctxXm, 48000, fileName); int result = jar_xm_create_context_from_file(&ctxXm, 48000, fileName);
if (!result) // XM context created successfully if (result > 0) // XM context created successfully
{ {
jar_xm_set_max_loop_count(ctxXm, 0); // Set infinite number of loops music->ctxType = MUSIC_MODULE_XM;
jar_xm_set_max_loop_count(ctxXm, 0); // Set infinite number of loops
// NOTE: Only stereo is supported for XM // NOTE: Only stereo is supported for XM
music->stream = InitAudioStream(48000, 16, 2); music->stream = InitAudioStream(48000, 16, 2);
music->sampleCount = (unsigned int)jar_xm_get_remaining_samples(ctxXm); music->sampleCount = (unsigned int)jar_xm_get_remaining_samples(ctxXm);
music->sampleLeft = music->sampleCount; music->sampleLeft = music->sampleCount;
music->ctxType = MUSIC_MODULE_XM; music->loopCount = 0; // Infinite loop by default
music->loopCount = 0; // Infinite loop by default musicLoaded = true;
music->ctxData = ctxXm;
TraceLog(LOG_INFO, "[%s] XM number of samples: %i", fileName, music->sampleCount); TraceLog(LOG_INFO, "[%s] XM number of samples: %i", fileName, music->sampleCount);
TraceLog(LOG_INFO, "[%s] XM track length: %11.6f sec", fileName, (float)music->sampleCount/48000.0f); TraceLog(LOG_INFO, "[%s] XM track length: %11.6f sec", fileName, (float)music->sampleCount/48000.0f);
music->ctxData = ctxXm;
} }
else musicLoaded = false;
} }
#endif #endif
#if defined(SUPPORT_FILEFORMAT_MOD) #if defined(SUPPORT_FILEFORMAT_MOD)
@ -1323,30 +1276,30 @@ Music LoadMusicStream(const char *fileName)
music->ctxData = ctxMod; music->ctxData = ctxMod;
jar_mod_init(ctxMod); jar_mod_init(ctxMod);
int result = jar_mod_load_file(ctxMod, fileName);
if (jar_mod_load_file(ctxMod, fileName)) if (result > 0)
{ {
music->ctxType = MUSIC_MODULE_MOD;
// NOTE: Only stereo is supported for MOD // NOTE: Only stereo is supported for MOD
music->stream = InitAudioStream(48000, 16, 2); music->stream = InitAudioStream(48000, 16, 2);
music->sampleCount = (unsigned int)jar_mod_max_samples(ctxMod); music->sampleCount = (unsigned int)jar_mod_max_samples(ctxMod);
music->sampleLeft = music->sampleCount; music->sampleLeft = music->sampleCount;
music->ctxType = MUSIC_MODULE_MOD; music->loopCount = 0; // Infinite loop by default
music->loopCount = 0; // Infinite loop by default musicLoaded = true;
TraceLog(LOG_INFO, "[%s] MOD number of samples: %i", fileName, music->sampleLeft); TraceLog(LOG_INFO, "[%s] MOD number of samples: %i", fileName, music->sampleLeft);
TraceLog(LOG_INFO, "[%s] MOD track length: %11.6f sec", fileName, (float)music->sampleCount/48000.0f); TraceLog(LOG_INFO, "[%s] MOD track length: %11.6f sec", fileName, (float)music->sampleCount/48000.0f);
} }
else musicLoaded = false;
} }
#endif #endif
else musicLoaded = false;
if (!musicLoaded) if (!musicLoaded)
{ {
if (false) { }
#if defined(SUPPORT_FILEFORMAT_OGG) #if defined(SUPPORT_FILEFORMAT_OGG)
if (music->ctxType == MUSIC_AUDIO_OGG) stb_vorbis_close((stb_vorbis *)music->ctxData); else if (music->ctxType == MUSIC_AUDIO_OGG) stb_vorbis_close((stb_vorbis *)music->ctxData);
#else
if (false) {}
#endif #endif
#if defined(SUPPORT_FILEFORMAT_FLAC) #if defined(SUPPORT_FILEFORMAT_FLAC)
else if (music->ctxType == MUSIC_AUDIO_FLAC) drflac_free((drflac *)music->ctxData); else if (music->ctxType == MUSIC_AUDIO_FLAC) drflac_free((drflac *)music->ctxData);
@ -1377,10 +1330,9 @@ void UnloadMusicStream(Music music)
CloseAudioStream(music->stream); CloseAudioStream(music->stream);
if (false) { }
#if defined(SUPPORT_FILEFORMAT_OGG) #if defined(SUPPORT_FILEFORMAT_OGG)
if (music->ctxType == MUSIC_AUDIO_OGG) stb_vorbis_close((stb_vorbis *)music->ctxData); else if (music->ctxType == MUSIC_AUDIO_OGG) stb_vorbis_close((stb_vorbis *)music->ctxData);
#else
if (false) {}
#endif #endif
#if defined(SUPPORT_FILEFORMAT_FLAC) #if defined(SUPPORT_FILEFORMAT_FLAC)
else if (music->ctxType == MUSIC_AUDIO_FLAC) drflac_free((drflac *)music->ctxData); else if (music->ctxType == MUSIC_AUDIO_FLAC) drflac_free((drflac *)music->ctxData);
@ -1403,7 +1355,7 @@ void PlayMusicStream(Music music)
{ {
if (music != NULL) if (music != NULL)
{ {
AudioBuffer *audioBuffer = (AudioBuffer *)music->stream.buffer; AudioBuffer *audioBuffer = music->stream.buffer;
if (audioBuffer == NULL) if (audioBuffer == NULL)
{ {
@ -1473,7 +1425,7 @@ void UpdateMusicStream(Music music)
bool streamEnding = false; bool streamEnding = false;
unsigned int subBufferSizeInFrames = ((AudioBuffer *)music->stream.buffer)->bufferSizeInFrames/2; unsigned int subBufferSizeInFrames = music->stream.buffer->bufferSizeInFrames/2;
// NOTE: Using dynamic allocation because it could require more than 16KB // NOTE: Using dynamic allocation because it could require more than 16KB
void *pcm = RL_CALLOC(subBufferSizeInFrames*music->stream.channels*music->stream.sampleSize/8, 1); void *pcm = RL_CALLOC(subBufferSizeInFrames*music->stream.channels*music->stream.sampleSize/8, 1);

View File

@ -80,40 +80,44 @@
// Wave type, defines audio wave data // Wave type, defines audio wave data
typedef struct Wave { typedef struct Wave {
unsigned int sampleCount; // Number of samples unsigned int sampleCount; // Total number of samples
unsigned int sampleRate; // Frequency (samples per second) unsigned int sampleRate; // Frequency (samples per second)
unsigned int sampleSize; // Bit depth (bits per sample): 8, 16, 32 (24 not supported) unsigned int sampleSize; // Bit depth (bits per sample): 8, 16, 32 (24 not supported)
unsigned int channels; // Number of channels (1-mono, 2-stereo) unsigned int channels; // Number of channels (1-mono, 2-stereo)
void *data; // Buffer data pointer void *data; // Buffer data pointer
} Wave; } Wave;
// Sound source type typedef struct rAudioBuffer rAudioBuffer;
typedef struct Sound {
void *audioBuffer; // Pointer to internal data used by the audio system
unsigned int source; // Audio source id
unsigned int buffer; // Audio buffer id
int format; // Audio format specifier
} Sound;
// Music type (file streaming from memory)
// NOTE: Anything longer than ~10 seconds should be streamed
typedef struct MusicData *Music;
// Audio stream type // Audio stream type
// NOTE: Useful to create custom audio streams not bound to a specific file // NOTE: Useful to create custom audio streams not bound to a specific file
typedef struct AudioStream { typedef struct AudioStream {
unsigned int sampleRate; // Frequency (samples per second) unsigned int sampleRate; // Frequency (samples per second)
unsigned int sampleSize; // Bit depth (bits per sample): 8, 16, 32 (24 not supported) unsigned int sampleSize; // Bit depth (bits per sample): 8, 16, 32 (24 not supported)
unsigned int channels; // Number of channels (1-mono, 2-stereo) unsigned int channels; // Number of channels (1-mono, 2-stereo)
void *audioBuffer; // Pointer to internal data used by the audio system. rAudioBuffer *buffer; // Pointer to internal data used by the audio system
int format; // Audio format specifier
unsigned int source; // Audio source id
unsigned int buffers[2]; // Audio buffers (double buffering)
} AudioStream; } AudioStream;
// Sound source type
typedef struct Sound {
unsigned int sampleCount; // Total number of samples
AudioStream stream; // Audio stream
} Sound;
// Music stream type (audio file streaming from memory)
// NOTE: Anything longer than ~10 seconds should be streamed
typedef struct MusicStream {
int ctxType; // Type of music context (audio filetype)
void *ctxData; // Audio context data, depends on type
unsigned int sampleCount; // Total number of samples
unsigned int sampleLeft; // Number of samples left to end
unsigned int loopCount; // Loops count (times music will play), 0 means infinite loop
AudioStream stream; // Audio stream
} MusicStream, *Music;
#ifdef __cplusplus #ifdef __cplusplus
extern "C" { // Prevents name mangling of functions extern "C" { // Prevents name mangling of functions
#endif #endif
@ -126,60 +130,66 @@ extern "C" { // Prevents name mangling of functions
//---------------------------------------------------------------------------------- //----------------------------------------------------------------------------------
// Module Functions Declaration // Module Functions Declaration
//---------------------------------------------------------------------------------- //----------------------------------------------------------------------------------
void InitAudioDevice(void); // Initialize audio device and context
void CloseAudioDevice(void); // Close the audio device and context
bool IsAudioDeviceReady(void); // Check if audio device has been initialized successfully
void SetMasterVolume(float volume); // Set master volume (listener)
Wave LoadWave(const char *fileName); // Load wave data from file // Audio device management functions
Wave LoadWaveEx(void *data, int sampleCount, int sampleRate, int sampleSize, int channels); // Load wave data from raw array data RLAPI void InitAudioDevice(void); // Initialize audio device and context
Sound LoadSound(const char *fileName); // Load sound from file RLAPI void CloseAudioDevice(void); // Close the audio device and context
Sound LoadSoundFromWave(Wave wave); // Load sound from wave data RLAPI bool IsAudioDeviceReady(void); // Check if audio device has been initialized successfully
void UpdateSound(Sound sound, const void *data, int samplesCount);// Update sound buffer with new data RLAPI void SetMasterVolume(float volume); // Set master volume (listener)
void UnloadWave(Wave wave); // Unload wave data
void UnloadSound(Sound sound); // Unload sound // Wave/Sound loading/unloading functions
void PlaySound(Sound sound); // Play a sound RLAPI Wave LoadWave(const char *fileName); // Load wave data from file
void PlaySoundMulti(Sound sound); // Play a sound using the multi channel buffer pool RLAPI Sound LoadSound(const char *fileName); // Load sound from file
int GetSoundsPlaying(void); // Get number of sounds playing in the multichannel buffer pool RLAPI Sound LoadSoundFromWave(Wave wave); // Load sound from wave data
void PauseSound(Sound sound); // Pause a sound RLAPI void UpdateSound(Sound sound, const void *data, int samplesCount);// Update sound buffer with new data
void ResumeSound(Sound sound); // Resume a paused sound RLAPI void UnloadWave(Wave wave); // Unload wave data
void StopSound(Sound sound); // Stop playing a sound RLAPI void UnloadSound(Sound sound); // Unload sound
void StopSoundMulti(void); // Stop any sound played with PlaySoundMulti() RLAPI void ExportWave(Wave wave, const char *fileName); // Export wave data to file
bool IsSoundPlaying(Sound sound); // Check if a sound is currently playing RLAPI void ExportWaveAsCode(Wave wave, const char *fileName); // Export wave sample data to code (.h)
void SetSoundVolume(Sound sound, float volume); // Set volume for a sound (1.0 is max level)
void SetSoundPitch(Sound sound, float pitch); // Set pitch for a sound (1.0 is base level) // Wave/Sound management functions
void WaveFormat(Wave *wave, int sampleRate, int sampleSize, int channels); // Convert wave data to desired format RLAPI void PlaySound(Sound sound); // Play a sound
Wave WaveCopy(Wave wave); // Copy a wave to a new wave RLAPI void StopSound(Sound sound); // Stop playing a sound
void WaveCrop(Wave *wave, int initSample, int finalSample); // Crop a wave to defined samples range RLAPI void PauseSound(Sound sound); // Pause a sound
float *GetWaveData(Wave wave); // Get samples data from wave as a floats array RLAPI void ResumeSound(Sound sound); // Resume a paused sound
Music LoadMusicStream(const char *fileName); // Load music stream from file RLAPI void PlaySoundMulti(Sound sound); // Play a sound (using multichannel buffer pool)
void UnloadMusicStream(Music music); // Unload music stream RLAPI void StopSoundMulti(void); // Stop any sound playing (using multichannel buffer pool)
void PlayMusicStream(Music music); // Start music playing RLAPI int GetSoundsPlaying(void); // Get number of sounds playing in the multichannel
void UpdateMusicStream(Music music); // Updates buffers for music streaming RLAPI bool IsSoundPlaying(Sound sound); // Check if a sound is currently playing
void StopMusicStream(Music music); // Stop music playing RLAPI void SetSoundVolume(Sound sound, float volume); // Set volume for a sound (1.0 is max level)
void PauseMusicStream(Music music); // Pause music playing RLAPI void SetSoundPitch(Sound sound, float pitch); // Set pitch for a sound (1.0 is base level)
void ResumeMusicStream(Music music); // Resume playing paused music RLAPI void WaveFormat(Wave *wave, int sampleRate, int sampleSize, int channels); // Convert wave data to desired format
bool IsMusicPlaying(Music music); // Check if music is playing RLAPI Wave WaveCopy(Wave wave); // Copy a wave to a new wave
void SetMusicVolume(Music music, float volume); // Set volume for music (1.0 is max level) RLAPI void WaveCrop(Wave *wave, int initSample, int finalSample); // Crop a wave to defined samples range
void SetMusicPitch(Music music, float pitch); // Set pitch for a music (1.0 is base level) RLAPI float *GetWaveData(Wave wave); // Get samples data from wave as a floats array
void SetMusicLoopCount(Music music, int count); // Set music loop count (loop repeats)
float GetMusicTimeLength(Music music); // Get music time length (in seconds) // Music management functions
float GetMusicTimePlayed(Music music); // Get current music time played (in seconds) RLAPI Music LoadMusicStream(const char *fileName); // Load music stream from file
RLAPI void UnloadMusicStream(Music music); // Unload music stream
RLAPI void PlayMusicStream(Music music); // Start music playing
RLAPI void UpdateMusicStream(Music music); // Updates buffers for music streaming
RLAPI void StopMusicStream(Music music); // Stop music playing
RLAPI void PauseMusicStream(Music music); // Pause music playing
RLAPI void ResumeMusicStream(Music music); // Resume playing paused music
RLAPI bool IsMusicPlaying(Music music); // Check if music is playing
RLAPI void SetMusicVolume(Music music, float volume); // Set volume for music (1.0 is max level)
RLAPI void SetMusicPitch(Music music, float pitch); // Set pitch for a music (1.0 is base level)
RLAPI void SetMusicLoopCount(Music music, int count); // Set music loop count (loop repeats)
RLAPI float GetMusicTimeLength(Music music); // Get music time length (in seconds)
RLAPI float GetMusicTimePlayed(Music music); // Get current music time played (in seconds)
// AudioStream management functions // AudioStream management functions
AudioStream InitAudioStream(unsigned int sampleRate, RLAPI AudioStream InitAudioStream(unsigned int sampleRate, unsigned int sampleSize, unsigned int channels); // Init audio stream (to stream raw audio pcm data)
unsigned int sampleSize, RLAPI void UpdateAudioStream(AudioStream stream, const void *data, int samplesCount); // Update audio stream buffers with data
unsigned int channels); // Init audio stream (to stream raw audio pcm data) RLAPI void CloseAudioStream(AudioStream stream); // Close audio stream and free memory
void UpdateAudioStream(AudioStream stream, const void *data, int samplesCount); // Update audio stream buffers with data RLAPI bool IsAudioBufferProcessed(AudioStream stream); // Check if any audio stream buffers requires refill
void CloseAudioStream(AudioStream stream); // Close audio stream and free memory RLAPI void PlayAudioStream(AudioStream stream); // Play audio stream
bool IsAudioBufferProcessed(AudioStream stream); // Check if any audio stream buffers requires refill RLAPI void PauseAudioStream(AudioStream stream); // Pause audio stream
void PlayAudioStream(AudioStream stream); // Play audio stream RLAPI void ResumeAudioStream(AudioStream stream); // Resume audio stream
void PauseAudioStream(AudioStream stream); // Pause audio stream RLAPI bool IsAudioStreamPlaying(AudioStream stream); // Check if audio stream is playing
void ResumeAudioStream(AudioStream stream); // Resume audio stream RLAPI void StopAudioStream(AudioStream stream); // Stop audio stream
bool IsAudioStreamPlaying(AudioStream stream); // Check if audio stream is playing RLAPI void SetAudioStreamVolume(AudioStream stream, float volume); // Set volume for audio stream (1.0 is max level)
void StopAudioStream(AudioStream stream); // Stop audio stream RLAPI void SetAudioStreamPitch(AudioStream stream, float pitch); // Set pitch for audio stream (1.0 is base level)
void SetAudioStreamVolume(AudioStream stream, float volume); // Set volume for audio stream (1.0 is max level)
void SetAudioStreamPitch(AudioStream stream, float pitch); // Set pitch for audio stream (1.0 is base level)
#ifdef __cplusplus #ifdef __cplusplus
} }