mirror of
https://thingvellir.net/git/overte
synced 2025-03-27 23:52:03 +01:00
simplify audio code, allow passing of Head object
This commit is contained in:
parent
8a7db1f63c
commit
4d914128ea
6 changed files with 97 additions and 439 deletions
|
@ -10,7 +10,7 @@ int init_port (int baud);
|
|||
int read_sensors(int first_measurement, float * avg_adc_channels, int * adc_channels, int * samples_averaged, int * LED_state);
|
||||
|
||||
#define NUM_CHANNELS 6
|
||||
#define SERIAL_PORT_NAME "/dev/tty.usbmodem1411"
|
||||
#define SERIAL_PORT_NAME "/dev/tty.usbmodemfa141"
|
||||
|
||||
// Acceleration sensors, in screen/world coord system (X = left/right, Y = Up/Down, Z = fwd/back)
|
||||
#define ACCEL_X 4
|
||||
|
|
354
Source/audio.cpp
354
Source/audio.cpp
|
@ -2,49 +2,36 @@
|
|||
// audio.cpp
|
||||
// interface
|
||||
//
|
||||
// Created by Seiji Emery on 9/2/12.
|
||||
// Copyright (c) 2012 __MyCompanyName__. All rights reserved.
|
||||
// Created by Stephen Birarda on 1/22/13.
|
||||
// Copyright (c) 2013 Rosedale Lab. All rights reserved.
|
||||
//
|
||||
|
||||
/**
|
||||
* @file audio.cpp
|
||||
* Low level audio i/o portaudio wrapper.
|
||||
*
|
||||
* @author Seiji Emery
|
||||
*
|
||||
*/
|
||||
|
||||
#include <cstdlib>
|
||||
#include <cstdio>
|
||||
#include <cstring>
|
||||
#include "audio.h"
|
||||
|
||||
// static member definitions
|
||||
// (required – will cause linker errors if left out...):
|
||||
bool Audio::initialized;
|
||||
Audio::AudioData *Audio::data;
|
||||
PaStream *Audio::stream;
|
||||
PaError Audio::err;
|
||||
float Audio::AudioData::inputGain;
|
||||
PaStream *Audio::stream;
|
||||
Audio::AudioData *Audio::data;
|
||||
|
||||
|
||||
/**
|
||||
* Audio callback used by portaudio.
|
||||
* Communicates with Audio via a shared pointer to Audio::data.
|
||||
* Writes input audio channels (if they exist) into Audio::data->buffer,
|
||||
multiplied by Audio::data->inputGain.
|
||||
multiplied by Audio::data->inputGain.
|
||||
* Then writes Audio::data->buffer into output audio channels, and clears
|
||||
the portion of Audio::data->buffer that has been read from for reuse.
|
||||
the portion of Audio::data->buffer that has been read from for reuse.
|
||||
*
|
||||
* @param[in] inputBuffer A pointer to an internal portaudio data buffer containing data read by portaudio.
|
||||
* @param[out] outputBuffer A pointer to an internal portaudio data buffer to be read by the configured output device.
|
||||
* @param[in] frames Number of frames that portaudio requests to be read/written.
|
||||
(Valid size of input/output buffers = frames * number of channels (2) * sizeof data type (float)).
|
||||
(Valid size of input/output buffers = frames * number of channels (2) * sizeof data type (float)).
|
||||
* @param[in] timeInfo Portaudio time info. Currently unused.
|
||||
* @param[in] statusFlags Portaudio status flags. Currently unused.
|
||||
* @param[in] userData Pointer to supplied user data (in this case, a pointer to Audio::data).
|
||||
Used to communicate with external code (since portaudio calls this function from another thread).
|
||||
Used to communicate with external code (since portaudio calls this function from another thread).
|
||||
* @return Should be of type PaStreamCallbackResult. Return paComplete to end the stream, or paContinue to continue (default).
|
||||
Can be used to end the stream from within the callback.
|
||||
Can be used to end the stream from within the callback.
|
||||
*/
|
||||
|
||||
int audioCallback (const void *inputBuffer,
|
||||
|
@ -52,83 +39,19 @@ int audioCallback (const void *inputBuffer,
|
|||
unsigned long frames,
|
||||
const PaStreamCallbackTimeInfo *timeInfo,
|
||||
PaStreamCallbackFlags statusFlags,
|
||||
void *userData)
|
||||
void *userData)
|
||||
{
|
||||
Audio::AudioData *data = (Audio::AudioData*)userData;
|
||||
float *input = (float*)inputBuffer;
|
||||
float *output = (float*)outputBuffer;
|
||||
Audio::AudioData *data = (Audio::AudioData *) userData;
|
||||
int16_t *input = (int16_t *) inputBuffer;
|
||||
int16_t *output = (int16_t *) outputBuffer;
|
||||
|
||||
#if WRITE_AUDIO_INPUT_TO_OUTPUT
|
||||
if (input != NULL) {// && Audio::writeAudioInputToOutput) {
|
||||
// combine input into data buffer
|
||||
|
||||
// temp variables (frames and bufferPos need to remain untouched so they can be used in the second block of code)
|
||||
unsigned int f = (unsigned int)frames,
|
||||
p = data->bufferPos;
|
||||
for (; p < data->bufferLength && f > 0; --f, ++p) {
|
||||
#if WRITE_AUDIO_INPUT_TO_BUFFER
|
||||
data->buffer[p].l +=
|
||||
data->inputBuffer[p].l = (*input++) * data->inputGain;
|
||||
data->buffer[p].r +=
|
||||
data->inputBuffer[p].r = (*input++) * data->inputGain;
|
||||
#else
|
||||
data->buffer[p].l += (*input++) * data->inputGain;
|
||||
data->buffer[p].r += (*input++) * data->inputGain;
|
||||
#endif
|
||||
}
|
||||
if (f > 0) {
|
||||
// handle data->buffer wraparound
|
||||
for (p = 0; f > 0; --f, ++p) {
|
||||
#if WRITE_AUDIO_INPUT_TO_BUFFER
|
||||
data->buffer[p].l +=
|
||||
data->inputBuffer[p].l = (*input++) * data->inputGain;
|
||||
data->buffer[p].r +=
|
||||
data->inputBuffer[p].r = (*input++) * data->inputGain;
|
||||
#else
|
||||
data->buffer[p].l += (*input++) * data->inputGain;
|
||||
data->buffer[p].r += (*input++) * data->inputGain;
|
||||
#endif
|
||||
}
|
||||
}
|
||||
// check if we have input data
|
||||
if (input != NULL) {
|
||||
memcpy(data->buffer, input, data->bufferLength * 2);
|
||||
}
|
||||
|
||||
#elif WRITE_AUDIO_INPUT_TO_BUFFER
|
||||
if (input != NULL) {// && Audio::writeAudioInputToBuffer) {
|
||||
unsigned int f = (unsigned int)frames,
|
||||
p = data->bufferPos;
|
||||
for (; p < data->bufferLength && f > 0; --f, ++p) {
|
||||
data->inputBuffer[p].l = (*input++) * data->inputGain;
|
||||
data->inputBuffer[p].r = (*input++) * data->inputGain;
|
||||
}
|
||||
if (f > 0) {
|
||||
// handle data->buffer wraparound
|
||||
for (p = 0; f > 0; --f, ++p) {
|
||||
data->inputBuffer[p].l = (*input++) * data->inputGain;
|
||||
data->inputBuffer[p].r = (*input++) * data->inputGain;
|
||||
}
|
||||
}
|
||||
}
|
||||
#endif
|
||||
|
||||
// Write data->buffer into outputBuffer
|
||||
if (data->bufferPos + frames >= data->bufferLength) {
|
||||
// wraparound: write first section (end of buffer) first
|
||||
|
||||
// note: buffer is just an array of a struct of floats, so it can be typecast to float*
|
||||
memcpy(output, (float*)(data->buffer + data->bufferPos), // write data buffer
|
||||
(data->bufferLength - data->bufferPos) * 2 * sizeof(float));
|
||||
memset((float*)(data->buffer + data->bufferPos), 0, // clear data buffer
|
||||
(data->bufferLength - data->bufferPos) * 2 * sizeof(float));
|
||||
frames -= (data->bufferLength - data->bufferPos); // adjust frames to be written
|
||||
data->bufferPos = 0; // reset position to start
|
||||
}
|
||||
|
||||
memcpy(output, (float*)(data->buffer + data->bufferPos), // write data buffer
|
||||
frames * 2 * sizeof(float));
|
||||
memset((float*)(data->buffer + data->bufferPos), 0, // clear data buffer
|
||||
frames * 2 * sizeof(float));
|
||||
data->bufferPos += frames; // update position
|
||||
|
||||
memcpy(output, data->buffer, data->bufferLength * 2);
|
||||
|
||||
return paContinue;
|
||||
}
|
||||
|
||||
|
@ -137,25 +60,32 @@ int audioCallback (const void *inputBuffer,
|
|||
* Should be called at the beginning of program exection.
|
||||
* @seealso Audio::terminate
|
||||
* @return Returns true if successful or false if an error occurred.
|
||||
Use Audio::getError() to retrieve the error code.
|
||||
Use Audio::getError() to retrieve the error code.
|
||||
*/
|
||||
bool Audio::init()
|
||||
bool Audio::init()
|
||||
{
|
||||
return Audio::init(NULL);
|
||||
}
|
||||
|
||||
bool Audio::init(Head* mainHead)
|
||||
{
|
||||
initialized = true;
|
||||
|
||||
data = new AudioData();
|
||||
data->linkedHead = mainHead;
|
||||
|
||||
err = Pa_Initialize();
|
||||
if (err != paNoError) goto error;
|
||||
|
||||
err = Pa_OpenDefaultStream(&stream,
|
||||
err = Pa_OpenDefaultStream(&stream,
|
||||
1, // input channels
|
||||
1, // output channels
|
||||
paFloat32, // sample format
|
||||
paInt16, // sample format
|
||||
22050, // sample rate (hz)
|
||||
512, // frames per buffer
|
||||
audioCallback, // callback function
|
||||
(void*)data); // user data to be passed to callback
|
||||
(void *) data); // user data to be passed to callback
|
||||
|
||||
if (err != paNoError) goto error;
|
||||
|
||||
err = Pa_StartStream(stream);
|
||||
|
@ -174,221 +104,29 @@ error:
|
|||
* Close the running audio stream, and deinitialize portaudio.
|
||||
* Should be called at the end of program execution.
|
||||
* @return Returns true if the initialization was successful, or false if an error occured.
|
||||
The error code may be retrieved by Audio::getError().
|
||||
The error code may be retrieved by Audio::getError().
|
||||
*/
|
||||
bool Audio::terminate ()
|
||||
{
|
||||
if (!initialized)
|
||||
if (!initialized) {
|
||||
return true;
|
||||
initialized = false;
|
||||
// err = Pa_StopStream(stream);
|
||||
// if (err != paNoError) goto error;
|
||||
} else {
|
||||
initialized = false;
|
||||
|
||||
err = Pa_CloseStream(stream);
|
||||
if (err != paNoError) goto error;
|
||||
|
||||
delete data;
|
||||
|
||||
err = Pa_Terminate();
|
||||
if (err != paNoError) goto error;
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
err = Pa_CloseStream(stream);
|
||||
if (err != paNoError) goto error;
|
||||
|
||||
delete data;
|
||||
|
||||
err = Pa_Terminate();
|
||||
if (err != paNoError) goto error;
|
||||
|
||||
return true;
|
||||
error:
|
||||
fprintf(stderr, "-- portaudio termination error --\n");
|
||||
fprintf(stderr, "PortAudio error (%d): %s\n", err, Pa_GetErrorText(err));
|
||||
return false;
|
||||
}
|
||||
|
||||
/**
|
||||
* Write a stereo audio stream (float*) to the audio buffer.
|
||||
* Values should be clamped between -1.0f and 1.0f.
|
||||
* @param[in] offset Write offset from the start of the audio buffer.
|
||||
* @param[in] length Length of audio channels to be read.
|
||||
* @param[in] left Left channel of the audio stream.
|
||||
* @param[in] right Right channel of the audio stream.
|
||||
*/
|
||||
void Audio::writeAudio (unsigned int offset, unsigned int length, float const *left, float const *right) {
|
||||
if (data->buffer == NULL)
|
||||
return;
|
||||
if (length > data->bufferLength) {
|
||||
fprintf(stderr, "Audio::writeAudio length exceeded (%d). Truncating to %d.\n", length, data->bufferLength);
|
||||
length = data->bufferLength;
|
||||
}
|
||||
unsigned int p = data->bufferPos + offset;
|
||||
if (p > data->bufferLength)
|
||||
p -= data->bufferLength;
|
||||
for (; p < data->bufferLength && length > 0; --length, ++p) {
|
||||
data->buffer[p].l = *left++;
|
||||
data->buffer[p].r = *right++;
|
||||
}
|
||||
if (length > 0) {
|
||||
p = 0;
|
||||
for (; length > 0; --length, ++p) {
|
||||
data->buffer[p].l = *left++;
|
||||
data->buffer[p].r = *right++;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Write a repeated stereo sample (float) to the audio buffer.
|
||||
* Values should be clamped between -1.0f and 1.0f.
|
||||
* @param[in] offset Write offset from the start of the audio buffer.
|
||||
* @param[in] length Length of tone.
|
||||
* @param[in] left Left component.
|
||||
* @param[in] right Right component.
|
||||
*/
|
||||
void Audio::writeTone (unsigned int offset, unsigned int length, float const left, float const right) {
|
||||
if (data->buffer == NULL)
|
||||
return;
|
||||
if (length > data->bufferLength) {
|
||||
fprintf(stderr, "Audio::writeTone length exceeded (%d). Truncating to %d.\n", length, data->bufferLength);
|
||||
length = data->bufferLength;
|
||||
}
|
||||
unsigned int p = data->bufferPos + offset;
|
||||
if (p > data->bufferLength)
|
||||
p -= data->bufferLength;
|
||||
for (; p < data->bufferLength && length > 0; --length, ++p) {
|
||||
data->buffer[p].l = left;
|
||||
data->buffer[p].r = right;
|
||||
}
|
||||
if (length > 0) {
|
||||
p = 0;
|
||||
for (; length > 0; --length, ++p) {
|
||||
data->buffer[p].l = left;
|
||||
data->buffer[p].r = right;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Write a stereo audio stream (float*) to the audio buffer.
|
||||
* Audio stream is added to the existing contents of the audio buffer.
|
||||
* Values should be clamped between -1.0f and 1.0f.
|
||||
* @param[in] offset Write offset from the start of the audio buffer.
|
||||
* @param[in] length Length of audio channels to be read.
|
||||
* @param[in] left Left channel of the audio stream.
|
||||
* @param[in] right Right channel of the audio stream.
|
||||
*/
|
||||
void Audio::addAudio (unsigned int offset, unsigned int length, float const *left, float const *right) {
|
||||
if (data->buffer == NULL)
|
||||
return;
|
||||
if (length > data->bufferLength) {
|
||||
fprintf(stderr, "Audio::addAudio length exceeded (%d). Truncating to %d.\n", length, data->bufferLength);
|
||||
length = data->bufferLength;
|
||||
}
|
||||
unsigned int p = data->bufferPos + offset;
|
||||
if (p > data->bufferLength)
|
||||
p -= data->bufferLength;
|
||||
for (; p < data->bufferLength && length > 0; --length, ++p) {
|
||||
data->buffer[p].l += *left++;
|
||||
data->buffer[p].r += *right++;
|
||||
}
|
||||
if (length > 0) {
|
||||
p = 0;
|
||||
for (; length > 0; --length, ++p) {
|
||||
data->buffer[p].l += *left++;
|
||||
data->buffer[p].r += *right++;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Write a repeated stereo sample (float) to the audio buffer.
|
||||
* Sample is added to the existing contents of the audio buffer.
|
||||
* Values should be clamped between -1.0f and 1.0f.
|
||||
* @param[in] offset Write offset from the start of the audio buffer.
|
||||
* @param[in] length Length of tone.
|
||||
* @param[in] left Left component.
|
||||
* @param[in] right Right component.
|
||||
*/
|
||||
void Audio::addTone (unsigned int offset, unsigned int length, float const left, float const right) {
|
||||
if (data->buffer == NULL)
|
||||
return;
|
||||
if (length > data->bufferLength) {
|
||||
fprintf(stderr, "Audio::writeTone length exceeded (%d). Truncating to %d.\n", length, data->bufferLength);
|
||||
length = data->bufferLength;
|
||||
}
|
||||
unsigned int p = data->bufferPos + offset;
|
||||
if (p > data->bufferLength)
|
||||
p -= data->bufferLength;
|
||||
for (; p < data->bufferLength && length > 0; --length, ++p) {
|
||||
data->buffer[p].l += left;
|
||||
data->buffer[p].r += right;
|
||||
}
|
||||
if (length > 0) {
|
||||
p = 0;
|
||||
for (; length > 0; --length, ++p) {
|
||||
data->buffer[p].l += left;
|
||||
data->buffer[p].r += right;
|
||||
}
|
||||
}
|
||||
}
|
||||
/**
|
||||
* Clear a section of the audio buffer.
|
||||
* @param[in] offset Offset from the start of the audio buffer.
|
||||
* @param[in] length Length of section to clear.
|
||||
*/
|
||||
void Audio::clearAudio(unsigned int offset, unsigned int length) {
|
||||
if (data->buffer == NULL)
|
||||
return;
|
||||
if (length > data->bufferLength) {
|
||||
fprintf(stderr, "Audio::clearAudio length exceeded (%d). Truncating to %d.\n", length, data->bufferLength);
|
||||
length = data->bufferLength;
|
||||
}
|
||||
unsigned int p = data->bufferPos + offset;
|
||||
if (p > data->bufferLength)
|
||||
p -= data->bufferLength;
|
||||
if (length + p < data->bufferLength) {
|
||||
memset((float*)(data->buffer + p), 0,
|
||||
sizeof(float) * 2 * length);
|
||||
} else {
|
||||
memset((float*)(data->buffer + p), 0,
|
||||
sizeof(float) * 2 * (data->bufferLength - p));
|
||||
memset((float*)(data->buffer + p), 0,
|
||||
sizeof(float) * 2 * (data->bufferLength + p - data->bufferLength));
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Read audio input into the target buffer.
|
||||
* @param[in] offset Offset from the start of the input audio buffer to read from.
|
||||
* @param[in] length Length of the target buffer.
|
||||
* @param[out] left Left channel of the target buffer.
|
||||
* @param[out] right Right channel of the target buffer.
|
||||
*/
|
||||
void Audio::readAudioInput (unsigned int offset, unsigned int length, float *left, float *right) {
|
||||
#if WRITE_AUDIO_INPUT_TO_BUFFER
|
||||
if (data->inputBuffer == NULL)
|
||||
return;
|
||||
if (length + offset > data->bufferLength) {
|
||||
fprintf(stderr, "Audio::readAudioInput length exceeded (%d + %d). Truncating to %d + %d.\n", offset, length, offset, data->bufferLength - offset);
|
||||
length = data->bufferLength - offset;
|
||||
}
|
||||
unsigned int p = data->bufferPos + offset;
|
||||
if (p > data->bufferLength)
|
||||
p -= data->bufferLength;
|
||||
for (; p < data->bufferLength && length > 0; --length, ++p) {
|
||||
*left++ = data->inputBuffer[p].l;
|
||||
*right++ = data->inputBuffer[p].r;
|
||||
}
|
||||
if (length > 0) {
|
||||
p = 0;
|
||||
for (; length > 0; --length, ++p) {
|
||||
*left++ = data->inputBuffer[p].l;
|
||||
*right++ = data->inputBuffer[p].r;
|
||||
}
|
||||
}
|
||||
#else
|
||||
return;
|
||||
#endif
|
||||
}
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
|
151
Source/audio.h
151
Source/audio.h
|
@ -2,140 +2,60 @@
|
|||
// audio.h
|
||||
// interface
|
||||
//
|
||||
// Created by Seiji Emery on 9/2/12.
|
||||
// Copyright (c) 2012 __MyCompanyName__. All rights reserved.
|
||||
// Created by Stephen Birarda on 1/22/13.
|
||||
// Copyright (c) 2013 Rosedale Lab. All rights reserved.
|
||||
//
|
||||
|
||||
#ifndef interface_audio_h
|
||||
#define interface_audio_h
|
||||
#ifndef __interface__audio__
|
||||
#define __interface__audio__
|
||||
|
||||
#include <iostream>
|
||||
#include "portaudio.h"
|
||||
#include "head.h"
|
||||
|
||||
// Note: main documentation in audio.cpp
|
||||
|
||||
/**
|
||||
* If enabled, direct the audio callback to write the audio input buffer
|
||||
* directly into the audio output buffer.
|
||||
*/
|
||||
#define WRITE_AUDIO_INPUT_TO_OUTPUT 1
|
||||
/**
|
||||
* If enabled, create an additional buffer to store audio input
|
||||
* and direct the audio callback to write the audio input to this buffer.
|
||||
*/
|
||||
#define WRITE_AUDIO_INPUT_TO_BUFFER 0
|
||||
|
||||
// Note: I initially used static const bools within the Audio class and normal
|
||||
// 'if' blocks instead of preprocessor - under the assumption that the compiler
|
||||
// would optimize out the redundant code.
|
||||
// However, as that apparently did not work (for some reason or another - even
|
||||
// with full compiler optimization turned on), I've switched to using preprocessor
|
||||
// macros instead (which is probably faster anyways (at compile-time)).
|
||||
|
||||
/**
|
||||
* Low level audio interface.
|
||||
*
|
||||
* Contains static methods that write to an internal audio buffer, which
|
||||
is read from by a portaudio callback.
|
||||
* Responsible for initializing and terminating portaudio. Audio::init()
|
||||
and Audio::terminate() should be called at the beginning and end of
|
||||
program execution.
|
||||
*/
|
||||
class Audio {
|
||||
public:
|
||||
// Initializes portaudio. Should be called at the beginning of program execution.
|
||||
static bool init ();
|
||||
// Deinitializes portaudio. Should be called at the end of program execution.
|
||||
static bool terminate ();
|
||||
|
||||
// Write methods: write to internal audio buffer.
|
||||
static void writeAudio (unsigned int offset, unsigned int length, float const *left, float const *right);
|
||||
static void addAudio (unsigned int offset, unsigned int length, float const *left, float const *right);
|
||||
static void writeTone (unsigned int offset, unsigned int length, float const left, float const right);
|
||||
static void addTone (unsigned int offset, unsigned int length, float const left, float const right);
|
||||
static void clearAudio (unsigned int offset, unsigned int length);
|
||||
|
||||
// Read data from internal 'input' audio buffer to an external audio buffer.
|
||||
// (*only* works if WRITE_AUDIO_INPUT_TO_BUFFER is enabled).
|
||||
static void readAudioInput (unsigned int offset, unsigned int length, float *left, float *right);
|
||||
|
||||
/**
|
||||
* Set the audio input gain. (multiplier applied to mic input)
|
||||
*/
|
||||
static void setInputGain (float gain) {
|
||||
data->inputGain = gain;
|
||||
}
|
||||
/**
|
||||
* Get the internal portaudio error code (paNoError if none).
|
||||
* Use in conjunction with Audio::init() or Audio::terminate(), as it is not
|
||||
impacted by any other methods.
|
||||
*/
|
||||
const PaError getError () { return err; }
|
||||
// initializes audio I/O
|
||||
static bool init();
|
||||
static bool init(Head* mainHead);
|
||||
|
||||
// terminates audio I/O
|
||||
static bool terminate();
|
||||
private:
|
||||
/**
|
||||
* Set to true by Audio::init() and false by Audio::terminate().
|
||||
* Used to prevent Audio::terminate() from deleting uninitialized memory.
|
||||
*/
|
||||
static bool initialized;
|
||||
|
||||
/**
|
||||
* Internal audio data.
|
||||
* Used to communicate with the audio callback code via a shared pointer.
|
||||
*/
|
||||
static struct AudioData {
|
||||
/**
|
||||
* Internal (stereo) audio buffer.
|
||||
* Written to by Audio I/O methods and the audio callback.
|
||||
* As this is a ring buffer, it should not be written to directly – thus methods
|
||||
like Audio::writeAudio are provided.
|
||||
*/
|
||||
struct BufferFrame{
|
||||
float l, r;
|
||||
} *buffer, *inputBuffer;
|
||||
/**
|
||||
* Length of the audio buffer.
|
||||
*/
|
||||
const static unsigned int bufferLength = 1000;
|
||||
/**
|
||||
* Current position (start) within the ring buffer.
|
||||
* Updated by the audio callback.
|
||||
*/
|
||||
unsigned int bufferPos;
|
||||
/**
|
||||
* Audio input gain (multiplier applied to the incoming audio stream).
|
||||
* Use Audio::setInputGain() to modify this.
|
||||
*/
|
||||
static float inputGain;// = 1.f;
|
||||
// struct for left/right data in audio buffer
|
||||
struct BufferFrame {
|
||||
int16_t left, right;
|
||||
} *buffer;
|
||||
|
||||
AudioData () : bufferPos(0) {
|
||||
inputGain = 1.0f;
|
||||
Head* linkedHead;
|
||||
|
||||
// length in bytes of audio buffer
|
||||
const static unsigned int bufferLength = 1024;
|
||||
|
||||
AudioData() {
|
||||
// alloc memory for buffer
|
||||
buffer = new BufferFrame[bufferLength];
|
||||
memset((float*)buffer, 0, sizeof(float) * bufferLength * 2);
|
||||
#if WRITE_AUDIO_INPUT_TO_BUFFER
|
||||
inputBuffer = new BufferFrame[bufferLength];
|
||||
memset((float*)inputBuffer, 0, sizeof(float) * bufferLength * 2);
|
||||
#else
|
||||
inputBuffer = NULL;
|
||||
#endif
|
||||
memset(buffer, 0, sizeof(int16_t) * bufferLength * 2);
|
||||
}
|
||||
~AudioData () {
|
||||
|
||||
~AudioData() {
|
||||
delete[] buffer;
|
||||
#if WRITE_AUDIO_INPUT_TO_BUFFER
|
||||
delete[] inputBuffer;
|
||||
#endif
|
||||
}
|
||||
}*data;
|
||||
/**
|
||||
* Internal audio stream handle.
|
||||
*/
|
||||
static PaStream *stream;
|
||||
/**
|
||||
* Internal error code (used only by Audio::init() and Audio::terminate()).
|
||||
*/
|
||||
} *data;
|
||||
|
||||
// protects constructor so that public init method is used
|
||||
Audio();
|
||||
|
||||
// hold potential error returned from PortAudio functions
|
||||
static PaError err;
|
||||
|
||||
Audio (); // prevent instantiation (private constructor)
|
||||
// audio stream handle
|
||||
static PaStream *stream;
|
||||
|
||||
// give access to AudioData class from audioCallback
|
||||
friend int audioCallback (const void*, void*, unsigned long, const PaStreamCallbackTimeInfo*, PaStreamCallbackFlags, void*);
|
||||
};
|
||||
|
||||
|
@ -147,5 +67,4 @@ int audioCallback (const void *inputBuffer,
|
|||
PaStreamCallbackFlags statusFlags,
|
||||
void *userData);
|
||||
|
||||
|
||||
#endif
|
||||
#endif /* defined(__interface__audio__) */
|
||||
|
|
|
@ -9,7 +9,7 @@
|
|||
#include "cube.h"
|
||||
|
||||
#define MAX_CUBES 250000
|
||||
#define SMALLEST_CUBE 0.005
|
||||
#define SMALLEST_CUBE 0.2
|
||||
|
||||
float cubes_position[MAX_CUBES*3];
|
||||
float cubes_scale[MAX_CUBES];
|
||||
|
|
|
@ -103,7 +103,7 @@ ParticleSystem balls(0,
|
|||
);
|
||||
|
||||
|
||||
Cloud cloud(0, // Particles
|
||||
Cloud cloud(50000, // Particles
|
||||
box, // Bounding Box
|
||||
false // Wrap
|
||||
);
|
||||
|
@ -310,7 +310,11 @@ void init(void)
|
|||
int i;
|
||||
|
||||
if (audio_on) {
|
||||
Audio::init();
|
||||
if (serial_on) {
|
||||
Audio::init(&myHead);
|
||||
} else {
|
||||
Audio::init();
|
||||
}
|
||||
printf( "Audio started.\n" );
|
||||
}
|
||||
|
||||
|
@ -609,11 +613,11 @@ void display(void)
|
|||
glDisable(GL_DEPTH_TEST);
|
||||
glDisable(GL_LIGHTING);
|
||||
|
||||
lattice.render(WIDTH, HEIGHT);
|
||||
// lattice.render(WIDTH, HEIGHT);
|
||||
//drawvec3(100, 100, 0.15, 0, 1.0, 0, myHead.getPos(), 0, 1, 0);
|
||||
glPointParameterfvARB( GL_POINT_DISTANCE_ATTENUATION_ARB, pointer_attenuation_quadratic );
|
||||
|
||||
myFinger.render();
|
||||
// myFinger.render();
|
||||
|
||||
if (mouse_pressed == 1)
|
||||
{
|
||||
|
@ -794,9 +798,6 @@ void key(unsigned char k, int x, int y)
|
|||
float add[] = {0.001, 0.001, 0.001};
|
||||
field_add(add, pos);
|
||||
}
|
||||
if ((k == 't') && (audio_on)) {
|
||||
Audio::writeTone(0, 400, 1.0f, 0.5f);
|
||||
}
|
||||
if (k == '1')
|
||||
{
|
||||
myHead.SetNewHeadTarget((randFloat()-0.5)*20.0, (randFloat()-0.5)*20.0);
|
||||
|
|
|
@ -8,7 +8,6 @@
|
|||
|
||||
/* Begin PBXBuildFile section */
|
||||
5325C25016AF4DBE0051A40B /* agent.cpp in Sources */ = {isa = PBXBuildFile; fileRef = 5325C22916AF4DBE0051A40B /* agent.cpp */; };
|
||||
5325C25116AF4DBE0051A40B /* audio.cpp in Sources */ = {isa = PBXBuildFile; fileRef = 5325C22B16AF4DBE0051A40B /* audio.cpp */; };
|
||||
5325C25216AF4DBE0051A40B /* cloud.cpp in Sources */ = {isa = PBXBuildFile; fileRef = 5325C22D16AF4DBE0051A40B /* cloud.cpp */; };
|
||||
5325C25316AF4DBE0051A40B /* cube.cpp in Sources */ = {isa = PBXBuildFile; fileRef = 5325C22F16AF4DBE0051A40B /* cube.cpp */; };
|
||||
5325C25416AF4DBE0051A40B /* field.cpp in Sources */ = {isa = PBXBuildFile; fileRef = 5325C23116AF4DBE0051A40B /* field.cpp */; };
|
||||
|
@ -25,6 +24,7 @@
|
|||
5325C25F16AF4DBE0051A40B /* SerialInterface.cpp in Sources */ = {isa = PBXBuildFile; fileRef = 5325C24916AF4DBE0051A40B /* SerialInterface.cpp */; };
|
||||
5325C26016AF4DBE0051A40B /* texture.cpp in Sources */ = {isa = PBXBuildFile; fileRef = 5325C24B16AF4DBE0051A40B /* texture.cpp */; };
|
||||
5325C26116AF4DBE0051A40B /* util.cpp in Sources */ = {isa = PBXBuildFile; fileRef = 5325C24D16AF4DBE0051A40B /* util.cpp */; };
|
||||
5325C26416AF4E2C0051A40B /* audio.cpp in Sources */ = {isa = PBXBuildFile; fileRef = 5325C26216AF4E2C0051A40B /* audio.cpp */; };
|
||||
532C7AF216AF298D00B1A969 /* CVBlob.framework in Frameworks */ = {isa = PBXBuildFile; fileRef = 532C792A16AF298900B1A969 /* CVBlob.framework */; };
|
||||
532C7CCC16AF301E00B1A969 /* grayson-particle.png in CopyFiles */ = {isa = PBXBuildFile; fileRef = 532C7AC316AF298D00B1A969 /* grayson-particle.png */; };
|
||||
532C7CCD16AF301E00B1A969 /* int-texture256-v2.png in CopyFiles */ = {isa = PBXBuildFile; fileRef = 532C7AC416AF298D00B1A969 /* int-texture256-v2.png */; };
|
||||
|
@ -81,8 +81,6 @@
|
|||
/* Begin PBXFileReference section */
|
||||
5325C22916AF4DBE0051A40B /* agent.cpp */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.cpp.cpp; path = agent.cpp; sourceTree = "<group>"; };
|
||||
5325C22A16AF4DBE0051A40B /* agent.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = agent.h; sourceTree = "<group>"; };
|
||||
5325C22B16AF4DBE0051A40B /* audio.cpp */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.cpp.cpp; path = audio.cpp; sourceTree = "<group>"; };
|
||||
5325C22C16AF4DBE0051A40B /* audio.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = audio.h; sourceTree = "<group>"; };
|
||||
5325C22D16AF4DBE0051A40B /* cloud.cpp */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.cpp.cpp; path = cloud.cpp; sourceTree = "<group>"; };
|
||||
5325C22E16AF4DBE0051A40B /* cloud.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = cloud.h; sourceTree = "<group>"; };
|
||||
5325C22F16AF4DBE0051A40B /* cube.cpp */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.cpp.cpp; path = cube.cpp; sourceTree = "<group>"; };
|
||||
|
@ -116,6 +114,8 @@
|
|||
5325C24D16AF4DBE0051A40B /* util.cpp */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.cpp.cpp; path = util.cpp; sourceTree = "<group>"; };
|
||||
5325C24E16AF4DBE0051A40B /* util.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = util.h; sourceTree = "<group>"; };
|
||||
5325C24F16AF4DBE0051A40B /* world.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = world.h; sourceTree = "<group>"; };
|
||||
5325C26216AF4E2C0051A40B /* audio.cpp */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.cpp.cpp; path = audio.cpp; sourceTree = "<group>"; };
|
||||
5325C26316AF4E2C0051A40B /* audio.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = audio.h; sourceTree = "<group>"; };
|
||||
532C792A16AF298900B1A969 /* CVBlob.framework */ = {isa = PBXFileReference; lastKnownFileType = wrapper.framework; name = CVBlob.framework; path = Frameworks/CVBlob.framework; sourceTree = "<group>"; };
|
||||
532C7AC316AF298D00B1A969 /* grayson-particle.png */ = {isa = PBXFileReference; lastKnownFileType = image.png; path = "grayson-particle.png"; sourceTree = "<group>"; };
|
||||
532C7AC416AF298D00B1A969 /* int-texture256-v2.png */ = {isa = PBXFileReference; lastKnownFileType = image.png; path = "int-texture256-v2.png"; sourceTree = "<group>"; };
|
||||
|
@ -584,8 +584,6 @@
|
|||
children = (
|
||||
5325C22916AF4DBE0051A40B /* agent.cpp */,
|
||||
5325C22A16AF4DBE0051A40B /* agent.h */,
|
||||
5325C22B16AF4DBE0051A40B /* audio.cpp */,
|
||||
5325C22C16AF4DBE0051A40B /* audio.h */,
|
||||
5325C22D16AF4DBE0051A40B /* cloud.cpp */,
|
||||
5325C22E16AF4DBE0051A40B /* cloud.h */,
|
||||
5325C22F16AF4DBE0051A40B /* cube.cpp */,
|
||||
|
@ -619,6 +617,8 @@
|
|||
5325C24D16AF4DBE0051A40B /* util.cpp */,
|
||||
5325C24E16AF4DBE0051A40B /* util.h */,
|
||||
5325C24F16AF4DBE0051A40B /* world.h */,
|
||||
5325C26216AF4E2C0051A40B /* audio.cpp */,
|
||||
5325C26316AF4E2C0051A40B /* audio.h */,
|
||||
);
|
||||
path = Source;
|
||||
sourceTree = "<group>";
|
||||
|
@ -1331,7 +1331,6 @@
|
|||
buildActionMask = 2147483647;
|
||||
files = (
|
||||
5325C25016AF4DBE0051A40B /* agent.cpp in Sources */,
|
||||
5325C25116AF4DBE0051A40B /* audio.cpp in Sources */,
|
||||
5325C25216AF4DBE0051A40B /* cloud.cpp in Sources */,
|
||||
5325C25316AF4DBE0051A40B /* cube.cpp in Sources */,
|
||||
5325C25416AF4DBE0051A40B /* field.cpp in Sources */,
|
||||
|
@ -1348,6 +1347,7 @@
|
|||
5325C25F16AF4DBE0051A40B /* SerialInterface.cpp in Sources */,
|
||||
5325C26016AF4DBE0051A40B /* texture.cpp in Sources */,
|
||||
5325C26116AF4DBE0051A40B /* util.cpp in Sources */,
|
||||
5325C26416AF4E2C0051A40B /* audio.cpp in Sources */,
|
||||
);
|
||||
runOnlyForDeploymentPostprocessing = 0;
|
||||
};
|
||||
|
|
Loading…
Reference in a new issue