Complete review of audio system

Still some work left...
This commit is contained in:
raysan5 2016-08-01 12:49:17 +02:00
parent a61b832c4a
commit 02c456432d
6 changed files with 483 additions and 804 deletions

View file

@ -57,7 +57,9 @@ int main()
// Create a RenderTexture2D to be used for render to texture // Create a RenderTexture2D to be used for render to texture
RenderTexture2D target = LoadRenderTexture(screenWidth, screenHeight); RenderTexture2D target = LoadRenderTexture(screenWidth, screenHeight);
PlayMusicStream(0, "resources/audio/2t2m_spa.xm"); // Play module stream Music xm = LoadMusicStream("resources/audio/2t2m_spa.xm");
PlayMusicStream(xm);
float timePlayed = 0.0f; float timePlayed = 0.0f;
@ -88,9 +90,9 @@ int main()
} }
// Get timePlayed scaled to bar dimensions // Get timePlayed scaled to bar dimensions
timePlayed = (GetMusicTimePlayed(0)/GetMusicTimeLength(0)*(screenWidth - 40))*2; timePlayed = (GetMusicTimePlayed(xm)/GetMusicTimeLength(xm)*(screenWidth - 40))*2;
UpdateMusicStream(0); // Update music buffer with new stream data UpdateMusicStream(xm); // Update music buffer with new stream data
//---------------------------------------------------------------------------------- //----------------------------------------------------------------------------------
// Draw // Draw
@ -129,6 +131,8 @@ int main()
UnloadShader(shader); // Unload shader UnloadShader(shader); // Unload shader
UnloadRenderTexture(target); // Unload render texture UnloadRenderTexture(target); // Unload render texture
UnloadMusicStream(xm); // Unload music stream buffers from RAM
CloseAudioDevice(); // Close audio device (music streaming is automatically stopped) CloseAudioDevice(); // Close audio device (music streaming is automatically stopped)
CloseWindow(); // Close window and OpenGL context CloseWindow(); // Close window and OpenGL context

View file

@ -24,7 +24,9 @@ int main()
InitAudioDevice(); // Initialize audio device InitAudioDevice(); // Initialize audio device
PlayMusicStream(0, "resources/audio/guitar_noodling.ogg"); // Play music stream Music music = LoadMusicStream("resources/audio/guitar_noodling.ogg");
PlayMusicStream(music);
int framesCounter = 0; int framesCounter = 0;
float timePlayed = 0.0f; float timePlayed = 0.0f;
@ -58,12 +60,12 @@ int main()
SetMusicVolume(volume); SetMusicVolume(volume);
} }
*/ */
if (IsWindowMinimized()) PauseMusicStream(0); if (IsWindowMinimized()) PauseMusicStream(music);
else ResumeMusicStream(0); else ResumeMusicStream(music);
timePlayed = GetMusicTimePlayed(0)/GetMusicTimeLength(0)*100*4; // We scale by 4 to fit 400 pixels timePlayed = GetMusicTimePlayed(music)/GetMusicTimeLength(music)*100*4; // We scale by 4 to fit 400 pixels
UpdateMusicStream(0); // Update music buffer with new stream data UpdateMusicStream(music); // Update music buffer with new stream data
//---------------------------------------------------------------------------------- //----------------------------------------------------------------------------------
// Draw // Draw
@ -83,9 +85,11 @@ int main()
// De-Initialization // De-Initialization
//-------------------------------------------------------------------------------------- //--------------------------------------------------------------------------------------
CloseAudioDevice(); // Close audio device (music streaming is automatically stopped) UnloadMusicStream(music); // Unload music stream buffers from RAM
CloseWindow(); // Close window and OpenGL context CloseAudioDevice(); // Close audio device (music streaming is automatically stopped)
CloseWindow(); // Close window and OpenGL context
//-------------------------------------------------------------------------------------- //--------------------------------------------------------------------------------------
return 0; return 0;

File diff suppressed because it is too large Load diff

View file

@ -75,10 +75,9 @@ typedef struct Wave {
short channels; short channels;
} Wave; } Wave;
typedef struct MusicBuffer { // Music type (file streaming from memory)
char *fileName; // NOTE: Anything longer than ~10 seconds should be streamed into a mix channel...
int index; // index in musicStreams typedef struct Music *Music;
} MusicBuffer;
#ifdef __cplusplus #ifdef __cplusplus
extern "C" { // Prevents name mangling of functions extern "C" { // Prevents name mangling of functions
@ -102,27 +101,24 @@ Sound LoadSoundFromRES(const char *rresName, int resId); // Load sound to
void UnloadSound(Sound sound); // Unload sound void UnloadSound(Sound sound); // Unload sound
void PlaySound(Sound sound); // Play a sound void PlaySound(Sound sound); // Play a sound
void PauseSound(Sound sound); // Pause a sound void PauseSound(Sound sound); // Pause a sound
void ResumeSound(Sound sound); // Resume a paused sound
void StopSound(Sound sound); // Stop playing a sound void StopSound(Sound sound); // Stop playing a sound
bool IsSoundPlaying(Sound sound); // Check if a sound is currently playing bool IsSoundPlaying(Sound sound); // Check if a sound is currently playing
void SetSoundVolume(Sound sound, float volume); // Set volume for a sound (1.0 is max level) 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) void SetSoundPitch(Sound sound, float pitch); // Set pitch for a sound (1.0 is base level)
MusicBuffer LoadMusicBufferStream(char *fileName, int index); Music LoadMusicStream(char *fileName); // Load music stream from file
int PlayMusicStream(MusicBuffer buffer); // Start music playing (open stream) void UnloadMusicStream(Music music); // Unload music stream
void UpdateMusicStream(MusicBuffer buffer); // Updates buffers for music streaming void PlayMusicStream(Music music); // Start music playing (open stream)
void StopMusicStream(MusicBuffer buffer); // Stop music playing (close stream) void UpdateMusicStream(Music music); // Updates buffers for music streaming
void PauseMusicStream(MusicBuffer buffer); // Pause music playing void StopMusicStream(Music music); // Stop music playing (close stream)
void ResumeMusicStream(MusicBuffer buffer); // Resume playing paused music void PauseMusicStream(Music music); // Pause music playing
bool IsMusicPlaying(MusicBuffer buffer); // Check if music is playing void ResumeMusicStream(Music music); // Resume playing paused music
void SetMusicVolume(MusicBuffer buffer float volume); // Set volume for music (1.0 is max level) bool IsMusicPlaying(Music music); // Check if music is playing
void SetMusicPitch(MusicBuffer buffer, float pitch); // Set pitch for a music (1.0 is base level) void SetMusicVolume(Music music, float volume); // Set volume for music (1.0 is max level)
float GetMusicTimeLength(MusicBuffer buffer); // Get music time length (in seconds) void SetMusicPitch(Music music, float pitch); // Set pitch for a music (1.0 is base level)
float GetMusicTimePlayed(MusicBuffer buffer); // Get current music time played (in seconds) float GetMusicTimeLength(Music music); // Get music time length (in seconds)
int GetMusicStreamCount(void); // Get number of streams loaded float GetMusicTimePlayed(Music music); // Get current music time played (in seconds)
int InitRawMixChannel(int sampleRate, int channels, bool floatingPoint); // Initialize raw audio mix channel for audio buffering
int BufferRawMixChannel(int mixc, void *data, unsigned short numberElements); // Buffers data directly to raw mix channel
void CloseRawMixChannel(int mixc); // Closes and frees raw mix channel
#ifdef __cplusplus #ifdef __cplusplus
} }

60
src/external/jar_xm.h vendored
View file

@ -102,7 +102,7 @@ int jar_xm_create_context_from_file(jar_xm_context_t** ctx, uint32_t rate, const
* @deprecated This function is unsafe! * @deprecated This function is unsafe!
* @see jar_xm_create_context_safe() * @see jar_xm_create_context_safe()
*/ */
int jar_xm_create_context(jar_xm_context_t**, const char* moddata, uint32_t rate); int jar_xm_create_context(jar_xm_context_t** ctx, const char* moddata, uint32_t rate);
/** Create a XM context. /** Create a XM context.
* *
@ -114,17 +114,17 @@ int jar_xm_create_context(jar_xm_context_t**, const char* moddata, uint32_t rate
* @returns 1 if module data is not sane * @returns 1 if module data is not sane
* @returns 2 if memory allocation failed * @returns 2 if memory allocation failed
*/ */
int jar_xm_create_context_safe(jar_xm_context_t**, const char* moddata, size_t moddata_length, uint32_t rate); int jar_xm_create_context_safe(jar_xm_context_t** ctx, const char* moddata, size_t moddata_length, uint32_t rate);
/** Free a XM context created by jar_xm_create_context(). */ /** Free a XM context created by jar_xm_create_context(). */
void jar_xm_free_context(jar_xm_context_t*); void jar_xm_free_context(jar_xm_context_t* ctx);
/** Play the module and put the sound samples in an output buffer. /** Play the module and put the sound samples in an output buffer.
* *
* @param output buffer of 2*numsamples elements (A left and right value for each sample) * @param output buffer of 2*numsamples elements (A left and right value for each sample)
* @param numsamples number of samples to generate * @param numsamples number of samples to generate
*/ */
void jar_xm_generate_samples(jar_xm_context_t*, float* output, size_t numsamples); void jar_xm_generate_samples(jar_xm_context_t* ctx, float* output, size_t numsamples);
/** Play the module, resample from 32 bit to 16 bit, and put the sound samples in an output buffer. /** Play the module, resample from 32 bit to 16 bit, and put the sound samples in an output buffer.
* *
@ -173,12 +173,12 @@ void jar_xm_generate_samples_8bit(jar_xm_context_t* ctx, char* output, size_t nu
* *
* @param loopcnt maximum number of loops. Use 0 to loop * @param loopcnt maximum number of loops. Use 0 to loop
* indefinitely. */ * indefinitely. */
void jar_xm_set_max_loop_count(jar_xm_context_t*, uint8_t loopcnt); void jar_xm_set_max_loop_count(jar_xm_context_t* ctx, uint8_t loopcnt);
/** Get the loop count of the currently playing module. This value is /** Get the loop count of the currently playing module. This value is
* 0 when the module is still playing, 1 when the module has looped * 0 when the module is still playing, 1 when the module has looped
* once, etc. */ * once, etc. */
uint8_t jar_xm_get_loop_count(jar_xm_context_t*); uint8_t jar_xm_get_loop_count(jar_xm_context_t* ctx);
@ -188,7 +188,7 @@ uint8_t jar_xm_get_loop_count(jar_xm_context_t*);
* *
* @return whether the channel was muted. * @return whether the channel was muted.
*/ */
bool jar_xm_mute_channel(jar_xm_context_t*, uint16_t, bool); bool jar_xm_mute_channel(jar_xm_context_t* ctx, uint16_t, bool);
/** Mute or unmute an instrument. /** Mute or unmute an instrument.
* *
@ -197,43 +197,43 @@ bool jar_xm_mute_channel(jar_xm_context_t*, uint16_t, bool);
* *
* @return whether the instrument was muted. * @return whether the instrument was muted.
*/ */
bool jar_xm_mute_instrument(jar_xm_context_t*, uint16_t, bool); bool jar_xm_mute_instrument(jar_xm_context_t* ctx, uint16_t, bool);
/** Get the module name as a NUL-terminated string. */ /** Get the module name as a NUL-terminated string. */
const char* jar_xm_get_module_name(jar_xm_context_t*); const char* jar_xm_get_module_name(jar_xm_context_t* ctx);
/** Get the tracker name as a NUL-terminated string. */ /** Get the tracker name as a NUL-terminated string. */
const char* jar_xm_get_tracker_name(jar_xm_context_t*); const char* jar_xm_get_tracker_name(jar_xm_context_t* ctx);
/** Get the number of channels. */ /** Get the number of channels. */
uint16_t jar_xm_get_number_of_channels(jar_xm_context_t*); uint16_t jar_xm_get_number_of_channels(jar_xm_context_t* ctx);
/** Get the module length (in patterns). */ /** Get the module length (in patterns). */
uint16_t jar_xm_get_module_length(jar_xm_context_t*); uint16_t jar_xm_get_module_length(jar_xm_context_t*);
/** Get the number of patterns. */ /** Get the number of patterns. */
uint16_t jar_xm_get_number_of_patterns(jar_xm_context_t*); uint16_t jar_xm_get_number_of_patterns(jar_xm_context_t* ctx);
/** Get the number of rows of a pattern. /** Get the number of rows of a pattern.
* *
* @note Pattern numbers go from 0 to * @note Pattern numbers go from 0 to
* jar_xm_get_number_of_patterns(...)-1. * jar_xm_get_number_of_patterns(...)-1.
*/ */
uint16_t jar_xm_get_number_of_rows(jar_xm_context_t*, uint16_t); uint16_t jar_xm_get_number_of_rows(jar_xm_context_t* ctx, uint16_t);
/** Get the number of instruments. */ /** Get the number of instruments. */
uint16_t jar_xm_get_number_of_instruments(jar_xm_context_t*); uint16_t jar_xm_get_number_of_instruments(jar_xm_context_t* ctx);
/** Get the number of samples of an instrument. /** Get the number of samples of an instrument.
* *
* @note Instrument numbers go from 1 to * @note Instrument numbers go from 1 to
* jar_xm_get_number_of_instruments(...). * jar_xm_get_number_of_instruments(...).
*/ */
uint16_t jar_xm_get_number_of_samples(jar_xm_context_t*, uint16_t); uint16_t jar_xm_get_number_of_samples(jar_xm_context_t* ctx, uint16_t);
@ -242,7 +242,7 @@ uint16_t jar_xm_get_number_of_samples(jar_xm_context_t*, uint16_t);
* @param bpm will receive the current BPM * @param bpm will receive the current BPM
* @param tempo will receive the current tempo (ticks per line) * @param tempo will receive the current tempo (ticks per line)
*/ */
void jar_xm_get_playing_speed(jar_xm_context_t*, uint16_t* bpm, uint16_t* tempo); void jar_xm_get_playing_speed(jar_xm_context_t* ctx, uint16_t* bpm, uint16_t* tempo);
/** Get the current position in the module being played. /** Get the current position in the module being played.
* *
@ -257,7 +257,7 @@ void jar_xm_get_playing_speed(jar_xm_context_t*, uint16_t* bpm, uint16_t* tempo)
* generated samples (divide by sample rate to get seconds of * generated samples (divide by sample rate to get seconds of
* generated audio) * generated audio)
*/ */
void jar_xm_get_position(jar_xm_context_t*, uint8_t* pattern_index, uint8_t* pattern, uint8_t* row, uint64_t* samples); void jar_xm_get_position(jar_xm_context_t* ctx, uint8_t* pattern_index, uint8_t* pattern, uint8_t* row, uint64_t* samples);
/** Get the latest time (in number of generated samples) when a /** Get the latest time (in number of generated samples) when a
* particular instrument was triggered in any channel. * particular instrument was triggered in any channel.
@ -265,7 +265,7 @@ void jar_xm_get_position(jar_xm_context_t*, uint8_t* pattern_index, uint8_t* pat
* @note Instrument numbers go from 1 to * @note Instrument numbers go from 1 to
* jar_xm_get_number_of_instruments(...). * jar_xm_get_number_of_instruments(...).
*/ */
uint64_t jar_xm_get_latest_trigger_of_instrument(jar_xm_context_t*, uint16_t); uint64_t jar_xm_get_latest_trigger_of_instrument(jar_xm_context_t* ctx, uint16_t);
/** Get the latest time (in number of generated samples) when a /** Get the latest time (in number of generated samples) when a
* particular sample was triggered in any channel. * particular sample was triggered in any channel.
@ -276,21 +276,21 @@ uint64_t jar_xm_get_latest_trigger_of_instrument(jar_xm_context_t*, uint16_t);
* @note Sample numbers go from 0 to * @note Sample numbers go from 0 to
* jar_xm_get_nubmer_of_samples(...,instr)-1. * jar_xm_get_nubmer_of_samples(...,instr)-1.
*/ */
uint64_t jar_xm_get_latest_trigger_of_sample(jar_xm_context_t*, uint16_t instr, uint16_t sample); uint64_t jar_xm_get_latest_trigger_of_sample(jar_xm_context_t* ctx, uint16_t instr, uint16_t sample);
/** Get the latest time (in number of generated samples) when any /** Get the latest time (in number of generated samples) when any
* instrument was triggered in a given channel. * instrument was triggered in a given channel.
* *
* @note Channel numbers go from 1 to jar_xm_get_number_of_channels(...). * @note Channel numbers go from 1 to jar_xm_get_number_of_channels(...).
*/ */
uint64_t jar_xm_get_latest_trigger_of_channel(jar_xm_context_t*, uint16_t); uint64_t jar_xm_get_latest_trigger_of_channel(jar_xm_context_t* ctx, uint16_t);
/** Get the number of remaining samples. Divide by 2 to get the number of individual LR data samples. /** Get the number of remaining samples. Divide by 2 to get the number of individual LR data samples.
* *
* @note This is the remaining number of samples before the loop starts module again, or halts if on last pass. * @note This is the remaining number of samples before the loop starts module again, or halts if on last pass.
* @note This function is very slow and should only be run once, if at all. * @note This function is very slow and should only be run once, if at all.
*/ */
uint64_t jar_xm_get_remaining_samples(jar_xm_context_t*); uint64_t jar_xm_get_remaining_samples(jar_xm_context_t* ctx);
#ifdef __cplusplus #ifdef __cplusplus
} }
@ -308,7 +308,7 @@ uint64_t jar_xm_get_remaining_samples(jar_xm_context_t*);
#include <math.h> #include <math.h>
#include <string.h> #include <string.h>
#if JAR_XM_DEBUG #ifdef JAR_XM_DEBUG
#include <stdio.h> #include <stdio.h>
#define DEBUG(fmt, ...) do { \ #define DEBUG(fmt, ...) do { \
fprintf(stderr, "%s(): " fmt "\n", __func__, __VA_ARGS__); \ fprintf(stderr, "%s(): " fmt "\n", __func__, __VA_ARGS__); \
@ -638,7 +638,7 @@ int jar_xm_create_context_safe(jar_xm_context_t** ctxp, const char* moddata, siz
/* Initialize most of the fields to 0, 0.f, NULL or false depending on type */ /* Initialize most of the fields to 0, 0.f, NULL or false depending on type */
memset(mempool, 0, bytes_needed); memset(mempool, 0, bytes_needed);
ctx = (*ctxp = (jar_xm_context_t*)mempool); ctx = (*ctxp = (jar_xm_context_t *)mempool);
ctx->allocated_memory = mempool; /* Keep original pointer for free() */ ctx->allocated_memory = mempool; /* Keep original pointer for free() */
mempool += sizeof(jar_xm_context_t); mempool += sizeof(jar_xm_context_t);
@ -685,20 +685,18 @@ int jar_xm_create_context_safe(jar_xm_context_t** ctxp, const char* moddata, siz
return 0; return 0;
} }
void jar_xm_free_context(jar_xm_context_t* context) { void jar_xm_free_context(jar_xm_context_t* ctx) {
free(context->allocated_memory); free(ctx->allocated_memory);
} }
void jar_xm_set_max_loop_count(jar_xm_context_t* context, uint8_t loopcnt) { void jar_xm_set_max_loop_count(jar_xm_context_t* ctx, uint8_t loopcnt) {
context->max_loop_count = loopcnt; ctx->max_loop_count = loopcnt;
} }
uint8_t jar_xm_get_loop_count(jar_xm_context_t* context) { uint8_t jar_xm_get_loop_count(jar_xm_context_t* ctx) {
return context->loop_count; return ctx->loop_count;
} }
bool jar_xm_mute_channel(jar_xm_context_t* ctx, uint16_t channel, bool mute) { bool jar_xm_mute_channel(jar_xm_context_t* ctx, uint16_t channel, bool mute) {
bool old = ctx->channels[channel - 1].muted; bool old = ctx->channels[channel - 1].muted;
ctx->channels[channel - 1].muted = mute; ctx->channels[channel - 1].muted = mute;

View file

@ -478,10 +478,9 @@ typedef struct Wave {
short channels; short channels;
} Wave; } Wave;
typedef struct MusicBuffer { // Music type (file streaming from memory)
char *fileName; // NOTE: Anything longer than ~10 seconds should be streamed
int index; // index in musicStreams typedef struct Music *Music;
} MusicBuffer;
// Texture formats // Texture formats
// NOTE: Support depends on OpenGL version and platform // NOTE: Support depends on OpenGL version and platform
@ -897,23 +896,24 @@ Sound LoadSoundFromRES(const char *rresName, int resId); // Load sound to
void UnloadSound(Sound sound); // Unload sound void UnloadSound(Sound sound); // Unload sound
void PlaySound(Sound sound); // Play a sound void PlaySound(Sound sound); // Play a sound
void PauseSound(Sound sound); // Pause a sound void PauseSound(Sound sound); // Pause a sound
void ResumeSound(Sound sound); // Resume a paused sound
void StopSound(Sound sound); // Stop playing a sound void StopSound(Sound sound); // Stop playing a sound
bool IsSoundPlaying(Sound sound); // Check if a sound is currently playing bool IsSoundPlaying(Sound sound); // Check if a sound is currently playing
void SetSoundVolume(Sound sound, float volume); // Set volume for a sound (1.0 is max level) 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) void SetSoundPitch(Sound sound, float pitch); // Set pitch for a sound (1.0 is base level)
MusicBuffer LoadMusicBufferStream(char *fileName, int index); Music LoadMusicStream(char *fileName); // Load music stream from file
int PlayMusicStream(MusicBuffer buffer); // Start music playing (open stream) void UnloadMusicStream(Music music); // Unload music stream
void UpdateMusicStream(MusicBuffer buffer); // Updates buffers for music streaming void PlayMusicStream(Music music); // Start music playing (open stream)
void StopMusicStream(MusicBuffer buffer); // Stop music playing (close stream) void UpdateMusicStream(Music music); // Updates buffers for music streaming
void PauseMusicStream(MusicBuffer buffer); // Pause music playing void StopMusicStream(Music music); // Stop music playing (close stream)
void ResumeMusicStream(MusicBuffer buffer); // Resume playing paused music void PauseMusicStream(Music music); // Pause music playing
bool IsMusicPlaying(MusicBuffer buffer); // Check if music is playing void ResumeMusicStream(Music music); // Resume playing paused music
void SetMusicVolume(MusicBuffer buffer, float volume); // Set volume for music (1.0 is max level) bool IsMusicPlaying(Music music); // Check if music is playing
void SetMusicPitch(MusicBuffer buffer, float pitch); // Set pitch for a music (1.0 is base level) void SetMusicVolume(Music music, float volume); // Set volume for music (1.0 is max level)
float GetMusicTimeLength(MusicBuffer buffer); // Get music time length (in seconds) void SetMusicPitch(Music music, float pitch); // Set pitch for a music (1.0 is base level)
float GetMusicTimePlayed(MusicBuffer buffer); // Get current music time played (in seconds) float GetMusicTimeLength(Music music); // Get music time length (in seconds)
int GetMusicStreamCount(void); // Get number of streams loaded float GetMusicTimePlayed(Music music); // Get current music time played (in seconds)
#ifdef __cplusplus #ifdef __cplusplus
} }