-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathBASSGinsu.hpp
3270 lines (2678 loc) · 90.4 KB
/
BASSGinsu.hpp
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
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
//
// BASS Ginsu Synthesizer Library
// by Tenjoin / xan1242
//
// Requirements:
// - BASS
// - BASSmix
// - vgmstream
//
// Usage:
// 1. Initialize the player (BASSGinsuPlayer) (creation args are the same as BASS_Init)
// 2. Create a stream with BASSGinsuPlayer::CreateStream for each car. Use a Ginsu file directly.
// 3. Get the signed 16-bit PCM data with BASSGinsu(Multi)Stream::GetData and pipe it to the game sound engine
// 4. Control the streams for each car with BASSGinsu(Multi)Stream's public functions (e.g. to set the RPM use SetFrequency)
// 5. Once done, destroy the BASSGinsuPlayer (or - for finer grain control, release each stream with BASSGinsuPlayer::ReleaseStream for each released car)
//
//
// TODO list:
// - Implement exceptions and error handling!
// - Implement reading of a Ginsu from memory!
// - Tri-crossfading
// - Channel generation on the fly (instead of keeping them all active at once)
// - Redline & idle re-pitch -- done? Maybe this was referring to re-pitching based on the RPM...
// - LPF based on throttle -- maybe implement BASSfx for this? or use the dx8 effect? -- done? not quite LPF but using a param EQ
// - Decel amount based on throttle -- basically, the xfade will depend on how hard the throttle is pushed -- done by tracking the rate?
// - Maybe some control for clutch release and LPF for it too
// - DOCUMENTATION IN GENERAL!
//
// - ...and other TODOs scattered around the code...
//
#pragma once
#ifndef MOD_BASSGINSU_H
#define MOD_BASSGINSU_H
#define NOMINMAX
#include <iostream>
#include <filesystem>
#include <fstream>
#include <vector>
#include <algorithm>
#include <chrono>
#include "FrameLimiter.hpp"
#include <bass.h>
#include <bassmix.h>
#ifdef __cplusplus
extern "C"
{
#endif
#include <vgmstream.h>
#ifdef __cplusplus
}
#endif
#ifdef _MSC_VER
#pragma comment(lib, "bass.lib")
#pragma comment(lib, "bassmix.lib")
#pragma comment(lib, "libvgmstream.lib")
#endif
struct GinsuDataLayout
{
uint8_t id[4];
uint8_t ver[2];
uint16_t flags;
float minFrequency;
float maxFrequency;
uint32_t segCount;
uint32_t cycleCount;
uint32_t sampleCount;
uint32_t sampleRate;
};
enum GIN_DIRECTION
{
GIN_BACKWARDS = -1,
GIN_NOWHERE,
GIN_FORWARDS,
GIN_NUM_DIRECTIONS
};
float cus_lerp(float a, float b, float t)
{
return a + t * (b - a);
}
class BASSGinsuStream
{
private:
// Ginsu stuff
GinsuDataLayout GinsuHead;
bool bDecelGin;
uint32_t* freqSamples;
uint32_t* cycleSamples;
float* cycleFreqs;
bool bLoaded;
// BASS stuff
HSAMPLE* hsv;
DWORD* chans;
HSTREAM gStream;
bool bFloatBuffer;
// Playback stuff
DWORD chan;
DWORD channext;
int oldcyc;
int prevcyc;
GIN_DIRECTION GinDir;
float freqCurrent;
float freqDelta;
float SampleToFrequency(uint32_t sample)
{
const int segCount = GinsuHead.segCount;
const int minFreq = GinsuHead.minFrequency;
const int maxFreq = GinsuHead.maxFrequency;
// Find the closest index in freqSamples
float sampleValue = static_cast<float>(sample);
float closestSampleValue = 0.0;
int closestIndex = 0;
float minDifference = std::abs(freqSamples[0] - sampleValue);
for (int i = 1; i < segCount; ++i)
{
float difference = std::abs(freqSamples[i] - sampleValue);
if (difference < minDifference)
{
minDifference = difference;
closestSampleValue = freqSamples[i];
closestIndex = i;
}
}
// Calculate the frequency based on the closest interpolated value
float startP = static_cast<float>(closestIndex) + (sampleValue - closestSampleValue) / (freqSamples[closestIndex + 1] - freqSamples[closestIndex]);
float freq = minFreq + (maxFreq - minFreq) * (startP / segCount);
return freq;
}
uint32_t FrequencyToSample(float freq)
{
float percentage = (freq - GinsuHead.minFrequency) / (GinsuHead.maxFrequency - GinsuHead.minFrequency);
if (percentage < 0.0)
percentage = 0.0;
if (percentage > 1.0)
percentage = 1.0;
// Calculate the number of points in the array
size_t numPoints = GinsuHead.segCount;
// Calculate the index of the lower and upper points for interpolation
float index = percentage * (numPoints);
size_t lowerIndex = static_cast<size_t>(index);
size_t upperIndex = (std::min)(lowerIndex + 1, numPoints);
// Calculate the interpolation factor
float interpolationFactor = index - lowerIndex;
float interpolatedValue = cus_lerp(freqSamples[lowerIndex], freqSamples[upperIndex], interpolationFactor);
return interpolatedValue;
}
uint32_t SampleToCycle(uint32_t sample, int oldCycle, bool bIsReverse)
{
if (bDecelGin)
bIsReverse = !bIsReverse;
// check for single-step first before going into a loop pass
if (oldCycle > 0)
{
if (bIsReverse)
{
if (sample >= cycleSamples[oldCycle])
return oldCycle;
if (sample >= cycleSamples[oldCycle - 1])
{
return oldCycle - 1;
}
}
else if (oldCycle <= (GinsuHead.cycleCount - 1))
{
//if (sample >= cycleSamples[oldCycle])
// return oldCycle;
if (sample >= cycleSamples[oldCycle + 1])
return oldCycle + 1;
}
}
if (sample >= cycleSamples[GinsuHead.cycleCount - 1])
return GinsuHead.cycleCount - 1;
for (int i = 0; i < GinsuHead.cycleCount; i++)
{
if (sample >= cycleSamples[GinsuHead.cycleCount - i - 1])
return GinsuHead.cycleCount - i - 1;
}
return 0;
}
void SetFreqOnChannels(DWORD channel1, DWORD channel2, float inFreq, int cyc, bool bIsFalling)
{
GIN_DIRECTION loc_dir = GinDir;
if (cyc == 0)
loc_dir = GIN_DIRECTION::GIN_FORWARDS;
else if (cyc >= (GinsuHead.cycleCount - 2))
loc_dir = GIN_DIRECTION::GIN_BACKWARDS;
float chanfreq = cycleFreqs[cyc];
float channextfreq = cycleFreqs[cyc + (1 * loc_dir)];
if (bIsFalling)
{
float t = chanfreq;
chanfreq = channextfreq;
channextfreq = t;
}
float freqscalar = inFreq / chanfreq;
float len1 = cycleSamples[cyc + (1 * loc_dir)] - cycleSamples[cyc];
float len22 = cycleSamples[cyc + (2 * loc_dir)] - cycleSamples[cyc + (1 * loc_dir)];
if (bIsFalling)
{
double t = len1;
len1 = len22;
len22 = t;
}
float diff_fac = len22 / len1;
float rate1 = (float)GinsuHead.sampleRate * freqscalar;
float rate2 = (float)GinsuHead.sampleRate * diff_fac * freqscalar;
BASS_ChannelSetAttribute(channel1, BASS_ATTRIB_FREQ, rate1);
BASS_ChannelSetAttribute(channel2, BASS_ATTRIB_FREQ, rate2);
}
void SetVolOnChannels(DWORD channel1, DWORD channel2, uint32_t samp, int cyc, bool bIsFalling)
{
GIN_DIRECTION loc_dir = GinDir;
if (cyc == 0)
loc_dir = GIN_DIRECTION::GIN_FORWARDS;
else if (cyc >= (GinsuHead.cycleCount - 2))
loc_dir = GIN_DIRECTION::GIN_BACKWARDS;
uint32_t ls = cycleSamples[cyc];
uint32_t le = cycleSamples[cyc + (1 * loc_dir)];
if (bIsFalling)
{
ls = cycleSamples[cyc + (1 * loc_dir)];
le = cycleSamples[cyc];
}
double chan1vol = 1.0f - (((double)samp - (double)ls) / ((double)le - (double)ls));
double chan2vol = 1.0f - chan1vol;
chan1vol = std::clamp(chan1vol, 0.0, 1.0);
chan2vol = std::clamp(chan2vol, 0.0, 1.0);
BASS_ChannelSetAttribute(channel1, BASS_ATTRIB_VOL, chan1vol);
BASS_ChannelSetAttribute(channel2, BASS_ATTRIB_VOL, chan2vol);
}
void SetPlaybackFrequency(float inFreq)
{
uint32_t samp = FrequencyToSample(inFreq);
int cycint = SampleToCycle(samp, oldcyc, freqDelta < 0);
GIN_DIRECTION loc_dir = GinDir;
if (cycint == 0)
loc_dir = GIN_DIRECTION::GIN_FORWARDS;
else if (cycint > (GinsuHead.cycleCount - 2))
loc_dir = GIN_DIRECTION::GIN_BACKWARDS;
// switch channels on new cycle
if (oldcyc != cycint)
{
if (oldcyc < 0)
{
oldcyc = 0;
}
BASS_ChannelSetAttribute(chan, BASS_ATTRIB_VOL, 0.0f);
BASS_ChannelSetAttribute(channext, BASS_ATTRIB_VOL, 0.0f);
if (cycint < oldcyc)
{
chan = chans[cycint + (1 * loc_dir)];
channext = chans[cycint];
}
else
{
chan = chans[cycint];
channext = chans[cycint + (1 * loc_dir)];
}
BASS_ChannelSetAttribute(chan, BASS_ATTRIB_VOL, 1.0f);
BASS_ChannelSetAttribute(channext, BASS_ATTRIB_VOL, 0.0f);
QWORD ls = cycleSamples[cycint] * 2;
QWORD le = cycleSamples[cycint + (1 * loc_dir)] * 2;
QWORD ls2 = cycleSamples[cycint + (1 * loc_dir)] * 2;
QWORD le2 = cycleSamples[cycint + (2 * loc_dir)] * 2;
if (cycint < oldcyc)
{
QWORD t = ls;
ls = ls2;
ls2 = t;
t = le;
le = le2;
le2 = t;
}
QWORD len = (le - ls);
QWORD len2 = (le2 - ls2);
QWORD basspos = BASS_ChannelGetPosition(chan, BASS_POS_BYTE);
float dist = (float)basspos / (float)len;
float newpos = cus_lerp(0, len2, dist);
BASS_ChannelSetPosition(channext, (QWORD)(newpos), BASS_POS_BYTE);
prevcyc = oldcyc;
oldcyc = cycint;
}
SetFreqOnChannels(chan, channext, inFreq, cycint, cycint < prevcyc);
SetVolOnChannels(chan, channext, samp, cycint, cycint < prevcyc);
if (freqCurrent != inFreq)
{
freqDelta = inFreq - freqCurrent;
freqCurrent = inFreq;
}
}
// parsing/loading code
bool ParseGinsu(std::filesystem::path filename)
{
std::ifstream ifile;
ifile.open(filename, std::ios::binary);
if (!ifile.is_open())
return false;
ifile.read((char*)&GinsuHead, sizeof(GinsuDataLayout));
size_t sz = (GinsuHead.segCount + 1) * sizeof(uint32_t);
freqSamples = (uint32_t*)malloc(sz);
if (freqSamples == nullptr)
return false;
memset(freqSamples, 0, sz);
for (int i = 0; i < GinsuHead.segCount + 1; i++)
{
uint32_t samp = 0;
ifile.read((char*)&samp, sizeof(uint32_t));
freqSamples[i] = samp;
}
// detect if it's a decel gin
int decgintester = freqSamples[1] - freqSamples[0];
bDecelGin = decgintester < 0;
sz = (GinsuHead.cycleCount + 1) * sizeof(uint32_t);
cycleSamples = (uint32_t*)malloc(sz);
if (cycleSamples == nullptr)
return false;
memset(cycleSamples, 0, sz);
for (int i = 0; i < GinsuHead.cycleCount; i++)
{
uint32_t cyc = 0;
ifile.read((char*)&cyc, sizeof(uint32_t));
cycleSamples[i] = cyc;
}
cycleSamples[GinsuHead.cycleCount] = UINT32_MAX;
ifile.close();
sz = (GinsuHead.cycleCount + 1) * sizeof(float);
cycleFreqs = (float*)malloc(sz);
if (cycleFreqs == nullptr)
return false;
memset(cycleFreqs, 0, sz);
for (int i = 0; i < GinsuHead.cycleCount; i++)
{
float f = SampleToFrequency(cycleSamples[i]);
cycleFreqs[i] = f;
}
cycleFreqs[GinsuHead.cycleCount] = cycleFreqs[GinsuHead.cycleCount - 1];
return true;
}
bool decodeAudioStream(const char* inputFile, int outputFrequency, int16_t** outputBuffer, size_t* bufferSize) {
VGMSTREAM* vgmstream = init_vgmstream(inputFile);
if (!vgmstream) {
return false; // Failed to initialize vgmstream
}
size_t totalSamples = (size_t)(vgmstream->num_samples);
size_t totalChannels = (size_t)(vgmstream->channels);
*bufferSize = totalSamples * totalChannels * sizeof(int16_t);
*outputBuffer = (int16_t*)malloc(*bufferSize);
int samples_to_do = (int)totalSamples;
int16_t* out_buffer = *outputBuffer;
// Decode the audio stream
while (samples_to_do > 0) {
int samples_done = render_vgmstream(out_buffer, samples_to_do, vgmstream);
if (samples_done <= 0) {
break; // Decoding finished or error occurred
}
out_buffer += samples_done * totalChannels;
samples_to_do -= samples_done;
}
close_vgmstream(vgmstream);
return true;
}
bool CreateChannels(std::filesystem::path filename)
{
int16_t* outputBuffer = nullptr;
size_t bufferSize = 0;
if (!decodeAudioStream(filename.string().c_str(), GinsuHead.sampleRate, &outputBuffer, &bufferSize))
return false;
hsv = (HSAMPLE*)malloc(GinsuHead.cycleCount * sizeof(HSAMPLE));
if (hsv == NULL)
return false;
chans = (DWORD*)malloc(GinsuHead.cycleCount * sizeof(DWORD));
if (chans == NULL)
return false;
for (int i = 0; i < GinsuHead.cycleCount; i++)
{
int16_t* cycSample = &outputBuffer[cycleSamples[i]];
DWORD len = 0;
if (i >= (GinsuHead.cycleCount - 1))
{
uintptr_t ep = (uintptr_t)(outputBuffer)+bufferSize;
len = ep - (uintptr_t)(&outputBuffer[cycleSamples[i]]);
}
else
len = (uintptr_t)(&outputBuffer[cycleSamples[i + 1]]) - (uintptr_t)(&outputBuffer[cycleSamples[i]]);
HSAMPLE ns = BASS_SampleCreate(len, GinsuHead.sampleRate, 1, 1, BASS_SAMPLE_MONO | BASS_SAMPLE_LOOP);
if (ns == 0)
return false;
if (BASS_SampleSetData(ns, cycSample) == FALSE)
return false;
hsv[i] = ns;
DWORD ch = BASS_SampleGetChannel(ns, BASS_SAMCHAN_STREAM | BASS_STREAM_DECODE);
if (ch == NULL)
return false;
BASS_ChannelSetAttribute(ch, BASS_ATTRIB_BUFFER, 0);
BASS_ChannelSetAttribute(ch, BASS_ATTRIB_VOL, 0.0f);
chans[i] = ch;
}
chan = chans[0];
channext = chans[1];
free(outputBuffer);
return true;
}
public:
bool Load(std::filesystem::path ginPath, bool bIsFloatBuffer)
{
bFloatBuffer = bIsFloatBuffer;
// TODO: error handling
if (!ParseGinsu(ginPath))
return false;
if (!CreateChannels(ginPath))
return false;
DWORD gStreamFlags = BASS_MIXER_NONSTOP | BASS_MIXER_NOSPEAKER | BASS_STREAM_DECODE;
if (bFloatBuffer)
gStreamFlags |= BASS_SAMPLE_FLOAT;
gStream = BASS_Mixer_StreamCreate(GinsuHead.sampleRate, 1, gStreamFlags);
if (gStream == NULL)
return false;
for (int i = 0; i < GinsuHead.cycleCount; i++)
{
if (BASS_Mixer_StreamAddChannel(gStream, chans[i], BASS_MIXER_CHAN_NORAMPIN) == FALSE)
return false;
}
BASS_ChannelSetAttribute(gStream, BASS_ATTRIB_BUFFER, 0);
bLoaded = true;
return true;
}
BOOL Play(BOOL restart)
{
if (!bLoaded)
return FALSE;
return BASS_ChannelPlay(gStream, restart);
}
BOOL Stop()
{
if (!bLoaded)
return FALSE;
return BASS_ChannelStop(gStream);
}
BOOL Pause()
{
if (!bLoaded)
return FALSE;
return BASS_ChannelPause(gStream);
}
BOOL SetVolume(float inVol)
{
if (!bLoaded)
return FALSE;
return BASS_ChannelSetAttribute(gStream, BASS_ATTRIB_VOL, inVol);
}
float GetVolume()
{
if (!bLoaded)
return 0.0f;
float ret = 0.0f;
BOOL res = BASS_ChannelGetAttribute(gStream, BASS_ATTRIB_VOL, &ret);
return ret;
}
HSTREAM GetStreamHandle()
{
if (!bLoaded)
return 0;
return gStream;
}
DWORD GetData(void* outBuffer, DWORD length)
{
if (!bLoaded)
return 0;
return BASS_ChannelGetData(gStream, outBuffer, length);
}
void SetFrequency(float inFreq)
{
if (!bLoaded)
return;
SetPlaybackFrequency(inFreq);
}
void SetFrequencyFromPercentage(float inFreqPct)
{
if (!bLoaded)
return;
float f = cus_lerp(GinsuHead.minFrequency, GinsuHead.maxFrequency, inFreqPct);
SetPlaybackFrequency(f);
}
float GetFrequency()
{
if (!bLoaded)
return 0.0f;
return freqCurrent;
}
float GetFrequencyFromPercentage(float inFreqPct)
{
if (!bLoaded)
return 0.0f;
float f = cus_lerp(GinsuHead.minFrequency, GinsuHead.maxFrequency, inFreqPct);
return f;
}
float GetFrequencyPercentage()
{
if (!bLoaded)
return 0.0f;
float d = (freqCurrent - GinsuHead.minFrequency) / (GinsuHead.maxFrequency - GinsuHead.minFrequency);
return d;
}
float GetFrequencyDelta()
{
if (!bLoaded)
return 0.0f;
return freqDelta;
}
bool bIsDecelGin()
{
if (!bLoaded)
return false;
return bDecelGin;
}
float GetMaxFrequency()
{
if (!bLoaded)
return 0.0f;
return GinsuHead.maxFrequency;
}
float GetMinFrequency()
{
if (!bLoaded)
return 0.0f;
return GinsuHead.minFrequency;
}
uint32_t GetSampleRate()
{
if (!bLoaded)
return 0;
return GinsuHead.sampleRate;
}
uint32_t GetSampleCount()
{
if (!bLoaded)
return 0;
return GinsuHead.sampleCount;
}
uint32_t GetCycleCount()
{
if (!bLoaded)
return 0;
return GinsuHead.cycleCount;
}
uint32_t GetSegCount()
{
if (!bLoaded)
return 0;
return GinsuHead.segCount;
}
BASSGinsuStream()
{
GinsuHead = { 0 };
bDecelGin = false;
freqSamples = nullptr;
cycleSamples = nullptr;
cycleFreqs = nullptr;
hsv = nullptr;
chans = nullptr;
gStream = 0;
oldcyc = -1;
prevcyc = 0;
chan = 0;
channext = 0;
GinDir = GIN_FORWARDS;
freqDelta = 0.0f;
freqCurrent = -1.0f;
bLoaded = false;
bFloatBuffer = false;
}
~BASSGinsuStream()
{
if (freqSamples)
free(freqSamples);
if (cycleSamples)
free(cycleSamples);
if (cycleFreqs)
free(cycleFreqs);
if (hsv)
for (int i = 0; i < GinsuHead.cycleCount; i++)
{
BASS_SampleFree(hsv[i]);
}
free(hsv);
if (chans)
{
free(chans);
}
if (gStream)
{
BASS_StreamFree(gStream);
}
}
};
// TODO: add inheritance from BASSGinsuStream and override stuff accordingly!
class BASSGinsuMultiStream
{
public:
BASSGinsuStream* accelStream;
BASSGinsuStream* decelStream;
private:
HSTREAM gStream;
bool bFloatBuffer;
HSAMPLE hsIdle;
HSAMPLE hsRedline;
HSAMPLE hsReverseWhine;
HSAMPLE hsForwardWhine;
HSAMPLE hsIdleWhine;
DWORD sampleRate;
DWORD chIdle;
DWORD chRedline;
DWORD chReverseWhine;
DWORD chForwardWhine;
DWORD chIdleWhine;
HFX fxACL;
HFX fxDCL;
BASS_DX8_PARAMEQ eqACL;
BASS_DX8_PARAMEQ eqDCL;
FrameLimiter::FPSLimitMode mFPSLimitMode;
std::chrono::high_resolution_clock::time_point lastTime;
double FPSLimit;
float freqCurrent;
float freqOld;
float freqDelta;
float freqDelta2;
float freqOldDelta;
float freqMin;
float freqMax;
float accelVol;
float decelVol;
float accelOldVol;
float decelOldVol;
float rateVol;
float rateOldVol;
float rateVolCurve;
float rateEqCurve;
float rateMinVol;
float rateEqAmount;
float rateEqOldAmount;
float rateRPMTarget;
float rateAccelXFadeRPMRatio;
float rateAccelXFadeRPMRange;
float rateDecelXFadeRPMRatio;
float rateDecelXFadeRPMRange;
float curRateRPMXFadeTarget;
float rateAccelEqXFadeRPMRatio;
float rateAccelEqXFadeRPMRange;
float rateDecelEqXFadeRPMRatio;
float rateDecelEqXFadeRPMRange;
float curRateEqRPMXFadeTarget;
float rateOfChange;
float eqMinGainACL;
float eqMinGainDCL;
float eqMaxGainACL;
float eqMaxGainDCL;
float eqFrequencyACL;
float eqFrequencyDCL;
float eqBandwidthACL;
float eqBandwidthDCL;
float throttleAmount;
float accelGlobalVol;
float decelGlobalVol;
float AccelXFadeRPMRatio;
float AccelXFadeRPMRange;
float DecelXFadeRPMRatio;
float DecelXFadeRPMRange;
float curRPMXFadeTarget;
float XFadeDelta;
float throttleCurve;
float throttleAccelMinVol;
float throttleDecelMinVol;
float accelRelease;
float decelRelease;
float decelFadePoint;
float redlineStart;
float redlineVol;
float redlineGlobalVol;
float redlinePitch;
float redlineSampleRate;
float redlineSmackStart;
std::chrono::high_resolution_clock::time_point redlineTime;
uint32_t redlineFadeTime;
bool bNeedToSetRedlineTimeIn;
bool bNeedToSetRedlineTimeOut;
float redlineAccelMinVol;
float redlineDecelMinVol;
float idleEnd;
float idleVol;
float idleGlobalVol;
float idlePitch;
float idleSampleRate;
float speedCurrent;
float speedDelta;
float speedDelta2;
float speedOldDelta;
float reverseWhineFadeRange;
float reverseWhineLastSpeed;
float reverseWhineLastVol;
float reverseWhineMinVol;
float reverseWhineVol;
float reverseWhineGlobalVol;
float reverseWhineSampleRate;
bool bReverseWhineEnable;
float forwardWhineFadeRange;
float forwardWhineDecelFadeRange;
float forwardWhineLastSpeed;
float forwardWhineLastVol;
float forwardWhineMinVol;
float forwardWhineVol;
float forwardWhineGlobalVol;
float forwardWhineSampleRate;
bool bForwardWhineEnable;
float idleWhineFadeRange;
float idleWhineMinVol;
float idleWhineVol;
float idleWhineGlobalVol;
float idleWhineSampleRate;
bool bIdleWhineEnable;
bool bCurrentlyShifting;
bool bAccelDirection;
bool bAccelDirectionSpeed;
bool bOldDirection;
bool bOldDirectionSpeed;
bool bLoaded;
bool bCurrentlyLoading;
BASSGinsuStream* CreateStream(std::filesystem::path ginPath, bool bIsFloatBuffer)
{
BASSGinsuStream* stream = new BASSGinsuStream();
if (stream == nullptr)
return nullptr;
if (!stream->Load(ginPath, bIsFloatBuffer))
{
delete stream;
return nullptr;
}
return stream;
}
float calculateNewSampleRate(float originalSampleRate, float semitonesShift)
{
// Define the constant for the twelfth root of 2 (used to calculate semitone frequency ratio).
//const float twelfthRootOf2 = powf(2.0, 1.0 / 12.0);
constexpr float twelfthRootOf2 = 1.0594630943592952645618252949463f;
// Calculate the frequency ratio for the specified number of semitones.
float frequencyRatio = powf(twelfthRootOf2, semitonesShift);
// Calculate the new sample rate.
float newSampleRate = originalSampleRate * frequencyRatio;
return newSampleRate;
}
float calculateRevWhineSemitones(float speedMPS)
{
constexpr float numerator = 16.0f / 3.6f + 1.0f; // 16.0f * 0.22274f + 1.0f;
constexpr float denominator = 32.0f / 3.6f + 1.0f; //32.0f * 0.22274f + 1.0f;
constexpr float nd = numerator / denominator;
//float result = -(3.0f / ::logf(nd)) * ::logf(0.22274f * speedKmh + 1.0f) - 11.0f;
//float result = -(3.0f / ::logf(nd)) * ::logf(speedKmh / 3.6f + 1.0f) - 11.0f;
float result = -(3.0f / ::logf(nd)) * ::logf(speedMPS + 1.0f) - 11.0f;
return result;
}
float calculateIdleWhineSemitones(float speedMPS)
{
constexpr float b = 0.1402f;
constexpr float numerator = 16.0f * b + 1.0f;
constexpr float denominator = 32.0f * b + 1.0f;
constexpr float nd = numerator / denominator;
float result = -(3.0f / ::logf(nd)) * ::logf(b * (speedMPS * 3.6f) + 1.0f) - 22.0f;
return result;
}
// TODO: this is a mathematical approximation. There should be a better way to calculate this (maybe based on actual gear ratios?)
float calculateFwdWhineSemitones(float speedMPS)
{
constexpr float b = 0.164001f;
//float numerator = -(3.0f / logf((16.0f * b + 1.0f) / (34.0f * b + 1.0f)));
constexpr float numerator = 5.0348324403659308499533463488627f;
float innerLog = logf(b * (speedMPS * 3.6f) + 1.0f);
float result = numerator * innerLog - 12.0f;
return result;
}
float linearToLogarithmic(float x, float b)
{
// Ensure that x is within the [0, 1.0] range
if (x < 0.0)
{
x = 0.0;
}
else if (x > 1.0)
{
x = 1.0;
}
// Perform the logarithmic transformation
// You can adjust the base to control the curve of the logarithmic scale
//float base = 10.0; // Change this to your desired base (e.g., 2.0 for a different curve)
return log10f(1.0f + (b - 1.0f) * x) / log10f(b);
}
// float exponentialInterpolation(float a, float b, float t) {
// if (t <= 0.0) {
// return a;
// }
// else if (t >= 1.0) {
// return b;
// }
// else {
// return a * std::powf(b / a, t);
// }
// }
// TODO: make this a bit better - the bigger the frequency delta - the louder the sounds should play
void SetPlaybackFrequency(float inFreq)
{
if (mFPSLimitMode == FrameLimiter::FPSLimitMode::FPS_REALTIME)