Debian Codegraph @

examples/audio/internal/fmod_api_test/fmod_api_test.dpr

raw
1 {
2 Copyright 2019-2021 Michalis Kamburelis.
3
4 This file is part of "Castle Game Engine".
5
6 "Castle Game Engine" is free software; see the file COPYING.txt,
7 included in this distribution, for details about the copyright.
8
9 "Castle Game Engine" is distributed in the hope that it will be useful,
10 but WITHOUT ANY WARRANTY; without even the implied warranty of
11 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
12
13 ----------------------------------------------------------------------------
14 }
15
16 {$ifdef MSWINDOWS} {$apptype CONSOLE} {$endif}
17
18 { Simple application to test direct FMOD calls. }
19
20 uses
21 { FMOD on Windows platform docs,
22 https://www.fmod.com/resources/documentation-api?version=2.0&page=platforms-windows.html
23 say to call
24 CoInitializeEx(nil, COINIT_APARTMENTTHREADED);
25 and
26 CoUninitialize()
27 It seems that ComObj unit in FPC calls this automatically.
28
29 TODO: In Delphi we should use
30 Windows, ActiveX,
31 and call
32 CoInitializeEx(nil, COINIT_APARTMENTTHREADED);
33 manually? }
34 {$ifdef MSWINDOWS} ComObj, {$endif}
35 SysUtils, TypInfo, CTypes,
36 CastleInternalFMOD, CastleUtils, CastleStringUtils, CastleTimeUtils;
37
38 type
39 EFMODError = class(Exception);
40
41 procedure CheckFMOD(const FMODResult: TFMOD_RESULT);
42 var
43 ErrorStr: String;
44 begin
45 if FMODResult <> FMOD_OK then
46 begin
47 ErrorStr := GetEnumName(TypeInfo(TFMOD_RESULT), Ord(FMODResult));
48 raise EFMODError.CreateFmt('FMOD error: %s', [ErrorStr]);
49 end;
50 end;
51
52 function SoundTypeToStr(const SoundType: TFMOD_SOUND_TYPE): String;
53 begin
54 // WriteStr was better in FPC, when TFMOD_SOUND_TYPE had assignments.
55 //WriteStr(Result, SoundType);
56 // GetEnumName would make FPC error then "No type info available for this type".
57 // But it no longer has assignments.
58
59 Result := GetEnumName(TypeInfo(TFMOD_SOUND_TYPE), Ord(SoundType));
60 end;
61
62 function SoundFormatToStr(const SoundFormat: TFMOD_SOUND_FORMAT): String;
63 begin
64 Result := GetEnumName(TypeInfo(TFMOD_SOUND_FORMAT), Ord(SoundFormat));
65 end;
66
67 var
68 FMODSystem: PFMOD_SYSTEM;
69
70 function PlaySound(const Url: String; const Loop: Boolean): PFMOD_CHANNEL;
71 var
72 // SoundInfo: TFMOD_CREATESOUNDEXINFO; // not needed now
73 Sound: PFMOD_SOUND;
74 SoundType: TFMOD_SOUND_TYPE;
75 SoundFormat: TFMOD_SOUND_FORMAT;
76 SoundChannels, SoundBits: CInt;
77 begin
78 // Not needed now
79 // FillChar(SoundInfo, SizeOf(cbsize), );
80 // SoundInfo.cbsize := SizeOf(cbsize);
81
82 CheckFMOD(FMOD_System_CreateSound(FMODSystem, PCharOrNil(Url), FMOD_DEFAULT or FMOD_2D,
83 nil { @SoundInfo }, @Sound));
84
85 CheckFMOD(FMOD_Sound_GetFormat(Sound, @SoundType, @SoundFormat, @SoundChannels, @SoundBits));
86
87 Writeln('Loaded sound information:' + NL +
88 ' Type: ' + SoundTypeToStr(SoundType) + NL +
89 ' Format: ' + SoundFormatToStr(SoundFormat) + NL +
90 ' Channels: ' + IntToStr(SoundChannels) + NL +
91 ' Bits: ' + IntToStr(SoundBits));
92
93 // Start in paused state, allows to adjust parameters like loop before starting
94 CheckFMOD(FMOD_System_PlaySound(FMODSystem, Sound, nil, { paused } 1, @Result));
95 if Loop then
96 CheckFMOD(FMOD_Channel_SetMode(Result, FMOD_LOOP_NORMAL))
97 else
98 CheckFMOD(FMOD_Channel_SetMode(Result, FMOD_LOOP_OFF));
99 CheckFMOD(FMOD_Channel_SetPaused(Result, 0));
100 end;
101
102 function IsPlaying(const Channel: PFMOD_CHANNEL): Boolean;
103 var
104 B: TFMOD_BOOL;
105 begin
106 { Note that looping sound will have IsPlaying forever until it's explicitly stopped,
107 and that's what we want. }
108 CheckFMOD(FMOD_Channel_IsPlaying(Channel, @B));
109 Result := B <> 0;
110 end;
111
112 var
113 TimeStart: TTimerResult;
114 Time: TFloatTime;
115 NewVolume, NewPitch: Single;
116 ChannelWaiting: PFMOD_CHANNEL;
117 begin
118 Check(FmodLibraryAvailable, 'FMOD library cannot be loaded');
119 FmodLibraryUsingBegin;
120
121 CheckFMOD(FMOD_System_Create(@FMODSystem));
122
123 CheckFMOD(FMOD_System_Init(FMODSystem, 256, FMOD_INIT_NORMAL, nil));
124
125 PlaySound('./data/tone.wav', true);
126 ChannelWaiting := PlaySound('./data/temple-adam-goh.ogg', false);
127
128 TimeStart := Timer;
129
130 while IsPlaying(ChannelWaiting) do
131 begin
132 Time := TimeStart.ElapsedTime;
133
134 { test: modulate sound volume while playing }
135 NewVolume := Abs(Cos(Time));
136 CheckFMOD(FMOD_Channel_SetVolume(ChannelWaiting, NewVolume));
137
138 NewPitch := Abs(Cos(Time)) + 1.0;
139 CheckFMOD(FMOD_Channel_SetPitch(ChannelWaiting, NewPitch));
140
141 { test: forcibly stop sound after 10 seconds }
142 if Time > 10 then
143 begin
144 CheckFMOD(FMOD_Channel_Stop(ChannelWaiting));
145 Break; // do not call FMOD_Channel_IsPlaying anymore, ChannelWaiting should not be used anymore
146 end;
147 end;
148
149 CheckFMOD(FMOD_System_Close(FMODSystem));
150
151 CheckFMOD(FMOD_System_Release(FMODSystem));
152 FMODSystem := nil;
153
154 FmodLibraryUsingEnd;
155
156 Writeln('Finished OK.');
157 end.