add note preview feature

This commit is contained in:
tildearrow 2021-12-28 18:23:57 -05:00
parent 0479650597
commit be3b4da834
6 changed files with 243 additions and 150 deletions

View File

@ -1542,12 +1542,14 @@ int DivEngine::calcFreq(int base, int pitch, bool period) {
void DivEngine::play() {
isBusy.lock();
freelance=false;
playSub(false);
isBusy.unlock();
}
void DivEngine::stop() {
isBusy.lock();
freelance=false;
playing=false;
extValuePresent=false;
isBusy.unlock();
@ -1693,7 +1695,7 @@ unsigned char DivEngine::getExtValue() {
}
bool DivEngine::isPlaying() {
return playing;
return (playing && !freelance);
}
bool DivEngine::isChannelMuted(int chan) {
@ -1902,7 +1904,7 @@ void DivEngine::addOrder(bool duplicate, bool where) {
}
song.ordersLen++;
curOrder++;
if (playing) {
if (playing && !freelance) {
playSub(false);
}
}
@ -1919,7 +1921,7 @@ void DivEngine::deleteOrder() {
}
song.ordersLen--;
if (curOrder>=song.ordersLen) curOrder=song.ordersLen-1;
if (playing) {
if (playing && !freelance) {
playSub(false);
}
isBusy.unlock();
@ -1937,7 +1939,7 @@ void DivEngine::moveOrderUp() {
song.orders.ord[i][curOrder]^=song.orders.ord[i][curOrder-1];
}
curOrder--;
if (playing) {
if (playing && !freelance) {
playSub(false);
}
isBusy.unlock();
@ -1955,17 +1957,39 @@ void DivEngine::moveOrderDown() {
song.orders.ord[i][curOrder]^=song.orders.ord[i][curOrder+1];
}
curOrder++;
if (playing) {
if (playing && !freelance) {
playSub(false);
}
isBusy.unlock();
}
void DivEngine::noteOn(int chan, int ins, int note, int vol) {
isBusy.lock();
pendingNotes.push(DivNoteEvent(chan,ins,note,vol,true));
if (!playing) {
reset();
freelance=true;
playing=true;
}
isBusy.unlock();
}
void DivEngine::noteOff(int chan) {
isBusy.lock();
pendingNotes.push(DivNoteEvent(chan,-1,-1,-1,false));
if (!playing) {
reset();
freelance=true;
playing=true;
}
isBusy.unlock();
}
void DivEngine::setOrder(unsigned char order) {
isBusy.lock();
curOrder=order;
if (order>=song.ordersLen) curOrder=0;
if (playing) {
if (playing && !freelance) {
playSub(false);
}
isBusy.unlock();

View File

@ -7,6 +7,7 @@
#include "blip_buf.h"
#include <mutex>
#include <map>
#include <queue>
#define DIV_VERSION "0.1"
#define DIV_ENGINE_VERSION 11
@ -67,6 +68,17 @@ struct DivChannelState {
inPorta(false) {}
};
struct DivNoteEvent {
int channel, ins, note, volume;
bool on;
DivNoteEvent(int c, int i, int n, int v, bool o):
channel(c),
ins(i),
note(n),
volume(v),
on(o) {}
};
class DivEngine {
DivDispatch* dispatch;
TAAudio* output;
@ -74,6 +86,7 @@ class DivEngine {
int chans;
bool active;
bool playing;
bool freelance;
bool speedAB;
bool endOfSong;
bool consoleMode;
@ -87,6 +100,7 @@ class DivEngine {
DivChannelState chan[17];
DivAudioEngines audioEngine;
std::map<String,String> conf;
std::queue<DivNoteEvent> pendingNotes;
bool isMuted[17];
std::mutex isBusy;
String configPath;
@ -302,6 +316,12 @@ class DivEngine {
// move order down
void moveOrderDown();
// play note
void noteOn(int chan, int ins, int note, int vol=-1);
// stop note
void noteOff(int chan);
// go to order
void setOrder(unsigned char order);
@ -347,6 +367,7 @@ class DivEngine {
chans(0),
active(false),
playing(false),
freelance(false),
speedAB(false),
endOfSong(false),
consoleMode(false),

View File

@ -705,6 +705,18 @@ bool DivEngine::nextTick(bool noAccum) {
cycles++;
}
while (!pendingNotes.empty()) {
DivNoteEvent& note=pendingNotes.front();
if (note.on) {
dispatchCmd(DivCommand(DIV_CMD_INSTRUMENT,note.channel,note.ins));
dispatchCmd(DivCommand(DIV_CMD_NOTE_ON,note.channel,note.note));
} else {
dispatchCmd(DivCommand(DIV_CMD_NOTE_OFF,note.channel));
}
pendingNotes.pop();
}
if (!freelance) {
if (--ticks<=0) {
ret=endOfSong;
if (endOfSong) {
@ -788,10 +800,12 @@ bool DivEngine::nextTick(bool noAccum) {
chan[i].arpYield=false;
}
}
}
// system tick
dispatch->tick();
if (!freelance) {
if (!noAccum) totalTicks++;
int hz;
@ -808,6 +822,7 @@ bool DivEngine::nextTick(bool noAccum) {
cmdsPerSecond=totalCmds-lastCmds;
lastCmds=totalCmds;
}
}
return ret;
}

View File

@ -124,7 +124,7 @@ struct DivSong {
speed1(6),
speed2(6),
arpLen(1),
pal(false),
pal(true),
customTempo(false),
hz(60),
patLen(64),

View File

@ -270,6 +270,8 @@ void FurnaceGUI::drawEditControls() {
if (ImGui::Button(ICON_FA_STOP "##Stop")) {
e->stop();
}
ImGui::SameLine();
ImGui::Checkbox("Edit",&edit);
ImGui::Text("Follow");
ImGui::SameLine();
@ -2083,6 +2085,9 @@ void FurnaceGUI::keyDown(SDL_Event& ev) {
} else if (ev.key.keysym.mod&KMOD_ALT) {
// nothing. prevents accidental OFF note.
} else switch (ev.key.keysym.sym) {
case SDLK_SPACE:
edit=!edit;
break;
case SDLK_UP:
moveCursor(0,-1);
break;
@ -2111,10 +2116,13 @@ void FurnaceGUI::keyDown(SDL_Event& ev) {
doInsert();
break;
default:
if (!ev.key.repeat) {
if (cursor.xFine==0) { // note
try {
int key=noteKeys.at(ev.key.keysym.sym);
int num=12*curOctave+key;
if (edit) {
DivPattern* pat=e->song.pat[cursor.xCoarse].getPattern(e->song.orders.ord[cursor.xCoarse][e->getOrder()],true);
prepareUndo(GUI_ACTION_PATTERN_EDIT);
@ -2130,13 +2138,23 @@ void FurnaceGUI::keyDown(SDL_Event& ev) {
pat->data[cursor.y][1]--;
}
pat->data[cursor.y][2]=curIns;
e->noteOn(cursor.xCoarse,curIns,num);
noteOffOnRelease=true;
noteOffOnReleaseKey=ev.key.keysym.sym;
noteOffOnReleaseChan=cursor.xCoarse;
}
makeUndo(GUI_ACTION_PATTERN_EDIT);
editAdvance();
curNibble=false;
} else {
e->noteOn(cursor.xCoarse,curIns,num);
noteOffOnRelease=true;
noteOffOnReleaseKey=ev.key.keysym.sym;
noteOffOnReleaseChan=cursor.xCoarse;
}
} catch (std::out_of_range& e) {
}
} else { // value
} else if (edit) { // value
try {
int num=valueKeys.at(ev.key.keysym.sym);
DivPattern* pat=e->song.pat[cursor.xCoarse].getPattern(e->song.orders.ord[cursor.xCoarse][e->getOrder()],true);
@ -2173,6 +2191,7 @@ void FurnaceGUI::keyDown(SDL_Event& ev) {
} catch (std::out_of_range& e) {
}
}
}
break;
}
break;
@ -2183,7 +2202,12 @@ void FurnaceGUI::keyDown(SDL_Event& ev) {
}
void FurnaceGUI::keyUp(SDL_Event& ev) {
if (noteOffOnRelease) {
if (ev.key.keysym.sym==noteOffOnReleaseKey) {
noteOffOnRelease=false;
e->noteOff(noteOffOnReleaseChan);
}
}
}
void FurnaceGUI::openFileDialog(FurnaceGUIFileDialogs type) {
@ -2739,6 +2763,7 @@ FurnaceGUI::FurnaceGUI():
e(NULL),
quit(false),
willCommit(false),
edit(false),
curFileDialog(GUI_FILE_OPEN),
scrW(1280),
scrH(800),
@ -2776,6 +2801,9 @@ FurnaceGUI::FurnaceGUI():
followPattern(true),
changeAllOrders(false),
curWindow(GUI_WINDOW_NOTHING),
noteOffOnRelease(false),
noteOffOnReleaseKey(0),
noteOffOnReleaseChan(0),
arpMacroScroll(0),
macroDragStart(0,0),
macroDragAreaSize(0,0),

View File

@ -1,4 +1,5 @@
#include "../engine/engine.h"
#include "SDL_keycode.h"
#include "imgui.h"
#include "imgui_impl_sdl.h"
#include "imgui_impl_sdlrenderer.h"
@ -130,7 +131,7 @@ class FurnaceGUI {
String workingDir, fileName, clipboard, errorString, lastError;
bool quit, willCommit;
bool quit, willCommit, edit;
FurnaceGUIFileDialogs curFileDialog;
@ -160,6 +161,10 @@ class FurnaceGUI {
bool selecting, curNibble, extraChannelButtons, followOrders, followPattern, changeAllOrders;
FurnaceGUIWindows curWindow;
bool noteOffOnRelease;
SDL_Keycode noteOffOnReleaseKey;
int noteOffOnReleaseChan;
std::map<SDL_Keycode,int> noteKeys;
std::map<SDL_Keycode,int> valueKeys;