aboutsummaryrefslogtreecommitdiff
path: root/src
diff options
context:
space:
mode:
authorJoshua Reisenauer <kd7tck@msn.com>2016-05-02 01:24:24 -0700
committerJoshua Reisenauer <kd7tck@msn.com>2016-05-02 01:24:24 -0700
commit9ef0240e99e7e6a626fdb8fee4f8a81eea21f3e2 (patch)
tree2f4c03cde96fa00c62feacd1523af7ce14a51374 /src
parent790bc7280685d714ddce3e1ee0afa11cee0c5e06 (diff)
downloadraylib-9ef0240e99e7e6a626fdb8fee4f8a81eea21f3e2.tar.gz
raylib-9ef0240e99e7e6a626fdb8fee4f8a81eea21f3e2.zip
resamples added
Ease of use considered in api and channels are more convenient as unsigned char type.
Diffstat (limited to 'src')
-rw-r--r--src/audio.c49
-rw-r--r--src/audio.h6
-rw-r--r--src/raylib.h6
3 files changed, 49 insertions, 12 deletions
diff --git a/src/audio.c b/src/audio.c
index 7b42b089..d935b6c7 100644
--- a/src/audio.c
+++ b/src/audio.c
@@ -97,6 +97,7 @@ typedef struct Music {
// a dedicated mix channel. All audio is 32bit floating point in stereo.
typedef struct AudioContext_t {
unsigned short sampleRate; // default is 48000
+ unsigned char channels; // 1=mono,2=stereo
unsigned char mixChannel; // 0-3 or mixA-mixD, each mix channel can receive up to one dedicated audio stream
ALenum alFormat; // openAL format specifier
ALuint alSource; // openAL source
@@ -125,8 +126,9 @@ static void UnloadWave(Wave wave); // Unload wave data
static bool BufferMusicStream(ALuint buffer); // Fill music buffers with data
static void EmptyMusicStream(void); // Empty music buffers
-// fill buffer with zeros
-static void FillAlBufferWithSilence(AudioContext_t *ac, ALuint buffer);
+static void FillAlBufferWithSilence(AudioContext_t *ac, ALuint buffer);// fill buffer with zeros
+static void ResampleShortToFloat(short *shorts, float *floats, unsigned short len); // pass two arrays of the same legnth in
+static void ResampleByteToFloat(char *chars, float *floats, unsigned short len); // pass two arrays of same length in
#if defined(AUDIO_STANDALONE)
const char *GetExtension(const char *fileName); // Get the extension for a filename
@@ -199,9 +201,9 @@ bool IsAudioDeviceReady(void)
// Audio contexts are for outputing custom audio waveforms, This will shut down any other sound sources currently playing
// The mixChannel is what mix channel you want to operate on, 0-3 are the ones available. Each mix channel can only be used one at a time.
-// exmple usage is InitAudioContext(48000, 0); // mixchannel 1, 48khz
-// all samples are floating point stereo by default
-AudioContext InitAudioContext(unsigned short sampleRate, unsigned char mixChannel)
+// exmple usage is InitAudioContext(48000, 0, 2); // mixchannel 1, 48khz, stereo
+// all samples are floating point by default
+AudioContext InitAudioContext(unsigned short sampleRate, unsigned char mixChannel, unsigned char channels)
{
if(mixChannel > MAX_AUDIO_CONTEXTS) return NULL;
if(!IsAudioDeviceReady()) InitAudioDevice();
@@ -210,11 +212,15 @@ AudioContext InitAudioContext(unsigned short sampleRate, unsigned char mixChanne
if(!mixChannelsActive_g[mixChannel]){
AudioContext_t *ac = malloc(sizeof(AudioContext_t));
ac->sampleRate = sampleRate;
+ ac->channels = channels;
ac->mixChannel = mixChannel;
mixChannelsActive_g[mixChannel] = ac;
// setup openAL format
- ac->alFormat = AL_FORMAT_STEREO_FLOAT32;
+ if(channels == 1)
+ ac->alFormat = AL_FORMAT_MONO_FLOAT32;
+ else
+ ac->alFormat = AL_FORMAT_STEREO_FLOAT32;
// Create an audio source
alGenSources(1, &ac->alSource);
@@ -289,6 +295,7 @@ bool UpdateAudioContext(AudioContext ctx, float *data, unsigned short dataLength
}
return true;
}
+ return false;
}
// fill buffer with zeros
@@ -298,6 +305,36 @@ static void FillAlBufferWithSilence(AudioContext_t *ac, ALuint buffer)
alBufferData(buffer, ac->alFormat, pcm, MUSIC_BUFFER_SIZE*sizeof(float), ac->sampleRate);
}
+// example usage:
+// short sh[3] = {1,2,3};float fl[3];
+// ResampleShortToFloat(sh,fl,3);
+static void ResampleShortToFloat(short *shorts, float *floats, unsigned short len)
+{
+ int x;
+ for(x=0;x<len;x++)
+ {
+ if(shorts[x] < 0)
+ floats[x] = (float)shorts[x] / 32766.f;
+ else
+ floats[x] = (float)shorts[x] / 32767.f;
+ }
+}
+
+// example usage:
+// char ch[3] = {1,2,3};float fl[3];
+// ResampleShortToFloat(ch,fl,3);
+static void ResampleByteToFloat(char *chars, float *floats, unsigned short len)
+{
+ int x;
+ for(x=0;x<len;x++)
+ {
+ if(chars[x] < 0)
+ floats[x] = (float)chars[x] / 127.f;
+ else
+ floats[x] = (float)chars[x] / 128.f;
+ }
+}
+
//----------------------------------------------------------------------------------
diff --git a/src/audio.h b/src/audio.h
index 4a198c59..d723ef1b 100644
--- a/src/audio.h
+++ b/src/audio.h
@@ -84,9 +84,9 @@ bool IsAudioDeviceReady(void); // True if call
// Audio contexts are for outputing custom audio waveforms, This will shut down any other sound sources currently playing
// The mixChannel is what mix channel you want to operate on, 0-3 are the ones available. Each mix channel can only be used one at a time.
-// exmple usage is InitAudioContext(48000, 0); // mixchannel 1, 48khz
-// all samples are floating point stereo by default
-AudioContext InitAudioContext(unsigned short sampleRate, unsigned char mixChannel);
+// exmple usage is InitAudioContext(48000, 0, 2); // mixchannel 1, 48khz, stereo
+// all samples are floating point by default
+AudioContext InitAudioContext(unsigned short sampleRate, unsigned char mixChannel, unsigned char channels);
void CloseAudioContext(AudioContext ctx); // Frees audio context
bool UpdateAudioContext(AudioContext ctx, float *data, unsigned short dataLength); // Pushes more audio data into context mix channel, if NULL is passed to data then zeros are played
diff --git a/src/raylib.h b/src/raylib.h
index 9c5a8258..3fa20116 100644
--- a/src/raylib.h
+++ b/src/raylib.h
@@ -870,9 +870,9 @@ bool IsAudioDeviceReady(void); // True if call
// Audio contexts are for outputing custom audio waveforms, This will shut down any other sound sources currently playing
// The mixChannel is what mix channel you want to operate on, 0-3 are the ones available. Each mix channel can only be used one at a time.
-// exmple usage is InitAudioContext(48000, 0); // mixchannel 1, 48khz
-// all samples are floating point stereo by default
-AudioContext InitAudioContext(unsigned short sampleRate, unsigned char mixChannel);
+// exmple usage is InitAudioContext(48000, 0, 2); // mixchannel 1, 48khz, stereo
+// all samples are floating point by default
+AudioContext InitAudioContext(unsigned short sampleRate, unsigned char mixChannel, unsigned char channels);
void CloseAudioContext(AudioContext ctx); // Frees audio context
bool UpdateAudioContext(AudioContext ctx, float *data, unsigned short dataLength); // Pushes more audio data into context mix channel, if NULL is passed to data then zeros are played