Compare commits

...

9 Commits

7 changed files with 328 additions and 117 deletions

341
main.cpp
View File

@@ -3,22 +3,30 @@
#include "audio.h" #include "audio.h"
#include "draw.h" #include "draw.h"
#include "note.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_NUM_SAMPLES SOUND_DURATION*SAMPLES_PER_SECOND
#define SOUND_SIZE_IN_BYTES SOUND_NUM_SAMPLES*AUDIO_FORMAT_SIZE #define SOUND_SIZE_IN_BYTES SOUND_NUM_SAMPLES*AUDIO_FORMAT_SIZE
struct selection_t {
int channel = 0;
int row = 0;
int part = 0;
};
/*#define get_note(x) (x>>10) /*#define get_note(x) (x>>10)
#define get_instrument(x) ((x>>8) & 0x3) #define get_instrument(x) ((x>>8) & 0x3)
#define get_volume(x) ((x>>4) & 0xF) #define get_volume(x) ((x>>4) & 0xF)
#define get_effect(x) (x & 0xF)*/ #define get_effect(x) (x & 0xF)*/
char note_names[12][3] = { "C ", "C#", "D ", "D#", "E ", "F ", "F#", "G ", "G#", "A ", "A#", "B " }; 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 };
selection_t sel_start, sel_end;
int selected_channel = 0;
int selected_row = 0;
int selected_part = 0;
int scroll = 0; int scroll = 0;
int base_octave = 4; int base_octave = 4;
int current_instrument = 0; int current_instrument = 0;
@@ -28,18 +36,33 @@ int current_effect = 0;
toneGen channel[4]; toneGen channel[4];
SDL_AudioDeviceID sdlAudioDevice; SDL_AudioDeviceID sdlAudioDevice;
Sint16 auxBuffer[512]; Sint16 auxBuffer[512];
uint16_t copied_note = 0;
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 numBytesGenerated = channel[0].getSamples(len >> 1, buffer) * 2;
for (int i=1; i<4; ++i) { while (true) {
channel[i].getSamples(len >> 1, auxBuffer); int numBytesGenerated = channel[0].getSamples(len >> 1, buffer) * 2;
for (int j=0; j<numBytesGenerated; ++j) buffer[j] += auxBuffer[j]; for (int i=1; i<4; ++i) {
} channel[i].getSamples(len >> 1, auxBuffer);
if (numBytesGenerated < len) { for (int j=0; j<numBytesGenerated; ++j) buffer[j] += auxBuffer[j];
int rest = len - numBytesGenerated; }
SDL_memset(&stream[numBytesGenerated], 0, rest); if (numBytesGenerated < len) {
SDL_PauseAudioDevice(sdlAudioDevice, 1); 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; }
} }
} }
@@ -50,9 +73,23 @@ const char get_hex(const uint8_t num) {
return hexstr[num]; return hexstr[num];
} }
char zero_padded[3] = "00";
const char* get_zero_padded(const uint8_t num) {
zero_padded[0] = 48 + int(num / 10);
zero_padded[1] = 48 + (num % 10);
return zero_padded;
}
void playNote(Note &note) {
channel[0].setup(note.Get());
channel[1].setup(0);
channel[2].setup(0);
channel[3].setup(0);
SDL_PauseAudioDevice(sdlAudioDevice, 0);
}
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); 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); SDL_Init(SDL_INIT_EVERYTHING);
sdlAudioDevice = SDL_OpenAudioDevice(NULL, 0, &audioSpec, NULL, 0); sdlAudioDevice = SDL_OpenAudioDevice(NULL, 0, &audioSpec, NULL, 0);
@@ -110,66 +147,114 @@ int main(int argc, char* argv[]) {
if (sdlEvent.type == SDL_QUIT) { exit = true; break;} if (sdlEvent.type == SDL_QUIT) { exit = true; break;}
if (sdlEvent.type == SDL_MOUSEBUTTONDOWN) { if (sdlEvent.type == SDL_MOUSEBUTTONDOWN) {
if (sdlEvent.button.button == SDL_BUTTON_LEFT) { if (sdlEvent.button.button == SDL_BUTTON_LEFT) {
if ((sdlEvent.button.x >= 3) && (sdlEvent.button.x <= 139) && (sdlEvent.button.y >= 14) && (sdlEvent.button.y <= 239) ) { if ((sdlEvent.button.x >= 13) && (sdlEvent.button.x <= 149) && (sdlEvent.button.y >= 14) && (sdlEvent.button.y <= 239) ) {
selected_row = scroll + int((sdlEvent.button.y-14)/7); //selected_channel_end = -1;
selected_channel = (sdlEvent.button.x-3)/35; sel_start.channel = (sdlEvent.button.x-13)/35;
int part_pos = (sdlEvent.button.x-3)-(selected_channel*35); sel_start.row = scroll + int((sdlEvent.button.y-14)/7);
int part_pos = (sdlEvent.button.x-13)-(sel_start.channel*35);
if (part_pos <= 10) { if (part_pos <= 10) {
selected_part = 0; sel_start.part = 0;
} else if (part_pos <= 15) { } else if (part_pos <= 15) {
selected_part = 1; sel_start.part = 1;
} else if (part_pos <= 21) { } else if (part_pos <= 21) {
selected_part = 2; sel_start.part = 2;
} else if (part_pos <= 26) { } else if (part_pos <= 26) {
selected_part = 3; sel_start.part = 3;
} else if (part_pos <= 31) { } else if (part_pos <= 31) {
selected_part = 4; sel_start.part = 4;
}
sel_end = sel_start;
}
for (int i=0; i<4; ++i) {
if ((sdlEvent.button.x >= 12+i*35) && (sdlEvent.button.x <= 45+i*35) && (sdlEvent.button.y >= 2) && (sdlEvent.button.y <= 13) ) {
muted[i] = not muted[i];
channel[i].setMute(muted[i]);
}
}
}
}
if (sdlEvent.type == SDL_MOUSEMOTION) {
if (sdlEvent.motion.state & SDL_BUTTON_LMASK != 0) {
if ((sdlEvent.motion.x >= 13) && (sdlEvent.motion.x <= 149) && (sdlEvent.motion.y >= 14) && (sdlEvent.motion.y <= 239) ) {
sel_end.channel = (sdlEvent.motion.x-13)/35;
sel_end.row = scroll + int((sdlEvent.motion.y-14)/7);
int part_pos = (sdlEvent.motion.x-13)-(sel_end.channel*35);
if (part_pos <= 10) {
sel_end.part = 0;
} else if (part_pos <= 15) {
sel_end.part = 1;
} else if (part_pos <= 21) {
sel_end.part = 2;
} else if (part_pos <= 26) {
sel_end.part = 3;
} else if (part_pos <= 31) {
sel_end.part = 4;
} }
} }
} }
} }
if (sdlEvent.type == SDL_MOUSEWHEEL) { if (sdlEvent.type == SDL_MOUSEWHEEL) {
scroll -= sdlEvent.wheel.y; scroll -= sdlEvent.wheel.y*4;
if (scroll < 0) scroll = 0; else if (scroll > 32) scroll = 32; if (scroll < 0) scroll = 0; else if (scroll > 32) scroll = 32;
} }
if (sdlEvent.type == SDL_KEYDOWN) { if (sdlEvent.type == SDL_KEYDOWN) {
SDL_Scancode key = sdlEvent.key.keysym.scancode; SDL_Scancode key = sdlEvent.key.keysym.scancode;
if (key == SDL_SCANCODE_DOWN) { if (key == SDL_SCANCODE_DOWN) {
if (selected_row < 63) selected_row++; if (sel_start.row < 63) sel_start.row++;
if (selected_row >= 32 + scroll) scroll = selected_row-31; if (sel_start.row >= 32 + scroll) scroll = sel_start.row-31;
sel_end = sel_start;
} else if (key == SDL_SCANCODE_UP) { } else if (key == SDL_SCANCODE_UP) {
if (selected_row > 0) selected_row--; if (sel_start.row > 0) sel_start.row--;
if (selected_row < scroll) scroll = selected_row; if (sel_start.row < scroll) scroll = sel_start.row;
sel_end = sel_start;
} else if (key == SDL_SCANCODE_LEFT) { } else if (key == SDL_SCANCODE_LEFT) {
if (selected_channel > 0 || selected_part > 0) { if (sel_start.channel > 0 || sel_start.part > 0) {
if (selected_part > 0) { if (sel_start.part > 0) {
selected_part--; sel_start.part--;
} else { } else {
selected_channel--; sel_start.channel--;
selected_part = 4; sel_start.part = 4;
} }
} }
sel_end = sel_start;
} else if (key == SDL_SCANCODE_RIGHT) { } else if (key == SDL_SCANCODE_RIGHT) {
if (selected_channel < 3 || selected_part < 4) { if (sel_start.channel < 3 || sel_start.part < 4) {
if (selected_part < 4) { if (sel_start.part < 4) {
selected_part++; sel_start.part++;
} else { } else {
selected_channel++; sel_start.channel++;
selected_part = 0; sel_start.part = 0;
} }
} }
sel_end = sel_start;
} }
Note note(pattern[selected_channel][selected_row]); Note note(song.GetCurrentNote(sel_start.channel, sel_start.row));
//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_RETURN) copied_note = note.Get();
if (key == SDL_SCANCODE_SPACE) { if (key == SDL_SCANCODE_SPACE) {
channel[0].setup(pattern[0][selected_row]); note.Set(copied_note);
channel[1].setup(pattern[1][selected_row]); playNote(note);
channel[2].setup(pattern[2][selected_row]); }
channel[3].setup(pattern[3][selected_row]); if (key == SDL_SCANCODE_F4) {
for (int i=0;i<4;++i) {
channel[i].setup(song.GetCurrentNote(i, sel_start.row));
channel[i].setMute(muted[i]);
}
SDL_PauseAudioDevice(sdlAudioDevice, 0); SDL_PauseAudioDevice(sdlAudioDevice, 0);
} }
if (selected_part==0) { if (key == SDL_SCANCODE_F5) {
song.Play();
for (int i=0;i<4;++i) {
channel[i].setup(song.GetCurrentNote(i));
channel[i].setMute(muted[i]);
}
SDL_PauseAudioDevice(sdlAudioDevice, 0);
}
if (key == SDL_SCANCODE_F8) {
song.Stop();
}
if (sel_start.part==0) {
bool m = true; bool m = true;
switch(key) { switch(key) {
case SDL_SCANCODE_Z: note.SetFirstNote(0, base_octave, current_instrument, current_volume, current_effect); break; case SDL_SCANCODE_Z: note.SetFirstNote(0, base_octave, current_instrument, current_volume, current_effect); break;
@@ -184,109 +269,126 @@ int main(int argc, char* argv[]) {
case SDL_SCANCODE_N: note.SetFirstNote(9, base_octave, current_instrument, current_volume, current_effect); break; case SDL_SCANCODE_N: note.SetFirstNote(9, base_octave, current_instrument, current_volume, current_effect); break;
case SDL_SCANCODE_J: note.SetFirstNote(10, base_octave, current_instrument, current_volume, current_effect); break; case SDL_SCANCODE_J: note.SetFirstNote(10, base_octave, current_instrument, current_volume, current_effect); break;
case SDL_SCANCODE_M: note.SetFirstNote(11, base_octave, current_instrument, current_volume, current_effect); break; case SDL_SCANCODE_M: note.SetFirstNote(11, base_octave, current_instrument, current_volume, current_effect); break;
case SDL_SCANCODE_A: note.SetSilence(); break;
default: m = false; break; default: m = false; break;
}; };
if (m) { if (m) {
channel[0].setup(note.Get()); playNote(note);
SDL_PauseAudioDevice(sdlAudioDevice, 0); /*channel[0].setup(note.Get());
channel[1].setup(0);
channel[2].setup(0);
channel[3].setup(0);
SDL_PauseAudioDevice(sdlAudioDevice, 0);*/
} }
} }
if (selected_part==1) { if (sel_start.part==1) {
switch(key) { switch(key) {
case SDL_SCANCODE_2: note.SetOctave(2); break; case SDL_SCANCODE_2: note.SetOctave(2); playNote(note); break;
case SDL_SCANCODE_3: note.SetOctave(3); break; case SDL_SCANCODE_3: note.SetOctave(3); playNote(note); break;
case SDL_SCANCODE_4: note.SetOctave(4); break; case SDL_SCANCODE_4: note.SetOctave(4); playNote(note); break;
case SDL_SCANCODE_5: note.SetOctave(5); break; case SDL_SCANCODE_5: note.SetOctave(5); playNote(note); break;
case SDL_SCANCODE_6: note.SetOctave(6); break; case SDL_SCANCODE_6: note.SetOctave(6); playNote(note); break;
default: break; default: break;
}; };
} }
if (selected_part==2) { if (sel_start.part==2) {
switch(key) { switch(key) {
case SDL_SCANCODE_0: note.SetInstrument(0); break; case SDL_SCANCODE_0: note.SetInstrument(0); playNote(note); break;
case SDL_SCANCODE_1: note.SetInstrument(1); break; case SDL_SCANCODE_1: note.SetInstrument(1); playNote(note); break;
case SDL_SCANCODE_2: note.SetInstrument(2); break; case SDL_SCANCODE_2: note.SetInstrument(2); playNote(note); break;
case SDL_SCANCODE_3: note.SetInstrument(3); break; case SDL_SCANCODE_3: note.SetInstrument(3); playNote(note); break;
default: break; default: break;
}; };
} }
if (selected_part==3) { if (sel_start.part==3) {
switch(key) { switch(key) {
case SDL_SCANCODE_0: note.SetVolume(0); break; case SDL_SCANCODE_0: note.SetVolume(0); playNote(note); break;
case SDL_SCANCODE_1: note.SetVolume(1); break; case SDL_SCANCODE_1: note.SetVolume(1); playNote(note); break;
case SDL_SCANCODE_2: note.SetVolume(2); break; case SDL_SCANCODE_2: note.SetVolume(2); playNote(note); break;
case SDL_SCANCODE_3: note.SetVolume(3); break; case SDL_SCANCODE_3: note.SetVolume(3); playNote(note); break;
case SDL_SCANCODE_4: note.SetVolume(4); break; case SDL_SCANCODE_4: note.SetVolume(4); playNote(note); break;
case SDL_SCANCODE_5: note.SetVolume(5); break; case SDL_SCANCODE_5: note.SetVolume(5); playNote(note); break;
case SDL_SCANCODE_6: note.SetVolume(6); break; case SDL_SCANCODE_6: note.SetVolume(6); playNote(note); break;
case SDL_SCANCODE_7: note.SetVolume(7); break; case SDL_SCANCODE_7: note.SetVolume(7); playNote(note); break;
case SDL_SCANCODE_8: note.SetVolume(8); break; case SDL_SCANCODE_8: note.SetVolume(8); playNote(note); break;
case SDL_SCANCODE_9: note.SetVolume(9); break; case SDL_SCANCODE_9: note.SetVolume(9); playNote(note); break;
case SDL_SCANCODE_A: note.SetVolume(10); break; case SDL_SCANCODE_A: note.SetVolume(10); playNote(note); break;
case SDL_SCANCODE_B: note.SetVolume(11); break; case SDL_SCANCODE_B: note.SetVolume(11); playNote(note); break;
case SDL_SCANCODE_C: note.SetVolume(12); break; case SDL_SCANCODE_C: note.SetVolume(12); playNote(note); break;
case SDL_SCANCODE_D: note.SetVolume(13); break; case SDL_SCANCODE_D: note.SetVolume(13);playNote(note); break;
case SDL_SCANCODE_E: note.SetVolume(14); break; case SDL_SCANCODE_E: note.SetVolume(14); playNote(note); break;
case SDL_SCANCODE_F: note.SetVolume(15); break; case SDL_SCANCODE_F: note.SetVolume(15); playNote(note); break;
default: break; default: break;
}; };
} }
if (selected_part==4) { if (sel_start.part==4) {
switch(key) { switch(key) {
case SDL_SCANCODE_0: note.SetEffect(0); break; case SDL_SCANCODE_0: note.SetEffect(0); playNote(note); break;
case SDL_SCANCODE_1: note.SetEffect(1); break; case SDL_SCANCODE_1: note.SetEffect(1); playNote(note); break;
case SDL_SCANCODE_2: note.SetEffect(2); break; case SDL_SCANCODE_2: note.SetEffect(2); playNote(note); break;
case SDL_SCANCODE_3: note.SetEffect(3); break; case SDL_SCANCODE_3: note.SetEffect(3); playNote(note); break;
case SDL_SCANCODE_4: note.SetEffect(4); break; case SDL_SCANCODE_4: note.SetEffect(4); playNote(note); break;
case SDL_SCANCODE_5: note.SetEffect(5); break; case SDL_SCANCODE_5: note.SetEffect(5); playNote(note); break;
case SDL_SCANCODE_6: note.SetEffect(6); break; case SDL_SCANCODE_6: note.SetEffect(6); playNote(note); break;
case SDL_SCANCODE_7: note.SetEffect(7); break; case SDL_SCANCODE_7: note.SetEffect(7); playNote(note); break;
case SDL_SCANCODE_8: note.SetEffect(8); break; case SDL_SCANCODE_8: note.SetEffect(8); playNote(note); break;
case SDL_SCANCODE_9: note.SetEffect(9); break; case SDL_SCANCODE_9: note.SetEffect(9); playNote(note); break;
case SDL_SCANCODE_A: note.SetEffect(10); break; case SDL_SCANCODE_A: note.SetEffect(10); playNote(note); break;
case SDL_SCANCODE_B: note.SetEffect(11); break; case SDL_SCANCODE_B: note.SetEffect(11); playNote(note); break;
case SDL_SCANCODE_C: note.SetEffect(12); break; case SDL_SCANCODE_C: note.SetEffect(12); playNote(note); break;
case SDL_SCANCODE_D: note.SetEffect(13); break; case SDL_SCANCODE_D: note.SetEffect(13); playNote(note); break;
case SDL_SCANCODE_E: note.SetEffect(14); break; case SDL_SCANCODE_E: note.SetEffect(14); playNote(note); break;
case SDL_SCANCODE_F: note.SetEffect(15); break; case SDL_SCANCODE_F: note.SetEffect(15); playNote(note); break;
default: break; default: break;
}; };
} }
pattern[selected_channel][selected_row] = note.Get(); song.SetCurrentNote(sel_start.channel, sel_start.row, note.Get());
} }
} }
draw_clear(color_dark_grey); draw_clear(color_dark_grey);
draw_fill(3, 14, 33, 225, color_black); draw_fill(13, 14, 33, 225, color_black);
draw_inset(2, 13, 34, 226, color_light_peach, color_dark_blue); draw_inset(12, 13, 34, 226, color_light_peach, color_dark_blue);
draw_fill(38, 14, 33, 225, color_black); draw_fill(48, 14, 33, 225, color_black);
draw_inset(37, 13, 34, 226, color_light_peach, color_dark_blue); draw_inset(47, 13, 34, 226, color_light_peach, color_dark_blue);
draw_fill(73, 14, 33, 225, color_black); draw_fill(83, 14, 33, 225, color_black);
draw_inset(72, 13, 34, 226, color_light_peach, color_dark_blue); draw_inset(82, 13, 34, 226, color_light_peach, color_dark_blue);
draw_fill(108, 14, 33, 225, color_black); draw_fill(118, 14, 33, 225, color_black);
draw_inset(107, 13, 34, 226, color_light_peach, color_dark_blue); draw_inset(117, 13, 34, 226, color_light_peach, color_dark_blue);
//draw_dotted(4, 71, 35, color_lavender); //draw_dotted(4, 71, 35, color_lavender);
int xp = 4, yp = 15; for (int y=0; y<32; ++y) {
draw_string(get_zero_padded(y+scroll), 2, 15+y*7, color_dark_blue);
}
int xp = 14, yp = 15;
for (int x=0; x<4; ++x) { for (int x=0; x<4; ++x) {
yp = 15; yp = 15;
for (int y=0; y<32; ++y) { 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 >= sel_start.channel && x <= sel_end.channel && (y+scroll) >= sel_start.row && (y+scroll) <= sel_end.row) {
draw_fill(xp-1, yp-1, 32, 7, color_dark_blue); draw_fill(xp-1, yp-1, 32, 7, color_dark_blue);
switch (selected_part) { if (x == sel_start.channel && (y+scroll) == sel_start.row) {
case 0: draw_fill(xp-1, yp-1, 11, 7, color_yellow); break; switch (sel_start.part) {
case 1: draw_fill(9+xp, yp-1, 6, 7, color_yellow); break; case 0: draw_fill(xp-1, yp-1, 11, 7, color_yellow); break;
case 2: draw_fill(15+xp, yp-1, 6, 7, color_yellow); break; case 1: draw_fill(9+xp, yp-1, 6, 7, color_yellow); break;
case 3: draw_fill(20+xp, yp-1, 6, 7, color_yellow); break; case 2: draw_fill(15+xp, yp-1, 6, 7, color_yellow); break;
case 4: draw_fill(25+xp, yp-1, 6, 7, color_yellow); break; case 3: draw_fill(20+xp, yp-1, 6, 7, color_yellow); break;
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]; //Uint16 current_note = pattern[x][y];
if (note.GetAbsoluteNote() == 0) { if (note.GetAbsoluteNote() == 0) {
uint8_t color = color_dark_blue; uint8_t color = color_dark_blue;
if (x == selected_channel && (y+scroll) == selected_row) color = color_dark_purple; if (x >= sel_start.channel && x <= sel_end.channel && (y+scroll) >= sel_start.row && (y+scroll) <= sel_end.row) color = color_dark_purple;
draw_string("...", xp, yp, color); draw_string("...", xp, yp, color);
draw_string("...", 16+xp, yp, color); draw_string("...", 16+xp, yp, color);
} else if (note.GetAbsoluteNote() == 63) {
uint8_t color = color_light_grey;
//if (x == selected_channel && (y+scroll) == selected_row) color = color_dark_purple;
draw_string("===", xp, yp, color);
draw_string("===", 16+xp, yp, color);
} else { } else {
//uint8_t note_num = get_note(current_note); //uint8_t note_num = get_note(current_note);
@@ -301,7 +403,7 @@ int main(int argc, char* argv[]) {
} }
xp += 35; xp += 35;
} }
//draw_string("......", 4, 15, color_dark_blue); //draw_string(get_zero_padded(0), 2, 15, color_dark_blue);
//draw_string("......", 38, 15, color_dark_blue); //draw_string("......", 38, 15, color_dark_blue);
/*int last_x = 0, last_y = 120; /*int last_x = 0, last_y = 120;
for (int x=0; x<1102; ++x) { for (int x=0; x<1102; ++x) {
@@ -310,8 +412,19 @@ int main(int argc, char* argv[]) {
last_x = x; last_y = y; last_x = x; last_y = y;
}*/ }*/
//draw_string("HOLA", 11, 11, color_black); for (int i=0; i<4; ++i) {
//draw_string("HOLA", 10, 10, color_white); if (muted[i]) {
draw_inset(12+i*35, 2, 33, 11, color_light_peach, color_dark_blue);
//draw_string("MUTE", 20+i*35, 6, color_black);
draw_string("MUTE", 19+i*35, 5, color_light_grey);
} else {
draw_outset(12+i*35, 2, 33, 11, color_light_peach, color_dark_blue);
draw_string("MUTE", 20+i*35, 6, color_black);
draw_string("MUTE", 19+i*35, 5, color_white);
}
}
draw_present(); draw_present();
//SDL_QueueAudio(sdlAudioDevice, &beeps[0], SOUND_SIZE_IN_BYTES*8); //SDL_QueueAudio(sdlAudioDevice, &beeps[0], SOUND_SIZE_IN_BYTES*8);

View File

@@ -64,6 +64,14 @@ void Note::SetFirstNote(const uint8_t note, const uint8_t octave, const uint8_t
this->note = note; this->note = note;
} }
void Note::SetSilence() {
this->note = 63;
this->octave = 0;
this->instrument = 0;
this->volume = 0;
this->effect = 0;
}
void Note::SetOctave(const uint8_t octave) { void Note::SetOctave(const uint8_t octave) {
this->octave = octave; this->octave = octave;
} }
@@ -89,6 +97,8 @@ const uint16_t Note::Get() {
const uint8_t Note::GetAbsoluteNote() { const uint8_t Note::GetAbsoluteNote() {
if ((this->note == 0) && (this->octave == 0)) { if ((this->note == 0) && (this->octave == 0)) {
return 0; return 0;
} else if (this->note == 63) {
return 63;
} else { } else {
uint8_t result = ((this->octave-2)*12)+this->note+1; uint8_t result = ((this->octave-2)*12)+this->note+1;
return result; return result;

1
note.h
View File

@@ -17,6 +17,7 @@ class Note {
void SetInstrument(const uint8_t instrument); void SetInstrument(const uint8_t instrument);
void SetVolume(const uint8_t volume); void SetVolume(const uint8_t volume);
void SetEffect(const uint8_t effect); void SetEffect(const uint8_t effect);
void SetSilence();
const uint16_t Get(); const uint16_t Get();
const uint8_t GetAbsoluteNote(); const uint8_t GetAbsoluteNote();

46
song.cpp Normal file
View File

@@ -0,0 +1,46 @@
#include "song.h"
Song::Song() {
this->playing = false;
this->current_pattern = 0;
this->current_row = 0;
}
void Song::SetCurrentPatternNum(const int pattern_num) {
this->current_pattern = pattern_num;
}
void Song::SetCurrentNote(const int channel, const int row, const uint16_t note) {
this->pattern[channel][row] = note;
}
const int Song::GetCurrentPatternNum() {
return this->current_pattern;
}
const int Song::GetCurrentRow() {
return this->current_row;
}
uint16_t Song::GetCurrentNote(const int channel, int row) {
return this->pattern[channel][row<0?this->current_row:row];
}
void Song::Play() {
this->current_row = 0;
this->playing = true;
}
void Song::Stop() {
this->playing = false;
}
const bool Song::IsPlaying() {
return this->playing;
}
void Song::Next() {
this->current_row++;
if (this->current_row == 64) this->current_row = 0;
}

28
song.h Normal file
View File

@@ -0,0 +1,28 @@
#pragma once
#include <cstdint>
#include <vector>
//typedef uint16_t[4][64] Pattern;
class Song {
public:
Song();
void SetCurrentPatternNum(const int pattern_num);
void SetCurrentNote(const int channel, const int row, const uint16_t note);
const int GetCurrentPatternNum();
const int GetCurrentRow();
uint16_t GetCurrentNote(const int channel, int row = -1);
void Play();
void Stop();
const bool IsPlaying();
void Next();
private:
bool playing;
int current_pattern;
int current_row;
uint16_t pattern[4][64];
};

View File

@@ -9,8 +9,9 @@ toneGen::toneGen() {
this->note = 34; this->note = 34;
this->instrument = CHIPTUNE_INSTRUMENT_NOISE; this->instrument = CHIPTUNE_INSTRUMENT_NOISE;
this->volume = 15; this->volume = 0;
this->effect = CHIPTUNE_EFFECT_FADEOUT; this->effect = CHIPTUNE_EFFECT_FADEOUT;
this->muted = false;
this->current_volume = this->nominal_volume = 1.0f; this->current_volume = this->nominal_volume = 1.0f;
@@ -21,6 +22,7 @@ toneGen::toneGen() {
this->old_df = this->df = notes[this->note]; this->old_df = this->df = notes[this->note];
this->f = -AUDIO_FORMAT_MAX_VALUE; this->f = -AUDIO_FORMAT_MAX_VALUE;
this->vibrato_counter = 0;
} }
void toneGen::setNoteLength(const int note_length) { void toneGen::setNoteLength(const int note_length) {
@@ -30,10 +32,13 @@ void toneGen::setNoteLength(const int 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;
if (note == 0) return;
this->note = note; this->note = note;
this->instrument = instrument; this->instrument = instrument;
this->volume = volume; this->volume = volume;
this->effect = effect; this->effect = effect;
//this->muted = false;
this->nominal_volume = float(volume) / 15.0f; this->nominal_volume = float(volume) / 15.0f;
this->current_volume = this->effect == CHIPTUNE_EFFECT_FADEIN ? 0.0f : this->nominal_volume; this->current_volume = this->effect == CHIPTUNE_EFFECT_FADEIN ? 0.0f : this->nominal_volume;
@@ -52,12 +57,13 @@ const int toneGen::getSamples(const int numSamples, Sint16 *buffer) {
for (int i=0; i<actual_samples_generated;++i) { for (int i=0; i<actual_samples_generated;++i) {
f += df; f += df;
vibrato_counter += 0.001f;
switch (this->effect) { switch (this->effect) {
case CHIPTUNE_EFFECT_NONE: case CHIPTUNE_EFFECT_NONE:
break; break;
case CHIPTUNE_EFFECT_VIBRATO: case CHIPTUNE_EFFECT_VIBRATO:
f += SDL_sinf((i+this->current_pos)/1000)*15; f += SDL_sinf(vibrato_counter)*8;
break; break;
case CHIPTUNE_EFFECT_DROP: case CHIPTUNE_EFFECT_DROP:
if (((i+this->current_pos)%15)==0 && df != 0) { df--; if (df < 100) this->current_volume = this->nominal_volume*df*0.01f; } if (((i+this->current_pos)%15)==0 && df != 0) { df--; if (df < 100) this->current_volume = this->nominal_volume*df*0.01f; }
@@ -88,9 +94,13 @@ const int toneGen::getSamples(const int numSamples, Sint16 *buffer) {
buffer[i] = (rand()*2)-AUDIO_FORMAT_MAX_VALUE; buffer[i] = (rand()*2)-AUDIO_FORMAT_MAX_VALUE;
break; break;
} }
buffer[i] *= this->current_volume; buffer[i] *= (muted ? 0 : this->current_volume);
} }
this->current_pos += actual_samples_generated; this->current_pos += actual_samples_generated;
return actual_samples_generated; return actual_samples_generated;
} }
void toneGen::setMute(const bool value) {
muted = value;
}

View File

@@ -21,6 +21,7 @@ class toneGen {
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);
void setMute(const bool value);
private: private:
uint8_t note, instrument, volume, effect; uint8_t note, instrument, volume, effect;
@@ -28,4 +29,6 @@ class toneGen {
Sint16 df, old_df, f; Sint16 df, old_df, f;
int note_length; int note_length;
int current_pos; int current_pos;
float vibrato_counter;
bool muted;
}; };