forked from sam-truscott/wumpusopenal
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Out_Wumpus.cpp
1314 lines (1107 loc) · 30.7 KB
/
Out_Wumpus.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
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
#include "Out_Wumpus.h"
#include "Version.h"
#ifndef NATIVE
#include "ConfigStatusForm.h"
#endif
#include "ConfigFile.h"
#include "Out_Renderer.h"
#include "Winamp.h"
#define DEBUG_BUFFER_SIZE 255
#ifdef _DEBUG
#include <crtdbg.h>
#endif
#define SYNC_START EnterCriticalSection(&critical_section)
#define SYNC_END LeaveCriticalSection(&critical_section)
namespace WinampOpenALOut {
Output_Wumpus::Output_Wumpus() {
// make sure all the pointers are set to zero
last_output_time = ZERO_TIME;
last_written_time = ZERO_TIME;
total_written = ZERO_TIME;
total_played = ZERO_TIME;
current_output_time = ZERO_TIME;
current_written_time = ZERO_TIME;
no_renderers = 0;
for ( char rend=0 ; rend < MAX_RENDERERS ; rend++ )
{
renderers[rend] = NULL;
}
is_playing = false;
stream_open = false;
pre_buffer = false;
pre_buffer_number = 0;
xram_detected = false;
xram_enabled = false;
sample_rate = 0;
number_of_channels = 0;
bits_per_sample = 0;
no_buffers = 0;
bytes_per_sample_channel = 0;
last_pause = 0;
volume = 0;
temp_size = 0;
memset(
temp,
0,
sizeof(temp));
conf_buffer_length = 0;
is_mono_expanded = false;
is_stereo_expanded = false;
split_out = false;
}
Output_Wumpus::~Output_Wumpus()
{
//ensure everything in memory is deleted
}
/*
_DllMainCRTStartup
This is called when the DLL is loaded
*/
BOOL WINAPI _DllMainCRTStartup(HANDLE hInst, ULONG ul_reason_for_call, LPVOID lpReserved)
{
if (ul_reason_for_call == DLL_PROCESS_ATTACH)
{
// optimisation where threads are no longer attached/detached - I think
DisableThreadLibraryCalls((HMODULE)hInst);
}
return TRUE;
}
int Output_Wumpus::get_current_output_time(
const __int64 the_current_output_time,
const unsigned int sample_rate)
{
return MulDiv( (int)(the_current_output_time & THIRTY_TWO_BIT_BIT_MASK),ONE_SECOND_IN_MS,sample_rate);
}
int Output_Wumpus::get_current_written_time(
const __int64 the_current_written_time,
const unsigned int sample_rate)
{
return MulDiv( (int)(the_current_written_time & THIRTY_TWO_BIT_BIT_MASK),ONE_SECOND_IN_MS,sample_rate);
}
void Output_Wumpus::OnError()
{
this->Close();
}
void Output_Wumpus::SwitchOutputDevice(const int device)
{
SYNC_START;
SwitchOutputDevice(device, split_out);
SYNC_END;
}
void Output_Wumpus::SwitchOutputDevice(const int device, const bool is_split)
{
SYNC_START;
/* stop the source so we dont hear anthing else */
this->Relocate(device, GetOutputTime(),is_split);
SYNC_END;
}
void Output_Wumpus::Relocate(
const int device,
const int current_position,
const bool is_split) {
SYNC_START;
// grab some temporary data for where we are now
// and the format of the data
const unsigned int tempSampleRate = sample_rate;
const unsigned int tempBitsPerSample = bits_per_sample;
const unsigned int tempNumberOfChannels = original_number_of_channels;
// shut down the thread and wait for it to shutdown
const bool tempStreamOpen = stream_open;
const int tempPause = last_pause;
const float tempVolume = volume;
/* stop playing */
this->Close();
// only switch devices if we have to
if(device != Framework::getInstance()->GetCurrentDevice())
{
// shutdown openal
Framework::getInstance()->ALFWShutdownOpenAL();
// re-initialise openal
Framework::getInstance()->ALFWInitOpenAL(device);
}
split_out = is_split;
if(tempStreamOpen)
{
// re-initialise everything
this->Open(
tempSampleRate,
tempNumberOfChannels,
tempBitsPerSample,
ZERO_TIME,ZERO_TIME);
// reset back to the current position
this->SetBufferTime(current_position);
// reset the volume
this->SetVolumeInternal(tempVolume);
// if it was paused, re-pause
if (tempPause)
{
this->Pause(tempPause);
}
}
SYNC_END;
}
void Output_Wumpus::CheckProcessedBuffers()
{
/*
* ask each of the renderers to check where they are
*/
for ( char rend=0 ; rend < no_renderers ; rend++ )
{
this->renderers[rend]->CheckProcessedBuffers();
}
}
void Output_Wumpus::CheckPlayState()
{
this->is_playing = false;
HANDLE threads[MAX_RENDERERS];
int threadCount = 0;
memset(threads,0,sizeof(HANDLE)*MAX_RENDERERS);
/*
* ask all of the renderers if they're playing
*/
for ( char rend=0 ; rend < no_renderers ; rend++ )
{
if ( this->renderers[rend] )
{
threads[threadCount++] = this->renderers[rend]->CheckPlayState();
this->is_playing |= this->renderers[rend]->IsPlaying();
}
}
for ( char rend=0 ; rend < threadCount ; rend++ )
{
ResumeThread(threads[rend]);
}
}
/*
config
this procedure is invoked by winamp when the configure button is clicked
*/
void Output_Wumpus::Config(const HWND hwnd)
{
#ifndef NATIVE
WinampOpenALOut::Config^ config = WinampOpenALOut::Config::GetInstance(this);
config->Load();
#endif
}
void Output_Wumpus::log_debug_msg(char* msg, char* file, int line)
{
/* basic logging to file - only if we're in debug mode */
#ifdef _DEBUG
FILE *debug_file = NULL;
fopen_s(&debug_file, "out_openal.log", "a+");
fprintf_s(debug_file,"%s, %d - %s\n", file, line, msg);
fclose(debug_file);
#endif
}
/*
about
this procedure is invoked by winamp when the about button is clicked
*/
void Output_Wumpus::About(const HWND hwnd)
{
MessageBoxA(hwnd,"Wumpus OpenAL Output Plug-in "
PI_VER "\nCompiled on " __DATE__ " - " __TIME__
"\n(c) 2013 Sam Truscott\n"
"\n"
"Download latest version and source code (GPL)\n"
"https://sourceforge.net/projects/winampopenalout/\n"
"\n"
"Thanks to:\n"
"\tTinuva\n"
"\tsoddit112\n"
"\tGoujon\n"
"\n"
"In memory of Alex Cicciu, 2009\n"
,"About",MB_OK);
}
/*
init
this procedure is called when the plugin is initialised
*/
void Output_Wumpus::Initialise(const HWND window)
{
InitializeCriticalSection(&critical_section);
SYNC_START;
/*
* set up the variables to a default state
*/
stream_open = false;
is_playing = false;
split_out = false;
no_buffers = NO_BUFFERS;
sample_rate = NO_SAMPLE_RATE;
bits_per_sample = NO_BITS_PER_SAMPLE;
number_of_channels = NO_NUMBER_OF_CHANNELS;
current_output_time = ZERO_TIME;
current_written_time = ZERO_TIME;
total_played = ZERO_TIME;
total_written = ZERO_TIME;
last_output_time = ZERO_TIME;
last_written_time = ZERO_TIME;
// load the config up
ConfigFile::Initialise(window);
/*
* initialise the winamp interface to get info
*/
Winamp::Initialise(window);
/*
* get the buffer length from the config file
*/
conf_buffer_length = ConfigFile::ReadInteger(CONF_BUFFER_LENGTH);
if(conf_buffer_length == ERROR_BUFFER ||
conf_buffer_length > CONF_BUFFER_LENGTH_MAX ||
conf_buffer_length < CONF_BUFFER_LENGTH_MIN)
{
conf_buffer_length = DEFC_BUFFER_LENGTH;
ConfigFile::WriteInteger(CONF_BUFFER_LENGTH, conf_buffer_length);
}
// read in the current device to use
int current_device = ConfigFile::ReadInteger(CONF_DEVICE);
if(current_device == -1)
{
current_device = DEFC_DEVICE;
ConfigFile::WriteInteger(CONF_DEVICE, current_device);
}
/*
* get the last used volume we used to with openal
*/
int last_volume = ConfigFile::ReadGlobalInteger(CONF_VOLUME);
if ( last_volume != -1)
{
volume = (last_volume / (ALfloat)VOLUME_DIVISOR);
}
else
{
volume = 1.0f;
}
/*
* get the 3D mode (split out) and effects settings
*/
split_out = ConfigFile::ReadBoolean(CONF_SPLIT);
/*
initialise openal itself - this has been modified
and will also select the default sound card
*/
if (!Framework::getInstance()->ALFWInitOpenAL(current_device))
{
MessageBoxA(NULL, "Could not initialise OpenAL", "Error", MB_OK);
}
#ifdef _DEBUGGING
char dbg[DEBUG_BUFFER_SIZE] = {'\0'};
sprintf_s(
dbg,
DEBUG_BUFFER_SIZE,
"Using Device {%d} with buffer of {%d}",
current_device,
conf_buffer_length);
this->log_debug_msg(dbg, __FILE__, __LINE__);
#endif
this->is_mono_expanded = ConfigFile::ReadBoolean(CONF_MONO_EXPAND);
this->is_stereo_expanded = ConfigFile::ReadBoolean(CONF_STEREO_EXPAND);
this->xram_enabled = ConfigFile::ReadBoolean(CONF_XRAM_ENABLED);
#ifdef _DEBUGGING
sprintf_s(
dbg,
DEBUG_BUFFER_SIZE,
"Mono expansion {%d}, Stereo expansion {%d}, Use XRAM? {%d}",
this->is_mono_expanded,
this->is_stereo_expanded,
this->xram_enabled);
this->log_debug_msg(dbg, __FILE__, __LINE__);
this->log_debug_msg("Looking for XRAM, all values need to be larger than Zero");
ALboolean xram_ext = alIsExtensionPresent("EAX-RAM");
sprintf_s(
dbg,
DEBUG_BUFFER_SIZE,
"XRAM Extension: {=%d}, SetMode {0x%08X}, GetMode {0x%08X}, Size {%d}, Free {%d}, Auto {%d}, Hardware {%d}, Accessible {%d}",
xram_ext,
alGetProcAddress("EAXSetBufferMode"),
alGetProcAddress("EAXGetBufferMode"),
alGetEnumValue("AL_EAX_RAM_SIZE"),
alGetEnumValue("AL_EAX_RAM_FREE"),
alGetEnumValue("AL_STORAGE_AUTOMATIC"),
alGetEnumValue("AL_STORAGE_HARDWARE"),
alGetEnumValue("AL_STORAGE_ACCESSIBLE"));
this->log_debug_msg(dbg, __FILE__, __LINE__);
#endif
/*
* fairly horrible bit of code to read in the speaker
* matrix (all the real numbers for where the speakers are)
*/
char setting[MATRIX_BUFFER_SIZE];
memset(setting, '\0', MATRIX_BUFFER_SIZE);
//pump out the default format for the string
// the _a_ is the x/y/z axis position
// the _r is the renderer number
strcpy_s(setting,MATRIX_BUFFER_SIZE,"matrix_a_r\0");
for (char rend=0 ; rend < MAX_RENDERERS ; rend++ )
{
this->renderers[rend] = NULL;
}
SYNC_END;
}
/*
quit
this procedure is invoked by winamp when it tells the plugin to quit
this is usually done when winamp is closed
*/
void Output_Wumpus::Quit() {
SYNC_START;
// if a steam is open, close it
if(stream_open)
{
this->Close();
stream_open = false;
}
ConfigFile::WriteInteger(CONF_VOLUME, (int)(volume * VOLUME_DIVISOR) );
// shutdown openal
Framework::getInstance()->ALFWShutdownOpenAL();
Framework::deleteInstance();
SYNC_END;
DeleteCriticalSection(&critical_section);
}
/*
open
this procedure is invoked by winamp when it opens a file
we store the sample rate, numchannels, bitsPerSample
the open al buffers are created and an open al source
*/
int Output_Wumpus::Open(
const int samplerate,
const int numchannels,
const int bitspersamp,
const int bufferlenms,
const int prebufferms) {
SYNC_START;
/*
* catch the case where someone tried 24-bit or higher
*/
if(bitspersamp > SIXTEEN_BIT_PER_SAMPLE)
{
MessageBoxA(NULL, "This Plug-In only supports 8 and 16bit audio, please disable 24bit audio in Winamp", "Whoops", MB_OK);
SYNC_END;
return -1;
}
#ifdef _DEBUGGING
char dbg[DEBUG_BUFFER_SIZE] = {'\0'};
sprintf_s(
dbg,
DEBUG_BUFFER_SIZE,
"Open: Sample rate {%d}, Channels {%d}, Bits {%d}",
samplerate,
numchannels,
bitspersamp);
log_debug_msg(dbg, __FILE__, __LINE__);
#endif
//record the format of the data we're getting
sample_rate = samplerate;
number_of_channels = numchannels;
bits_per_sample = bitspersamp;
// reset the play position back to zero
total_written = ZERO_TIME;
total_played = ZERO_TIME;
last_pause = 0;
temp_size = 0;
memset(temp, 0, sizeof(temp));
// determine the size of the buffer
bytes_per_sample_channel = ((bits_per_sample >> SHIFT_BITS_TO_BYTES)*number_of_channels);
/*
* just incase we haven't closed properly from last time,
* make sure by closing down the renderers
*/
for ( char rend=0 ; rend < MAX_RENDERERS ; rend++ )
{
if ( renderers[rend] )
{
renderers[rend]->Close();
delete renderers[rend];
renderers[rend] = NULL;
}
}
bool use_xram = false;
if ( xram_detected == true &&
xram_enabled == true &&
alGetEnumValue("AL_EAX_RAM_FREE") > 0)
{
use_xram = true;
}
/* stereo and mono expansion
* we need to store the original number of channels
* incase we need to expand them out and need to work
* out how much "real" data is in the stream
*/
original_number_of_channels = this->number_of_channels;
if ( this->is_stereo_expanded && this->number_of_channels == 2)
{
this->number_of_channels += 2;
}
else if ( this->is_mono_expanded && this->number_of_channels == 1)
{
this->number_of_channels += 3;
}
no_renderers = 0;
if ( split_out == true )
{
for ( unsigned char rend=0 ; rend < number_of_channels ; rend++ )
{
renderers[rend] = new Output_Renderer(
conf_buffer_length,
rend);
renderers[rend]->SetXRAMEnabled(use_xram);
// if we're splitting out, there will always be '1' channel
// because we'll split multiple channels out to many single renderers
renderers[rend]->Open(samplerate,1,bitspersamp,0,0);
no_renderers++;
}
}
else
{
/*
* otherwise, create one renderer and just use that to represent
* the whole stream
*/
renderers[0] = new Output_Renderer(
conf_buffer_length,
0);
renderers[0]->SetXRAMEnabled(use_xram);
renderers[0]->Open(samplerate,this->number_of_channels,bitspersamp,0,0);
no_renderers++;
}
#ifdef _DEBUGGING
sprintf_s(
dbg,
DEBUG_BUFFER_SIZE,
"-> Using {%d} renderers",
no_renderers);
this->log_debug_msg(dbg, __FILE__, __LINE__);
#endif
// allocate some buffers
alGetError();
// set the volume for the source
this->SetVolumeInternal(volume);
// we're not playing yet because we're prebuffering
is_playing = false;
// the stream is open and ready for the main thread
stream_open = true;
// start prebuffering
pre_buffer = true;
pre_buffer_number = NO_BUFFERS_PROCESSED;
SYNC_END;
return conf_buffer_length;
}
/*
close
this procedure is invoked by winamp when the file is closed
*/
void Output_Wumpus::Close()
{
SYNC_START;
stream_open = false;
/*
* loop through each renderer we're using and close each one down,
* also, delete the renderer and reclaim any memory etc.
*/
for ( char rend=0 ; rend < MAX_RENDERERS ; rend++ )
{
if ( renderers[rend] )
{
renderers[rend]->Close();
delete renderers[rend];
renderers[rend] = NULL;
}
}
// just incase the thread has exitted, assume playing has stopped
is_playing = IS_NOT_PLAYING;
no_buffers = NO_BUFFERS;
sample_rate = NO_SAMPLE_RATE;
bits_per_sample = NO_BITS_PER_SAMPLE;
number_of_channels = NO_NUMBER_OF_CHANNELS;
current_output_time = ZERO_TIME;
current_written_time = ZERO_TIME;
total_played = ZERO_TIME;
total_written = ZERO_TIME;
last_output_time = ZERO_TIME;
last_written_time = ZERO_TIME;
temp_size = 0;
memset(temp, 0, sizeof(temp));
SYNC_END;
}
/*
write
this procedure is invoked by winamp when it attempts to write
data to the plugin
*/
int Output_Wumpus::Write(char *buf, int len)
{
SYNC_START;
// if the buffer is valid (non-NULL)
if (buf) {
#ifdef _DEBUGGING
char dbg[DEBUG_BUFFER_SIZE] = {'\0'};
sprintf_s(
dbg,
DEBUG_BUFFER_SIZE,
"Writing data {0x%08X} with length {%d}",buf, len);
log_debug_msg(dbg, __FILE__, __LINE__);
#endif
/*
* if our internal buffer isn't full, copy the new data
* to the internal buffer and quit out
*/
if ( len + temp_size < MINIMUM_BUFFER_SIZE )
{
memcpy_s(
temp + temp_size,
sizeof(temp),
buf,
len);
temp_size = temp_size + len;
#ifdef _DEBUGGING
sprintf_s(
dbg,
DEBUG_BUFFER_SIZE,
"Temp buffer increased by {%d} to {%d}",len, temp_size);
log_debug_msg(dbg, __FILE__, __LINE__);
#endif
SYNC_END;
return 0;
}
char * to_write = NULL;
if ( temp_size > 0 )
{
const unsigned int to_write_size = len + temp_size;
to_write = new char[to_write_size];
#ifdef _DEBUGGING
/* this isnt required, all the data *should* be over-written */
memset(to_write, 0x00, to_write_size);
#endif
/* copy the first buffer in */
memcpy_s(
to_write,
to_write_size,
temp,
temp_size);
#ifdef _DEBUGGING
sprintf_s(
dbg,
DEBUG_BUFFER_SIZE,
"Copying {%d} bytes from temp", temp_size);
log_debug_msg(dbg, __FILE__, __LINE__);
#endif
/* copy what we can of the second */
void * add_write = to_write + temp_size;
const size_t add_write_size = to_write_size - temp_size;
memcpy_s(
add_write,
add_write_size,
buf,
len);
buf = to_write;
len += temp_size;
temp_size = 0;
memset(temp, 0, sizeof(temp));
}
// ############## MONO EXPANSION
if(is_mono_expanded && original_number_of_channels == 1)
{
ExpandMonoToQuad( &buf, &len);
}
// ############## STEREO EXPANSION
if(is_stereo_expanded && original_number_of_channels == 2)
{
ExpandStereoToQuad( &buf, &len);
}
total_written += len;
if ( split_out == true )
{
SplitAudioToMonoChannels(buf, len);
}
else
{
/* if we're not splitting out, throw it to the renderers */
for ( char rend=0; rend < no_renderers ; rend++ )
{
renderers[rend]->Write(buf,len);
}
}
/* now that there is data in the buffers check the play
state. if nothing is playing then either a buffer under-run
has occured or this is the first time the file has been written.
it could also be a small file that the monitor thread might not see
*/
if(!pre_buffer)
{
this->CheckPlayState();
}
/*
* check the pre-buffer limit
*/
if(pre_buffer)
{
if(++pre_buffer_number == PREBUFFER_LIMIT)
{
pre_buffer = false;
}
}
}
SYNC_END;
return 0;
}
void Output_Wumpus::ExpandMonoToQuad(char ** pbuf, int * plen)
{
int len = *plen;
char * buf = *pbuf;
// we're writing out four as much data so
// increase this value by three more times
const unsigned int new_len = (len*4);
// expand buffer here
char* new_buffer = new char[new_len];
#ifdef _DEBUGGING
memset(new_buffer, 0, new_len);
#endif
// set relative value to zero
int nPos = 0;
const unsigned char sample_size =
((bits_per_sample == 8) ? ONE_BYTE_SAMPLE : TWO_BYTE_SAMPLE);
/* expand the samples out */
for(int pos=0; pos < len; pos++) {
if ( sample_size == ONE_BYTE_SAMPLE)
{
new_buffer[nPos++] = buf[pos];
new_buffer[nPos++] = buf[pos];
new_buffer[nPos++] = buf[pos];
new_buffer[nPos++] = buf[pos];
}
else
{
const short* src = (short*)(buf + pos);
short* dst_a = (short*)(new_buffer + nPos);
short* dst_b = (short*)(new_buffer + nPos + TWO_BYTE_SAMPLE);
short* dst_c = (short*)(new_buffer + nPos + (TWO_BYTE_SAMPLE * 2));
short* dst_d = (short*)(new_buffer + nPos + (TWO_BYTE_SAMPLE * 4));
(*dst_a) = (*src);
(*dst_b) = (*src);
(*dst_c) = (*src);
(*dst_d) = (*src);
nPos += (sample_size * 4);
pos += sample_size;
}
}
delete buf;
buf = new_buffer;
len = new_len;
*plen = len;
*pbuf = buf;
}
void Output_Wumpus::ExpandStereoToQuad(char ** pbuf, int * plen)
{
int len = *plen;
char * buf = *pbuf;
// we're writing out twice as much data so
// increase this value again
const unsigned int old_length = len;
const unsigned int new_len = old_length * 2;
// expand buffer here
char* new_buffer = new char[new_len];
const unsigned char sample_size =
((bits_per_sample == 8) ? TWO_BYTE_SAMPLE : FOUR_BYTE_SAMPLE);
/* expand the samples out */
int new_pos = 0;
for(unsigned int pos=0; pos < old_length; )
{
if ( sample_size == TWO_BYTE_SAMPLE )
{
short* dst = (short*)(new_buffer + new_pos);
const short* src = (const short*)(buf + pos);
*dst++ = *src;
*dst = *src;
}
else
{
int* dst = (int*)(new_buffer + new_pos);
const int* src = (const int*)(buf + pos);
*dst++ = *src;
*dst = *src;
}
new_pos += (sample_size * 2);
pos += sample_size;
}
delete buf;
len = new_len;
buf = new_buffer;
*plen = len;
*pbuf = buf;
}
void Output_Wumpus::SplitAudioToMonoChannels(const char * buf, const int len)
{
// create a table of pointers to each channels buffer
char* buffers[MAX_RENDERERS];
memset(buffers,0, sizeof(char*) * MAX_RENDERERS);
const unsigned int renderer_size = len / no_renderers;
if ( bits_per_sample == EIGHT_BIT_PER_SAMPLE )
{
/*
* create a new buffer for each renderer
*/
for ( char rend=0; rend < no_renderers ; rend++ )
{
buffers[rend] = new char[renderer_size];
memset(buffers[rend],0, renderer_size);
}
/*
* treat each sample of the source as an array - one item for each channel.
* treat each sample of the destination as a pointer and add the sample
* index to it and copy the source to it.
*/
const char* src = (const char*)buf;
for ( unsigned int sample = 0 ; sample < renderer_size ; sample++ )
{
for ( unsigned char rend=0; rend < no_renderers ; rend++ )
{
char* dst = (char*)buffers[rend];
*(char*)(dst + sample) = (src[rend]);
}
src += number_of_channels;
}
} else {
/*
* create a new buffer for each renderer
*/
for ( char rend=0; rend < no_renderers ; rend++ )
{
buffers[rend] = new char[renderer_size];
memset(buffers[rend],0, renderer_size);
}
/*
* treat each sample of the source as an array - one item for each channel.
* treat each sample of the destination as a pointer and add the sample
* index to it and copy the source to it.
*/
const short* src = (const short*)buf;
for ( unsigned int sample = 0 ; sample < (renderer_size/2) ; sample++ )
{
for ( unsigned char rend=0; rend < no_renderers ; rend++ )
{
*(short*)(((short*)buffers[rend]) + sample) = (src[rend]);
}
src += number_of_channels;
}
}
/* we write in a sepeate loop to ensure that they're close
* together, if they're in the loop above the audio may drift */
for ( char rend=0; rend < no_renderers ; rend++ )
{
renderers[rend]->Write(buffers[rend], renderer_size);
}
delete buf;
}
/*
canwrite
this procedure is invoked by winamp when it determining
if more data can be written to the plugin
*/
int Output_Wumpus::CanWrite()
{
SYNC_START;
int r = EMPTY_THE_BUFFER;
if(stream_open)
{
/*
* find out if the first renderer can accept data,
* all the renderers should have the same amount of data
* free so we only need to ask one
*/
if ( no_renderers > 0 && renderers[0] )
{
r = renderers[0]->CanWrite();
}
/*
* don't attach the temp_size as it may cause overflows
* as we may tell winamp that we want more than we can handle
*/
if ( r >= temp_size)
{
r -= temp_size;
}
}
SYNC_END;
return r;
}
/*
isplaying
invoked by winamp to determine if there is still data in the buffers
return 0 if empty
return <0> if data present
*/
int Output_Wumpus::IsPlaying()
{
SYNC_START;
if ( stream_open )
{
this->CheckProcessedBuffers();
if(!pre_buffer)
{
this->CheckPlayState();
}
}
int r = is_playing && stream_open ? IS_PLAYING : IS_NOT_PLAYING;