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
206
207
208
209
210
211
212
213
use audio::{SoundBufferRef, SoundSource, SoundStatus};
use audio::csfml_audio_sys as ffi;
use csfml_system_sys::sfBool;
use sf_bool_ext::SfBoolExt;
use std::marker::PhantomData;
use std::mem;
use system::Time;
use system::Vector3f;
#[derive(Debug)]
pub struct Sound<'s> {
sound: *mut ffi::sfSound,
buffer: PhantomData<&'s SoundBufferRef>,
}
impl<'s> Sound<'s> {
pub fn new() -> Sound<'s> {
let s = unsafe { ffi::sfSound_create() };
assert!(!s.is_null(), "Failed to create Sound");
Sound {
sound: s,
buffer: PhantomData,
}
}
pub fn with_buffer(buffer: &SoundBufferRef) -> Sound {
let mut s = Sound::new();
s.set_buffer(buffer);
s
}
pub fn set_looping(&mut self, looping: bool) {
unsafe { ffi::sfSound_setLoop(self.sound, sfBool::from_bool(looping)) }
}
pub fn is_looping(&self) -> bool {
unsafe { ffi::sfSound_getLoop(self.sound) }.to_bool()
}
pub fn play(&mut self) {
unsafe { ffi::sfSound_play(self.sound) }
}
pub fn pause(&mut self) {
unsafe { ffi::sfSound_pause(self.sound) }
}
pub fn stop(&mut self) {
unsafe { ffi::sfSound_stop(self.sound) }
}
pub fn status(&self) -> SoundStatus {
unsafe { mem::transmute(ffi::sfSound_getStatus(self.sound)) }
}
pub fn playing_offset(&self) -> Time {
unsafe { Time::from_raw(ffi::sfSound_getPlayingOffset(self.sound)) }
}
pub fn set_playing_offset(&mut self, time_offset: Time) {
unsafe { ffi::sfSound_setPlayingOffset(self.sound, time_offset.raw()) }
}
pub fn set_buffer(&mut self, buffer: &'s SoundBufferRef) {
let ptr: *const SoundBufferRef = buffer;
unsafe { ffi::sfSound_setBuffer(self.sound, ptr as _) }
}
pub fn buffer(&self) -> Option<&SoundBufferRef> {
unsafe {
let ptr = ffi::sfSound_getBuffer(self.sound);
if ptr.is_null() {
None
} else {
Some(&*(ptr as *const SoundBufferRef))
}
}
}
}
impl<'a> Default for Sound<'a> {
fn default() -> Self {
Self::new()
}
}
impl<'s> Clone for Sound<'s> {
fn clone(&self) -> Self {
let s = unsafe { ffi::sfSound_copy(self.sound) };
assert!(!s.is_null(), "Failed to copy Sound");
Sound {
sound: s,
buffer: self.buffer,
}
}
}
impl<'s> SoundSource for Sound<'s> {
fn set_pitch(&mut self, pitch: f32) {
unsafe { ffi::sfSound_setPitch(self.sound, pitch) }
}
fn set_volume(&mut self, volume: f32) {
unsafe { ffi::sfSound_setVolume(self.sound, volume) }
}
fn set_position<P: Into<Vector3f>>(&mut self, position: P) {
unsafe { ffi::sfSound_setPosition(self.sound, position.into().raw()) }
}
fn set_relative_to_listener(&mut self, relative: bool) {
unsafe { ffi::sfSound_setRelativeToListener(self.sound, sfBool::from_bool(relative)) }
}
fn set_min_distance(&mut self, distance: f32) {
unsafe { ffi::sfSound_setMinDistance(self.sound, distance) }
}
fn set_attenuation(&mut self, attenuation: f32) {
unsafe { ffi::sfSound_setAttenuation(self.sound, attenuation) }
}
fn pitch(&self) -> f32 {
unsafe { ffi::sfSound_getPitch(self.sound) }
}
fn volume(&self) -> f32 {
unsafe { ffi::sfSound_getVolume(self.sound) }
}
fn position(&self) -> Vector3f {
unsafe { Vector3f::from_raw(ffi::sfSound_getPosition(self.sound)) }
}
fn is_relative_to_listener(&self) -> bool {
unsafe { ffi::sfSound_isRelativeToListener(self.sound).to_bool() }
}
fn min_distance(&self) -> f32 {
unsafe { ffi::sfSound_getMinDistance(self.sound) }
}
fn attenuation(&self) -> f32 {
unsafe { ffi::sfSound_getAttenuation(self.sound) }
}
}
impl<'s> Drop for Sound<'s> {
fn drop(&mut self) {
unsafe {
ffi::sfSound_destroy(self.sound);
}
}
}