1 unit f_addresource_sound
;
3 {$INCLUDE ../shared/a_modes.inc}
8 LCLIntf
, LCLType
, LMessages
, SysUtils
, Variants
, Classes
,
9 Graphics
, Controls
, Forms
, Dialogs
, f_addresource
,
10 ExtCtrls
, StdCtrls
, spectrum
, Buttons
, ComCtrls
, utils
;
13 TAddSoundForm
= class (TAddResourceForm
)
20 procedure FormCreate(Sender
: TObject
);
21 procedure bbPlayClick(Sender
: TObject
);
22 procedure TimerTimer(Sender
: TObject
);
23 procedure FormDestroy(Sender
: TObject
);
24 procedure FormClose(Sender
: TObject
; var Action
: TCloseAction
);
25 procedure bbStopClick(Sender
: TObject
);
26 procedure FormActivate(Sender
: TObject
);
27 procedure bOKClick(Sender
: TObject
);
28 procedure bEmptyClick(Sender
: TObject
);
31 FSpectrum
: TMiniSpectrum
;
34 procedure ShowSpectrum();
37 property SetResource
: String read FSetResource write FSetResource
;
41 AddSoundForm
: TAddSoundForm
;
46 BinEditor
, WADEDITOR
, e_log
, f_main
, g_language
47 {$IFNDEF NOSOUND}, fmod
, fmodtypes
, fmoderrors
;{$ELSE};{$ENDIF}
57 FMOD_SYSTEM
= Pointer;
58 FMOD_CHANNEL
= Pointer;
60 FMOD_CREATESOUNDEXINFO
= Pointer;
61 FMOD_RESULT
= Integer;
65 F_System
: FMOD_SYSTEM
;
66 SoundData
: Pointer = nil;
67 Sound
: FMOD_SOUND
= nil;
68 Channel
: FMOD_CHANNEL
= nil;
69 Playing
: Boolean = False;
71 procedure TAddSoundForm
.FormCreate(Sender
: TObject
);
83 res
:= FMOD_System_Create(F_System
);
84 if res
<> FMOD_OK
then
85 raise Exception
.Create('FMOD_System_Create failed!');
87 res
:= FMOD_System_GetVersion(F_System
, ver
);
88 if res
<> FMOD_OK
then
89 raise Exception
.Create('FMOD_System_GetVersion failed!');
91 if ver
< FMOD_VERSION
then
92 raise Exception
.Create('FMOD version is too old! Need '+IntToStr(FMOD_VERSION
));
94 res
:= FMOD_System_SetOutput(F_System
, FMOD_OUTPUTTYPE_WINMM
);
95 if res
<> FMOD_OK
then
96 raise Exception
.Create('FMOD_System_SetOutput failed!');
98 res
:= FMOD_System_SetSoftwareFormat(F_System
, 48000,
99 FMOD_SOUND_FORMAT_PCM16
, 0, 0, FMOD_DSP_RESAMPLER_LINEAR
);
100 if res
<> FMOD_OK
then
101 raise Exception
.Create('FMOD_System_SetSoftwareFormat failed!');
103 res
:= FMOD_System_Init(F_System
, 64, FMOD_INIT_NORMAL
, nil);
104 if res
<> FMOD_OK
then
105 raise Exception
.Create('FMOD_System_Init failed!');
108 Application
.MessageBox(FMOD_ErrorString(res
), 'Initialization', MB_OK
or MB_ICONHAND
);
113 FSpectrum
:= TMiniSpectrum
.Create(pSpectrum
);
114 FSpectrum
.Align
:= alClient
;
115 FSpectrum
.Enabled
:= True;
116 FSpectrum
.Style
:= ssBlock
;
119 function CreateSoundWAD(Resource
: String): Boolean;
122 FileName
, SectionName
, ResourceName
: String;
125 soundExInfo
: FMOD_CREATESOUNDEXINFO
;
134 g_ProcessResourceStr(Resource
, FileName
, SectionName
, ResourceName
);
136 WAD
:= TWADEditor_1
.Create
;
137 WAD
.ReadFile(FileName
);
139 if WAD
.GetResource(SectionName
, ResourceName
, SoundData
, ResLength
) then
141 sz
:= SizeOf(FMOD_CREATESOUNDEXINFO
);
142 FillMemory(@soundExInfo
, sz
, 0);
143 soundExInfo
.cbsize
:= sz
;
144 soundExInfo
.length
:= LongWord(ResLength
);
146 res
:= FMOD_System_CreateStream(F_System
, SoundData
,
147 FMOD_LOOP_OFF
or FMOD_2D
or FMOD_OPENMEMORY
,
148 @soundExInfo
, Sound
);
150 if res
<> FMOD_OK
then
152 e_WriteLog(Format('Error creating sound %s', [Resource
]), MSG_WARNING
);
153 e_WriteLog(FMOD_ErrorString(res
), MSG_WARNING
);
160 e_WriteLog(Format('Error loading sound %s', [Resource
]), MSG_WARNING
);
161 e_WriteLog(Format('WAD Reader error: %s', [WAD
.GetLastErrorStr
]), MSG_WARNING
);
171 procedure TAddSoundForm
.bbPlayClick(Sender
: TObject
);
178 if FResourceSelected
then
183 if not CreateSoundWAD(FFullResourceName
) then
186 res
:= FMOD_System_PlaySound(F_System
, FMOD_CHANNEL_FREE
,
187 Sound
, False, Channel
);
188 if res
<> FMOD_OK
then
190 Application
.MessageBox(FMOD_ErrorString(res
),
191 PChar(_lc
[I_MSG_SOUND_ERROR
]),
192 MB_OK
or MB_ICONHAND
);
198 FMOD_Channel_SetVolume(Channel
, 1.0);
200 FSpectrum
.SetChannel(Channel
);
205 procedure TAddSoundForm
.ShowSpectrum
;
207 if FSpectrum
.Enabled
then
211 procedure TAddSoundForm
.TimerTimer(Sender
: TObject
);
219 FMOD_System_Update(F_System
);
223 res
:= FMOD_Channel_IsPlaying(Channel
, b
);
224 if (res
<> FMOD_OK
) or (not b
) then
229 procedure TAddSoundForm
.FormDestroy(Sender
: TObject
);
238 res
:= FMOD_System_Close(F_System
);
239 if res
<> FMOD_OK
then
241 e_WriteLog('Error closing FMOD system!', MSG_FATALERROR
);
242 e_WriteLog(FMOD_ErrorString(res
), MSG_FATALERROR
);
246 res
:= FMOD_System_Release(F_System
);
247 if res
<> FMOD_OK
then
249 e_WriteLog('Error releasing FMOD system!', MSG_FATALERROR
);
250 e_WriteLog(FMOD_ErrorString(res
), MSG_FATALERROR
);
255 procedure Sound_StopRelease();
259 if Channel
<> nil then
260 FMOD_Channel_Stop(Channel
);
263 FMOD_Sound_Release(Sound
);
265 if SoundData
<> nil then
273 procedure TAddSoundForm
.FormClose(Sender
: TObject
;
274 var Action
: TCloseAction
);
278 Timer
.Enabled
:= False;
280 FSpectrum
.SetChannel(nil);
284 procedure TAddSoundForm
.bbStopClick(Sender
: TObject
);
288 FSpectrum
.SetChannel(nil);
292 procedure TAddSoundForm
.FormActivate(Sender
: TObject
);
294 FileName
, SectionName
, ResourceName
: String;
300 Timer
.Enabled
:= True;
302 // Уже есть выбранный ресурс:
303 if FSetResource
<> '' then
305 g_ProcessResourceStr(FSetResource
, FileName
, SectionName
, ResourceName
);
307 if FileName
= '' then
308 FileName
:= _lc
[I_WAD_SPECIAL_MAP
];
310 if SectionName
= '' then
314 a
:= cbWADList
.Items
.IndexOf(win2utf(FileName
));
317 cbWADList
.ItemIndex
:= a
;
318 cbWADList
.OnChange(nil);
322 a
:= cbSectionsList
.Items
.IndexOf(win2utf(SectionName
));
325 cbSectionsList
.ItemIndex
:= a
;
326 cbSectionsList
.OnChange(nil);
330 a
:= lbResourcesList
.Items
.IndexOf(win2utf(ResourceName
));
333 lbResourcesList
.ItemIndex
:= a
;
334 lbResourcesList
.OnClick(nil);
339 procedure TAddSoundForm
.bOKClick(Sender
: TObject
);
346 procedure TAddSoundForm
.bEmptyClick(Sender
: TObject
);