1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164
|
/*
* Modification History
*
* 2004-July-17 Jason Rohrer
* Created.
*
* 2004-July-21 Jason Rohrer
* Switched to a passive (callback-based) player. Old player saved here.
*/
#include "SoundPlayerActive.h"
#include <stdio.h>
SoundPlayer::SoundPlayer()
: mRealtimeSounds( new SimpleVector<SoundSamples *>() ) {
PaError error = OpenAudioStream( &mAudioStream, 44100, paFloat32,
( PABLIO_WRITE | PABLIO_MONO ) );
if( error == paNoError ) {
mAudioInitialized = true;
}
else {
mAudioInitialized = false;
fprintf( stderr,
"An error occured while setting up the sound stream\n" );
fprintf( stderr, "Error number: %d\n", error );
fprintf( stderr, "Error message: %s\n", Pa_GetErrorText( error ) );
}
}
SoundPlayer::~SoundPlayer() {
if( mAudioInitialized ) {
PaError error = CloseAudioStream( mAudioStream );
if( error != paNoError ) {
fprintf(
stderr,
"An error occured while shutting down the sound stream\n" );
fprintf( stderr, "Error number: %d\n", error);
fprintf( stderr, "Error message: %s\n", Pa_GetErrorText( error) );
}
}
int numSounds = mRealtimeSounds->size();
for( int i=0; i<numSounds; i++ ) {
delete [] *( mRealtimeSounds->getElement( i ) );
}
delete mRealtimeSounds;
}
void SoundPlayer::playSoundNow( SoundSamples *inSamples ) {
mRealtimeSounds->push_back( new SoundSamples( inSamples ) );
}
unsigned long SoundPlayer::getNumMusicSamplesNeeded() {
return GetAudioStreamWriteable( mAudioStream );
}
void SoundPlayer::addMoreMusic( SoundSamples *inSamples ) {
SoundSamples *mixingBuffer = new SoundSamples( inSamples );
unsigned long bufferLength = mixingBuffer->mSampleCount;
// add each pending realtime sound to the buffer
int i = 0;
// we may be removing sounds from the buffer as we use them up
// i is adjusted inside the while loop
while( i<mRealtimeSounds->size() ) {
SoundSamples *realtimeSound = *( mRealtimeSounds->getElement( i ) );
unsigned long soundLength = realtimeSound->mSampleCount;
// limit length of this mix based on whichever is shorter, the sound
// or the mixing buffer
unsigned long mixLength = bufferLength;
if( mixLength > soundLength ) {
mixLength = soundLength;
}
for( unsigned long j=0; j<mixLength; j++ ) {
mixingBuffer->mLeftChannel[j] += realtimeSound->mLeftChannel[j];
mixingBuffer->mRightChannel[j] += realtimeSound->mRightChannel[j];
}
if( mixLength == soundLength ) {
// we have used up all samples of this sound
delete realtimeSound;
mRealtimeSounds->deleteElement( i );
// don't increment i, since the next element drops into the current
// index
}
else {
// trim off the samples that we have used
realtimeSound->trim( mixLength );
// increment i to move on to the next sound
i++;
}
}
/*
float **samples = new float*[ bufferLength ];
unsigned long j;
for( j=0; j<bufferLength; j++ ) {
samples[j] = new float[2];
samples[j][0] = mixingBuffer->mLeftChannel[j];
samples[j][1] = mixingBuffer->mRightChannel[j];
}
WriteAudioStream( mAudioStream, samples, bufferLength );
for( j=0; j<bufferLength; j++ ) {
delete [] samples[j];
}
delete [] samples;
*/
float *samples = new float[ bufferLength ];
unsigned long j;
for( j=0; j<bufferLength; j++ ) {
samples[j] = mixingBuffer->mLeftChannel[j];
}
WriteAudioStream( mAudioStream, samples, bufferLength );
delete [] samples;
delete mixingBuffer;
}
|