Jypeli
9
The simple game programming library
SoundEffect.cs
Siirry tämän tiedoston dokumentaatioon.
1
using
System
;
2
using
System
.Diagnostics;
3
using
Microsoft
.
Xna
.
Framework
.Audio;
4
5
using
XnaSoundEffect
=
Microsoft
.
Xna
.
Framework
.Audio.SoundEffect;
6
using
System
.Collections.Generic;
7
using
System
.IO;
8
9
namespace
Jypeli
10
{
16
public
class
SoundEffect
17
{
18
List<Sound>
Instances
=
new
List<Sound>();
19
20
private
string
assetName
;
21
XnaSoundEffect
xnaEffect
;
22
Timer
posTimer
;
23
27
public
TimeSpan
Duration
{
get
{
DoLoad
();
return
xnaEffect
.Duration; } }
28
32
public
DoubleMeter
Position
{
get
;
set
; }
33
37
public
bool
IsPlaying
{
get
;
private
set
; }
38
39
private
static
string
[]
soundExtensions
= {
".wav"
,
".mp3"
,
".xnb"
};
40
41
private
void
DoLoad
()
42
{
43
if
(
xnaEffect
==
null
)
44
{
45
Debug.Assert(
assetName
!=
null
);
46
xnaEffect
=
FromContent
(
assetName
);
47
}
48
49
Position
.MaxValue =
xnaEffect
.Duration.TotalSeconds;
50
}
51
52
53
private
XnaSoundEffect
FromContent
(
string
assetname)
54
{
55
assetName
=
Game
.
FileExtensionCheck
(
assetName
,
soundExtensions
);
56
FileStream fs =
new
FileStream(
assetName
, FileMode.Open);
57
XnaSoundEffect
sound =
XnaSoundEffect
.FromStream(fs);
58
fs.Close();
59
return
sound;
60
61
}
62
internal
SoundEffect
(
string
assetName
)
63
{
64
this.assetName =
assetName
;
65
this.xnaEffect =
null
;
66
InitPosition
();
67
}
68
69
internal
SoundEffect
(
XnaSoundEffect
effect )
70
{
71
this.
Position
=
new
DoubleMeter
(0, 0, 0);
72
this.assetName =
null
;
73
xnaEffect
= effect;
74
InitPosition
();
75
}
76
77
private
void
InitPosition
()
78
{
79
Position
=
new
DoubleMeter
(0, 0, 0);
80
posTimer
=
new
Timer
();
81
posTimer
.
Interval
= 0.01;
82
posTimer
.
Timeout
+=
new
Action(
IncrementPosition
);
83
Position
.UpperLimit +=
EffectPlayed
;
84
}
85
86
private
void
IncrementPosition
()
87
{
88
Position
.Value +=
posTimer
.
Interval
;
89
}
90
91
private
void
EffectPlayed
()
92
{
93
posTimer
.
Stop
();
94
Position
.Reset();
95
Instances
.Clear();
96
IsPlaying
=
false
;
97
}
98
104
public
Sound
CreateSound
()
105
{
106
if
( !
Game
.
AudioEnabled
)
107
return
null
;
108
109
try
110
{
111
DoLoad
();
112
return
new
Sound
(
xnaEffect
.CreateInstance() );
113
}
114
catch
(NoAudioHardwareException)
115
{
116
Game
.
Instance
.
OnNoAudioHardwareException
();
117
return
null
;
118
}
119
}
120
125
public
bool
Play
()
126
{
127
DoLoad
();
128
Sound
sound =
CreateSound
();
129
if
(sound ==
null
)
return
false
;
130
131
StartPlaying
( sound );
132
return
true
;
133
}
134
142
public
bool
Play
(
double
volume,
double
pitch,
double
pan )
143
{
144
DoLoad
();
145
Sound
sound =
CreateSound
();
146
if
(sound ==
null
)
return
false
;
147
148
sound.
Volume
= volume;
149
sound.
Pitch
= pitch;
150
sound.
Pan
= pan;
151
152
StartPlaying
( sound );
153
return
true
;
154
}
155
156
private
void
StartPlaying
(
Sound
sound )
157
{
158
try
159
{
160
sound.
Play
();
161
Instances
.Add( sound );
162
Position
.Reset();
163
posTimer
.
Start
();
164
IsPlaying
=
true
;
165
}
166
catch
(InstancePlayLimitException)
167
{
168
// Too many sounds are playing at once
169
// Just ignore this for now...
170
}
171
}
172
176
public
void
Stop
()
177
{
178
foreach
(var sound
in
Instances
)
179
{
180
sound.Stop();
181
}
182
183
EffectPlayed
();
184
}
185
189
public
static
double
MasterVolume
190
{
191
set
{
XnaSoundEffect
.MasterVolume = (float)value; }
192
get
{
return
XnaSoundEffect
.MasterVolume; }
193
}
194
}
195
}
Jypeli.DoubleMeter
Mittari, joka mittaa double-tyyppisiä arvoja. Sidottavissa näyttöihin, kuten ValueDisplay ja BarGa...
Definition:
DoubleMeter.cs:11
Jypeli.Game.AudioEnabled
static bool AudioEnabled
Voiko ääniä soittaa.
Definition:
Game.cs:128
Jypeli.SoundEffect.EffectPlayed
void EffectPlayed()
Definition:
SoundEffect.cs:91
Jypeli.SoundEffect.Duration
TimeSpan Duration
Ääniefektin kesto sekunteina.
Definition:
SoundEffect.cs:27
Microsoft.Xna
Definition:
JypeliContentManager.cs:6
Jypeli.SoundEffect.Stop
void Stop()
Pysäyttää äänen toistamisen.
Definition:
SoundEffect.cs:176
Jypeli.SoundEffect
Ääniefekti. Yhdestä efektistä voi luoda CreateSound-metodilla monta ääntä (Sound),...
Definition:
SoundEffect.cs:17
Jypeli.Timer.Timeout
Action Timeout
Tapahtuu väliajoin.
Definition:
Timer.cs:44
Jypeli
Definition:
Automobile.cs:5
Jypeli.SoundEffect.SoundEffect
SoundEffect(string assetName)
Definition:
SoundEffect.cs:62
Jypeli.Sound.Volume
double Volume
Äänenvoimakkuus välillä 0.0 - 1.0.
Definition:
Sound.cs:41
Microsoft
Definition:
JypeliContentManager.cs:6
Jypeli.Sound
Definition:
Sound.cs:7
Microsoft.Xna.Framework
Definition:
JypeliContentManager.cs:6
Jypeli.SoundEffect.IsPlaying
bool IsPlaying
Toistetaanko ääntä parhaillaan.
Definition:
SoundEffect.cs:37
Jypeli.SoundEffect.xnaEffect
XnaSoundEffect xnaEffect
Definition:
SoundEffect.cs:21
Jypeli.Game.FileExtensionCheck
static string FileExtensionCheck(string file, string[] extensions)
Etsii millä päätteellä annettu tiedosto löytyy
Definition:
Content.cs:195
Jypeli.SoundEffect.soundExtensions
static string[] soundExtensions
Definition:
SoundEffect.cs:39
Jypeli.SoundEffect.StartPlaying
void StartPlaying(Sound sound)
Definition:
SoundEffect.cs:156
Jypeli.SoundEffect.MasterVolume
static double MasterVolume
Äänenvoimakkuuden taso 0.0 - 1.0
Definition:
SoundEffect.cs:190
Jypeli.SoundEffect.Instances
List< Sound > Instances
Definition:
SoundEffect.cs:18
Jypeli.Game.Instance
static Game Instance
Käynnissä olevan pelin pääolio.
Definition:
Game.cs:90
Jypeli.SoundEffect.FromContent
XnaSoundEffect FromContent(string assetname)
Definition:
SoundEffect.cs:53
Jypeli.Sound.Pan
double Pan
Äänen kuuluminen vasemmasta ja oikeasta kaiuttimesta. Arvot vaihtelevat välillä -1....
Definition:
Sound.cs:32
Jypeli.Timer.Interval
double Interval
Aika sekunneissa, jonka välein TimeOut tapahtuu.
Definition:
Timer.cs:87
Jypeli.SoundEffect.assetName
string assetName
Definition:
SoundEffect.cs:20
Jypeli.Timer.Stop
void Stop()
Pysäyttää ajastimen ja nollaa sen tilan.
Definition:
Timer.cs:292
Jypeli.SoundEffect.posTimer
Timer posTimer
Definition:
SoundEffect.cs:22
Jypeli.SoundEffect.InitPosition
void InitPosition()
Definition:
SoundEffect.cs:77
Jypeli.SoundEffect.SoundEffect
SoundEffect(XnaSoundEffect effect)
Definition:
SoundEffect.cs:69
Jypeli.SoundEffect.CreateSound
Sound CreateSound()
Luo Sound-tyyppisen olion. Oliolla on ominaisuuksia kuten voimakkuus ja korkeus joita voidaan muuttaa...
Definition:
SoundEffect.cs:104
XnaSoundEffect
Microsoft.Xna.Framework.Audio.SoundEffect XnaSoundEffect
Definition:
Content.cs:35
System
Definition:
CFFauxAttributes.cs:29
Jypeli.Timer
Ajastin, joka voidaan asettaa laukaisemaan tapahtumia tietyin väliajoin.
Definition:
Timer.cs:38
Jypeli.SoundEffect.IncrementPosition
void IncrementPosition()
Definition:
SoundEffect.cs:86
Jypeli.Sound.Pitch
double Pitch
Äänenkorkeus välillä -1.0 - 1.0.
Definition:
Sound.cs:53
Jypeli.SoundEffect.Position
DoubleMeter Position
Paikka äänessä sekunteina (missä kohtaa toistoa ollaan). Ei voi asettaa.
Definition:
SoundEffect.cs:32
Jypeli.Game.OnNoAudioHardwareException
void OnNoAudioHardwareException()
Definition:
Game.cs:216
Jypeli.Sound.Play
void Play(int retries=3)
Definition:
Sound.cs:63
Jypeli.SoundEffect.Play
bool Play(double volume, double pitch, double pan)
Soittaa äänen.
Definition:
SoundEffect.cs:142
Jypeli.Timer.Start
void Start()
Käynnistää ajastimen.
Definition:
Timer.cs:257
Jypeli.Game
Definition:
Content.cs:46
Jypeli.SoundEffect.Play
bool Play()
Soittaa äänen.
Definition:
SoundEffect.cs:125
Jypeli.SoundEffect.DoLoad
void DoLoad()
Definition:
SoundEffect.cs:41
Jypeli
Sound
SoundEffect.cs
Generoinut projektille Jypeli Sat Oct 17 2020 19:03:16
1.8.20