basic channels mixing pressing SPACE

This commit is contained in:
2021-09-15 12:20:57 +02:00
parent d5262780f4
commit 636fd2c0fb
3 changed files with 23 additions and 10 deletions

View File

@@ -25,15 +25,20 @@ int current_instrument = 0;
int current_volume = 2; int current_volume = 2;
int current_effect = 0; int current_effect = 0;
toneGen tonegen(SOUND_NUM_SAMPLES); toneGen channel[4];
SDL_AudioDeviceID sdlAudioDevice; SDL_AudioDeviceID sdlAudioDevice;
Sint16 auxBuffer[512];
void audioCallback(void* userdata, Uint8* stream, int len) { void audioCallback(void* userdata, Uint8* stream, int len) {
Sint16* buffer = (Sint16*)stream; Sint16* buffer = (Sint16*)stream;
int numSamples = tonegen.getSamples(len >> 1, buffer); int numBytesGenerated = channel[0].getSamples(len >> 1, buffer) * 2;
if (numSamples*2 < len) { for (int i=1; i<4; ++i) {
int rest = len - numSamples*2; channel[i].getSamples(len >> 1, auxBuffer);
SDL_memset(&stream[numSamples*2], 0, rest); for (int j=0; j<numBytesGenerated; ++j) buffer[j] += auxBuffer[j];
}
if (numBytesGenerated < len) {
int rest = len - numBytesGenerated;
SDL_memset(&stream[numBytesGenerated], 0, rest);
SDL_PauseAudioDevice(sdlAudioDevice, 1); SDL_PauseAudioDevice(sdlAudioDevice, 1);
} }
} }
@@ -46,6 +51,7 @@ const char get_hex(const uint8_t num) {
} }
int main(int argc, char* argv[]) { int main(int argc, char* argv[]) {
for (int i=0; i<4; ++i) channel[i].setNoteLength(SOUND_NUM_SAMPLES);
pattern[0][0] = (34<<10) + (2<<8) + (7<<4) + 5; pattern[0][0] = (34<<10) + (2<<8) + (7<<4) + 5;
SDL_Init(SDL_INIT_EVERYTHING); SDL_Init(SDL_INIT_EVERYTHING);
sdlAudioDevice = SDL_OpenAudioDevice(NULL, 0, &audioSpec, NULL, 0); sdlAudioDevice = SDL_OpenAudioDevice(NULL, 0, &audioSpec, NULL, 0);
@@ -157,7 +163,10 @@ int main(int argc, char* argv[]) {
//uint8_t base_note = 1+(base_octave-2)*12; //uint8_t base_note = 1+(base_octave-2)*12;
if ((key == SDL_SCANCODE_PERIOD) || (key == SDL_SCANCODE_DELETE)) note.Set(0); if ((key == SDL_SCANCODE_PERIOD) || (key == SDL_SCANCODE_DELETE)) note.Set(0);
if (key == SDL_SCANCODE_SPACE) { if (key == SDL_SCANCODE_SPACE) {
tonegen.setup(note.Get()); channel[0].setup(pattern[0][selected_row]);
channel[1].setup(pattern[1][selected_row]);
channel[2].setup(pattern[2][selected_row]);
channel[3].setup(pattern[3][selected_row]);
SDL_PauseAudioDevice(sdlAudioDevice, 0); SDL_PauseAudioDevice(sdlAudioDevice, 0);
} }
if (selected_part==0) { if (selected_part==0) {
@@ -178,7 +187,7 @@ int main(int argc, char* argv[]) {
default: m = false; break; default: m = false; break;
}; };
if (m) { if (m) {
tonegen.setup(note.Get()); channel[0].setup(note.Get());
SDL_PauseAudioDevice(sdlAudioDevice, 0); SDL_PauseAudioDevice(sdlAudioDevice, 0);
} }
} }

View File

@@ -4,8 +4,7 @@
Sint16 notes[64]; Sint16 notes[64];
toneGen::toneGen(const int note_length) { toneGen::toneGen() {
this->note_length = note_length;
this->current_pos = 0; this->current_pos = 0;
this->note = 34; this->note = 34;
@@ -24,6 +23,10 @@ toneGen::toneGen(const int note_length) {
this->f = -AUDIO_FORMAT_MAX_VALUE; 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) { void toneGen::setup(const uint8_t note, const uint8_t instrument, const uint8_t volume, const uint8_t effect) {
this->current_pos = 0; this->current_pos = 0;

View File

@@ -16,7 +16,8 @@
class toneGen { class toneGen {
public: 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 uint8_t note, const uint8_t instrument, const uint8_t volume, const uint8_t effect);
void setup(const uint16_t tracker_note); void setup(const uint16_t tracker_note);
const int getSamples(const int numSamples, Sint16 *buffer); const int getSamples(const int numSamples, Sint16 *buffer);