basic song play, better vibrato, copy/paste note

This commit is contained in:
2021-09-15 13:58:23 +02:00
parent f5fe2396dd
commit dc9fd548c6
5 changed files with 129 additions and 24 deletions

View File

@@ -3,8 +3,9 @@
#include "audio.h"
#include "draw.h"
#include "note.h"
#include "song.h"
#define SOUND_DURATION 0.25f
#define SOUND_DURATION 0.0625f
#define SOUND_NUM_SAMPLES SOUND_DURATION*SAMPLES_PER_SECOND
#define SOUND_SIZE_IN_BYTES SOUND_NUM_SAMPLES*AUDIO_FORMAT_SIZE
@@ -14,7 +15,8 @@
#define get_effect(x) (x & 0xF)*/
char note_names[12][3] = { "C ", "C#", "D ", "D#", "E ", "F ", "F#", "G ", "G#", "A ", "A#", "B " };
Uint16 pattern[4][64];
//Uint16 pattern[4][64];
Song song;
bool muted[4] = { false, false, false, false };
int selected_channel = 0;
@@ -29,18 +31,33 @@ int current_effect = 0;
toneGen channel[4];
SDL_AudioDeviceID sdlAudioDevice;
Sint16 auxBuffer[512];
uint16_t copied_note = 0;
void audioCallback(void* userdata, Uint8* stream, int len) {
Sint16* buffer = (Sint16*)stream;
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; j<numBytesGenerated; ++j) buffer[j] += auxBuffer[j];
}
if (numBytesGenerated < len) {
int rest = len - numBytesGenerated;
SDL_memset(&stream[numBytesGenerated], 0, rest);
SDL_PauseAudioDevice(sdlAudioDevice, 1);
while (true) {
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; j<numBytesGenerated; ++j) buffer[j] += auxBuffer[j];
}
if (numBytesGenerated < len) {
if (song.IsPlaying()) {
buffer += (numBytesGenerated >> 1);
len -= numBytesGenerated;
song.Next();
channel[0].setup(song.GetCurrentNote(0));
channel[1].setup(song.GetCurrentNote(1));
channel[2].setup(song.GetCurrentNote(2));
channel[3].setup(song.GetCurrentNote(3));
} else {
int rest = len - numBytesGenerated;
SDL_memset(&stream[numBytesGenerated], 0, rest);
SDL_PauseAudioDevice(sdlAudioDevice, 1);
return;
}
} else { return; }
}
}
@@ -60,7 +77,7 @@ const char* get_zero_padded(const uint8_t num) {
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;
song.SetCurrentNote(0, 0, (34<<10) + (2<<8) + (7<<4) + 5);
SDL_Init(SDL_INIT_EVERYTHING);
sdlAudioDevice = SDL_OpenAudioDevice(NULL, 0, &audioSpec, NULL, 0);
@@ -172,16 +189,36 @@ int main(int argc, char* argv[]) {
}
}
}
Note note(pattern[selected_channel][selected_row]);
Note note(song.GetCurrentNote(selected_channel, selected_row));
//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_RETURN) copied_note = note.Get();
if (key == SDL_SCANCODE_SPACE) {
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]);
note.Set(copied_note);
channel[0].setup(note.Get());
channel[1].setup(0);
channel[2].setup(0);
channel[3].setup(0);
SDL_PauseAudioDevice(sdlAudioDevice, 0);
}
if (key == SDL_SCANCODE_F4) {
channel[0].setup(song.GetCurrentNote(0, selected_row));
channel[1].setup(song.GetCurrentNote(1, selected_row));
channel[2].setup(song.GetCurrentNote(2, selected_row));
channel[3].setup(song.GetCurrentNote(3, selected_row));
SDL_PauseAudioDevice(sdlAudioDevice, 0);
}
if (key == SDL_SCANCODE_F5) {
song.Play();
channel[0].setup(song.GetCurrentNote(0));
channel[1].setup(song.GetCurrentNote(1));
channel[2].setup(song.GetCurrentNote(2));
channel[3].setup(song.GetCurrentNote(3));
SDL_PauseAudioDevice(sdlAudioDevice, 0);
}
if (key == SDL_SCANCODE_F8) {
song.Stop();
}
if (selected_part==0) {
bool m = true;
switch(key) {
@@ -201,6 +238,9 @@ int main(int argc, char* argv[]) {
};
if (m) {
channel[0].setup(note.Get());
channel[1].setup(0);
channel[2].setup(0);
channel[3].setup(0);
SDL_PauseAudioDevice(sdlAudioDevice, 0);
}
}
@@ -265,7 +305,7 @@ int main(int argc, char* argv[]) {
default: break;
};
}
pattern[selected_channel][selected_row] = note.Get();
song.SetCurrentNote(selected_channel, selected_row, note.Get());
}
}
draw_clear(color_dark_grey);
@@ -286,7 +326,9 @@ int main(int argc, char* argv[]) {
for (int x=0; x<4; ++x) {
yp = 15;
for (int y=0; y<32; ++y) {
if (x == selected_channel && (y+scroll) == selected_row) {
if ((song.IsPlaying()) && (y+scroll == song.GetCurrentRow())) {
draw_fill(xp-1, yp-1, 32, 7, color_yellow);
} else if (x == selected_channel && (y+scroll) == selected_row) {
draw_fill(xp-1, yp-1, 32, 7, color_dark_blue);
switch (selected_part) {
case 0: draw_fill(xp-1, yp-1, 11, 7, color_yellow); break;
@@ -296,7 +338,7 @@ int main(int argc, char* argv[]) {
case 4: draw_fill(25+xp, yp-1, 6, 7, color_yellow); break;
}
}
Note note(pattern[x][y+scroll]);
Note note(song.GetCurrentNote(x, y+scroll));
//Uint16 current_note = pattern[x][y];
if (note.GetAbsoluteNote() == 0) {
uint8_t color = color_dark_blue;