-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathChuff.cpp
303 lines (243 loc) · 10 KB
/
Chuff.cpp
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
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
#include "Chuff.h"
#include "Directives.h";
#ifdef _Audio
#include "AudioFileSourceSPIFFS.h"
#include "AudioGeneratorWAV.h"
#include "AudioOutputMixer.h"
#endif
#ifdef _AudioDAC
#include "AudioOutputI2SDAC.h"
#endif
#ifdef _AudioNoDAC
#include "AudioOutputI2SNoDAC.h"
#endif
#ifdef _Audio
AudioGeneratorWAV *wav[2];
AudioFileSourceSPIFFS *file[2];
AudioOutputMixer *mixer;
AudioOutputMixerStub *stub[2];
#endif
#ifdef _AudioDAC
AudioOutputI2SDAC *out;
#endif
#ifdef _AudioNoDAC
AudioOutputI2SNoDAC *out;
#endif
int ChuffCycle;
long LastChuff;
bool WavDoneMsgSent;
bool PlayingSoundEffect;
bool ChuffPlaying;
long SteamOnStarted;
long SteamPulseDuration =50;
long ChuffPeriod;
String LastFilePlayed="--";
String ChuffType; // ="/ch"; //String ChuffType ="/99_H1_"; //alternate chuff sound
uint8_t SoundEffect_Request[3];
extern void DebugSprintfMsgSend(int CX);
extern char DebugMsg[127];
extern uint8_t NodeMCUPinD[12];
extern uint8_t CV[256];
void SetChuffPeriod(long Setting){
ChuffPeriod=Setting;
}
void SetChuffPeriodFromSpeed(uint16_t value){
uint16_t SavedValue;
uint16_t ChuffPeriod;
// if we have a change to motor speed, change chuff period!:
SavedValue=value; // to uint 16t
ChuffPeriod=4000; // just to cover div by zero case its not covered later.
if (SavedValue<=0){ChuffPeriod=4000; }
else{
ChuffPeriod=2590/SavedValue;} /// Proper calcs below, but you will probably need to play with the value to fit your wheel etc.
if (ChuffPeriod<=100){ChuffPeriod=100;} //sets a minimum chuff
SetChuffPeriod(ChuffPeriod);
//typically 10mph=259ms quarter chuff rate at 10MPH
// e.g. We have 1609*10 m/hr = 16090/3600 m per second = 4.46 m per second
// wheel of 1.4m dia is 4.4 circumference, so we have ~ 1 rotation per second
// so with 4 chuffs per rev we get ~250ms "chuffperiod" at 10MPH
// DebugSprintfMsgSend( sprintf ( DebugMsg, "Chuff set at %d ms for speed %d mph", ChuffPeriod,value));
}
void SetSoundEffect(uint8_t Data1,uint8_t Data2,uint8_t Data3){
SoundEffect_Request[1]=Data1;
SoundEffect_Request[2]=Data2;
SoundEffect_Request[3]=Data3;
}
void SetUpAudio(uint32_t TimeNow){
#ifdef _Audio
Serial.printf("-- Sound System Initiating -- \n");
#ifdef _AudioDAC
out = new AudioOutputI2SDAC();
Serial.printf("-- Using I2S DAC -- \n");
#endif
#ifdef _AudioNoDAC
out = new AudioOutputI2SNoDAC(); Serial.printf("-- Using I2S No DAC -- \n");
#endif
mixer = new AudioOutputMixer(128, out);
delay(100);
SteamPulseDuration=5; //5 for use as strobe
SteamOnStarted=TimeNow;
WavDoneMsgSent=false;
ChuffCycle=0;
LastChuff=TimeNow+4000;
PlayingSoundEffect=false;
ChuffPlaying=false;
ChuffPeriod=4000;
// delay(2000); // allow time for setups..?
LastFilePlayed="--";
SoundEffect_Request[1]=0;
SoundEffect_Request[2]=0;
SoundEffect_Request[3]=0;
/*//Temporary override sound volumes
CV[100]=127; // Overall volume control
CV[101]=127; //volume for F1
CV[102]=127;
CV[103]=127;
CV[104]=127;
CV[105]=127;
CV[106]=127;
CV[107]=127;
CV[108]=127; // Volume for F8
CV[110]=127; // volume for chuffs
CV[111]=127; // volume for Brake Squeal
//
*/
bitWrite(SoundEffect_Request[2],0,1); //set F9 is chuffs on initially
BeginPlayND(0,"/initiated.wav",64);// this wav file will play before anything else, but does not do the file, stub,wav deletes.
BeginPlayND(1,"/initiated.wav",64);// this wav file will play before anything else.
Serial.printf("-- Sound System Setup Completed -- \n");
#endif //_Audio
}
void BeginPlay(int Channel,const char *wavfilename, uint8_t CVVolume){
#ifdef _Audio
float Volume;
Volume=(float)CVVolume*CV[100]/16384;
delete stub[Channel];
stub[Channel] = mixer->NewInput();
stub[Channel]->SetGain(Volume);
// if (wav[Channel]->isRunning()) {wav[Channel]->stop();
//Serial.printf("*audio previous wav stopped\n");
// }
delete file[Channel]; // housekeeping ?
file[Channel]= new AudioFileSourceSPIFFS(wavfilename);
delete wav[Channel];
wav[Channel] = new AudioGeneratorWAV();
wav[Channel]->begin(file[Channel], stub[Channel]);
if (Channel==1){PlayingSoundEffect=true; // if ch1 works ch 0 will, but we d not need lots of messages for chuffs
Serial.printf(" CH:");
Serial.print(Channel);
Serial.printf(" Playing <");
Serial.print(wavfilename);
Serial.printf("> at volume :");
Serial.println(Volume);}
#endif
}
void BeginPlayND(int Channel,const char *wavfilename, uint8_t CVVolume){ // no deletes version
#ifdef _Audio
uint32_t NOW;
NOW=micros();
float Volume;
Volume=(float)CVVolume*CV[100]/16384;
//delete stub[Channel];
stub[Channel] = mixer->NewInput();
stub[Channel]->SetGain(Volume);
// if (wav[Channel]->isRunning()) {wav[Channel]->stop();
//Serial.printf("*audio previous wav stopped\n");
// }
//delete file[Channel]; // housekeeping ?
file[Channel]= new AudioFileSourceSPIFFS(wavfilename);
//delete wav[Channel];
wav[Channel] = new AudioGeneratorWAV();
wav[Channel]->begin(file[Channel], stub[Channel]);
PlayingSoundEffect=true;
Serial.printf(" Setting up first play CH:");
Serial.print(Channel);
Serial.printf(" Playing <");
Serial.print(wavfilename);
Serial.printf("> at volume :");
Serial.println(Volume);
#endif
}
bool TimeToChuff(uint32_t TimeNow){
if (ChuffPeriod>=3500){return false;} // switches off chuff at very low or stopped (4000ms )speeds
if (TimeNow<=(LastChuff+ChuffPeriod)){return false;}
else {LastChuff=TimeNow; return true;}
}
extern bool POWERON;
void Chuff (String ChuffChoice){
String Chuff;
// if (ChuffPeriod>=60){ChuffChoice="/ch";} // test for switching sound effects with speed this works, but my fast sound clips need work
// else{ChuffChoice="/CHF_";}
ChuffType=ChuffChoice;
// if (wav[0]>isRunning()) {wav[0]->stop(); delay(1);
// #ifdef _AudioDebug
// // Serial.print-wav -Truncated\n");
// #endif
// }// truncate play
LastChuff=millis();
#ifdef _LOCO_SERVO_Driven_Port
#ifdef SteamOutputPin //steamoutputpin stuff here for one puff per chuff
SteamOnStarted=millis(); digitalWrite (NodeMCUPinD[SteamOutputPin],HIGH); //steamoutputpin stuff here for one puff per chuff
#endif
if((bitRead(SoundEffect_Request[2],0)==1)&& (POWERON)){ //F9 is chuffs on
switch (ChuffCycle){
case 0:Chuff=ChuffType+"1.wav";BeginPlay(0,Chuff.c_str(),CV[110]);ChuffCycle=1;
//Stuff here only for strobe use, one per rev to help set chuff rate
//SteamOnStarted=millis(); digitalWrite (NodeMCUPinD[SteamOutputPin],HIGH);
break;
case 1:Chuff=ChuffType+"2.wav";BeginPlay(0,Chuff.c_str(),CV[110]);ChuffCycle=2;break;
case 2:Chuff=ChuffType+"3.wav";BeginPlay(0,Chuff.c_str(),CV[110]);ChuffCycle=3;break;
case 3:Chuff=ChuffType+"4.wav";BeginPlay(0,Chuff.c_str(),CV[110]);ChuffCycle=0;break;
}}
#endif
}
void AudioLoop(int32_t TimeNow){
#ifdef _Audio
#ifdef SteamOutputPin
if ((SteamOnStarted+SteamPulseDuration)<=TimeNow){digitalWrite (NodeMCUPinD[SteamOutputPin],LOW);}
#endif
if (wav[0]->isRunning()) {
if (!wav[0]->loop()) { Serial.printf("S0\n");wav[0]->stop(); stub[0]->stop();}// Running[0]=false;Serial.printf("stopping 0\n"); }
}
if (wav[1]->isRunning()) {
if (!wav[1]->loop()) { Serial.printf("S1\n");wav[1]->stop(); stub[1]->stop();PlayingSoundEffect=false;}// Running[1]=false;Serial.printf("stopping 1\n");}
}
#endif
}
bool SoundEffectPlaying(void){
return PlayingSoundEffect;
}
void SoundEffects(void) {
if(bitRead(SoundEffect_Request[1],0)==1){
if (!PlayingSoundEffect){BeginPlay(1,"/F1.wav",CV[101]);
DebugSprintfMsgSend( sprintf ( DebugMsg, "sfx-F1"));}
}
if(bitRead(SoundEffect_Request[1],1)==1){
if (!PlayingSoundEffect){BeginPlay(1,"/F2.wav",CV[102]);
DebugSprintfMsgSend( sprintf ( DebugMsg, "sfx-F2"));}
}
if(bitRead(SoundEffect_Request[1],2)==1){
if (!PlayingSoundEffect){BeginPlay(1,"/F3.wav",CV[103]);
DebugSprintfMsgSend( sprintf ( DebugMsg, "sfx-F3"));}
}
if(bitRead(SoundEffect_Request[1],3)==1){
if (!PlayingSoundEffect){BeginPlay(1,"/F4.wav",CV[104]);
DebugSprintfMsgSend( sprintf ( DebugMsg, "sfx-F4"));}
}
if(bitRead(SoundEffect_Request[1],4)==1){
if (!PlayingSoundEffect){BeginPlay(1,"/F5.wav",CV[105]);
DebugSprintfMsgSend( sprintf ( DebugMsg, "sfx-F5"));}
}
if(bitRead(SoundEffect_Request[1],5)==1){
if (!PlayingSoundEffect){BeginPlay(1,"/F6.wav",CV[106]);
DebugSprintfMsgSend( sprintf ( DebugMsg, "sfx-F6"));}
}
if(bitRead(SoundEffect_Request[1],6)==1){
if (!PlayingSoundEffect){BeginPlay(1,"/F7.wav",CV[107]);
DebugSprintfMsgSend( sprintf ( DebugMsg, "sfx-F7"));}
}
if(bitRead(SoundEffect_Request[1],7)==1){ // this is "F8" on DCC sounds
if (!PlayingSoundEffect){BeginPlay(1,"/F8.wav",CV[108]);
DebugSprintfMsgSend( sprintf ( DebugMsg, "sfx-F8"));}
}
}