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
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;
@ -88,9 +90,9 @@ int main()
}
// 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
@ -129,6 +131,8 @@ int main()
UnloadShader(shader); // Unload shader
UnloadRenderTexture(target); // Unload render texture
UnloadMusicStream(xm); // Unload music stream buffers from RAM
CloseAudioDevice(); // Close audio device (music streaming is automatically stopped)
CloseWindow(); // Close window and OpenGL context

View File

@ -24,7 +24,9 @@ int main()
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;
float timePlayed = 0.0f;
@ -58,12 +60,12 @@ int main()
SetMusicVolume(volume);
}
*/
if (IsWindowMinimized()) PauseMusicStream(0);
else ResumeMusicStream(0);
if (IsWindowMinimized()) PauseMusicStream(music);
else ResumeMusicStream(music);
timePlayed = GetMusicTimePlayed(0)/GetMusicTimeLength(0)*100*4; // We scale by 4 to fit 400 pixels
UpdateMusicStream(0); // Update music buffer with new stream data
timePlayed = GetMusicTimePlayed(music)/GetMusicTimeLength(music)*100*4; // We scale by 4 to fit 400 pixels
UpdateMusicStream(music); // Update music buffer with new stream data
//----------------------------------------------------------------------------------
// Draw
@ -83,9 +85,11 @@ int main()
// 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;

File diff suppressed because it is too large Load Diff

View File

@ -75,10 +75,9 @@ typedef struct Wave {
short channels;
} Wave;
typedef struct MusicBuffer {
char *fileName;
int index; // index in musicStreams
} MusicBuffer;
// Music type (file streaming from memory)
// NOTE: Anything longer than ~10 seconds should be streamed into a mix channel...
typedef struct Music *Music;
#ifdef __cplusplus
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 PlaySound(Sound sound); // Play 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
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 SetSoundPitch(Sound sound, float pitch); // Set pitch for a sound (1.0 is base level)
MusicBuffer LoadMusicBufferStream(char *fileName, int index);
int PlayMusicStream(MusicBuffer buffer); // Start music playing (open stream)
void UpdateMusicStream(MusicBuffer buffer); // Updates buffers for music streaming
void StopMusicStream(MusicBuffer buffer); // Stop music playing (close stream)
void PauseMusicStream(MusicBuffer buffer); // Pause music playing
void ResumeMusicStream(MusicBuffer buffer); // Resume playing paused music
bool IsMusicPlaying(MusicBuffer buffer); // Check if music is playing
void SetMusicVolume(MusicBuffer buffer float volume); // Set volume for music (1.0 is max level)
void SetMusicPitch(MusicBuffer buffer, float pitch); // Set pitch for a music (1.0 is base level)
float GetMusicTimeLength(MusicBuffer buffer); // Get music time length (in seconds)
float GetMusicTimePlayed(MusicBuffer buffer); // Get current music time played (in seconds)
int GetMusicStreamCount(void); // Get number of streams loaded
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
Music LoadMusicStream(char *fileName); // Load music stream from file
void UnloadMusicStream(Music music); // Unload music stream
void PlayMusicStream(Music music); // Start music playing (open stream)
void UpdateMusicStream(Music music); // Updates buffers for music streaming
void StopMusicStream(Music music); // Stop music playing (close stream)
void PauseMusicStream(Music music); // Pause music playing
void ResumeMusicStream(Music music); // Resume playing paused music
bool IsMusicPlaying(Music music); // Check if music is playing
void SetMusicVolume(Music music, float volume); // Set volume for music (1.0 is max level)
void SetMusicPitch(Music music, float pitch); // Set pitch for a music (1.0 is base level)
float GetMusicTimeLength(Music music); // Get music time length (in seconds)
float GetMusicTimePlayed(Music music); // Get current music time played (in seconds)
#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!
* @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.
*
@ -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 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(). */
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.
*
* @param output buffer of 2*numsamples elements (A left and right value for each sample)
* @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.
*
@ -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
* 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
* 0 when the module is still playing, 1 when the module has looped
* 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.
*/
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.
*
@ -197,43 +197,43 @@ bool jar_xm_mute_channel(jar_xm_context_t*, uint16_t, bool);
*
* @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. */
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. */
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. */
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). */
uint16_t jar_xm_get_module_length(jar_xm_context_t*);
/** 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.
*
* @note Pattern numbers go from 0 to
* 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. */
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.
*
* @note Instrument numbers go from 1 to
* 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 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.
*
@ -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 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
* 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
* 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
* 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
* 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
* instrument was triggered in a given channel.
*
* @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.
*
* @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.
*/
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
}
@ -308,7 +308,7 @@ uint64_t jar_xm_get_remaining_samples(jar_xm_context_t*);
#include <math.h>
#include <string.h>
#if JAR_XM_DEBUG
#ifdef JAR_XM_DEBUG
#include <stdio.h>
#define DEBUG(fmt, ...) do { \
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 */
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() */
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;
}
void jar_xm_free_context(jar_xm_context_t* context) {
free(context->allocated_memory);
void jar_xm_free_context(jar_xm_context_t* ctx) {
free(ctx->allocated_memory);
}
void jar_xm_set_max_loop_count(jar_xm_context_t* context, uint8_t loopcnt) {
context->max_loop_count = loopcnt;
void jar_xm_set_max_loop_count(jar_xm_context_t* ctx, uint8_t loopcnt) {
ctx->max_loop_count = loopcnt;
}
uint8_t jar_xm_get_loop_count(jar_xm_context_t* context) {
return context->loop_count;
uint8_t jar_xm_get_loop_count(jar_xm_context_t* ctx) {
return ctx->loop_count;
}
bool jar_xm_mute_channel(jar_xm_context_t* ctx, uint16_t channel, bool mute) {
bool old = ctx->channels[channel - 1].muted;
ctx->channels[channel - 1].muted = mute;

View File

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