-
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathcftSound.cxs
205 lines (194 loc) · 6.38 KB
/
cftSound.cxs
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
Strict
#rem
Title: fantomCX
Description: A 2D game framework for the Cerberus X programming language
Author: Michael Hartlef
Contact: [email protected]
Website: http://www.fantomgl.com
License: MIT
#End
'nav:<blockquote><nav><b>fantomCX documentation</b> | <a href="index.html">Home</a> | <a href="classes.html">Classes</a> | <a href="3rdpartytools.html">3rd party tools</a> | <a href="examples.html">Examples</a> | <a href="changes.html">Changes</a></nav></blockquote>
Import fantomCX
'#DOCON#
'***************************************
'summery:The class [b]ftSound[/b] is an object to play sounds easily.
Class ftSound
'#DOCOFF#
Field sound:Sound
Field name:String
Field engine:ftEngine
Field channel:Int=0
Field loop:Bool=False
Field volume:Float = 1.0
Field pan:Float = 0.0
Field rate:Float = 1.0
Field isMusic:Bool = False
Field isPaused:Bool = False
Field soundNode:list.Node<ftSound> = Null
'#DOCON#
'------------------------------------------
'summery:Returns a free sound channel index.
Method GetFreeSoundChannel:Int()
Local msc:Int = engine.maxSoundChannel
Local fsc:Int = engine.firstSoundChannel
For Local i:Int = fsc To msc
If ChannelState(i)=0 Then
Return i
End
End
' If no sound channel is free, or the app is running on Android/Flash...
engine.nextSoundChannel += 1
If engine.nextSoundChannel > msc Then engine.nextSoundChannel = fsc
Return engine.nextSoundChannel
End
'------------------------------------------
'changes:2.0:New command
'summery:Return the loop flag of a sound or a music.
'seeAlso:SetLoop
Method GetLoop:Bool()
Return Self.loop
End
'------------------------------------------
'summery:Returns the pan value of a sound.
'seeAlso:SetPan
Method GetPan:Float()
Return Self.pan
End
'------------------------------------------
'summery:Returns the pitch rate of a sound.
'seeAlso:SetPitchRate
Method GetPitchRate:Float()
Return Self.rate
End
'------------------------------------------
'summery:Returns the flag of a sound.
'seeAlso:SetPaused
Method GetPaused:Bool()
Return Self.isPaused
End
'------------------------------------------
'summery:Returns the volume of a sound. Ranges from 0.0 to 1.0.
'seeAlso:SetVolume
Method GetVolume:Float()
Return Self.volume
End
'------------------------------------------
'changes:2.0:New command
'summery:Set the loop flag of a sound or a music. It only has an effect on newly started sounds and music.
'seeAlso:GetLoop
Method SetLoop:Void(loopFlag:Bool)
Self.loop = loopFlag
End
'------------------------------------------
'summery:Pause/resume a sound or a music.
'seeAlso:GetPaused
Method SetPaused:Void(pauseFlag:Bool)
If Self.isMusic = False Then
If pauseFlag = False
ResumeChannel(Self.channel)
Else
PauseChannel(Self.channel)
Endif
Else
If pauseFlag = False
ResumeMusic()
Else
PauseMusic()
Endif
Endif
Self.isPaused = pauseFlag
End
'------------------------------------------
'summery:Plays a sound. If no channel is provided, fantomCX will determine the channel automatically.
'seeAlso:Stop
Method Play:Int(c:Int = -1)
If Self.isMusic = False Then
If c = -1 Then c = GetFreeSoundChannel()
channel = c
If sound <> Null Then
If engine.hasSound = True Then
PlaySound(sound, c, loop)
SetChannelVolume( channel, Self.volume * engine.volumeSFX )
SetChannelPan( channel, Self.pan )
SetChannelRate( channel, Self.rate )
Endif
Endif
Else
If engine.hasMusic = True Then
PlayMusic( name, loop )
SetMusicVolume(volume * engine.volumeMUS)
Endif
Endif
Self.isPaused = False
Return channel
End
'------------------------------------------
#Rem
'summery:Set the pan value of a sound. Ranges from -1.0 to 1.0.
A value of -1.0 is on the left side, a value of 1.0 represents the right side.
#End
'seeAlso:GetPan
Method SetPan:Void(channelPan:Float = 0.0)
Self.pan = channelPan
If Self.isMusic = False Then
SetChannelPan( channel, Self.pan)
Endif
End
'------------------------------------------
#Rem
'summery:Set the rate of a sound.
The default base note (1.0) is 'A4'. You can use the ftGetPitchRate("C3") function to determine the rate from a given note.
#End
'seeAlso:GetPitchRate
Method SetPitchRate:Void(channelRate:Float = 1.0)
Self.rate = channelRate
If Self.isMusic = False Then
SetChannelRate( channel, Self.rate)
Endif
End
'------------------------------------------
'summery:Set the volume of a sound. Ranges from 0.0 to 1.0.
'seeAlso:GetVolume
Method SetVolume:Void(vol:Float = 1.0)
Self.volume = vol
If Self.isMusic = False Then
If Self.loop = True Then
SetChannelVolume( channel, volume * engine.volumeSFX)
Endif
Else
SetMusicVolume(volume * engine.volumeMUS)
Endif
End
'------------------------------------------
'summery:Stop playing the sound. If a channel is given, it will stop that specific sound channel.
'seeAlso:Play
Method Stop:Void(c:Int=-1)
If c = -1 Then c = channel
If Self.isMusic = False Then
StopChannel(c)
Else
StopMusic()
Endif
Self.isPaused = False
End
'------------------------------------------
'#DOCOFF#
Method Remove:Void()
If Self.isMusic = False Then
'StopChannel(channel)
Else
StopMusic()
Endif
Self.soundNode.Remove()
Self.engine = Null
End
'#DOCON#
End
#rem
footer:This fantomCX framework is released under the MIT license:
[quote]Copyright (c) 2011-2018 Michael Hartlef
Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the software, and to permit persons to whom the software is furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in all copies or substantial portions of the software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
[/quote]
#end