First stage of audio API update
Look over changes and give feedback please.
This commit is contained in:
parent
f707c1ca46
commit
91f1f324c0
3 changed files with 92 additions and 4 deletions
52
src/audio.c
52
src/audio.c
|
@ -97,9 +97,10 @@ typedef enum { INFO = 0, ERROR, WARNING, DEBUG, OTHER } TraceLogType;
|
||||||
//----------------------------------------------------------------------------------
|
//----------------------------------------------------------------------------------
|
||||||
// Global Variables Definition
|
// Global Variables Definition
|
||||||
//----------------------------------------------------------------------------------
|
//----------------------------------------------------------------------------------
|
||||||
|
static bool mixChannelsActive_g[4]; // What mix channels are currently active
|
||||||
static bool musicEnabled = false;
|
static bool musicEnabled = false;
|
||||||
static Music currentMusic; // Current music loaded
|
static Music currentMusic; // Current music loaded
|
||||||
// NOTE: Only one music file playing at a time
|
// NOTE: Only one music file playing at a time
|
||||||
|
|
||||||
//----------------------------------------------------------------------------------
|
//----------------------------------------------------------------------------------
|
||||||
// Module specific Functions Declaration
|
// Module specific Functions Declaration
|
||||||
|
@ -164,6 +165,53 @@ void CloseAudioDevice(void)
|
||||||
alcCloseDevice(device);
|
alcCloseDevice(device);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// True if call to InitAudioDevice() was successful and CloseAudioDevice() has not been called yet
|
||||||
|
bool AudioDeviceReady(void)
|
||||||
|
{
|
||||||
|
ALCcontext *context = alcGetCurrentContext();
|
||||||
|
if (context == NULL) return false;
|
||||||
|
else{
|
||||||
|
ALCdevice *device = alcGetContextsDevice(context);
|
||||||
|
if (device == NULL) return false;
|
||||||
|
else return true;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
//----------------------------------------------------------------------------------
|
||||||
|
// Module Functions Definition - Custom audio output
|
||||||
|
//----------------------------------------------------------------------------------
|
||||||
|
|
||||||
|
// Audio contexts are for outputing custom audio waveforms, This will shut down any other sound sources currently playing
|
||||||
|
// The mix_t is what mix channel you want to operate on, mixA->mixD are the ones available. Each mix channel can only be used one at a time.
|
||||||
|
// exmple usage is InitAudioContext(48000, 16, mixA, stereo);
|
||||||
|
AudioContext* InitAudioContext(unsigned short sampleRate, unsigned char bitsPerSample, mix_t mixChannel, channel_t channels)
|
||||||
|
{
|
||||||
|
if(!AudioDeviceReady()) InitAudioDevice();
|
||||||
|
else StopMusicStream();
|
||||||
|
|
||||||
|
if(!mixChannelsActive_g[mixChannel]){
|
||||||
|
AudioContext *ac = malloc(sizeof(AudioContext));
|
||||||
|
ac->sampleRate = sampleRate;
|
||||||
|
ac->bitsPerSample = bitsPerSample;
|
||||||
|
ac->mixChannel = mixChannel;
|
||||||
|
ac->channels = channels;
|
||||||
|
mixChannelsActive_g[mixChannel] = true;
|
||||||
|
return ac;
|
||||||
|
}
|
||||||
|
return NULL;
|
||||||
|
}
|
||||||
|
|
||||||
|
// Frees buffer in audio context
|
||||||
|
void CloseAudioContext(AudioContext *ctx)
|
||||||
|
{
|
||||||
|
if(ctx){
|
||||||
|
mixChannelsActive_g[ctx->mixChannel] = false;
|
||||||
|
free(ctx);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
//----------------------------------------------------------------------------------
|
//----------------------------------------------------------------------------------
|
||||||
// Module Functions Definition - Sounds loading and playing (.WAV)
|
// Module Functions Definition - Sounds loading and playing (.WAV)
|
||||||
//----------------------------------------------------------------------------------
|
//----------------------------------------------------------------------------------
|
||||||
|
|
25
src/audio.h
25
src/audio.h
|
@ -41,8 +41,12 @@
|
||||||
//----------------------------------------------------------------------------------
|
//----------------------------------------------------------------------------------
|
||||||
#ifndef __cplusplus
|
#ifndef __cplusplus
|
||||||
// Boolean type
|
// Boolean type
|
||||||
typedef enum { false, true } bool;
|
#ifndef true
|
||||||
|
typedef enum { false, true } bool;
|
||||||
|
#endif
|
||||||
#endif
|
#endif
|
||||||
|
typedef enum { silence, mono, stereo } channel_t;
|
||||||
|
typedef enum { mixA, mixB, mixC, mixD } mix_t; // Used for mixing/muxing up to four diferent audio streams
|
||||||
|
|
||||||
// Sound source type
|
// Sound source type
|
||||||
typedef struct Sound {
|
typedef struct Sound {
|
||||||
|
@ -59,6 +63,16 @@ typedef struct Wave {
|
||||||
short channels;
|
short channels;
|
||||||
} Wave;
|
} Wave;
|
||||||
|
|
||||||
|
// Audio Context, used to create custom audio streams that are not bound to a sound file. There can be
|
||||||
|
// no more than 4 concurrent audio contexts in use. This is due to each active context being tied to
|
||||||
|
// a dedicated mix channel.
|
||||||
|
typedef struct AudioContext {
|
||||||
|
unsigned short sampleRate; // default is 48000
|
||||||
|
unsigned char bitsPerSample; // 16 is default
|
||||||
|
mix_t mixChannel; // 0-3 or mixA-mixD, each mix channel can receive up to one dedicated audio stream
|
||||||
|
channel_t channels; // 1=mono, 2=stereo
|
||||||
|
} AudioContext;
|
||||||
|
|
||||||
#ifdef __cplusplus
|
#ifdef __cplusplus
|
||||||
extern "C" { // Prevents name mangling of functions
|
extern "C" { // Prevents name mangling of functions
|
||||||
#endif
|
#endif
|
||||||
|
@ -73,6 +87,13 @@ extern "C" { // Prevents name mangling of functions
|
||||||
//----------------------------------------------------------------------------------
|
//----------------------------------------------------------------------------------
|
||||||
void InitAudioDevice(void); // Initialize audio device and context
|
void InitAudioDevice(void); // Initialize audio device and context
|
||||||
void CloseAudioDevice(void); // Close the audio device and context (and music stream)
|
void CloseAudioDevice(void); // Close the audio device and context (and music stream)
|
||||||
|
bool AudioDeviceReady(void); // True if call to InitAudioDevice() was successful and CloseAudioDevice() has not been called yet
|
||||||
|
|
||||||
|
// Audio contexts are for outputing custom audio waveforms, This will shut down any other sound sources currently playing
|
||||||
|
// The mix_t is what mix channel you want to operate on, mixA->mixD are the ones available. Each mix channel can only be used one at a time.
|
||||||
|
// exmple usage is InitAudioContext(48000, 16, mixA, stereo);
|
||||||
|
AudioContext* InitAudioContext(unsigned short sampleRate, unsigned char bitsPerSample, mix_t mixChannel, channel_t channels);
|
||||||
|
void CloseAudioContext(AudioContext *ctx); // Frees audio context
|
||||||
|
|
||||||
Sound LoadSound(char *fileName); // Load sound to memory
|
Sound LoadSound(char *fileName); // Load sound to memory
|
||||||
Sound LoadSoundFromWave(Wave wave); // Load sound to memory from wave data
|
Sound LoadSoundFromWave(Wave wave); // Load sound to memory from wave data
|
||||||
|
@ -92,7 +113,7 @@ void PauseMusicStream(void); // Pause music p
|
||||||
void ResumeMusicStream(void); // Resume playing paused music
|
void ResumeMusicStream(void); // Resume playing paused music
|
||||||
bool MusicIsPlaying(void); // Check if music is playing
|
bool MusicIsPlaying(void); // Check if music is playing
|
||||||
void SetMusicVolume(float volume); // Set volume for music (1.0 is max level)
|
void SetMusicVolume(float volume); // Set volume for music (1.0 is max level)
|
||||||
float GetMusicTimeLength(void); // Get current music time length (in seconds)
|
float GetMusicTimeLength(void); // Get music time length (in seconds)
|
||||||
float GetMusicTimePlayed(void); // Get current music time played (in seconds)
|
float GetMusicTimePlayed(void); // Get current music time played (in seconds)
|
||||||
|
|
||||||
#ifdef __cplusplus
|
#ifdef __cplusplus
|
||||||
|
|
19
src/raylib.h
19
src/raylib.h
|
@ -265,6 +265,8 @@
|
||||||
typedef enum { false, true } bool;
|
typedef enum { false, true } bool;
|
||||||
#endif
|
#endif
|
||||||
#endif
|
#endif
|
||||||
|
typedef enum { silence, mono, stereo } channel_t;
|
||||||
|
typedef enum { mixA, mixB, mixC, mixD } mix_t; // Used for mixing/muxing up to four diferent audio streams
|
||||||
|
|
||||||
// byte type
|
// byte type
|
||||||
typedef unsigned char byte;
|
typedef unsigned char byte;
|
||||||
|
@ -446,6 +448,16 @@ typedef struct Wave {
|
||||||
short channels;
|
short channels;
|
||||||
} Wave;
|
} Wave;
|
||||||
|
|
||||||
|
// Audio Context, used to create custom audio streams that are not bound to a sound file. There can be
|
||||||
|
// no more than 4 concurrent audio contexts in use. This is due to each active context being tied to
|
||||||
|
// a dedicated mix channel.
|
||||||
|
typedef struct AudioContext {
|
||||||
|
unsigned short sampleRate; // default is 48000
|
||||||
|
unsigned char bitsPerSample; // 16 is default
|
||||||
|
mix_t mixChannel; // 0-3 or mixA-mixD, each mix channel can receive up to one dedicated audio stream
|
||||||
|
channel_t channels; // 1=mono, 2=stereo
|
||||||
|
} AudioContext;
|
||||||
|
|
||||||
// Texture formats
|
// Texture formats
|
||||||
// NOTE: Support depends on OpenGL version and platform
|
// NOTE: Support depends on OpenGL version and platform
|
||||||
typedef enum {
|
typedef enum {
|
||||||
|
@ -863,6 +875,13 @@ void DrawPhysicObjectInfo(PhysicObject *pObj, Vector2 position, int fontSize);
|
||||||
//------------------------------------------------------------------------------------
|
//------------------------------------------------------------------------------------
|
||||||
void InitAudioDevice(void); // Initialize audio device and context
|
void InitAudioDevice(void); // Initialize audio device and context
|
||||||
void CloseAudioDevice(void); // Close the audio device and context (and music stream)
|
void CloseAudioDevice(void); // Close the audio device and context (and music stream)
|
||||||
|
bool AudioDeviceReady(void); // True if call to InitAudioDevice() was successful and CloseAudioDevice() has not been called yet
|
||||||
|
|
||||||
|
// Audio contexts are for outputing custom audio waveforms, This will shut down any other sound sources currently playing
|
||||||
|
// The mix_t is what mix channel you want to operate on, mixA->mixD are the ones available. Each mix channel can only be used one at a time.
|
||||||
|
// exmple usage is InitAudioContext(48000, 16, mixA, stereo);
|
||||||
|
AudioContext* InitAudioContext(unsigned short sampleRate, unsigned char bitsPerSample, mix_t mixChannel, channel_t channels);
|
||||||
|
void CloseAudioContext(AudioContext *ctx); // Frees audio context
|
||||||
|
|
||||||
Sound LoadSound(char *fileName); // Load sound to memory
|
Sound LoadSound(char *fileName); // Load sound to memory
|
||||||
Sound LoadSoundFromWave(Wave wave); // Load sound to memory from wave data
|
Sound LoadSoundFromWave(Wave wave); // Load sound to memory from wave data
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue