| 1 |
/** |
| 2 |
* Furnace Tracker - multi-system chiptune tracker |
| 3 |
* Copyright (C) 2021-2025 tildearrow and contributors |
| 4 |
* |
| 5 |
* This program is free software; you can redistribute it and/or modify |
| 6 |
* it under the terms of the GNU General Public License as published by |
| 7 |
* the Free Software Foundation; either version 2 of the License, or |
| 8 |
* (at your option) any later version. |
| 9 |
* |
| 10 |
* This program is distributed in the hope that it will be useful, |
| 11 |
* but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 12 |
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 13 |
* GNU General Public License for more details. |
| 14 |
* |
| 15 |
* You should have received a copy of the GNU General Public License along |
| 16 |
* with this program; if not, write to the Free Software Foundation, Inc., |
| 17 |
* 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA. |
| 18 |
*/ |
| 19 |
|
| 20 |
// thanks asiekierka! (I used your SAP-R export code as a base for this) |
| 21 |
|
| 22 |
#include "ipod.h" |
| 23 |
#include "../engine.h" |
| 24 |
#include "../ta-log.h" |
| 25 |
#include <fmt/printf.h> |
| 26 |
#include <array> |
| 27 |
#include <vector> |
| 28 |
|
| 29 |
void DivExportiPod::run() { |
| 30 |
int BEEPER=-1; |
| 31 |
int IGNORED=0; |
| 32 |
|
| 33 |
// Locate system index. |
| 34 |
for (int i=0; i<e->song.systemLen; i++) { |
| 35 |
if (e->song.system[i] == DIV_SYSTEM_PCSPKR) { |
| 36 |
if (BEEPER>=0) { |
| 37 |
IGNORED++; |
| 38 |
logAppendf("Ignoring duplicate Beeper id %d",i); |
| 39 |
break; |
| 40 |
} |
| 41 |
BEEPER=i; |
| 42 |
logAppendf("PC Speaker detected as chip id %d",i); |
| 43 |
break; |
| 44 |
} else { |
| 45 |
IGNORED++; |
| 46 |
logAppendf("Ignoring chip id %d, system id %d",i,(int)e->song.system[i]); |
| 47 |
break; |
| 48 |
} |
| 49 |
} |
| 50 |
if (BEEPER<0) { |
| 51 |
logAppendf("ERROR: Could not find PC Speaker/Beeper"); |
| 52 |
failed=true; |
| 53 |
running=false; |
| 54 |
return; |
| 55 |
} |
| 56 |
if (IGNORED>0) { |
| 57 |
logAppendf("WARNING: tone export ignoring unsup sys count: %d",IGNORED); |
| 58 |
} |
| 59 |
|
| 60 |
size_t tickCount=0; |
| 61 |
|
| 62 |
double rate = 1000.0; |
| 63 |
|
| 64 |
e->stop(); |
| 65 |
e->repeatPattern=false; |
| 66 |
e->setOrder(0); |
| 67 |
|
| 68 |
logAppend("playing and logging register writes..."); |
| 69 |
|
| 70 |
int oldFreq = 0; |
| 71 |
int freq = 0; |
| 72 |
|
| 73 |
e->synchronizedSoft([&]() { |
| 74 |
double origRate = e->got.rate; |
| 75 |
e->got.rate=rate; |
| 76 |
|
| 77 |
// Determine loop point. |
| 78 |
int loopOrder=0; |
| 79 |
int loopRow=0; |
| 80 |
int loopEnd=0; |
| 81 |
e->walkSong(loopOrder,loopRow,loopEnd); |
| 82 |
logAppendf("loop point: %d %d",loopOrder,loopRow); |
| 83 |
e->warnings=""; |
| 84 |
|
| 85 |
auto w = new SafeWriter; |
| 86 |
w->init(); |
| 87 |
|
| 88 |
w->writeText(fmt::sprintf("%s\n", e->song.name)); |
| 89 |
|
| 90 |
// Reset the playback state. |
| 91 |
e->curOrder=0; |
| 92 |
e->freelance=false; |
| 93 |
e->playing=false; |
| 94 |
e->extValuePresent=false; |
| 95 |
e->remainingLoops=-1; |
| 96 |
|
| 97 |
e->disCont[BEEPER].dispatch->toggleRegisterDump(true); |
| 98 |
|
| 99 |
// Prepare to write song data. |
| 100 |
e->playSub(false); |
| 101 |
bool done=false; |
| 102 |
|
| 103 |
logAppend("writing data..."); |
| 104 |
progress[0].amount=0.15f; |
| 105 |
|
| 106 |
int wait_ms = 0; |
| 107 |
|
| 108 |
while (!done) { |
| 109 |
if (e->nextTick(false,true) || !e->playing) { |
| 110 |
done=true; |
| 111 |
} |
| 112 |
|
| 113 |
// get register dumps |
| 114 |
uint8_t* regPool = e->disCont[BEEPER].dispatch->getRegisterPool(); |
| 115 |
int chipClock = e->disCont[BEEPER].dispatch->chipClock; |
| 116 |
freq = (int)(regPool[0]|(regPool[1]<<8)); |
| 117 |
if (freq > 0) freq = chipClock/freq; |
| 118 |
|
| 119 |
// write wait |
| 120 |
tickCount++; |
| 121 |
int totalWait=e->cycles; |
| 122 |
if (totalWait>0 && !done) { |
| 123 |
while (totalWait) { |
| 124 |
wait_ms++; |
| 125 |
if (freq != oldFreq) { |
| 126 |
w->writeText(fmt::sprintf("%d %d\n", oldFreq, wait_ms)); |
| 127 |
oldFreq = freq; |
| 128 |
wait_ms = 0; |
| 129 |
} |
| 130 |
totalWait--; |
| 131 |
tickCount++; |
| 132 |
} |
| 133 |
} |
| 134 |
} |
| 135 |
// end of song |
| 136 |
|
| 137 |
// done - close out. |
| 138 |
e->got.rate=origRate; |
| 139 |
e->disCont[BEEPER].dispatch->getRegisterWrites().clear(); |
| 140 |
e->disCont[BEEPER].dispatch->toggleRegisterDump(false); |
| 141 |
|
| 142 |
e->remainingLoops=-1; |
| 143 |
e->playing=false; |
| 144 |
e->freelance=false; |
| 145 |
e->extValuePresent=false; |
| 146 |
|
| 147 |
output.push_back(DivROMExportOutput("export.tone",w)); |
| 148 |
}); |
| 149 |
|
| 150 |
|
| 151 |
progress[0].amount=1.0f; |
| 152 |
|
| 153 |
logAppend("finished!"); |
| 154 |
|
| 155 |
running=false; |
| 156 |
} |
| 157 |
|
| 158 |
bool DivExportiPod::go(DivEngine* eng) { |
| 159 |
progress[0].name="Progress"; |
| 160 |
progress[0].amount=0.0f; |
| 161 |
|
| 162 |
e=eng; |
| 163 |
running=true; |
| 164 |
failed=false; |
| 165 |
mustAbort=false; |
| 166 |
exportThread=new std::thread(&DivExportiPod::run,this); |
| 167 |
return true; |
| 168 |
} |
| 169 |
|
| 170 |
void DivExportiPod::wait() { |
| 171 |
if (exportThread!=NULL) { |
| 172 |
logV("waiting for export thread..."); |
| 173 |
exportThread->join(); |
| 174 |
delete exportThread; |
| 175 |
} |
| 176 |
} |
| 177 |
|
| 178 |
void DivExportiPod::abort() { |
| 179 |
mustAbort=true; |
| 180 |
wait(); |
| 181 |
} |
| 182 |
|
| 183 |
bool DivExportiPod::isRunning() { |
| 184 |
return running; |
| 185 |
} |
| 186 |
|
| 187 |
bool DivExportiPod::hasFailed() { |
| 188 |
return failed; |
| 189 |
} |
| 190 |
|
| 191 |
DivROMExportProgress DivExportiPod::getProgress(int index) { |
| 192 |
if (index<0 || index>1) return progress[1]; |
| 193 |
return progress[index]; |
| 194 |
} |