Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

#1: Fixes for incompatibility on OSX #2

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
12 changes: 12 additions & 0 deletions cpp/Makefile
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
.PHONY: clean bundle

CC=g++
generator: main.cpp
$(CC) -o generator `sdl-config --cflags --libs` main.cpp

bundle: generator
sh makeAppBundle

clean:
$(RM) generator

Binary file added cpp/generator
Binary file not shown.
128 changes: 128 additions & 0 deletions cpp/main.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,128 @@
#include <SDL/SDL.h>
#include <SDL/SDL_audio.h>
#include <queue>
#include <cmath>

const int AMPLITUDE = 28000;
const int FREQUENCY = 44100;

struct BeepObject
{
double freq;
int samplesLeft;
};

class Beeper
{
private:
double v;
std::queue<BeepObject> beeps;
public:
Beeper();
~Beeper();
void beep(double freq, int duration);
void generateSamples(Sint16 *stream, int length);
void wait();
};

void audio_callback(void*, Uint8*, int);

Beeper::Beeper()
{
SDL_AudioSpec desiredSpec;

desiredSpec.freq = FREQUENCY;
desiredSpec.format = AUDIO_S16SYS;
desiredSpec.channels = 1;
desiredSpec.samples = 2048;
desiredSpec.callback = audio_callback;
desiredSpec.userdata = this;

SDL_AudioSpec obtainedSpec;

// you might want to look for errors here
SDL_OpenAudio(&desiredSpec, &obtainedSpec);

// start play audio
SDL_PauseAudio(0);
}

Beeper::~Beeper()
{
SDL_CloseAudio();
}

void Beeper::generateSamples(Sint16 *stream, int length)
{
int i = 0;
while (i < length) {

if (beeps.empty()) {
while (i < length) {
stream[i] = 0;
i++;
}
return;
}
BeepObject& bo = beeps.front();

int samplesToDo = std::min(i + bo.samplesLeft, length);
bo.samplesLeft -= samplesToDo - i;

while (i < samplesToDo) {
stream[i] = AMPLITUDE * std::sin(v * 2 * M_PI / FREQUENCY);
i++;
v += bo.freq;
}

if (bo.samplesLeft == 0) {
beeps.pop();
}
}
}

void Beeper::beep(double freq, int duration)
{
BeepObject bo;
bo.freq = freq;
bo.samplesLeft = duration * FREQUENCY / 1000;

SDL_LockAudio();
beeps.push(bo);
SDL_UnlockAudio();
}

void Beeper::wait()
{
int size;
do {
SDL_Delay(20);
SDL_LockAudio();
size = beeps.size();
SDL_UnlockAudio();
} while (size > 0);

}

void audio_callback(void *_beeper, Uint8 *_stream, int _length)
{
Sint16 *stream = (Sint16*) _stream;
int length = _length / 2;
Beeper* beeper = (Beeper*) _beeper;

beeper->generateSamples(stream, length);
}

int main(int argc, char* argv[])
{
SDL_Init(SDL_INIT_AUDIO);

int duration = 1000;
double Hz = 440;

Beeper b;
b.beep(Hz, duration);
b.wait();

return 0;
}
Binary file added generator
Binary file not shown.
1 change: 1 addition & 0 deletions generator.c
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@ void usage() {
}

int main(int argc, char* argv[]) {
errno = 0;
/* This will hold our data */
SDL_AudioSpec spec;
/* This will hold the requested frequency */
Expand Down