now using AudioCallback
This commit is contained in:
25
main.cpp
25
main.cpp
@@ -25,7 +25,20 @@ int current_instrument = 0;
|
||||
int current_volume = 2;
|
||||
int current_effect = 0;
|
||||
|
||||
SDL_AudioSpec audioSpec{SAMPLES_PER_SECOND, AUDIO_FORMAT, 1, 0, 512, 0, 0, NULL, NULL};
|
||||
toneGen tonegen(SOUND_NUM_SAMPLES);
|
||||
SDL_AudioDeviceID sdlAudioDevice;
|
||||
|
||||
void audioCallback(void* userdata, Uint8* stream, int len) {
|
||||
Sint16* buffer = (Sint16*)stream;
|
||||
int numSamples = tonegen.getSamples(len >> 1, buffer);
|
||||
if (numSamples*2 < len) {
|
||||
int rest = len - numSamples*2;
|
||||
SDL_memset(&stream[numSamples*2], 0, rest);
|
||||
SDL_PauseAudioDevice(sdlAudioDevice, 1);
|
||||
}
|
||||
}
|
||||
|
||||
SDL_AudioSpec audioSpec{SAMPLES_PER_SECOND, AUDIO_FORMAT, 1, 0, 512, 0, 0, &audioCallback, NULL};
|
||||
|
||||
const char get_hex(const uint8_t num) {
|
||||
static const char hexstr[17] = "0123456789ABCDEF";
|
||||
@@ -35,15 +48,14 @@ const char get_hex(const uint8_t num) {
|
||||
int main(int argc, char* argv[]) {
|
||||
pattern[0][0] = (34<<10) + (2<<8) + (7<<4) + 5;
|
||||
SDL_Init(SDL_INIT_EVERYTHING);
|
||||
SDL_AudioDeviceID sdlAudioDevice = SDL_OpenAudioDevice(NULL, 0, &audioSpec, NULL, 0);
|
||||
sdlAudioDevice = SDL_OpenAudioDevice(NULL, 0, &audioSpec, NULL, 0);
|
||||
|
||||
AUDIO_FORMAT_TYPE beeps[int(SOUND_NUM_SAMPLES*8)];
|
||||
AUDIO_FORMAT_TYPE beeps2[int(SOUND_NUM_SAMPLES*8)];
|
||||
AUDIO_FORMAT_TYPE *pointer = beeps;
|
||||
|
||||
toneGen tonegen(SOUND_NUM_SAMPLES);
|
||||
|
||||
tonegen.setup(22, CHIPTUNE_INSTRUMENT_TRIANGLE, 13, CHIPTUNE_EFFECT_DROP);
|
||||
/*tonegen.setup(22, CHIPTUNE_INSTRUMENT_TRIANGLE, 13, CHIPTUNE_EFFECT_DROP);
|
||||
int numSamples = tonegen.getSamples(SOUND_NUM_SAMPLES, pointer); pointer += int(SOUND_NUM_SAMPLES);
|
||||
tonegen.setup(32, CHIPTUNE_INSTRUMENT_SQUARE, 0, CHIPTUNE_EFFECT_NONE);
|
||||
numSamples = tonegen.getSamples(SOUND_NUM_SAMPLES, pointer); pointer += int(SOUND_NUM_SAMPLES);
|
||||
@@ -77,6 +89,7 @@ int main(int argc, char* argv[]) {
|
||||
numSamples = tonegen.getSamples(SOUND_NUM_SAMPLES, pointer); pointer += int(SOUND_NUM_SAMPLES);
|
||||
tonegen.setup(47, CHIPTUNE_INSTRUMENT_SQUARE, 1, CHIPTUNE_EFFECT_NONE);
|
||||
numSamples = tonegen.getSamples(SOUND_NUM_SAMPLES, pointer); pointer += int(SOUND_NUM_SAMPLES);
|
||||
*/
|
||||
|
||||
for (int i=0; i<SOUND_NUM_SAMPLES*8;++i) {
|
||||
beeps[i] += beeps2[i];
|
||||
@@ -145,8 +158,6 @@ int main(int argc, char* argv[]) {
|
||||
if ((key == SDL_SCANCODE_PERIOD) || (key == SDL_SCANCODE_DELETE)) note.Set(0);
|
||||
if (key == SDL_SCANCODE_SPACE) {
|
||||
tonegen.setup(note.Get());
|
||||
numSamples = tonegen.getSamples(SOUND_NUM_SAMPLES, beeps);
|
||||
SDL_QueueAudio(sdlAudioDevice, &beeps[0], SOUND_SIZE_IN_BYTES);
|
||||
SDL_PauseAudioDevice(sdlAudioDevice, 0);
|
||||
}
|
||||
if (selected_part==0) {
|
||||
@@ -168,8 +179,6 @@ int main(int argc, char* argv[]) {
|
||||
};
|
||||
if (m) {
|
||||
tonegen.setup(note.Get());
|
||||
numSamples = tonegen.getSamples(SOUND_NUM_SAMPLES, beeps);
|
||||
SDL_QueueAudio(sdlAudioDevice, &beeps[0], SOUND_SIZE_IN_BYTES);
|
||||
SDL_PauseAudioDevice(sdlAudioDevice, 0);
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user