fixes for procedural audio input

This commit is contained in:
Stephen Birarda 2013-12-05 14:49:28 -08:00
parent 21076af739
commit 5e73e803e9
2 changed files with 15 additions and 16 deletions

View file

@ -186,11 +186,6 @@ void Audio::handleAudioInput() {
QByteArray inputByteArray = _inputDevice->read(CALLBACK_IO_BUFFER_SIZE); QByteArray inputByteArray = _inputDevice->read(CALLBACK_IO_BUFFER_SIZE);
if (_isBufferSendCallback) { if (_isBufferSendCallback) {
// this is the second half of a full buffer of data
// zero out the monoAudioSamples array
memset(monoAudioSamples, 0, BUFFER_LENGTH_BYTES_PER_CHANNEL);
// copy samples from the inputByteArray to the stereoInputBuffer // copy samples from the inputByteArray to the stereoInputBuffer
memcpy((char*) (stereoInputBuffer + bufferSizeSamples), inputByteArray.data(), inputByteArray.size()); memcpy((char*) (stereoInputBuffer + bufferSizeSamples), inputByteArray.data(), inputByteArray.size());
@ -204,6 +199,10 @@ void Audio::handleAudioInput() {
_lastInputLoudness = loudness; _lastInputLoudness = loudness;
} else { } else {
// this is the first half of a full buffer of data
// zero out the monoAudioSamples array
memset(monoAudioSamples, 0, BUFFER_LENGTH_BYTES_PER_CHANNEL);
// take samples we have in this callback and store them in the first half of the static buffer // take samples we have in this callback and store them in the first half of the static buffer
// to send off in the next callback // to send off in the next callback
memcpy((char*) stereoInputBuffer, inputByteArray.data(), inputByteArray.size()); memcpy((char*) stereoInputBuffer, inputByteArray.data(), inputByteArray.size());
@ -516,7 +515,7 @@ void Audio::render(int screenWidth, int screenHeight) {
} }
// Take a pointer to the acquired microphone input samples and add procedural sounds // Take a pointer to the acquired microphone input samples and add procedural sounds
void Audio::addProceduralSounds(int16_t* inputBuffer, int16_t* stereoOutput, int numSamples) { void Audio::addProceduralSounds(int16_t* monoInput, int16_t* stereoUpsampledOutput, int numSamples) {
const float MAX_AUDIBLE_VELOCITY = 6.0; const float MAX_AUDIBLE_VELOCITY = 6.0;
const float MIN_AUDIBLE_VELOCITY = 0.1; const float MIN_AUDIBLE_VELOCITY = 0.1;
const int VOLUME_BASELINE = 400; const int VOLUME_BASELINE = 400;
@ -531,8 +530,8 @@ void Audio::addProceduralSounds(int16_t* inputBuffer, int16_t* stereoOutput, int
// Add a noise-modulated sinewave with volume that tapers off with speed increasing // Add a noise-modulated sinewave with volume that tapers off with speed increasing
if ((speed > MIN_AUDIBLE_VELOCITY) && (speed < MAX_AUDIBLE_VELOCITY)) { if ((speed > MIN_AUDIBLE_VELOCITY) && (speed < MAX_AUDIBLE_VELOCITY)) {
for (int i = 0; i < numSamples; i++) { for (int i = 0; i < numSamples; i++) {
inputBuffer[i] += (int16_t)(sinf((float) (_proceduralEffectSample + i) / SOUND_PITCH ) monoInput[i] += (int16_t)(sinf((float) (_proceduralEffectSample + i) / SOUND_PITCH )
* volume * (1.f + randFloat() * 0.25f) * speed); * volume * (1.f + randFloat() * 0.25f) * speed);
} }
} }
const float COLLISION_SOUND_CUTOFF_LEVEL = 0.01f; const float COLLISION_SOUND_CUTOFF_LEVEL = 0.01f;
@ -545,17 +544,17 @@ void Audio::addProceduralSounds(int16_t* inputBuffer, int16_t* stereoOutput, int
for (int i = 0; i < numSamples; i++) { for (int i = 0; i < numSamples; i++) {
t = (float) _proceduralEffectSample + (float) i; t = (float) _proceduralEffectSample + (float) i;
sample = sinf(t * _collisionSoundFrequency) + sample = sinf(t * _collisionSoundFrequency)
sinf(t * _collisionSoundFrequency / DOWN_TWO_OCTAVES) + + sinf(t * _collisionSoundFrequency / DOWN_TWO_OCTAVES)
sinf(t * _collisionSoundFrequency / DOWN_FOUR_OCTAVES * UP_MAJOR_FIFTH); + sinf(t * _collisionSoundFrequency / DOWN_FOUR_OCTAVES * UP_MAJOR_FIFTH);
sample *= _collisionSoundMagnitude * COLLISION_SOUND_MAX_VOLUME; sample *= _collisionSoundMagnitude * COLLISION_SOUND_MAX_VOLUME;
int16_t collisionSample = (int16_t) sample; int16_t collisionSample = (int16_t) sample;
inputBuffer[i] += collisionSample; monoInput[i] += collisionSample;
for (int j = (i * 4); j < (i * 4) + 4; j++) { for (int j = (i * 4); j < (i * 4) + 4; j++) {
stereoOutput[j] += collisionSample; stereoUpsampledOutput[j] += collisionSample;
} }
_collisionSoundMagnitude *= _collisionSoundDuration; _collisionSoundMagnitude *= _collisionSoundDuration;
@ -578,10 +577,10 @@ void Audio::addProceduralSounds(int16_t* inputBuffer, int16_t* stereoOutput, int
int16_t collisionSample = (int16_t) sample; int16_t collisionSample = (int16_t) sample;
inputBuffer[i] += collisionSample; monoInput[i] += collisionSample;
for (int j = (i * 4); j < (i * 4) + 4; j++) { for (int j = (i * 4); j < (i * 4) + 4; j++) {
stereoOutput[j] += collisionSample; stereoUpsampledOutput[j] += collisionSample;
} }
_drumSoundVolume *= (1.f - _drumSoundDecay); _drumSoundVolume *= (1.f - _drumSoundDecay);

View file

@ -113,7 +113,7 @@ private:
inline void performIO(int16_t* inputLeft, int16_t* outputLeft, int16_t* outputRight); inline void performIO(int16_t* inputLeft, int16_t* outputLeft, int16_t* outputRight);
// Add sounds that we want the user to not hear themselves, by adding on top of mic input signal // Add sounds that we want the user to not hear themselves, by adding on top of mic input signal
void addProceduralSounds(int16_t* inputBuffer, int16_t* stereoOutput, int numSamples); void addProceduralSounds(int16_t* monoInput, int16_t* stereoUpsampledOutput, int numSamples);
void renderToolIcon(int screenHeight); void renderToolIcon(int screenHeight);
}; };