Files
chirping/main.cpp

325 lines
17 KiB
C++
Executable File

#include <SDL2/SDL.h>
#include "tonegen.h"
#include "audio.h"
#include "draw.h"
#include "note.h"
#define SOUND_DURATION 0.25f
#define SOUND_NUM_SAMPLES SOUND_DURATION*SAMPLES_PER_SECOND
#define SOUND_SIZE_IN_BYTES SOUND_NUM_SAMPLES*AUDIO_FORMAT_SIZE
/*#define get_note(x) (x>>10)
#define get_instrument(x) ((x>>8) & 0x3)
#define get_volume(x) ((x>>4) & 0xF)
#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];
int selected_channel = 0;
int selected_row = 0;
int selected_part = 0;
int scroll = 0;
int base_octave = 4;
int current_instrument = 0;
int current_volume = 2;
int current_effect = 0;
toneGen channel[4];
SDL_AudioDeviceID sdlAudioDevice;
Sint16 auxBuffer[512];
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);
}
}
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";
return hexstr[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;
SDL_Init(SDL_INIT_EVERYTHING);
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.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);
tonegen.setup(32, CHIPTUNE_INSTRUMENT_NOISE, 4, CHIPTUNE_EFFECT_FADEOUT);
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);
tonegen.setup(22, CHIPTUNE_INSTRUMENT_TRIANGLE, 13, CHIPTUNE_EFFECT_DROP);
numSamples = tonegen.getSamples(SOUND_NUM_SAMPLES, pointer); pointer += int(SOUND_NUM_SAMPLES);
tonegen.setup(22, CHIPTUNE_INSTRUMENT_TRIANGLE, 13, CHIPTUNE_EFFECT_DROP);
numSamples = tonegen.getSamples(SOUND_NUM_SAMPLES, pointer); pointer += int(SOUND_NUM_SAMPLES);
tonegen.setup(32, CHIPTUNE_INSTRUMENT_NOISE, 4, CHIPTUNE_EFFECT_FADEOUT);
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);
pointer = beeps2;
tonegen.setup(35, CHIPTUNE_INSTRUMENT_SQUARE, 1, CHIPTUNE_EFFECT_NONE);
numSamples = tonegen.getSamples(SOUND_NUM_SAMPLES, pointer); pointer += int(SOUND_NUM_SAMPLES);
tonegen.setup(37, CHIPTUNE_INSTRUMENT_SQUARE, 1, CHIPTUNE_EFFECT_NONE);
numSamples = tonegen.getSamples(SOUND_NUM_SAMPLES, pointer); pointer += int(SOUND_NUM_SAMPLES);
tonegen.setup(39, CHIPTUNE_INSTRUMENT_SQUARE, 1, CHIPTUNE_EFFECT_NONE);
numSamples = tonegen.getSamples(SOUND_NUM_SAMPLES, pointer); pointer += int(SOUND_NUM_SAMPLES);
tonegen.setup(40, CHIPTUNE_INSTRUMENT_SQUARE, 1, CHIPTUNE_EFFECT_NONE);
numSamples = tonegen.getSamples(SOUND_NUM_SAMPLES, pointer); pointer += int(SOUND_NUM_SAMPLES);
tonegen.setup(42, CHIPTUNE_INSTRUMENT_SQUARE, 1, CHIPTUNE_EFFECT_NONE);
numSamples = tonegen.getSamples(SOUND_NUM_SAMPLES, pointer); pointer += int(SOUND_NUM_SAMPLES);
tonegen.setup(44, CHIPTUNE_INSTRUMENT_SQUARE, 1, CHIPTUNE_EFFECT_NONE);
numSamples = tonegen.getSamples(SOUND_NUM_SAMPLES, pointer); pointer += int(SOUND_NUM_SAMPLES);
tonegen.setup(46, CHIPTUNE_INSTRUMENT_SQUARE, 1, CHIPTUNE_EFFECT_NONE);
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];
}
draw_init();
bool exit = false;
SDL_Event sdlEvent;
while (!exit) {
while (SDL_PollEvent(&sdlEvent)) {
if (sdlEvent.type == SDL_QUIT) { exit = true; break;}
if (sdlEvent.type == SDL_MOUSEBUTTONDOWN) {
if (sdlEvent.button.button == SDL_BUTTON_LEFT) {
if ((sdlEvent.button.x >= 3) && (sdlEvent.button.x <= 139) && (sdlEvent.button.y >= 14) && (sdlEvent.button.y <= 239) ) {
selected_row = scroll + int((sdlEvent.button.y-14)/7);
selected_channel = (sdlEvent.button.x-3)/35;
int part_pos = (sdlEvent.button.x-3)-(selected_channel*35);
if (part_pos <= 10) {
selected_part = 0;
} else if (part_pos <= 15) {
selected_part = 1;
} else if (part_pos <= 21) {
selected_part = 2;
} else if (part_pos <= 26) {
selected_part = 3;
} else if (part_pos <= 31) {
selected_part = 4;
}
}
}
}
if (sdlEvent.type == SDL_MOUSEWHEEL) {
scroll -= sdlEvent.wheel.y;
if (scroll < 0) scroll = 0; else if (scroll > 32) scroll = 32;
}
if (sdlEvent.type == SDL_KEYDOWN) {
SDL_Scancode key = sdlEvent.key.keysym.scancode;
if (key == SDL_SCANCODE_DOWN) {
if (selected_row < 63) selected_row++;
if (selected_row >= 32 + scroll) scroll = selected_row-31;
} else if (key == SDL_SCANCODE_UP) {
if (selected_row > 0) selected_row--;
if (selected_row < scroll) scroll = selected_row;
} else if (key == SDL_SCANCODE_LEFT) {
if (selected_channel > 0 || selected_part > 0) {
if (selected_part > 0) {
selected_part--;
} else {
selected_channel--;
selected_part = 4;
}
}
} else if (key == SDL_SCANCODE_RIGHT) {
if (selected_channel < 3 || selected_part < 4) {
if (selected_part < 4) {
selected_part++;
} else {
selected_channel++;
selected_part = 0;
}
}
}
Note note(pattern[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_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]);
SDL_PauseAudioDevice(sdlAudioDevice, 0);
}
if (selected_part==0) {
bool m = true;
switch(key) {
case SDL_SCANCODE_Z: note.SetFirstNote(0, base_octave, current_instrument, current_volume, current_effect); break;
case SDL_SCANCODE_S: note.SetFirstNote(1, base_octave, current_instrument, current_volume, current_effect); break;
case SDL_SCANCODE_X: note.SetFirstNote(2, base_octave, current_instrument, current_volume, current_effect); break;
case SDL_SCANCODE_D: note.SetFirstNote(3, base_octave, current_instrument, current_volume, current_effect); break;
case SDL_SCANCODE_C: note.SetFirstNote(4, base_octave, current_instrument, current_volume, current_effect); break;
case SDL_SCANCODE_V: note.SetFirstNote(5, base_octave, current_instrument, current_volume, current_effect); break;
case SDL_SCANCODE_G: note.SetFirstNote(6, base_octave, current_instrument, current_volume, current_effect); break;
case SDL_SCANCODE_B: note.SetFirstNote(7, base_octave, current_instrument, current_volume, current_effect); break;
case SDL_SCANCODE_H: note.SetFirstNote(8, 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_M: note.SetFirstNote(11, base_octave, current_instrument, current_volume, current_effect); break;
default: m = false; break;
};
if (m) {
channel[0].setup(note.Get());
SDL_PauseAudioDevice(sdlAudioDevice, 0);
}
}
if (selected_part==1) {
switch(key) {
case SDL_SCANCODE_2: note.SetOctave(2); break;
case SDL_SCANCODE_3: note.SetOctave(3); break;
case SDL_SCANCODE_4: note.SetOctave(4); break;
case SDL_SCANCODE_5: note.SetOctave(5); break;
case SDL_SCANCODE_6: note.SetOctave(6); break;
default: break;
};
}
if (selected_part==2) {
switch(key) {
case SDL_SCANCODE_0: note.SetInstrument(0); break;
case SDL_SCANCODE_1: note.SetInstrument(1); break;
case SDL_SCANCODE_2: note.SetInstrument(2); break;
case SDL_SCANCODE_3: note.SetInstrument(3); break;
default: break;
};
}
if (selected_part==3) {
switch(key) {
case SDL_SCANCODE_0: note.SetVolume(0); break;
case SDL_SCANCODE_1: note.SetVolume(1); break;
case SDL_SCANCODE_2: note.SetVolume(2); break;
case SDL_SCANCODE_3: note.SetVolume(3); break;
case SDL_SCANCODE_4: note.SetVolume(4); break;
case SDL_SCANCODE_5: note.SetVolume(5); break;
case SDL_SCANCODE_6: note.SetVolume(6); break;
case SDL_SCANCODE_7: note.SetVolume(7); break;
case SDL_SCANCODE_8: note.SetVolume(8); break;
case SDL_SCANCODE_9: note.SetVolume(9); break;
case SDL_SCANCODE_A: note.SetVolume(10); break;
case SDL_SCANCODE_B: note.SetVolume(11); break;
case SDL_SCANCODE_C: note.SetVolume(12); break;
case SDL_SCANCODE_D: note.SetVolume(13); break;
case SDL_SCANCODE_E: note.SetVolume(14); break;
case SDL_SCANCODE_F: note.SetVolume(15); break;
default: break;
};
}
if (selected_part==4) {
switch(key) {
case SDL_SCANCODE_0: note.SetEffect(0); break;
case SDL_SCANCODE_1: note.SetEffect(1); break;
case SDL_SCANCODE_2: note.SetEffect(2); break;
case SDL_SCANCODE_3: note.SetEffect(3); break;
case SDL_SCANCODE_4: note.SetEffect(4); break;
case SDL_SCANCODE_5: note.SetEffect(5); break;
case SDL_SCANCODE_6: note.SetEffect(6); break;
case SDL_SCANCODE_7: note.SetEffect(7); break;
case SDL_SCANCODE_8: note.SetEffect(8); break;
case SDL_SCANCODE_9: note.SetEffect(9); break;
case SDL_SCANCODE_A: note.SetEffect(10); break;
case SDL_SCANCODE_B: note.SetEffect(11); break;
case SDL_SCANCODE_C: note.SetEffect(12); break;
case SDL_SCANCODE_D: note.SetEffect(13); break;
case SDL_SCANCODE_E: note.SetEffect(14); break;
case SDL_SCANCODE_F: note.SetEffect(15); break;
default: break;
};
}
pattern[selected_channel][selected_row] = note.Get();
}
}
draw_clear(color_dark_grey);
draw_fill(3, 14, 33, 225, color_black);
draw_inset(2, 13, 34, 226, color_light_peach, color_dark_blue);
draw_fill(38, 14, 33, 225, color_black);
draw_inset(37, 13, 34, 226, color_light_peach, color_dark_blue);
draw_fill(73, 14, 33, 225, color_black);
draw_inset(72, 13, 34, 226, color_light_peach, color_dark_blue);
draw_fill(108, 14, 33, 225, color_black);
draw_inset(107, 13, 34, 226, color_light_peach, color_dark_blue);
//draw_dotted(4, 71, 35, color_lavender);
int xp = 4, yp = 15;
for (int x=0; x<4; ++x) {
yp = 15;
for (int y=0; y<32; ++y) {
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;
case 1: draw_fill(9+xp, yp-1, 6, 7, color_yellow); break;
case 2: draw_fill(15+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]);
//Uint16 current_note = pattern[x][y];
if (note.GetAbsoluteNote() == 0) {
uint8_t color = color_dark_blue;
if (x == selected_channel && (y+scroll) == selected_row) color = color_dark_purple;
draw_string("...", xp, yp, color);
draw_string("...", 16+xp, yp, color);
} else {
//uint8_t note_num = get_note(current_note);
draw_string(note_names[note.GetNote()], xp, yp, color_white);
draw_char(48+note.GetOctave(), 10+xp, yp, color_light_grey);
draw_char(48+note.GetInstrument(), 16+xp, yp, color_pink);
draw_char(get_hex(note.GetVolume()), 21+xp, yp, color_blue);
draw_char(get_hex(note.GetEffect()), 26+xp, yp, color_lavender);
}
if ((y!=0) && (((y+scroll)%8)==0)) draw_dotted(xp, yp, xp+30, color_lavender);
yp += 7;
}
xp += 35;
}
//draw_string("......", 4, 15, color_dark_blue);
//draw_string("......", 38, 15, color_dark_blue);
/*int last_x = 0, last_y = 120;
for (int x=0; x<1102; ++x) {
int y = 700 - (350+300*(float(beeps[x*10])/float(AUDIO_FORMAT_MAX_VALUE)));
SDL_RenderDrawLine(sdlRenderer, x, y, last_x, last_y);
last_x = x; last_y = y;
}*/
//draw_string("HOLA", 11, 11, color_black);
//draw_string("HOLA", 10, 10, color_white);
draw_present();
//SDL_QueueAudio(sdlAudioDevice, &beeps[0], SOUND_SIZE_IN_BYTES*8);
//SDL_PauseAudioDevice(sdlAudioDevice, 0);
}
draw_quit();
SDL_Quit();
return 0;
}