DEADSOFTWARE

more sound fixes: it can leak now, but at least it doesn't segfault
[d2df-sdl.git] / src / engine / e_sound.pas
1 unit e_sound;
3 interface
5 uses
6 sdl2,
7 SDL2_mixer,
8 e_log,
9 SysUtils;
11 type
12 TSoundRec = record
13 Data: Pointer;
14 Sound: PMix_Chunk;
15 Music: PMix_Music;
16 isMusic: Boolean;
17 nRefs: Integer;
18 end;
20 TBasicSound = class (TObject)
21 private
22 FChanNum: Integer; // <0: no channel allocated
24 protected
25 FID: DWORD;
26 FMusic: Boolean;
27 FPosition: DWORD;
28 FPriority: Integer;
30 function RawPlay(Pan: Single; Volume: Single; aPos: DWORD): Boolean;
31 function GetChan (): Integer;
33 property Channel: Integer read GetChan;
35 public
36 constructor Create();
37 destructor Destroy(); override;
38 procedure SetID(ID: DWORD);
39 procedure FreeSound();
40 function IsPlaying(): Boolean;
41 procedure Stop();
42 function IsPaused(): Boolean;
43 procedure Pause(Enable: Boolean);
44 function GetVolume(): Single;
45 procedure SetVolume(Volume: Single);
46 function GetPan(): Single;
47 procedure SetPan(Pan: Single);
48 function IsMuted(): Boolean;
49 procedure Mute(Enable: Boolean);
50 function GetPosition(): DWORD;
51 procedure SetPosition(aPos: DWORD);
52 procedure SetPriority(priority: Integer);
53 end;
55 const
56 NO_SOUND_ID = DWORD(-1);
58 function e_InitSoundSystem(): Boolean;
60 function e_LoadSound(FileName: string; var ID: DWORD; isMusic: Boolean): Boolean;
61 function e_LoadSoundMem(pData: Pointer; Length: Integer; var ID: DWORD; isMusic: Boolean): Boolean;
63 // returns channel number or -1
64 function e_PlaySound(ID: DWORD): Integer;
65 function e_PlaySoundPan(ID: DWORD; Pan: Single): Integer;
66 function e_PlaySoundVolume(ID: DWORD; Volume: Single): Integer;
67 function e_PlaySoundPanVolume(ID: DWORD; Pan, Volume: Single): Integer;
69 procedure e_ModifyChannelsVolumes(SoundMod: Single; setMode: Boolean);
70 procedure e_MuteChannels(Enable: Boolean);
71 procedure e_StopChannels();
73 procedure e_DeleteSound(ID: DWORD);
74 procedure e_RemoveAllSounds();
75 procedure e_ReleaseSoundSystem();
76 procedure e_SoundUpdate();
78 var
79 e_SoundsArray: array of TSoundRec = nil;
81 implementation
83 uses
84 g_window, g_options, BinEditor;
86 const
87 N_CHANNELS = 512;
88 N_MUSCHAN = N_CHANNELS+42;
90 type
91 TChanInfo = record
92 id: DWORD; // sound id
93 muted: Boolean;
94 oldvol: Integer; // for muted
95 pan: Single;
96 end;
98 var
99 SoundMuted: Boolean = False;
100 SoundInitialized: Boolean = False;
101 ChanSIds: array[0..N_CHANNELS] of TChanInfo;
102 MusVolume: Integer = MIX_MAX_VOLUME;
105 procedure chanFinished (chan: Integer); cdecl;
106 begin
107 //e_WriteLog(Format('chanFinished: %d', [chan]), MSG_NOTIFY);
108 if (chan >= 0) and (chan < N_CHANNELS) then
109 begin
110 if ChanSIds[chan].id <> NO_SOUND_ID then
111 begin
112 if (ChanSIds[chan].id <= High(e_SoundsArray)) and (e_SoundsArray[ChanSIds[chan].id].nRefs > 0) then
113 begin
114 Dec(e_SoundsArray[ChanSIds[chan].id].nRefs);
115 end;
116 ChanSIds[chan].id := NO_SOUND_ID;
117 end;
118 end;
119 end;
122 procedure dumpMusicType (ms: PMix_Music);
123 begin
124 if ms = nil then
125 begin
126 e_WriteLog('MUSIC FORMAT: NONE', MSG_NOTIFY);
127 end
128 else
129 begin
130 case Mix_GetMusicType(ms^) of
131 TMix_MusicType.MUS_NONE:
132 e_WriteLog('MUSIC FORMAT: NONE', MSG_NOTIFY);
133 TMix_MusicType.MUS_CMD:
134 e_WriteLog('MUSIC FORMAT: CMD', MSG_NOTIFY);
135 TMix_MusicType.MUS_WAV:
136 e_WriteLog('MUSIC FORMAT: WAV', MSG_NOTIFY);
137 TMix_MusicType.MUS_MOD:
138 e_WriteLog('MUSIC FORMAT: MOD', MSG_NOTIFY);
139 TMix_MusicType.MUS_MID:
140 e_WriteLog('MUSIC FORMAT: MID', MSG_NOTIFY);
141 TMix_MusicType.MUS_OGG:
142 e_WriteLog('MUSIC FORMAT: OGG', MSG_NOTIFY);
143 TMix_MusicType.MUS_MP3:
144 e_WriteLog('MUSIC FORMAT: MP3', MSG_NOTIFY);
145 TMix_MusicType.MUS_MP3_MAD:
146 e_WriteLog('MUSIC FORMAT: MP3_MAD', MSG_NOTIFY);
147 TMix_MusicType.MUS_FLAC:
148 e_WriteLog('MUSIC FORMAT: FLAC', MSG_NOTIFY);
149 TMix_MusicType.MUS_MODPLUG:
150 e_WriteLog('MUSIC FORMAT: MODPLUG', MSG_NOTIFY);
151 otherwise
152 e_WriteLog('MUSIC FORMAT: UNKNOWN', MSG_NOTIFY);
153 end;
154 end;
155 end;
157 function e_InitSoundSystem(): Boolean;
158 var
159 res, i: Integer;
160 begin
161 if SoundInitialized then begin Result := true; Exit end;
163 Result := False;
164 SoundInitialized := False;
166 // wow, this is actually MIDI player!
167 // we need module player
168 res := Mix_Init(MIX_INIT_FLAC or MIX_INIT_MOD or MIX_INIT_MODPLUG or MIX_INIT_MP3 or MIX_INIT_OGG or MIX_INIT_FLUIDSYNTH);
169 e_WriteLog(Format('SDL: res=0x%x', [res]), MSG_NOTIFY);
170 if (res and MIX_INIT_FLAC) <> 0 then e_WriteLog('SDL: FLAC playback is active', MSG_NOTIFY);
171 if (res and MIX_INIT_MOD) <> 0 then e_WriteLog('SDL: MOD playback is active', MSG_NOTIFY);
172 if (res and MIX_INIT_MODPLUG) <> 0 then e_WriteLog('SDL: MODPLUG playback is active', MSG_NOTIFY);
173 if (res and MIX_INIT_MP3) <> 0 then e_WriteLog('SDL: MP3 playback is active', MSG_NOTIFY);
174 if (res and MIX_INIT_OGG) <> 0 then e_WriteLog('SDL: OGG playback is active', MSG_NOTIFY);
175 if (res and MIX_INIT_FLUIDSYNTH) <> 0 then e_WriteLog('SDL: FLUIDSYNTH playback is active', MSG_NOTIFY);
177 res := Mix_OpenAudio(48000, AUDIO_S16LSB, 2, 2048);
178 if res = -1 then res := Mix_OpenAudio(44100, AUDIO_S16LSB, 2, 2048);
179 if res = -1 then
180 begin
181 e_WriteLog('Error initializing SDL mixer:', MSG_FATALERROR);
182 e_WriteLog(Mix_GetError(), MSG_FATALERROR);
183 Exit;
184 end;
186 Mix_AllocateChannels(N_CHANNELS);
187 Mix_ChannelFinished(chanFinished);
189 for i := 0 to N_CHANNELS-1 do
190 begin
191 ChanSIds[i].id := NO_SOUND_ID;
192 ChanSIds[i].muted := SoundMuted;
193 ChanSIds[i].oldvol := MIX_MAX_VOLUME;
194 ChanSIds[i].pan := 1.0;
195 end;
196 MusVolume := MIX_MAX_VOLUME;
198 SoundInitialized := True;
199 Result := True;
200 end;
202 function e_isMusic (id: DWORD): Boolean;
203 begin
204 Result := False;
205 if (e_SoundsArray <> nil) and (id <= High(e_SoundsArray)) then
206 begin
207 Result := (e_SoundsArray[id].Music <> nil);
208 end;
209 end;
211 function e_isSound (id: DWORD): Boolean;
212 begin
213 Result := False;
214 if (e_SoundsArray <> nil) and (id <= High(e_SoundsArray)) then
215 begin
216 Result := (e_SoundsArray[id].Sound <> nil);
217 end;
218 end;
220 function FindESound(): DWORD;
221 var
222 i: Integer;
223 begin
224 if e_SoundsArray <> nil then
225 begin
226 for i := 0 to High(e_SoundsArray) do
227 if (e_SoundsArray[i].Sound = nil) and (e_SoundsArray[i].Music = nil) then
228 begin
229 Result := i;
230 Exit;
231 end;
232 end;
233 if e_SoundsArray = nil then
234 begin
235 SetLength(e_SoundsArray, 16);
236 Result := 0;
237 end
238 else
239 begin
240 Result := High(e_SoundsArray) + 1;
241 SetLength(e_SoundsArray, Length(e_SoundsArray) + 16);
242 end;
243 for i := Result to High(e_SoundsArray) do
244 begin
245 e_SoundsArray[i].Sound := nil;
246 e_SoundsArray[i].Music := nil;
247 e_SoundsArray[i].Data := nil;
248 e_SoundsArray[i].isMusic := False;
249 e_SoundsArray[i].nRefs := 0;
250 end;
251 end;
253 function e_LoadSound(FileName: String; var ID: DWORD; isMusic: Boolean): Boolean;
254 var
255 find_id: DWORD;
256 begin
257 ID := NO_SOUND_ID;
258 Result := False;
259 if not SoundInitialized then Exit;
261 if isMusic then e_WriteLog('Loading music '+FileName+'...', MSG_NOTIFY)
262 else e_WriteLog('Loading sound '+FileName+'...', MSG_NOTIFY);
265 if isMusic then
266 begin
267 e_WriteLog('IGNORING MUSIC FROM FILE', MSG_WARNING);
268 Exit;
269 end;
272 find_id := FindESound();
274 e_SoundsArray[find_id].Data := nil;
275 e_SoundsArray[find_id].isMusic := isMusic;
276 e_SoundsArray[find_id].nRefs := 0;
278 if isMusic then
279 begin
280 e_WriteLog(Format(' MUSIC SLOT: %u', [find_id]), MSG_NOTIFY);
281 e_SoundsArray[find_id].Music := Mix_LoadMUS(PAnsiChar(FileName));
282 if e_SoundsArray[find_id].Music = nil then
283 begin
284 e_WriteLog(Format('ERROR LOADING MUSIC:', [find_id]), MSG_WARNING);
285 e_WriteLog(Mix_GetError(), MSG_WARNING);
286 Exit;
287 end;
288 dumpMusicType(e_SoundsArray[find_id].Music);
289 end
290 else
291 begin
292 e_SoundsArray[find_id].Sound := Mix_LoadWAV(PAnsiChar(FileName));
293 if e_SoundsArray[find_id].Sound = nil then Exit;
294 end;
296 ID := find_id;
298 Result := True;
299 end;
301 function e_LoadSoundMem(pData: Pointer; Length: Integer; var ID: DWORD; isMusic: Boolean): Boolean;
302 var
303 find_id: DWORD;
304 rw: PSDL_RWops;
305 pc: PChar;
306 isid3: Boolean;
307 begin
308 ID := NO_SOUND_ID;
309 Result := False;
310 if not SoundInitialized then Exit;
311 isid3 := False;
314 if isMusic then
315 begin
316 e_WriteLog('IGNORING MUSIC FROM MEMORY', MSG_WARNING);
317 Exit;
318 end;
321 //FIXME: correctly skip ID3
323 pc := PChar(pData);
324 if (Length > $400) and (pc[0] = 'I') and (pc[1] = 'D') and (pc[2] = '3') then
325 begin
326 isid3 := True;
327 Inc(pc, $400);
328 pData := Pointer(pc);
329 Dec(Length, $400);
330 e_WriteLog('MUSIC: MP3 ID3 WORKAROUND APPLIED!', MSG_WARNING);
331 end;
334 rw := SDL_RWFromConstMem(pData, Length);
335 if rw = nil then Exit;
337 find_id := FindESound();
339 e_SoundsArray[find_id].Data := pData;
340 if isid3 then e_SoundsArray[find_id].Data := nil;
341 e_SoundsArray[find_id].isMusic := isMusic;
342 e_SoundsArray[find_id].nRefs := 0;
344 if isMusic then
345 begin
346 e_WriteLog(Format(' MUSIC SLOT: %u', [find_id]), MSG_NOTIFY);
347 e_SoundsArray[find_id].Music := Mix_LoadMUS_RW(rw, 0);
348 if e_SoundsArray[find_id].Music = nil then
349 begin
350 e_WriteLog(Format('ERROR LOADING MUSIC:', [find_id]), MSG_WARNING);
351 e_WriteLog(Mix_GetError(), MSG_WARNING);
352 end
353 else
354 begin
355 dumpMusicType(e_SoundsArray[find_id].Music);
356 end;
357 //SDL_FreeRW(rw);
359 if e_SoundsArray[find_id].Music <> nil then
360 begin
361 Mix_FreeMusic(e_SoundsArray[find_id].Music);
362 end;
363 e_SoundsArray[find_id].Music := nil;
364 Exit;
366 end
367 else
368 begin
369 e_SoundsArray[find_id].Sound := Mix_LoadWAV_RW(rw, 0);
370 end;
371 //SDL_FreeRW(rw); // somehow it segfaults...
372 if (e_SoundsArray[find_id].Sound = nil) and (e_SoundsArray[find_id].Music = nil) then
373 begin
374 e_SoundsArray[find_id].Data := nil;
375 Exit;
376 end;
378 ID := find_id;
380 Result := True;
381 end;
383 function e_PlaySound (ID: DWORD): Integer;
384 var
385 res: Integer = -1;
386 begin
387 Result := -1;
388 if not SoundInitialized then Exit;
390 if e_isSound(ID) then
391 begin
392 if e_SoundsArray[ID].nRefs >= gMaxSimSounds then Exit;
393 Inc(e_SoundsArray[ID].nRefs);
394 res := Mix_PlayChannel(-1, e_SoundsArray[ID].Sound, 0);
395 if res >= 0 then
396 begin
397 ChanSIds[res].id := ID;
398 ChanSIds[res].muted := SoundMuted;
399 if SoundMuted then Mix_Volume(res, 0) else Mix_Volume(res, ChanSIds[res].oldvol);
401 if e_SoundsArray[ID].isMusic then
402 res := Mix_PlayChannel(-1, e_SoundsArray[ID].Sound, -1)
403 else
404 res := Mix_PlayChannel(-1, e_SoundsArray[ID].Sound, 0);
406 end;
407 end
408 else
409 begin
410 if not e_isMusic(ID) then Exit;
411 Mix_HaltMusic();
412 res := Mix_PlayMusic(e_SoundsArray[ID].Music, -1);
413 if res >= 0 then res := N_MUSCHAN;
414 if SoundMuted then Mix_VolumeMusic(0) else Mix_VolumeMusic(MusVolume);
415 Result := res;
416 end;
418 Result := res;
419 end;
421 function e_chanSetPan (chan: Integer; Pan: Single): Boolean;
422 var
423 l, r: UInt8;
424 begin
425 Result := True;
426 if chan = N_MUSCHAN then
427 begin
428 // no panning for music
429 end
430 else if chan >= 0 then
431 begin
432 if Pan < -1.0 then Pan := -1.0 else if Pan > 1.0 then Pan := 1.0;
433 Pan := Pan+1.0; // 0..2
434 l := trunc(127.0*(2.0-Pan));
435 r := trunc(127.0*Pan);
436 Mix_SetPanning(chan, l, r);
437 ChanSIds[chan].pan := Pan;
438 end
439 else
440 begin
441 Result := False;
442 end;
443 end;
445 function e_chanSetVol (chan: Integer; Volume: Single): Boolean;
446 var
447 vol: Integer;
448 begin
449 Result := True;
450 if Volume < 0 then Volume := 0 else if Volume > 1 then Volume := 1;
451 vol := trunc(Volume*MIX_MAX_VOLUME);
452 if chan = N_MUSCHAN then
453 begin
454 MusVolume := vol;
455 if SoundMuted then Mix_VolumeMusic(0) else Mix_VolumeMusic(vol);
456 end
457 else if chan >= 0 then
458 begin
459 ChanSIds[chan].oldvol := vol;
460 if ChanSIds[chan].muted then Mix_Volume(chan, 0) else Mix_Volume(chan, vol);
461 end
462 else
463 begin
464 Result := False;
465 end;
466 end;
468 function e_PlaySoundPan(ID: DWORD; Pan: Single): Integer;
469 var
470 chan: Integer;
471 begin
472 Result := -1;
473 chan := e_PlaySound(ID);
474 e_chanSetPan(chan, Pan);
475 Result := chan;
476 end;
478 function e_PlaySoundVolume(ID: DWORD; Volume: Single): Integer;
479 var
480 chan: Integer;
481 begin
482 Result := -1;
483 chan := e_PlaySound(ID);
484 e_chanSetVol(chan, Volume);
485 Result := chan;
486 end;
488 function e_PlaySoundPanVolume(ID: DWORD; Pan, Volume: Single): Integer;
489 var
490 chan: Integer;
491 begin
492 Result := -1;
493 chan := e_PlaySound(ID);
494 e_chanSetPan(chan, Pan);
495 e_chanSetVol(chan, Volume);
496 Result := chan;
497 end;
499 procedure e_DeleteSound(ID: DWORD);
500 var
501 i: Integer;
502 begin
503 if ID > High(e_SoundsArray) then Exit;
504 if (e_SoundsArray[ID].Sound = nil) and (e_SoundsArray[ID].Music = nil) then Exit;
506 for i := 0 to N_CHANNELS-1 do
507 begin
508 if ChanSIds[i].id = ID then
509 begin
510 ChanSIds[i].id := NO_SOUND_ID;
511 Mix_HaltChannel(i);
512 end;
513 end;
515 if e_SoundsArray[ID].Sound <> nil then Mix_FreeChunk(e_SoundsArray[ID].Sound);
516 if e_SoundsArray[ID].Music <> nil then Mix_FreeMusic(e_SoundsArray[ID].Music);
517 if e_SoundsArray[ID].Data <> nil then FreeMem(e_SoundsArray[ID].Data);
519 e_SoundsArray[ID].Sound := nil;
520 e_SoundsArray[ID].Music := nil;
521 e_SoundsArray[ID].Data := nil;
522 e_SoundsArray[ID].nRefs := 0;
523 end;
525 procedure e_ModifyChannelsVolumes(SoundMod: Single; setMode: Boolean);
526 var
527 i: Integer;
528 vol: Single;
529 ovol: Integer;
530 begin
531 for i := 0 to N_CHANNELS-1 do
532 begin
533 ovol := ChanSIds[i].oldvol;
534 if setMode then
535 begin
536 vol := SoundMod;
537 end
538 else
539 begin
540 vol := (MIX_MAX_VOLUME+0.0)/ovol;
541 vol := vol*SoundMod;
542 end;
543 if vol < 0 then vol := 0 else if vol > 1 then vol := 1;
544 ChanSIds[i].oldvol := trunc(vol*MIX_MAX_VOLUME);
545 //if i = 0 then e_WriteLog(Format('modifying volumes: vol=%f; newvol=%d', [vol, ChanSIds[i].oldvol]), MSG_WARNING);
546 if ChanSIds[i].muted then Mix_Volume(i, 0) else Mix_Volume(i, ChanSIds[i].oldvol);
547 end;
548 ovol := Mix_VolumeMusic(-1);
549 if ovol >= 0 then
550 begin
551 if setMode then
552 begin
553 vol := SoundMod;
554 end
555 else
556 begin
557 vol := (MIX_MAX_VOLUME+0.0)/ovol;
558 vol := vol * SoundMod;
559 end;
560 if vol < 0 then vol := 0 else if vol > 1 then vol := 1;
561 MusVolume := trunc(vol*MIX_MAX_VOLUME);
562 if SoundMuted then Mix_VolumeMusic(0) else Mix_VolumeMusic(MusVolume);
563 end;
564 end;
566 procedure e_MuteChannels(Enable: Boolean);
567 var
568 i: Integer;
569 begin
570 //if Enable = SoundMuted then Exit;
571 SoundMuted := Enable;
572 for i := 0 to N_CHANNELS-1 do
573 begin
574 if ChanSIds[i].muted <> SoundMuted then
575 begin
576 ChanSIds[i].muted := SoundMuted;
577 //e_WriteLog(Format('gmuting sound for channel %d', [i]), MSG_WARNING);
578 if ChanSIds[i].muted then Mix_Volume(i, 0) else Mix_Volume(i, ChanSIds[i].oldvol);
579 end;
580 end;
581 if SoundMuted then Mix_VolumeMusic(0) else Mix_VolumeMusic(MusVolume);
582 end;
584 procedure e_StopChannels();
585 var
586 i: Integer;
587 begin
588 Mix_HaltChannel(-1);
589 Mix_HaltMusic();
590 for i := 0 to High(e_SoundsArray) do e_SoundsArray[i].nRefs := 0;
591 for i := 0 to N_CHANNELS-1 do ChanSIds[i].id := NO_SOUND_ID;
592 end;
594 procedure e_RemoveAllSounds();
595 var
596 i: Integer;
597 begin
598 if SoundInitialized then e_StopChannels();
599 for i := 0 to High(e_SoundsArray) do e_DeleteSound(i);
600 SetLength(e_SoundsArray, 0);
601 e_SoundsArray := nil;
602 end;
604 procedure e_ReleaseSoundSystem();
605 begin
606 e_RemoveAllSounds();
607 if SoundInitialized then
608 begin
609 Mix_CloseAudio();
610 SoundInitialized := False;
611 end;
612 end;
614 procedure e_SoundUpdate();
615 begin
616 //FMOD_System_Update(F_System);
617 end;
620 { TBasicSound: }
622 constructor TBasicSound.Create();
623 begin
624 FID := NO_SOUND_ID;
625 FMusic := False;
626 FChanNum := -1;
627 FPosition := 0;
628 FPriority := 128;
629 end;
631 destructor TBasicSound.Destroy();
632 begin
633 FreeSound();
634 inherited;
635 end;
637 function TBasicSound.GetChan (): Integer;
638 begin
639 if (FID <> NO_SOUND_ID) and (FChanNum >= 0) and (FChanNum < N_CHANNELS) then
640 begin
641 if ChanSIds[FChanNum].id <> FID then FChanNum := -1;
642 end
643 else if e_isMusic(FID) then
644 begin
645 FChanNum := N_MUSCHAN;
646 end;
647 Result := FChanNum;
648 end;
650 procedure TBasicSound.FreeSound();
651 begin
652 if FID = NO_SOUND_ID then Exit;
653 Stop();
654 FID := NO_SOUND_ID;
655 FMusic := False;
656 FPosition := 0;
657 FChanNum := -1;
658 end;
660 // aPos: msecs
661 function TBasicSound.RawPlay(Pan: Single; Volume: Single; aPos: DWORD): Boolean;
662 begin
663 Result := False;
664 if (FID = NO_SOUND_ID) or not SoundInitialized then Exit;
665 FChanNum := e_PlaySoundPanVolume(FID, Pan, Volume);
666 Result := (FChanNum >= 0);
667 //TODO: aPos
668 end;
670 procedure TBasicSound.SetID(ID: DWORD);
671 begin
672 FreeSound();
673 FID := ID;
674 if ID <> NO_SOUND_ID then
675 begin
676 FMusic := e_SoundsArray[ID].isMusic;
677 end;
678 FChanNum := -1;
679 end;
681 function TBasicSound.IsPlaying(): Boolean;
682 var
683 chan: Integer;
684 begin
685 Result := False;
686 if e_isSound(FID) then
687 begin
688 //e_WriteLog(Format('IsPlaying: FID=%u; FChanNum=%d', [FID, FChanNum]), MSG_WARNING);
689 chan := Channel;
690 if chan < 0 then
691 begin
692 //e_WriteLog(Format('IsPlaying: FID=%u; ONA', [FID]), MSG_WARNING);
693 Exit;
694 end;
695 //Result := (Mix_Playing(chan) > 0)
696 //e_WriteLog(Format('IsPlaying: FID=%u; TAN', [FID]), MSG_WARNING);
697 Result := True;
698 end
699 else if e_isMusic(FID) then
700 begin
701 Result := (Mix_PlayingMusic() > 0);
702 end;
703 end;
705 procedure TBasicSound.Stop();
706 var
707 chan: Integer;
708 begin
709 if e_isSound(FID) then
710 begin
711 chan := Channel;
712 if chan >= 0 then
713 begin
714 //GetPosition();
715 Mix_HaltChannel(chan);
716 end;
717 end
718 else if e_isMusic(FID) then
719 begin
720 Mix_HaltMusic();
721 end;
722 FChanNum := -1;
723 end;
725 function TBasicSound.IsPaused(): Boolean;
726 var
727 chan: Integer;
728 begin
729 Result := False;
730 if e_isSound(FID) then
731 begin
732 chan := Channel;
733 if chan < 0 then Exit;
734 Result := (Mix_Paused(chan) > 0);
735 end
736 else if e_isMusic(FID) then
737 begin
738 Result := (Mix_PausedMusic() > 0);
739 end;
740 end;
742 procedure TBasicSound.Pause(Enable: Boolean);
743 var
744 chan: Integer;
745 pl: Boolean;
746 begin
747 if e_isSound(FID) then
748 begin
749 chan := Channel;
750 if chan < 0 then Exit;
751 pl := not (Mix_Paused(chan) > 0);
752 if pl <> Enable then
753 begin
754 if Enable then Mix_Resume(chan) else Mix_Pause(chan);
755 end;
756 end
757 else if e_isMusic(FID) then
758 begin
759 pl := not (Mix_PausedMusic() > 0);
760 if pl <> Enable then
761 begin
762 if Enable then Mix_ResumeMusic() else Mix_PauseMusic();
763 end;
764 end;
766 if Enable then
767 begin
768 res := FMOD_Channel_GetPosition(FChanNum, FPosition, FMOD_TIMEUNIT_MS);
769 if res <> FMOD_OK then
770 begin
771 end;
772 end;
774 end;
776 function TBasicSound.GetVolume(): Single;
777 var
778 chan: Integer;
779 begin
780 Result := 0.0;
781 if e_isSound(FID) then
782 begin
783 chan := Channel;
784 if chan < 0 then Exit;
785 Result := (ChanSIds[chan].oldvol+0.0)/(MIX_MAX_VOLUME+0.0);
786 end
787 else if e_isMusic(FID) then
788 begin
789 Result := (MusVolume+0.0)/(MIX_MAX_VOLUME+0.0);
790 end;
791 end;
793 procedure TBasicSound.SetVolume(Volume: Single);
794 var
795 chan: Integer;
796 begin
797 if e_isSound(FID) then
798 begin
799 chan := Channel;
800 if chan < 0 then Exit;
801 //e_WriteLog(Format('SetVolume: chan=%d; Volume=%f', [chan, Volume]), MSG_WARNING);
802 e_chanSetVol(chan, Volume);
803 end
804 else if e_isMusic(FID) then
805 begin
806 //e_WriteLog(Format('SetVolume: chan=MUSIC; Volume=%f', [Volume]), MSG_WARNING);
807 e_chanSetVol(N_MUSCHAN, Volume);
808 end;
809 end;
811 function TBasicSound.GetPan(): Single;
812 var
813 chan: Integer;
814 begin
815 Result := 1.0;
816 if e_isSound(FID) then
817 begin
818 chan := Channel;
819 if chan < 0 then Exit;
820 Result := ChanSIds[chan].pan;
821 end;
822 end;
824 procedure TBasicSound.SetPan(Pan: Single);
825 var
826 chan: Integer;
827 begin
828 if e_isSound(FID) then
829 begin
830 chan := Channel;
831 if chan < 0 then Exit;
832 e_chanSetPan(chan, Pan);
833 end;
834 end;
836 function TBasicSound.IsMuted(): Boolean;
837 var
838 chan: Integer;
839 begin
840 Result := False;
841 if e_isSound(FID) then
842 begin
843 chan := Channel;
844 if chan < 0 then Exit;
845 Result := ChanSIds[chan].muted;
846 end
847 else if e_isMusic(FID) then
848 begin
849 Result := SoundMuted;
850 end;
851 end;
853 procedure TBasicSound.Mute(Enable: Boolean);
854 var
855 chan: Integer;
856 begin
857 if e_isSound(FID) then
858 begin
859 chan := Channel;
860 if chan < 0 then Exit;
861 if ChanSIds[chan].muted <> Enable then
862 begin
863 //e_WriteLog(Format('muting sound for channel %d', [cnan]), MSG_WARNING);
864 ChanSIds[chan].muted := Enable;
865 if ChanSIds[chan].muted then Mix_Volume(chan, 0) else Mix_Volume(chan, ChanSIds[chan].oldvol);
866 end;
867 end
868 else if e_isMusic(FID) then
869 begin
870 if Enable then Mix_VolumeMusic(0) else Mix_VolumeMusic(MusVolume);
871 end;
872 end;
874 //TODO
875 function TBasicSound.GetPosition(): DWORD;
876 begin
877 Result := 0;
879 if FChanNum < 0 then Exit;
880 res := FMOD_Channel_GetPosition(FChanNum, FPosition, FMOD_TIMEUNIT_MS);
881 if res <> FMOD_OK then
882 begin
883 Exit;
884 end;
885 Result := FPosition;
887 end;
889 //TODO
890 procedure TBasicSound.SetPosition(aPos: DWORD);
891 begin
892 FPosition := aPos;
894 if FChanNum < 0 then Exit;
895 res := FMOD_Channel_SetPosition(FChanNum, FPosition, FMOD_TIMEUNIT_MS);
896 if res <> FMOD_OK then
897 begin
898 end;
900 end;
902 //TODO
903 procedure TBasicSound.SetPriority(priority: Integer);
904 begin
906 if (FChanNum <> nil) and (FPriority <> priority) and
907 (priority >= 0) and (priority <= 256) then
908 begin
909 FPriority := priority;
910 res := FMOD_Channel_SetPriority(FChanNum, priority);
911 if res <> FMOD_OK then
912 begin
913 end;
914 end;
916 end;
918 end.