Skip to content

Instantly share code, notes, and snippets.

@colinsullivan
Created January 12, 2012 07:02
Show Gist options
  • Save colinsullivan/1599126 to your computer and use it in GitHub Desktop.
Save colinsullivan/1599126 to your computer and use it in GitHub Desktop.
Simple Audio and Accelerometer Callbacks that are creating artifacts.
#define SRATE 44100
#define FRAMESIZE 512
#define NUMCHANNELS 2
double g_t;
bool g_sineSwitch;
double g_freq;
@implementation hw1ViewController
void audioCallback(Float32 * buff, UInt32 frameSize, void * userData) {
for (int i = 0; i < frameSize; i++) {
Float32 sample = sin( TWO_PI * g_freq * g_t++ / SRATE );
for (int j = 0; j < NUMCHANNELS; j++) {
buff[i*NUMCHANNELS+j] = 0.0;
if (g_sineSwitch) {
buff[i*NUMCHANNELS+j] = sample;
}
}
}
}
void accelCallback(double x, double y, double z, void * data) {
NSLog(@"x: %f", x);
g_freq = 440.0 + x*100.0;
}
// ...
- (void)viewDidLoad
{
[super viewDidLoad];
g_sineSwitch = false;
g_t = 0.0;
g_freq = 440.0;
// start the accelerometer and multitouch callbacks
MoAccel::setUpdateInterval(1.0);
MoAccel::addCallback(accelCallback, NULL);
NSLog(@"starting real-time audio...");
// init the audio layer
bool result = MoAudio::init(SRATE, FRAMESIZE, NUMCHANNELS);
if (!result) {
NSLog(@"cannot initialize real-time audio!");
return;
}
// start the audio layer, registering a callback method
result = MoAudio::start(audioCallback, (void*)&accelCallback);
if (!result) {
// something went wrong
NSLog(@"cannot start real-time audio!");
return;
}
// MoTouch::addCallback(touchCallback, NULL);
}
// ...
@end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment