forked from adamdruppe/arsd
-
Notifications
You must be signed in to change notification settings - Fork 0
/
simpleaudio.d
4366 lines (3724 loc) · 127 KB
/
simpleaudio.d
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
// FIXME: add a query devices thing
// FIXME: add the alsa sequencer interface cuz then i don't need the virtual raw midi sigh. or at elast load "virtual" and auto connect it somehow
/**
The purpose of this module is to provide audio functions for
things like playback, capture, and volume on both Windows
(via the mmsystem calls) and Linux (through ALSA).
It is only aimed at the basics, and will be filled in as I want
a particular feature. I don't generally need super configurability
and see it as a minus, since I don't generally care either, so I'm
going to be going for defaults that just work. If you need more though,
you can hack the source or maybe just use it for the operating system
bindings.
For example, I'm starting this because I want to write a volume
control program for my linux box, so that's what is going first.
That will consist of a listening callback for volume changes and
being able to get/set the volume.
TODO:
* pre-resampler that loads a clip and prepares it for repeated fast use
* controls so you can tell a particular thing to keep looping until you tell it to stop, or stop after the next loop, etc (think a phaser sound as long as you hold the button down)
* playFile function that detects automatically. basically:
if(args[1].endsWith("ogg"))
a.playOgg(args[1]);
else if(args[1].endsWith("wav"))
a.playWav(args[1]);
else if(mp3)
a.playMp3(args[1]);
* play audio high level with options to wait until completion or return immediately
* midi mid-level stuff but see [arsd.midi]!
* some kind of encoder???????
I will probably NOT do OSS anymore, since my computer doesn't even work with it now.
Ditto for Macintosh, as I don't have one and don't really care about them.
License:
GPL3 unless you compile with `-version=without_resampler` and do *not* use
the mp3 functions, in which case it is BSL-1.0.
*/
module arsd.simpleaudio;
// http://webcache.googleusercontent.com/search?q=cache:NqveBqL0AOUJ:https://www.alsa-project.org/alsa-doc/alsa-lib/group___p_c_m.html&hl=en&gl=us&strip=1&vwsrc=0
version(without_resampler) {
} else {
version(X86)
version=with_resampler;
version(X86_64)
version=with_resampler;
}
enum BUFFER_SIZE_FRAMES = 1024;//512;//2048;
enum BUFFER_SIZE_SHORT = BUFFER_SIZE_FRAMES * 2;
/// A reasonable default volume for an individual sample. It doesn't need to be large; in fact it needs to not be large so mixing doesn't clip too much.
enum DEFAULT_VOLUME = 20;
version(Demo_simpleaudio)
void main() {
/+
version(none) {
import iv.stb.vorbis;
int channels;
short* decoded;
auto v = new VorbisDecoder("test.ogg");
auto ao = AudioOutput(0);
ao.fillData = (short[] buffer) {
auto got = v.getSamplesShortInterleaved(2, buffer.ptr, buffer.length);
if(got == 0) {
ao.stop();
}
};
ao.play();
return;
}
auto thread = new AudioPcmOutThread();
thread.start();
thread.playOgg("test.ogg");
Thread.sleep(5.seconds);
//Thread.sleep(150.msecs);
thread.beep();
Thread.sleep(250.msecs);
thread.blip();
Thread.sleep(250.msecs);
thread.boop();
Thread.sleep(1000.msecs);
/*
thread.beep(800, 500);
Thread.sleep(500.msecs);
thread.beep(366, 500);
Thread.sleep(600.msecs);
thread.beep(800, 500);
thread.beep(366, 500);
Thread.sleep(500.msecs);
Thread.sleep(150.msecs);
thread.beep(200);
Thread.sleep(150.msecs);
thread.beep(100);
Thread.sleep(150.msecs);
thread.noise();
Thread.sleep(150.msecs);
*/
thread.stop();
thread.join();
return;
/*
auto aio = AudioMixer(0);
import std.stdio;
writeln(aio.muteMaster);
*/
/*
mciSendStringA("play test.wav", null, 0, null);
Sleep(3000);
import std.stdio;
if(auto err = mciSendStringA("play test2.wav", null, 0, null))
writeln(err);
Sleep(6000);
return;
*/
// output about a second of random noise to demo PCM
auto ao = AudioOutput(0);
short[BUFFER_SIZE_SHORT] randomSpam = void;
import core.stdc.stdlib;
foreach(ref s; randomSpam)
s = cast(short)((cast(short) rand()) - short.max / 2);
int loopCount = 40;
//import std.stdio;
//writeln("Should be about ", loopCount * BUFFER_SIZE_FRAMES * 1000 / SampleRate, " microseconds");
int loops = 0;
// only do simple stuff in here like fill the data, set simple
// variables, or call stop anything else might cause deadlock
ao.fillData = (short[] buffer) {
buffer[] = randomSpam[0 .. buffer.length];
loops++;
if(loops == loopCount)
ao.stop();
};
ao.play();
return;
+/
// Play a C major scale on the piano to demonstrate midi
auto midi = MidiOutput(0);
ubyte[16] buffer = void;
ubyte[] where = buffer[];
midi.writeRawMessageData(where.midiProgramChange(1, 1));
for(ubyte note = MidiNote.C; note <= MidiNote.C + 12; note++) {
where = buffer[];
midi.writeRawMessageData(where.midiNoteOn(1, note, 127));
import core.thread;
Thread.sleep(dur!"msecs"(500));
midi.writeRawMessageData(where.midiNoteOff(1, note, 127));
if(note != 76 && note != 83)
note++;
}
import core.thread;
Thread.sleep(dur!"msecs"(500)); // give the last note a chance to finish
}
/++
Provides an interface to control a sound.
History:
Added December 23, 2020
+/
interface SampleController {
/++
Pauses playback, keeping its position. Use [resume] to pick up where it left off.
+/
void pause();
/++
Resumes playback after a call to [pause].
+/
void resume();
/++
Stops playback. Once stopped, it cannot be restarted
except by creating a new sample from the [AudioOutputThread]
object.
+/
void stop();
/++
Reports the current stream position, in seconds, if available (NaN if not).
+/
float position();
/++
If the sample has finished playing. Happens when it runs out or if it is stopped.
+/
bool finished();
/++
If the sample has been paused.
History:
Added May 26, 2021 (dub v10.0)
+/
bool paused();
}
private class DummySample : SampleController {
void pause() {}
void resume() {}
void stop() {}
float position() { return float.init; }
bool finished() { return true; }
bool paused() { return true; }
}
private class SampleControlFlags : SampleController {
void pause() { paused_ = true; }
void resume() { paused_ = false; }
void stop() { paused_ = false; stopped = true; }
bool paused_;
bool stopped;
bool finished_;
float position() { return currentPosition; }
bool finished() { return finished_; }
bool paused() { return paused_; }
float currentPosition = 0.0;
}
/++
Wraps [AudioPcmOutThreadImplementation] with RAII semantics for better
error handling and disposal than the old way.
DO NOT USE THE `new` OPERATOR ON THIS! Just construct it inline:
---
auto audio = AudioOutputThread(true);
audio.beep();
---
History:
Added May 9, 2020 to replace the old [AudioPcmOutThread] class
that proved pretty difficult to use correctly.
+/
struct AudioOutputThread {
@disable this();
static if(__VERSION__ < 2098)
mixin(q{ @disable new(size_t); }); // gdc9 requires the arg fyi, but i mix it in because dmd deprecates before semantic so it can't be versioned out ugh
else
@disable new(); // but new dmd is strict about not allowing it
@disable void start() {} // you aren't supposed to control the thread yourself!
/++
You should call `exit` instead of join. It will signal the thread to exit and then call join for you.
If you absolutely must call join, use [rawJoin] instead.
History:
Disabled on December 30, 2021
+/
@disable void join(bool a = false) {} // you aren't supposed to control the thread yourself!
/++
Don't call this unless you're sure you know what you're doing.
You should use `audioOutputThread.exit();` instead.
+/
Throwable rawJoin(bool rethrow = true) {
if(impl is null)
return null;
return impl.join(rethrow);
}
/++
Pass `true` to enable the audio thread. Otherwise, it will
just live as a dummy mock object that you should not actually
try to use.
History:
Parameter `default` added on Nov 8, 2020.
The sample rate parameter was not correctly applied to the device on Linux until December 24, 2020.
+/
this(bool enable, int SampleRate = 44100, int channels = 2, string device = "default") {
if(enable) {
impl = new AudioPcmOutThreadImplementation(SampleRate, channels, device);
impl.refcount++;
impl.start();
impl.waitForInitialization();
impl.priority = Thread.PRIORITY_MAX;
}
}
/// ditto
this(bool enable, string device, int SampleRate = 44100, int channels = 2) {
this(enable, SampleRate, channels, device);
}
/// Keeps an internal refcount.
this(this) {
if(impl)
impl.refcount++;
}
/// When the internal refcount reaches zero, it stops the audio and rejoins the thread, throwing any pending exception (yes the dtor can throw! extremely unlikely though).
~this() {
if(impl) {
impl.refcount--;
if(impl.refcount == 0) {
impl.exit(true);
}
}
}
/++
Returns true if the output is suspended. Use `suspend` and `unsuspend` to change this.
History:
Added December 21, 2021 (dub v10.5)
+/
bool suspended() {
if(impl)
return impl.suspended();
return true;
}
/++
This allows you to check `if(audio)` to see if it is enabled.
+/
bool opCast(T : bool)() {
return impl !is null;
}
/++
Other methods are forwarded to the implementation of type
[AudioPcmOutThreadImplementation]. See that for more information
on what you can do.
This opDispatch template will forward all other methods directly
to that [AudioPcmOutThreadImplementation] if this is live, otherwise
it does nothing.
+/
template opDispatch(string name) {
static if(is(typeof(__traits(getMember, impl, name)) Params == __parameters))
auto opDispatch(Params params) {
if(impl)
return __traits(getMember, impl, name)(params);
static if(!is(typeof(return) == void))
return typeof(return).init;
}
else static assert(0);
}
// since these are templates, the opDispatch won't trigger them, so I have to do it differently.
// the dummysample is good anyway.
SampleController playEmulatedOpl3Midi()(string filename) {
if(impl)
return impl.playEmulatedOpl3Midi(filename);
return new DummySample;
}
SampleController playEmulatedOpl3Midi()(immutable(ubyte)[] data) {
if(impl)
return impl.playEmulatedOpl3Midi(data);
return new DummySample;
}
SampleController playOgg()(string filename, bool loop = false) {
if(impl)
return impl.playOgg(filename, loop);
return new DummySample;
}
SampleController playOgg()(immutable(ubyte)[] data, bool loop = false) {
if(impl)
return impl.playOgg(data, loop);
return new DummySample;
}
SampleController playMp3()(string filename) {
if(impl)
return impl.playMp3(filename);
return new DummySample;
}
SampleController playMp3()(immutable(ubyte)[] data) {
if(impl)
return impl.playMp3(data);
return new DummySample;
}
SampleController playWav()(string filename) {
if(impl)
return impl.playWav(filename);
return new DummySample;
}
SampleController playWav()(immutable(ubyte)[] data) {
if(impl)
return impl.playWav(data);
return new DummySample;
}
/// provides automatic [arsd.jsvar] script wrapping capability. Make sure the
/// script also finishes before this goes out of scope or it may end up talking
/// to a dead object....
auto toArsdJsvar() {
return impl;
}
/+
alias getImpl this;
AudioPcmOutThreadImplementation getImpl() {
assert(impl !is null);
return impl;
}
+/
private AudioPcmOutThreadImplementation impl;
}
/++
Old thread implementation. I decided to deprecate it in favor of [AudioOutputThread] because
RAII semantics make it easier to get right at the usage point. See that to go forward.
History:
Deprecated on May 9, 2020.
+/
deprecated("Use AudioOutputThread instead.") class AudioPcmOutThread {}
/+
/++
+/
void mmsleep(Duration time) {
version(Windows) {
static HANDLE timerQueue;
static HANDLE event;
if(event is null)
event = CreateEvent(null, false, false, null);
extern(Windows)
static void cb(PVOID ev, BOOLEAN) {
HANDLE e = cast(HANDLE) ev;
SetEvent(e);
}
//if(timerQueue is null)
//timerQueue = CreateTimerQueue();
// DeleteTimerQueueEx(timerQueue, null);
HANDLE nt;
auto ret = CreateTimerQueueTimer(&nt, timerQueue, &cb, event /+ param +/, cast(DWORD) time.total!"msecs", 0 /* period */, WT_EXECUTEDEFAULT);
if(!ret)
throw new Exception("fail");
//DeleteTimerQueueTimer(timerQueue, nt, INVALID_HANDLE_VALUE);
WaitForSingleObject(event, 1000);
}
}
+/
/++
A clock you can use for multimedia applications. It compares time elapsed against
a position variable you pass in to figure out how long to wait to get to that point.
Very similar to Phobos' [std.datetime.stopwatch.StopWatch|StopWatch] but with built-in
wait capabilities.
For example, suppose you want something to happen 60 frames per second:
---
MMClock clock;
Duration frame;
clock.restart();
while(running) {
frame += 1.seconds / 60;
bool onSchedule = clock.waitUntil(frame);
do_essential_frame_work();
if(onSchedule) {
// if we're on time, do other work too.
// but if we weren't on time, skipping this
// might help catch back up to where we're
// supposed to be.
do_would_be_nice_frame_work();
}
}
---
+/
struct MMClock {
import core.time;
private Duration position;
private MonoTime lastPositionUpdate;
private bool paused;
int speed = 1000; /// 1000 = 1.0, 2000 = 2.0, 500 = 0.5, etc.
private void updatePosition() {
auto now = MonoTime.currTime;
position += (now - lastPositionUpdate) * speed / 1000;
lastPositionUpdate = now;
}
/++
Restarts the clock from position zero.
+/
void restart() {
position = Duration.init;
lastPositionUpdate = MonoTime.currTime;
}
/++
Pauses the clock.
+/
void pause() {
if(paused) return;
updatePosition();
paused = true;
}
void unpause() {
if(!paused) return;
lastPositionUpdate = MonoTime.currTime;
paused = false;
}
/++
Goes to sleep until the real clock catches up to the given
`position`.
Returns: `true` if you're on schedule, returns false if the
given `position` is already in the past. In that case,
you might want to consider skipping some work to get back
on time.
+/
bool waitUntil(Duration position) {
auto diff = timeUntil(position);
if(diff < 0.msecs)
return false;
if(diff == 0.msecs)
return true;
import core.thread;
Thread.sleep(diff);
return true;
}
/++
+/
Duration timeUntil(Duration position) {
updatePosition();
return (position - this.position) * 1000 / speed;
}
/++
Returns the current time on the clock since the
last call to [restart], excluding times when the
clock was paused.
+/
Duration currentPosition() {
updatePosition();
return position;
}
}
import core.thread;
/++
Makes an audio thread for you that you can make
various sounds on and it will mix them with good
enough latency for simple games.
DO NOT USE THIS DIRECTLY. Instead, access it through
[AudioOutputThread].
---
auto audio = AudioOutputThread(true);
audio.beep();
// you need to keep the main program alive long enough
// to keep this thread going to hear anything
Thread.sleep(1.seconds);
---
+/
final class AudioPcmOutThreadImplementation : Thread {
private this(int SampleRate, int channels, string device = "default") {
this.isDaemon = true;
this.SampleRate = SampleRate;
this.channels = channels;
this.device = device;
super(&run);
}
private int SampleRate;
private int channels;
private int refcount;
private string device;
private void waitForInitialization() {
shared(AudioOutput*)* ao = cast(shared(AudioOutput*)*) &this.ao;
//int wait = 0;
while(isRunning && *ao is null) {
Thread.sleep(5.msecs);
//wait += 5;
}
//import std.stdio; writeln(wait);
if(*ao is null) {
exit(true);
}
}
/++
Asks the device to pause/unpause. This may not actually do anything on some systems.
You should probably use [suspend] and [unsuspend] instead.
+/
@scriptable
void pause() {
if(ao) {
ao.pause();
}
}
/// ditto
@scriptable
void unpause() {
if(ao) {
ao.unpause();
}
}
/++
Stops the output thread. Using the object after it is stopped is not recommended which is why
this is now deprecated.
You probably want [suspend] or [exit] instead. Use [suspend] if you want to stop playing, and
close the output device, but keep the thread alive so you can [unsuspend] later. After calling
[suspend], you can call [unsuspend] and then continue using the other method normally again.
Use [exit] if you want to stop playing, close the output device, and terminate the worker thread.
After calling [exit], you may not call any methods on the thread again.
The one exception is if you are inside an audio callback and want to stop the thread and prepare
it to be [AudioOutputThread.rawJoin]ed. Preferably, you'd avoid doing this - the channels can
simply return false to indicate that they are done. But if you must do that, call [rawStop] instead.
History:
`stop` was deprecated and `rawStop` added on December 30, 2021 (dub v10.5)
+/
deprecated("You want to use either suspend or exit instead, or rawStop if you must but see the docs.")
void stop() {
if(ao) {
ao.stop();
}
}
/// ditto
void rawStop() {
if(ao) { ao.stop(); }
}
/++
Makes some old-school style sound effects. Play with them to see what they actually sound like.
Params:
freq = frequency of the wave in hertz
dur = duration in milliseconds
volume = amplitude of the wave, between 0 and 100
balance = stereo balance. 50 = both speakers equally, 0 = all to the left, none to the right, 100 = all to the right, none to the left.
attack = a parameter to the change of frequency
freqBase = the base frequency in the sound effect algorithm
History:
The `balance` argument was added on December 13, 2021 (dub v10.5)
+/
@scriptable
void beep(int freq = 900, int dur = 150, int volume = DEFAULT_VOLUME, int balance = 50) {
Sample s;
s.operation = 0; // square wave
s.frequency = SampleRate / freq;
s.duration = dur * SampleRate / 1000;
s.volume = volume;
s.balance = balance;
addSample(s);
}
/// ditto
@scriptable
void noise(int dur = 150, int volume = DEFAULT_VOLUME, int balance = 50) {
Sample s;
s.operation = 1; // noise
s.frequency = 0;
s.volume = volume;
s.duration = dur * SampleRate / 1000;
s.balance = balance;
addSample(s);
}
/// ditto
@scriptable
void boop(float attack = 8, int freqBase = 500, int dur = 150, int volume = DEFAULT_VOLUME, int balance = 50) {
Sample s;
s.operation = 5; // custom
s.volume = volume;
s.duration = dur * SampleRate / 1000;
s.balance = balance;
s.f = delegate short(int x) {
auto currentFrequency = cast(float) freqBase / (1 + cast(float) x / (cast(float) SampleRate / attack));
import std.math;
auto freq = 2 * PI / (cast(float) SampleRate / currentFrequency);
return cast(short) (sin(cast(float) freq * cast(float) x) * short.max * volume / 100);
};
addSample(s);
}
/// ditto
@scriptable
void blip(float attack = 6, int freqBase = 800, int dur = 150, int volume = DEFAULT_VOLUME, int balance = 50) {
Sample s;
s.operation = 5; // custom
s.volume = volume;
s.duration = dur * SampleRate / 1000;
s.balance = balance;
s.f = delegate short(int x) {
auto currentFrequency = cast(float) freqBase * (1 + cast(float) x / (cast(float) SampleRate / attack));
import std.math;
auto freq = 2 * PI / (cast(float) SampleRate / currentFrequency);
return cast(short) (sin(cast(float) freq * cast(float) x) * short.max * volume / 100);
};
addSample(s);
}
version(none)
void custom(int dur = 150, int volume = DEFAULT_VOLUME) {
Sample s;
s.operation = 5; // custom
s.volume = volume;
s.duration = dur * SampleRate / 1000;
s.f = delegate short(int x) {
auto currentFrequency = 500.0 / (1 + cast(float) x / (cast(float) SampleRate / 8));
import std.math;
auto freq = 2 * PI / (cast(float) SampleRate / currentFrequency);
return cast(short) (sin(cast(float) freq * cast(float) x) * short.max * volume / 100);
};
addSample(s);
}
/++
Plays the given midi files with the nuked opl3 emulator.
Requires nukedopl3.d (module [arsd.nukedopl3]) to be compiled in, which is GPL.
History:
Added December 24, 2020.
License:
If you use this function, you are opting into the GPL version 2 or later.
Authors:
Based on ketmar's code.
+/
SampleController playEmulatedOpl3Midi()(string filename, bool loop = false) {
import std.file;
auto bytes = cast(immutable(ubyte)[]) std.file.read(filename);
return playEmulatedOpl3Midi(bytes);
}
/// ditto
SampleController playEmulatedOpl3Midi()(immutable(ubyte)[] data, bool loop = false) {
import arsd.nukedopl3;
auto scf = new SampleControlFlags;
auto player = new OPLPlayer(this.SampleRate, true, channels == 2);
player.looped = loop;
player.load(data);
player.play();
addChannel(
delegate bool(short[] buffer) {
if(scf.paused) {
buffer[] = 0;
return true;
}
if(!player.playing) {
scf.finished_ = true;
return false;
}
auto pos = player.generate(buffer[]);
scf.currentPosition += cast(float) buffer.length / SampleRate/ channels;
if(pos == 0 || scf.stopped) {
scf.finished_ = true;
return false;
}
return !scf.stopped;
}
);
return scf;
}
/++
Requires vorbis.d to be compiled in (module arsd.vorbis)
Returns:
An implementation of [SampleController] which lets you pause, etc., the file.
Please note that the static type may change in the future. It will always be a subtype of [SampleController], but it may be more specialized as I add more features and this will not necessarily match its sister functions, [playMp3] and [playWav], though all three will share an ancestor in [SampleController]. Therefore, if you use `auto`, there's no guarantee the static type won't change in future versions and I will NOT consider that a breaking change since the base interface will remain compatible.
History:
Automatic resampling support added Nov 7, 2020.
Return value changed from `void` to a sample control object on December 23, 2020.
+/
SampleController playOgg()(string filename, bool loop = false) {
import arsd.vorbis;
auto v = new VorbisDecoder(filename);
return playOgg(v, loop);
}
/// ditto
SampleController playOgg()(immutable(ubyte)[] data, bool loop = false) {
import arsd.vorbis;
auto v = new VorbisDecoder(cast(int) data.length, delegate int(void[] buffer, uint ofs, VorbisDecoder vb) nothrow @nogc {
if(buffer is null)
return 0;
ubyte[] buf = cast(ubyte[]) buffer;
if(ofs + buf.length <= data.length) {
buf[] = data[ofs .. ofs + buf.length];
return cast(int) buf.length;
} else {
buf[0 .. data.length - ofs] = data[ofs .. $];
return cast(int) data.length - ofs;
}
});
return playOgg(v, loop);
}
// no compatibility guarantees, I can change this overload at any time!
/* private */ SampleController playOgg(VorbisDecoder)(VorbisDecoder v, bool loop = false) {
auto scf = new SampleControlFlags;
/+
If you want 2 channels:
if the file has 2+, use them.
If the file has 1, duplicate it for the two outputs.
If you want 1 channel:
if the file has 1, use it
if the file has 2, average them.
+/
if(v.sampleRate == SampleRate && v.chans == channels) {
plain_fallback:
addChannel(
delegate bool(short[] buffer) {
if(scf.paused) {
buffer[] = 0;
return true;
}
if(cast(int) buffer.length != buffer.length)
throw new Exception("eeeek");
plain:
auto got = v.getSamplesShortInterleaved(2, buffer.ptr, cast(int) buffer.length);
if(got == 0) {
if(loop) {
v.seekStart();
scf.currentPosition = 0;
return true;
}
scf.finished_ = true;
return false;
} else {
scf.currentPosition += cast(float) got / v.sampleRate;
}
if(scf.stopped)
scf.finished_ = true;
return !scf.stopped;
}
);
} else {
version(with_resampler) {
auto resampleContext = new class ResamplingContext {
this() {
super(scf, v.sampleRate, SampleRate, v.chans, channels);
}
override void loadMoreSamples() {
float*[2] tmp;
tmp[0] = buffersIn[0].ptr;
tmp[1] = buffersIn[1].ptr;
loop:
auto actuallyGot = v.getSamplesFloat(v.chans, tmp.ptr, cast(int) buffersIn[0].length);
if(actuallyGot == 0 && loop) {
v.seekStart();
scf.currentPosition = 0;
goto loop;
}
resamplerDataLeft.dataIn = buffersIn[0][0 .. actuallyGot];
if(v.chans > 1)
resamplerDataRight.dataIn = buffersIn[1][0 .. actuallyGot];
}
};
addChannel(&resampleContext.fillBuffer);
} else goto plain_fallback;
}
return scf;
}
/++
Requires mp3.d to be compiled in (module [arsd.mp3]) which is LGPL licensed.
That LGPL license will extend to your code.
Returns:
An implementation of [SampleController] which lets you pause, etc., the file.
Please note that the static type may change in the future. It will always be a subtype of [SampleController], but it may be more specialized as I add more features and this will not necessarily match its sister functions, [playOgg] and [playWav], though all three will share an ancestor in [SampleController]. Therefore, if you use `auto`, there's no guarantee the static type won't change in future versions and I will NOT consider that a breaking change since the base interface will remain compatible.
History:
Automatic resampling support added Nov 7, 2020.
Return value changed from `void` to a sample control object on December 23, 2020.
The `immutable(ubyte)[]` overload was added December 30, 2020.
+/
SampleController playMp3()(string filename) {
import std.stdio;
auto fi = new File(filename); // just let the GC close it... otherwise random segfaults happen... blargh
auto reader = delegate(void[] buf) {
return cast(int) fi.rawRead(buf[]).length;
};
return playMp3(reader);
}
/// ditto
SampleController playMp3()(immutable(ubyte)[] data) {
return playMp3( (void[] buffer) {
ubyte[] buf = cast(ubyte[]) buffer;
if(data.length >= buf.length) {
buf[] = data[0 .. buf.length];
data = data[buf.length .. $];
return cast(int) buf.length;
} else {
auto it = data.length;
buf[0 .. data.length] = data[];
buf[data.length .. $] = 0;
data = data[$ .. $];
return cast(int) it;
}
});
}
// no compatibility guarantees, I can change this overload at any time!
/* private */ SampleController playMp3()(int delegate(void[]) reader) {
import arsd.mp3;
auto mp3 = new MP3Decoder(reader);
if(!mp3.valid)
throw new Exception("file not valid");
auto scf = new SampleControlFlags;
if(mp3.sampleRate == SampleRate && mp3.channels == channels) {
plain_fallback:
auto next = mp3.frameSamples;