-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmod.cpp
More file actions
132 lines (117 loc) · 2.79 KB
/
mod.cpp
File metadata and controls
132 lines (117 loc) · 2.79 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
#include "mod.h"
#include "pwm.h"
#include <FatFS.h>
extern "C" {
#include "micromod.h"
}
extern struct Config {
String fmFreq;
String fmFreqList;
String fmOffset;
int cwWPM;
String cwFreq;
int cwVolume;
int modVolume;
String cwMessage;
String cwMessage1;
String cwMessage2;
String cwMessage3;
String idCallsign;
String payload;
int ardfSlot;
int ardfCycleMin;
int idIntervalMin;
int freqCycleSec;
String runWindow;
String currentTime;
float batLowVolts;
float batCutoffVolts;
bool sleepBetweenSlots;
} config;
#define MOD_SAMPLE_RATE 22050
#define MOD_BLOCK 128
void playMOD(const char *path) {
File f = FatFS.open(path, "r");
if (!f) {
Serial.print("MOD not found: ");
Serial.println(path);
return;
}
size_t fsize = f.size();
if (fsize < 1084) {
Serial.println("MOD too small");
f.close();
return;
}
signed char header[1084];
if (f.read((uint8_t *)header, 1084) != 1084) {
Serial.println("MOD header read failed");
f.close();
return;
}
long modLen = micromod_calculate_mod_file_len(header);
if (modLen <= 0) {
Serial.println("Not a recognised MOD");
f.close();
return;
}
if ((size_t)modLen > fsize)
modLen = fsize;
signed char *buf = (signed char *)malloc(modLen);
if (!buf) {
Serial.print("MOD: out of memory (needs ");
Serial.print(modLen);
Serial.println(" bytes)");
f.close();
return;
}
memcpy(buf, header, 1084);
if (modLen > 1084) {
f.seek(1084);
f.read((uint8_t *)buf + 1084, modLen - 1084);
}
f.close();
if (micromod_initialise(buf, MOD_SAMPLE_RATE) < 0) {
Serial.println("MOD init failed");
free(buf);
return;
}
micromod_set_gain(config.modVolume > 0 ? config.modVolume : 32);
char songName[24];
micromod_get_string(0, songName);
Serial.print("MOD: ");
Serial.print(songName);
Serial.print(" (");
Serial.print(modLen);
Serial.println(" bytes)");
long totalSamples = micromod_calculate_song_duration();
audioBusy = true;
pwm.setPin(15);
pwm.begin(MOD_SAMPLE_RATE);
short stereoBuf[MOD_BLOCK * 2];
long played = 0;
while (played < totalSamples) {
long toRender = MOD_BLOCK;
if (played + toRender > totalSamples)
toRender = totalSamples - played;
memset(stereoBuf, 0, (size_t)toRender * 2 * sizeof(short));
micromod_get_audio(stereoBuf, toRender);
for (long i = 0; i < toRender; i++) {
int mono = (int)stereoBuf[i * 2] + (int)stereoBuf[i * 2 + 1];
if (mono > 32767)
mono = 32767;
else if (mono < -32768)
mono = -32768;
while (pwm.availableForWrite() == 0)
yield();
pwm.write((int16_t)mono);
}
played += toRender;
}
while (pwm.availableForWrite() < 16)
yield();
pwm.end();
audioIdle();
audioBusy = false;
free(buf);
}