From d5262780f4922194bd38dbc57cf0397c2f3ff9dc Mon Sep 17 00:00:00 2001 From: Raimon Zamora Date: Wed, 15 Sep 2021 11:58:22 +0200 Subject: [PATCH 1/3] now using AudioCallback --- main.cpp | 25 +++++++++++++++++-------- 1 file changed, 17 insertions(+), 8 deletions(-) diff --git a/main.cpp b/main.cpp index 33a163e..199c71e 100755 --- a/main.cpp +++ b/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 Date: Wed, 15 Sep 2021 12:20:57 +0200 Subject: [PATCH 2/3] basic channels mixing pressing SPACE --- main.cpp | 23 ++++++++++++++++------- tonegen.cpp | 7 +++++-- tonegen.h | 3 ++- 3 files changed, 23 insertions(+), 10 deletions(-) diff --git a/main.cpp b/main.cpp index 199c71e..86d6609 100755 --- a/main.cpp +++ b/main.cpp @@ -25,15 +25,20 @@ int current_instrument = 0; int current_volume = 2; int current_effect = 0; -toneGen tonegen(SOUND_NUM_SAMPLES); +toneGen channel[4]; SDL_AudioDeviceID sdlAudioDevice; +Sint16 auxBuffer[512]; 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); + int numBytesGenerated = channel[0].getSamples(len >> 1, buffer) * 2; + for (int i=1; i<4; ++i) { + channel[i].getSamples(len >> 1, auxBuffer); + for (int j=0; jnote_length = note_length; +toneGen::toneGen() { this->current_pos = 0; this->note = 34; @@ -24,6 +23,10 @@ toneGen::toneGen(const int note_length) { this->f = -AUDIO_FORMAT_MAX_VALUE; } +void toneGen::setNoteLength(const int note_length) { + this->note_length = note_length; +} + void toneGen::setup(const uint8_t note, const uint8_t instrument, const uint8_t volume, const uint8_t effect) { this->current_pos = 0; diff --git a/tonegen.h b/tonegen.h index ddc3f44..e6c9878 100755 --- a/tonegen.h +++ b/tonegen.h @@ -16,7 +16,8 @@ class toneGen { public: - toneGen(const int note_length); + toneGen(); + void setNoteLength(const int note_length); void setup(const uint8_t note, const uint8_t instrument, const uint8_t volume, const uint8_t effect); void setup(const uint16_t tracker_note); const int getSamples(const int numSamples, Sint16 *buffer); From dc23d8f9c1219e126723877d12aa862ae0cc5d2c Mon Sep 17 00:00:00 2001 From: Raimon Zamora Date: Wed, 15 Sep 2021 12:33:07 +0200 Subject: [PATCH 3/3] canvie de comp --- song.h | 16 ++++++++++++++++ 1 file changed, 16 insertions(+) create mode 100644 song.h diff --git a/song.h b/song.h new file mode 100644 index 0000000..b131f7c --- /dev/null +++ b/song.h @@ -0,0 +1,16 @@ +#pragma once + +#include +#include + +//typedef uint16_t[4][64] Pattern; + +class Song { + public: + Song(); + + uint16_t GetCurrentNote(const int channel); + + private: + std::vector patterns; +};