/
FrotzSound.cpp
1224 lines (1062 loc) · 34.1 KB
/
FrotzSound.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
/////////////////////////////////////////////////////////////////////////////
// Windows Frotz
// Frotz sound classes
/////////////////////////////////////////////////////////////////////////////
#include "StdAfx.h"
#include "FrotzSound.h"
#include "FrotzWnd.h"
#include "sndfile.h"
#include <math.h>
#include <map>
extern "C"
{
#include "blorblow.h"
void end_of_sound(zword routine);
}
#ifdef _DEBUG
#define new DEBUG_NEW
#undef THIS_FILE
static char THIS_FILE[] = __FILE__;
#endif
// The single application instance
extern FrotzApp theApp;
FrotzSound::FrotzSound(int sound, unsigned short eos, BYTE* data, int length, bool del)
{
m_sound = sound;
m_eos = eos;
m_data = data;
m_delete = del;
m_length = length;
}
FrotzSound::~FrotzSound()
{
if (m_delete)
delete[] m_data;
}
// Get the number of the sound
int FrotzSound::GetSoundNumber(void)
{
return m_sound;
}
// Get the Z-code to routine at the end
unsigned short FrotzSound::GetEndRoutine(void)
{
return m_eos;
}
// Get the sound volume in decibels
int FrotzSound::GetDecibelVolume(int volume)
{
if (volume < 1)
volume = 1;
else if (volume > 8)
volume = 8;
// Convert volume from the range 1 to 8 into decibels
double decibels = -10.0 * (log(8.0/(double)volume) / log(2.0));
if (decibels > 0.0)
decibels = 0.0;
else if (decibels < -100.0)
decibels = -100.0;
return (int)decibels;
}
// Initialize the sound engine
bool FrotzSound::Init(FrotzWnd* wnd)
{
if (CDSoundEngine::GetSoundEngine().Initialize(NULL,wnd,WM_SOUND_NOTIFY))
return true;
::MessageBox(AfxGetMainWnd()->GetSafeHwnd(),
CResString(IDS_FAIL_DIRECTSOUND),CResString(IDS_FROTZ),MB_ICONWARNING|MB_OK);
return false;
}
// Stop the sound engine
void FrotzSound::ShutDown(void)
{
Stop(0);
TextToSpeech::GetSpeechEngine().Destroy();
CDSoundEngine::GetSoundEngine().Destroy();
}
// Start playing a sound
void FrotzSound::Play(int sound, bb_map_t* map, int repeat, int volume, unsigned short eos)
{
if (CDSoundEngine::GetSoundEngine().GetStatus() == CDSoundEngine::STATUS_READY)
{
bb_result_t result;
if (bb_load_resource_snd(map,bb_method_Memory,&result,sound,NULL) == bb_err_None)
{
BYTE* data = (BYTE*)result.data.ptr;
int length = result.length;
unsigned int id = map->chunks[result.chunknum].type;
// Look for a recognized format
if (id == bb_make_id('F','O','R','M'))
{
delete m_soundEffect;
m_soundEffect = new FrotzSoundAIFF(sound,eos,data,length);
m_soundEffect->Play(repeat,volume);
}
else if (id == bb_make_id('M','O','D',' '))
{
delete m_soundMusic;
m_soundMusic = new FrotzSoundMOD(sound,eos,data,length,false);
m_soundMusic->Play(repeat,volume);
}
else if (id == bb_make_id('S','O','N','G'))
{
delete m_soundMusic;
MODBuilder build;
m_soundMusic = build.CreateMODFromSONG(sound,eos,data,length,map);
if (m_soundMusic)
m_soundMusic->Play(repeat,volume);
}
else if (id == bb_make_id('O','G','G','V'))
{
delete m_soundMusic;
m_soundMusic = new FrotzSoundOGG(sound,eos,data,length);
m_soundMusic->Play(repeat,volume);
}
}
}
}
// Stop playing a sound
void FrotzSound::Stop(int sound)
{
if (m_soundEffect != NULL)
{
if ((sound == 0) || (m_soundEffect->GetSoundNumber() == sound))
{
delete m_soundEffect;
m_soundEffect = NULL;
}
}
if (m_soundMusic != NULL)
{
if ((sound == 0) || (m_soundMusic->GetSoundNumber() == sound))
{
delete m_soundMusic;
m_soundMusic = NULL;
}
}
}
// Called to check if a sound has finished
void FrotzSound::OnNotify(void)
{
if (m_soundEffect != NULL)
{
if (m_soundEffect->IsPlaying() == false)
{
zword routine = m_soundEffect->GetEndRoutine();
delete m_soundEffect;
m_soundEffect = NULL;
end_of_sound(routine);
}
}
if (m_soundMusic != NULL)
{
if (m_soundMusic->IsPlaying() == false)
{
zword routine = m_soundMusic->GetEndRoutine();
delete m_soundMusic;
m_soundMusic = NULL;
end_of_sound(routine);
}
}
}
enum BlorbTaskState
{
BlorbTaskNotInit,
BlorbTaskNeverShow,
BlorbTaskCanShow,
BlorbTaskShown
};
static HRESULT CALLBACK BlorbTaskCallback(HWND dlg, UINT msg, WPARAM wParam, LPARAM lParam, LONG_PTR lpRefData)
{
switch (msg)
{
case TDN_BUTTON_CLICKED:
return S_OK;
case TDN_HYPERLINK_CLICKED:
::ShellExecute(0,NULL,CString(CStringW((LPCWSTR)lParam)),NULL,NULL,SW_SHOWNORMAL);
::EndDialog(dlg,IDOK);
return S_FALSE;
case TDN_VERIFICATION_CLICKED:
{
BlorbTaskState* state = (BlorbTaskState*)lpRefData;
*state = wParam ? BlorbTaskNeverShow : BlorbTaskCanShow;
theApp.WriteProfileInt("Files","Show Infocom Blorb Message",(*state == BlorbTaskNeverShow) ? 0 : 1);
}
return S_FALSE;
}
return S_FALSE;
}
// Show message about missing Infocom Blorb file, if appropriate
void FrotzSound::MsgInfocomBlorb(void)
{
static BlorbTaskState state = BlorbTaskNotInit;
if (state == BlorbTaskNotInit)
state = theApp.GetProfileInt("Files","Show Infocom Blorb Message",1) ? BlorbTaskCanShow : BlorbTaskNeverShow;
if (state != BlorbTaskCanShow)
return;
CString blorbName(story_name);
int sep = blorbName.ReverseFind('\\');
if (sep > 0)
blorbName = blorbName.Mid(sep+1);
int ext = blorbName.ReverseFind('.');
if (ext > 0)
blorbName = blorbName.Left(ext);
blorbName.Append(".blb");
CStringW title, instruction, contentFmt, content, verify;
title.LoadString(IDS_TITLE);
instruction.LoadString(IDS_INFOCOM_BLORB1);
contentFmt.LoadString(IDS_INFOCOM_BLORB2);
LPCSTR url = "https://ifarchive.org/indexes/if-archive/infocom/media/blorb/";
content.Format(contentFmt,url,url,(LPCSTR)blorbName);
verify.LoadString(IDS_INFOCOM_BLORB3);
TASKDIALOGCONFIG task = { sizeof(TASKDIALOGCONFIG), 0 };
task.hwndParent = AfxGetMainWnd()->GetSafeHwnd();
task.hInstance = ::GetModuleHandle(NULL);
task.dwFlags = TDF_ENABLE_HYPERLINKS|TDF_ALLOW_DIALOG_CANCELLATION|TDF_POSITION_RELATIVE_TO_WINDOW|TDF_SIZE_TO_CONTENT;
task.dwCommonButtons = TDCBF_OK_BUTTON;
task.pszWindowTitle = title;
task.pszMainIcon = MAKEINTRESOURCEW(IDI_INFOCOM);
task.pszMainInstruction = instruction;
task.pszContent = content;
task.pszVerificationText = verify;
task.pfCallback = BlorbTaskCallback;
task.lpCallbackData = (LONG_PTR)&state;
BOOL dontShowAgain = FALSE;
::TaskDialogIndirect(&task,NULL,NULL,&dontShowAgain);
state = BlorbTaskShown;
}
FrotzSound* FrotzSound::m_soundEffect = NULL;
FrotzSound* FrotzSound::m_soundMusic = NULL;
/////////////////////////////////////////////////////////////////////////////
// Class for AIFF sounds
/////////////////////////////////////////////////////////////////////////////
FrotzSoundAIFF::FrotzSoundAIFF(int sound, unsigned short eos, BYTE* data, int length) :
FrotzSound(sound,eos,data,length,false)
{
m_renderPtr = NULL;
m_renderMin = NULL;
m_renderMax = NULL;
m_duration = 0;
}
FrotzSoundAIFF::~FrotzSoundAIFF()
{
RemoveFromList();
}
bool FrotzSoundAIFF::Play(int repeat, int volume)
{
SampleData data;
if (GetSampleData(data) == false)
return false;
// Create a buffer
if (CreateBuffer(data.channels,(int)data.rate,data.bits) == false)
return false;
// Set the duration of the sample
if (repeat > 0)
m_duration = (DWORD)ceil((data.samples * repeat * 1000.0) / data.rate);
else
m_duration = -1;
// Set up the current position for rendering wave data
m_renderPtr = data.data;
m_renderMin = m_renderPtr;
m_renderMax = data.data + ((data.bits>>3)*data.samples*data.channels);
// Fill the buffer with sample data
m_repeat = (repeat < 0) ? -1 : repeat - 1;
if (FillBuffer(GetBufferSize()) == false)
return false;
// Set the volume for the buffer
SetBufferVolume(GetDecibelVolume(volume) * 100L);
// Start the buffer playing
return PlayBuffer(false);
}
bool FrotzSoundAIFF::IsPlaying(void)
{
return m_Active;
}
// Write sample data into the supplied PCM sample buffers
void FrotzSoundAIFF::WriteSampleData(unsigned char* sample, int len)
{
switch (m_Format.wBitsPerSample)
{
case 8:
{
for (int i = 0; i < len; i++)
{
if (CheckRenderPtr())
*(sample++) = (unsigned char)((*(m_renderPtr++)) ^ 0x80);
else
*(sample++) = 0x80;
}
}
break;
case 16:
{
unsigned short* writePtr = (unsigned short*)sample;
for (int i = 0; i < len; i += 2)
{
if (CheckRenderPtr())
{
*(writePtr++) = (unsigned short)
(((*m_renderPtr)<<8) | *(m_renderPtr+1));
m_renderPtr += 2;
}
else
*(writePtr++) = 0;
}
}
break;
}
}
// Test that the current point into the AIFF buffer is valid
bool FrotzSoundAIFF::CheckRenderPtr(void)
{
if (m_renderPtr >= m_renderMax)
{
// Fail if this is the end of the last repeat
if (m_repeat == 0)
return false;
// If not looping forever, decrement the repeat counter
if (m_repeat > 0)
m_repeat--;
// Reset the pointer
m_renderPtr = m_renderMin;
}
return true;
}
// Check if the sound has finished playing
bool FrotzSoundAIFF::IsSoundOver(DWORD tick)
{
if (m_Active == false)
return true;
// Check if sound is playing forever
if (m_duration < 0)
return false;
return (tick > m_StartTime + m_duration);
}
// Get a type identifier for the sound
int FrotzSoundAIFF::GetType(void)
{
return (int)'A';
}
// Get details of the sample
bool FrotzSoundAIFF::GetSampleData(SampleData& data)
{
if (m_data == NULL)
return false;
// Check for AIFF header
if (strncmp((char*)m_data,"FORM",4) != 0)
return false;
if (strncmp((char*)(m_data+8),"AIFF",4) != 0)
return false;
// Find the COMM chunk
BYTE* chunk = FindChunk("COMM");
if (chunk == NULL)
return false;
// Read in details of the sample
data.channels = ReadShort(chunk);
data.samples = ReadLong(chunk+2);
data.bits = ReadShort(chunk+6);
data.rate = ReadExtended(chunk+8);
// Find the SSND chunk
data.data = FindChunk("SSND");
if (data.data == NULL)
return false;
// Set up default repeat information
data.repeat1 = 0;
data.repeat2 = 0;
// Find the INST chunk
chunk = FindChunk("INST");
if (chunk != NULL)
{
// Look for a sustain loop
if (ReadShort(chunk+8) != 0)
{
unsigned short mark1 = ReadShort(chunk+10);
unsigned short mark2 = ReadShort(chunk+12);
// Find the MARK chunk
chunk = FindChunk("MARK");
if (chunk != NULL)
{
unsigned short markers = ReadShort(chunk);
BYTE* mark = chunk+2;
for (int i = 0; i < markers; i++)
{
unsigned short id = ReadShort(mark);
unsigned long pos = ReadLong(mark+2);
if (id == mark1)
data.repeat1 = pos;
else if (id == mark2)
data.repeat2 = pos;
unsigned char nameLen = mark[6]+1;
if ((nameLen % 2) == 1)
nameLen++;
mark += nameLen+6;
}
}
}
}
return true;
}
// Find an AIFF chunk
BYTE* FrotzSoundAIFF::FindChunk(LPCTSTR chunk)
{
BYTE* data = m_data+12;
while (true)
{
if (strncmp((char*)data,chunk,4) == 0)
return data+8;
// Move to the next chunk
data += ReadLong(data+4)+8;
if ((data - m_data) > m_length-8)
break;
}
return NULL;
}
unsigned short FrotzSoundAIFF::ReadShort(const unsigned char *bytes)
{
return (unsigned short)(
((unsigned short)(bytes[0] & 0xFF) << 8) |
((unsigned short)(bytes[1] & 0xFF)));
}
unsigned long FrotzSoundAIFF::ReadLong(const unsigned char *bytes)
{
return (unsigned long)(
((unsigned long)(bytes[0] & 0xFF) << 24) |
((unsigned long)(bytes[1] & 0xFF) << 16) |
((unsigned long)(bytes[2] & 0xFF) << 8) |
((unsigned long)(bytes[3] & 0xFF)));
}
/*
* Copyright (C) 1988-1991 Apple Computer, Inc.
* All rights reserved.
*
* Machine-independent I/O routines for IEEE floating-point numbers.
*
* NaN's and infinities are converted to HUGE_VAL or HUGE, which
* happens to be infinity on IEEE machines. Unfortunately, it is
* impossible to preserve NaN's in a machine-independent way.
* Infinities are, however, preserved on IEEE machines.
*
* These routines have been tested on the following machines:
* Apple Macintosh, MPW 3.1 C compiler
* Apple Macintosh, THINK C compiler
* Silicon Graphics IRIS, MIPS compiler
* Cray X/MP and Y/MP
* Digital Equipment VAX
*
* Implemented by Malcolm Slaney and Ken Turkowski.
*
* Malcolm Slaney contributions during 1988-1990 include big- and little-
* endian file I/O, conversion to and from Motorola's extended 80-bit
* floating-point format, and conversions to and from IEEE single-
* precision floating-point format.
*
* In 1991, Ken Turkowski implemented the conversions to and from
* IEEE double-precision format, added more precision to the extended
* conversions, and accommodated conversions involving +/- infinity,
* NaN's, and denormalized numbers.
*/
#define UnsignedToFloat(u) (((double)((long)(u - 2147483647L - 1))) + 2147483648.0)
double FrotzSoundAIFF::ReadExtended(const unsigned char *bytes)
{
double f;
int expon;
unsigned long hiMant, loMant;
expon = ((bytes[0] & 0x7F) << 8) | (bytes[1] & 0xFF);
hiMant = ReadLong(bytes+2);
loMant = ReadLong(bytes+6);
if (expon == 0 && hiMant == 0 && loMant == 0)
f = 0;
else
{
if (expon == 0x7FFF) /* Infinity or NaN */
f = -1;
else
{
expon -= 16383;
f = ldexp(UnsignedToFloat(hiMant),expon -= 31);
f += ldexp(UnsignedToFloat(loMant),expon -= 32);
}
}
if (bytes[0] & 0x80)
return -f;
return f;
}
/////////////////////////////////////////////////////////////////////////////
// Class for MOD music
/////////////////////////////////////////////////////////////////////////////
FrotzSoundMOD::FrotzSoundMOD(int sound, unsigned short eos, BYTE* data, int length, bool del) :
FrotzSound(sound,eos,data,length,del)
{
m_player = new CSoundFile;
m_duration = 0;
}
FrotzSoundMOD::~FrotzSoundMOD()
{
RemoveFromList();
delete m_player;
}
bool FrotzSoundMOD::Play(int repeat, int volume)
{
if (m_data == NULL)
return false;
// Only one MOD can be playing at any given time, so stop
// any currently playing MODs
CDSoundEngine& engine = CDSoundEngine::GetSoundEngine();
engine.StopSounds(GetType());
WAVEFORMATEX& format = engine.GetPrimaryFormat();
CSoundFile::SetWaveConfig(format.nSamplesPerSec,format.wBitsPerSample,format.nChannels);
CSoundFile::SetWaveConfigEx(TRUE,FALSE,FALSE,TRUE,TRUE,TRUE,FALSE);
// Load the song into MODPlug
if (!m_player->Create(m_data,m_length))
return false;
// Set the number of repeats and the duration
if (repeat > 0)
{
m_player->SetRepeatCount(repeat-1);
m_duration = m_player->GetSongTime() * repeat;
}
else
{
m_player->SetRepeatCount(-1);
m_duration = -1;
}
// Create a buffer
if (CreateBuffer(format.nChannels,format.nSamplesPerSec,format.wBitsPerSample) == false)
return false;
// Fill the buffer with sample data
if (FillBuffer(GetBufferSize()) == false)
return false;
// Set the volume for the buffer
SetBufferVolume(GetDecibelVolume(volume) * 100L);
// Start the buffer playing
return PlayBuffer(false);
}
bool FrotzSoundMOD::IsPlaying(void)
{
return m_Active;
}
void FrotzSoundMOD::WriteSampleData(unsigned char* sample, int len)
{
WAVEFORMATEX& format = CDSoundEngine::GetSoundEngine().GetPrimaryFormat();
// Render audio
int bytes = m_player->Read(sample,len) * format.nBlockAlign;
// Has the MOD been completely rendered?
if (bytes < len)
{
for (int i = bytes; i < len; i++)
{
sample[i] = (unsigned char)
((format.wBitsPerSample == 8) ? 0x80 : 0x00);
}
}
}
// Check if the sound has finished playing
bool FrotzSoundMOD::IsSoundOver(DWORD tick)
{
if (m_Active == false)
return true;
// Check if sound is playing forever
if (m_duration < 0)
return false;
return (tick > m_StartTime + m_duration);
}
// Get a type identifier for the sound
int FrotzSoundMOD::GetType(void)
{
return (int)'M';
}
/////////////////////////////////////////////////////////////////////////////
// Class to build a MOD from a SONG and AIFF samples
/////////////////////////////////////////////////////////////////////////////
MODBuilder::MODBuilder() : m_mod(NULL)
{
}
MODBuilder::~MODBuilder()
{
delete[] m_mod;
int index = 0;
FrotzSoundAIFF* aiff = NULL;
// Discard all the loaded samples
POSITION pos = m_samples.GetStartPosition();
while (pos != NULL)
{
m_samples.GetNextAssoc(pos,index,aiff);
delete aiff;
}
}
// Convert a SONG and AIFF samples into a MOD
FrotzSoundMOD* MODBuilder::CreateMODFromSONG(int sound, unsigned short eos, BYTE* data, int length, bb_map_t* map)
{
const int SAMPLE_NAME = 0;
const int SAMPLE_LENGTH = 22;
const int SAMPLE_REPEAT_POS = 26;
const int SAMPLE_REPEAT_LEN = 28;
const int SAMPLE_SIZE = 30;
int modLen = length;
// Loop over the samples
for (int i = 0; i < 31; i++)
{
BYTE* sample = data + 20 + i*SAMPLE_SIZE;
if (*(sample + SAMPLE_NAME) != 0)
{
// Get the sample resource index
int index = 0;
if (sscanf((const char*)(sample + SAMPLE_NAME),"SND%d",&index) != 1)
return NULL;
// Attempt to load the sample
bb_result_t result;
if (bb_load_resource_snd(map,bb_method_Memory,&result,index,NULL) != bb_err_None)
return NULL;
if (map->chunks[result.chunknum].type != bb_make_id('F','O','R','M'))
return NULL;
FrotzSoundAIFF* aiff = new FrotzSoundAIFF(index,0,(BYTE*)result.data.ptr,result.length);
m_samples[index] = aiff;
// Get details of the sample
FrotzSoundAIFF::SampleData data;
if (aiff->GetSampleData(data) == false)
return NULL;
// Add to the total size of the MOD
modLen += data.samples + 2;
}
}
// Create a buffer for the MOD and put the SONG data at the start
m_mod = new BYTE[modLen];
::CopyMemory(m_mod,data,length);
// Copy the samples into the buffer
int writePos = length;
for (int i = 0; i < 31; i++)
{
BYTE* sample = data + 20 + i*SAMPLE_SIZE;
if (*(sample + SAMPLE_NAME) != 0)
{
// Get the sample resource index
int index = 0;
sscanf((const char*)(sample + SAMPLE_NAME),"SND%d",&index);
// Get the sample
FrotzSoundAIFF* aiff = m_samples[index];
FrotzSoundAIFF::SampleData data;
aiff->GetSampleData(data);
// Update the sample length
WriteHeaderValue(i,SAMPLE_LENGTH,data.samples+2);
// Update repeat information
if ((data.repeat1 != 0) && (data.repeat2 != 0))
{
WriteHeaderValue(i,SAMPLE_REPEAT_POS,data.repeat1+2);
WriteHeaderValue(i,SAMPLE_REPEAT_LEN,data.repeat2-data.repeat1);
}
// Copy into the buffer, converting to 8-bit mono
*(m_mod+(writePos++)) = 0;
*(m_mod+(writePos++)) = 0;
for (unsigned long j = 0; j < data.samples; j++)
*(m_mod+writePos+j) = *(data.data+(j*data.channels*(data.bits>>3)));
writePos += data.samples;
}
}
FrotzSoundMOD* mod = new FrotzSoundMOD(sound,eos,m_mod,modLen,true);
m_mod = NULL;
return mod;
}
// Write a value into the MOD header
void MODBuilder::WriteHeaderValue(int sample, int offset, unsigned long value)
{
const int SAMPLE_SIZE = 30;
unsigned short val = (unsigned short)(value>>1);
BYTE* header = m_mod + 20 + (sample*SAMPLE_SIZE);
header[offset] = (BYTE)(val >> 8);
header[offset+1] = (BYTE)(val - (header[offset] << 8));
}
/////////////////////////////////////////////////////////////////////////////
// Class for Ogg Vorbis music
/////////////////////////////////////////////////////////////////////////////
FrotzSoundOGG::FrotzSoundOGG(int sound, unsigned short eos, BYTE* data, int length) :
FrotzSound(sound,eos,data,length,false)
{
m_renderPtr = NULL;
m_duration = 0;
}
FrotzSoundOGG::~FrotzSoundOGG()
{
RemoveFromList();
if (m_streamOpen)
ov_clear(&m_stream);
}
bool FrotzSoundOGG::Play(int repeat, int volume)
{
m_renderPtr = m_data;
if (m_renderPtr == NULL)
return false;
// Open the stream
ov_callbacks VorbisCBs;
VorbisCBs.read_func = VorbisRead;
VorbisCBs.close_func = VorbisClose;
VorbisCBs.seek_func = VorbisSeek;
VorbisCBs.tell_func = VorbisTell;
if (ov_open_callbacks(this,&m_stream,NULL,0,VorbisCBs) < 0)
return false;
m_streamOpen = true;
vorbis_info* info = ov_info(&m_stream,-1);
// Create a buffer
if (CreateBuffer(info->channels,info->rate,16) == false)
return false;
// Set the duration of the sample
if (repeat > 0)
m_duration = (DWORD)ceil(ov_time_total(&m_stream,-1) * repeat * 1000.0);
else
m_duration = -1;
// Fill the buffer with sample data
m_repeat = (repeat < 0) ? -1 : repeat - 1;
if (FillBuffer(GetBufferSize()) == false)
return false;
// Set the volume for the buffer
SetBufferVolume(GetDecibelVolume(volume) * 100L);
// Start the buffer playing
return PlayBuffer(false);
}
bool FrotzSoundOGG::IsPlaying(void)
{
return m_Active;
}
// Write sample data into the supplied PCM sample buffers
void FrotzSoundOGG::WriteSampleData(unsigned char* sample, int len)
{
int stream, current = 0;
while (current < len)
{
long read =
ov_read(&m_stream,(char*)(sample+current),(len-current),0,2,1,&stream);
if (read > 0)
current += read;
else
{
if (m_repeat > 0)
{
ov_pcm_seek(&m_stream,0);
m_repeat--;
}
else if (m_repeat == -1)
ov_pcm_seek(&m_stream,0);
else
{
while (current < len)
sample[current++] = 0;
}
}
}
}
// Check if the sound has finished playing
bool FrotzSoundOGG::IsSoundOver(DWORD tick)
{
if (m_Active == false)
return true;
// Check if sound is playing forever
if (m_duration < 0)
return false;
return (tick > m_StartTime + m_duration);
}
// Get a type identifier for the sound
int FrotzSoundOGG::GetType(void)
{
return (int)'O';
}
size_t FrotzSoundOGG::VorbisRead(void* ptr, size_t byteSize, size_t sizeToRead, void* src)
{
FrotzSoundOGG* sound = (FrotzSoundOGG*)src;
int read = sizeToRead;
int maxRead = (sound->m_data + sound->m_length - sound->m_renderPtr) / byteSize;
if (read > maxRead)
read = maxRead;
memcpy(ptr,sound->m_renderPtr,read * byteSize);
sound->m_renderPtr += read * byteSize;
return read;
}
int FrotzSoundOGG::VorbisSeek(void* src, ogg_int64_t offset, int whence)
{
FrotzSoundOGG* sound = (FrotzSoundOGG*)src;
switch (whence)
{
case SEEK_SET:
sound->m_renderPtr = sound->m_data + offset;
return 0;
case SEEK_CUR:
sound->m_renderPtr += offset;
return 0;
case SEEK_END:
sound->m_renderPtr = sound->m_data + sound->m_length;
return 0;
}
return -1;
}
int FrotzSoundOGG::VorbisClose(void*)
{
return 0;
}
long FrotzSoundOGG::VorbisTell(void* src)
{
FrotzSoundOGG* sound = (FrotzSoundOGG*)src;
return sound->m_renderPtr - sound->m_data;
}
/////////////////////////////////////////////////////////////////////
// Interface to SAPI 5
/////////////////////////////////////////////////////////////////////
#ifndef __ISpVoice_INTERFACE_DEFINED__
typedef enum SPVPRIORITY {} SPVPRIORITY;
typedef enum SPEVENTENUM {} SPEVENTENUM;
typedef enum SPDATAKEYLOCATION {} SPDATAKEYLOCATION;
typedef enum SPEAKFLAGS {
SPF_ASYNC = (1L<<0)
} SPEAKFLAGS;
MIDL_INTERFACE("5EFF4AEF-8487-11D2-961C-00C04F8EE628")
ISpNotifySource : public IUnknown
{
public:
virtual HRESULT STDMETHODCALLTYPE SetNotifySink(IUnknown*) = 0;
virtual HRESULT STDMETHODCALLTYPE SetNotifyWindowMessage(HWND, UINT, WPARAM, LPARAM) = 0;
virtual HRESULT STDMETHODCALLTYPE SetNotifyCallbackFunction(VOID*, WPARAM, LPARAM) = 0;
virtual HRESULT STDMETHODCALLTYPE SetNotifyCallbackInterface(IUnknown*, WPARAM, LPARAM) = 0;
virtual HRESULT STDMETHODCALLTYPE SetNotifyWin32Event(VOID) = 0;
virtual HRESULT STDMETHODCALLTYPE WaitForNotifyEvent(DWORD) = 0;
virtual HANDLE STDMETHODCALLTYPE GetNotifyEventHandle(VOID) = 0;
};
MIDL_INTERFACE("BE7A9CCE-5F9E-11D2-960F-00C04F8EE628")
ISpEventSource : public ISpNotifySource
{
public:
virtual HRESULT STDMETHODCALLTYPE SetInterest(ULONGLONG, ULONGLONG) = 0;
virtual HRESULT STDMETHODCALLTYPE GetEvents(ULONG, VOID*, ULONG*) = 0;
virtual HRESULT STDMETHODCALLTYPE GetInfo(VOID*) = 0;
};
MIDL_INTERFACE("6C44DF74-72B9-4992-A1EC-EF996E0422D4")
ISpVoice : public ISpEventSource
{
public:
virtual HRESULT STDMETHODCALLTYPE SetOutput(IUnknown*, BOOL) = 0;
virtual HRESULT STDMETHODCALLTYPE GetOutputObjectToken(IUnknown**) = 0;
virtual HRESULT STDMETHODCALLTYPE GetOutputStream(IUnknown**) = 0;
virtual HRESULT STDMETHODCALLTYPE Pause(VOID) = 0;
virtual HRESULT STDMETHODCALLTYPE Resume(VOID) = 0;
virtual HRESULT STDMETHODCALLTYPE SetVoice(IUnknown*) = 0;
virtual HRESULT STDMETHODCALLTYPE GetVoice(IUnknown**) = 0;
virtual HRESULT STDMETHODCALLTYPE Speak(LPCWSTR, DWORD, ULONG*) = 0;
virtual HRESULT STDMETHODCALLTYPE SpeakStream(IStream*, DWORD, ULONG*) = 0;
virtual HRESULT STDMETHODCALLTYPE GetStatus(VOID*, LPWSTR*) = 0;
virtual HRESULT STDMETHODCALLTYPE Skip(LPCWSTR, long, ULONG*) = 0;
virtual HRESULT STDMETHODCALLTYPE SetPriority(SPVPRIORITY) = 0;
virtual HRESULT STDMETHODCALLTYPE GetPriority(SPVPRIORITY*) = 0;
virtual HRESULT STDMETHODCALLTYPE SetAlertBoundary(SPEVENTENUM) = 0;
virtual HRESULT STDMETHODCALLTYPE GetAlertBoundary(SPEVENTENUM*) = 0;
virtual HRESULT STDMETHODCALLTYPE SetRate(long) = 0;
virtual HRESULT STDMETHODCALLTYPE GetRate(long*) = 0;
virtual HRESULT STDMETHODCALLTYPE SetVolume(USHORT) = 0;
virtual HRESULT STDMETHODCALLTYPE GetVolume(USHORT*) = 0;
virtual HRESULT STDMETHODCALLTYPE WaitUntilDone(ULONG) = 0;
virtual HRESULT STDMETHODCALLTYPE SetSyncSpeakTimeout(ULONG) = 0;
virtual HRESULT STDMETHODCALLTYPE GetSyncSpeakTimeout(ULONG*) = 0;
virtual HANDLE STDMETHODCALLTYPE SpeakCompleteEvent(VOID) = 0;
virtual HRESULT STDMETHODCALLTYPE IsUISupported(LPCWSTR, VOID*, ULONG, BOOL*) = 0;
virtual HRESULT STDMETHODCALLTYPE DisplayUI(HWND, LPCWSTR, LPCWSTR, VOID*, ULONG) = 0;
};
MIDL_INTERFACE("14056581-E16C-11D2-BB90-00C04F8EE6C0")
ISpDataKey : public IUnknown
{
public:
virtual HRESULT STDMETHODCALLTYPE SetData(LPCWSTR, ULONG, const BYTE*) = 0;
virtual HRESULT STDMETHODCALLTYPE GetData(LPCWSTR, ULONG*, BYTE*) = 0;
virtual HRESULT STDMETHODCALLTYPE SetStringValue(LPCWSTR, LPCWSTR) = 0;