more work on ui, first functionality

This commit is contained in:
2021-09-05 20:36:44 +02:00
parent c9a428c4c2
commit dda58872ad
4 changed files with 278 additions and 23 deletions

158
main.cpp
View File

@@ -2,18 +2,35 @@
#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;
SDL_AudioSpec audioSpec{SAMPLES_PER_SECOND, AUDIO_FORMAT, 1, 0, 512, 0, 0, NULL, NULL};
const char get_hex(const uint8_t num) {
static const char hexstr[17] = "0123456789ABCDEF";
return hexstr[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);
@@ -70,24 +87,119 @@ int main(int argc, char* argv[]) {
while (SDL_PollEvent(&sdlEvent)) {
if (sdlEvent.type == SDL_QUIT) { exit = true; break;}
if (sdlEvent.type == SDL_KEYDOWN) {
if (sdlEvent.key.keysym.scancode == SDL_SCANCODE_DOWN && selected_row < 32) selected_row++;
if (sdlEvent.key.keysym.scancode == SDL_SCANCODE_UP && selected_row > 0) selected_row--;
if (sdlEvent.key.keysym.scancode == SDL_SCANCODE_LEFT && (selected_channel > 0 || selected_part > 0)) {
if (selected_part > 0) {
selected_part--;
} else {
selected_channel--;
selected_part = 4;
SDL_Scancode key = sdlEvent.key.keysym.scancode;
if (key == SDL_SCANCODE_DOWN) {
if (selected_row < 32) selected_row++;
} else if (key == SDL_SCANCODE_UP) {
if (selected_row > 0) 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;
}
}
}
if (sdlEvent.key.keysym.scancode == SDL_SCANCODE_RIGHT && (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) note.Set(0);
if (selected_part==0) {
bool m = true;
switch(key) {
case SDL_SCANCODE_Z: if (note.GetNote()==0) note.SetOctave(base_octave); note.SetNote(0); break;
case SDL_SCANCODE_S: if (note.GetNote()==0) note.SetOctave(base_octave); note.SetNote(1); break;
case SDL_SCANCODE_X: if (note.GetNote()==0) note.SetOctave(base_octave); note.SetNote(2); break;
case SDL_SCANCODE_D: if (note.GetNote()==0) note.SetOctave(base_octave); note.SetNote(3); break;
case SDL_SCANCODE_C: if (note.GetNote()==0) note.SetOctave(base_octave); note.SetNote(4); break;
case SDL_SCANCODE_V: if (note.GetNote()==0) note.SetOctave(base_octave); note.SetNote(5); break;
case SDL_SCANCODE_G: if (note.GetNote()==0) note.SetOctave(base_octave); note.SetNote(6); break;
case SDL_SCANCODE_B: if (note.GetNote()==0) note.SetOctave(base_octave); note.SetNote(7); break;
case SDL_SCANCODE_H: if (note.GetNote()==0) note.SetOctave(base_octave); note.SetNote(8); break;
case SDL_SCANCODE_N: if (note.GetNote()==0) note.SetOctave(base_octave); note.SetNote(9); break;
case SDL_SCANCODE_J: if (note.GetNote()==0) note.SetOctave(base_octave); note.SetNote(10); break;
case SDL_SCANCODE_M: if (note.GetNote()==0) note.SetOctave(base_octave); note.SetNote(11); break;
default: m = false; break;
};
if (m) {
tonegen.setup(note.Get());
numSamples = tonegen.getSamples(SOUND_NUM_SAMPLES, beeps);
SDL_QueueAudio(sdlAudioDevice, &beeps[0], SOUND_SIZE_IN_BYTES);
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);
@@ -115,17 +227,21 @@ int main(int argc, char* argv[]) {
case 4: draw_fill(25+xp, yp-1, 6, 7, color_yellow); break;
}
}
if (x==0 && y==0) {
draw_string("G#", xp, yp, color_white);
draw_char('1', 10+xp, yp, color_light_grey);
draw_char('5', 16+xp, yp, color_pink);
draw_char('2', 21+xp, yp, color_blue);
draw_char('3', 26+xp, yp, color_lavender);
} else {
Note note(pattern[x][y]);
//Uint16 current_note = pattern[x][y];
if (note.GetAbsoluteNote() == 0) {
uint8_t color = color_dark_blue;
if (x == selected_channel && y == 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);
}
yp += 7;
if ((y%8)==7) {

106
note.cpp Normal file
View File

@@ -0,0 +1,106 @@
#include "note.h"
const uint8_t get_octave_from_wholenote(const uint8_t wholenote) {
return wholenote == 0 ? 0 : 2+int((wholenote-1)/12);
}
const uint8_t get_note_from_wholenote(const uint8_t wholenote) {
return wholenote == 0 ? 0 : (wholenote-1)%12;
}
Note::Note() {
note = octave = instrument = volume = effect = 0;
}
Note::Note(const uint16_t note) {
this->Set(note);
}
Note::Note(const uint8_t note, const uint8_t instrument, const uint8_t volume, const uint8_t effect) {
this->note = note;
this->instrument = instrument;
this->volume = volume;
this->effect = effect;
this->octave = get_octave_from_wholenote(this->note);
this->note = get_note_from_wholenote(this->note);
}
Note::Note(const uint8_t note, const uint8_t octave, const uint8_t instrument, const uint8_t volume, const uint8_t effect) {
this->note = note;
this->octave = octave;
this->instrument = instrument;
this->volume = volume;
this->effect = effect;
}
void Note::Set(const uint16_t note) {
this->note = note >> 10;
this->instrument = (note >> 8) & 0x3;
this->volume = (note >> 4) & 0xf;
this->effect = note & 0xf;
this->octave = get_octave_from_wholenote(this->note);
this->note = get_note_from_wholenote(this->note);
}
void Note::SetAbsoluteNote(const uint8_t note) {
this->octave = get_octave_from_wholenote(note);
this->note = get_note_from_wholenote(note);
}
void Note::SetNote(const uint8_t note) {
this->note = note;
}
void Note::SetOctave(const uint8_t octave) {
this->octave = octave;
}
void Note::SetInstrument(const uint8_t instrument) {
this->instrument = instrument;
}
void Note::SetVolume(const uint8_t volume) {
this->volume = volume;
}
void Note::SetEffect(const uint8_t effect) {
this->effect = effect;
}
const uint16_t Note::Get() {
uint16_t result = (this->GetAbsoluteNote() << 10) + (this->instrument << 8) + (this->volume << 4) + this->effect;
return result;
}
const uint8_t Note::GetAbsoluteNote() {
if ((this->note == 0) && (this->octave == 0)) {
return 0;
} else {
uint8_t result = ((this->octave-2)*12)+this->note+1;
return result;
}
}
const uint8_t Note::GetNote() {
return this->note;
}
const uint8_t Note::GetOctave() {
return this->octave;
}
const uint8_t Note::GetInstrument() {
return this->instrument;
}
const uint8_t Note::GetVolume() {
return this->volume;
}
const uint8_t Note::GetEffect() {
return this->effect;
}

33
note.h Normal file
View File

@@ -0,0 +1,33 @@
#pragma once
#include <cstdint>
class Note {
public:
Note();
Note(const uint16_t note);
Note(const uint8_t note, const uint8_t instrument, const uint8_t volume, const uint8_t effect);
Note(const uint8_t note, const uint8_t octave, const uint8_t instrument, const uint8_t volume, const uint8_t effect);
void Set(const uint16_t note);
void SetAbsoluteNote(const uint8_t note);
void SetNote(const uint8_t note);
void SetOctave(const uint8_t octave);
void SetInstrument(const uint8_t instrument);
void SetVolume(const uint8_t volume);
void SetEffect(const uint8_t effect);
const uint16_t Get();
const uint8_t GetAbsoluteNote();
const uint8_t GetNote();
const uint8_t GetOctave();
const uint8_t GetInstrument();
const uint8_t GetVolume();
const uint8_t GetEffect();
private:
uint8_t note;
uint8_t octave;
uint8_t instrument;
uint8_t volume;
uint8_t effect;
};

View File

@@ -8,7 +8,7 @@ toneGen::toneGen(const int note_length) {
this->note_length = note_length;
this->current_pos = 0;
this->note = 32;
this->note = 34;
this->instrument = CHIPTUNE_INSTRUMENT_NOISE;
this->volume = 15;
this->effect = CHIPTUNE_EFFECT_FADEOUT;
@@ -16,7 +16,7 @@ toneGen::toneGen(const int note_length) {
this->current_volume = this->nominal_volume = 1.0f;
for (int i=0; i<64; ++i) {
const float NOTE_FREQ = 440 * pow(1.059463094359, i-32);
const float NOTE_FREQ = 440 * pow(1.059463094359, i-34);
notes[i] = 65536 / int(SAMPLES_PER_SECOND/NOTE_FREQ);
}