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
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
use crate::{
audio::{SoundSource, SoundStatus},
sf_bool_ext::SfBoolExt,
system::{Time, Vector3f},
};
use csfml_audio_sys::*;
use csfml_system_sys::*;
use std::{os::raw::c_void, panic};
pub trait SoundStream {
fn get_data(&mut self) -> (&mut [i16], bool);
fn seek(&mut self, offset: Time);
fn channel_count(&self) -> u32;
fn sample_rate(&self) -> u32;
}
#[derive(Debug)]
pub struct SoundStreamPlayer<'a, S: SoundStream + 'a> {
sf_sound_stream: *mut sfSoundStream,
stream: &'a mut S,
}
unsafe extern "C" fn get_data_callback<S: SoundStream>(
chunk: *mut sfSoundStreamChunk,
user_data: *mut c_void,
) -> sfBool {
use std::convert::TryInto;
let stream = user_data as *mut S;
let (data, keep_playing) =
match panic::catch_unwind(panic::AssertUnwindSafe(|| (*stream).get_data())) {
Ok(ret) => ret,
Err(_) => {
eprintln!("sound_stream: Stopping playback beacuse `get_data` panicked.");
(&mut [][..], false)
}
};
(*chunk).samples = data.as_mut_ptr();
(*chunk).sampleCount = data
.len()
.try_into()
.expect("Overflow casting data length to sample count");
sfBool::from_bool(keep_playing)
}
unsafe extern "C" fn seek_callback<S: SoundStream>(offset: sfTime, user_data: *mut c_void) {
let stream = user_data as *mut S;
let result = panic::catch_unwind(panic::AssertUnwindSafe(|| {
(*stream).seek(Time::from_raw(offset))
}));
if result.is_err() {
eprintln!("sound_stream: Failed to seek because `seek` panicked.");
}
}
impl<'a, S: SoundStream> SoundStreamPlayer<'a, S> {
pub fn new(sound_stream: &'a mut S) -> Self {
let ptr: *mut S = sound_stream;
SoundStreamPlayer {
sf_sound_stream: unsafe {
sfSoundStream_create(
Some(get_data_callback::<S>),
Some(seek_callback::<S>),
sound_stream.channel_count(),
sound_stream.sample_rate(),
ptr as *mut _,
)
},
stream: sound_stream,
}
}
pub fn play(&mut self) {
unsafe {
sfSoundStream_play(self.sf_sound_stream);
}
}
pub fn pause(&mut self) {
unsafe {
sfSoundStream_pause(self.sf_sound_stream);
}
}
#[must_use]
pub fn status(&self) -> SoundStatus {
unsafe { SoundStatus(sfSoundStream_getStatus(self.sf_sound_stream)) }
}
pub fn stop(&mut self) -> &mut S {
unsafe {
sfSoundStream_stop(self.sf_sound_stream);
}
self.stream
}
#[must_use]
pub fn playing_offset(&self) -> Time {
unsafe { Time::from_raw(sfSoundStream_getPlayingOffset(self.sf_sound_stream)) }
}
pub fn set_playing_offset(&mut self, offset: Time) {
unsafe { sfSoundStream_setPlayingOffset(self.sf_sound_stream, offset.raw()) }
}
#[must_use]
pub fn channel_count(&self) -> u32 {
unsafe { sfSoundStream_getChannelCount(self.sf_sound_stream) }
}
#[must_use]
pub fn sample_rate(&self) -> u32 {
unsafe { sfSoundStream_getSampleRate(self.sf_sound_stream) }
}
#[must_use]
pub fn is_looping(&self) -> bool {
unsafe { sfSoundStream_getLoop(self.sf_sound_stream).into_bool() }
}
pub fn set_looping(&mut self, looping: bool) {
unsafe { sfSoundStream_setLoop(self.sf_sound_stream, SfBoolExt::from_bool(looping)) }
}
}
impl<'a, S: SoundStream> SoundSource for SoundStreamPlayer<'a, S> {
fn set_pitch(&mut self, pitch: f32) {
unsafe { sfSoundStream_setPitch(self.sf_sound_stream, pitch) }
}
fn set_volume(&mut self, volume: f32) {
unsafe { sfSoundStream_setVolume(self.sf_sound_stream, volume) }
}
fn set_position<P: Into<Vector3f>>(&mut self, position: P) {
unsafe { sfSoundStream_setPosition(self.sf_sound_stream, position.into().raw()) }
}
fn set_relative_to_listener(&mut self, relative: bool) {
unsafe {
sfSoundStream_setRelativeToListener(
self.sf_sound_stream,
SfBoolExt::from_bool(relative),
)
}
}
fn set_min_distance(&mut self, distance: f32) {
unsafe { sfSoundStream_setMinDistance(self.sf_sound_stream, distance) }
}
fn set_attenuation(&mut self, attenuation: f32) {
unsafe { sfSoundStream_setAttenuation(self.sf_sound_stream, attenuation) }
}
fn pitch(&self) -> f32 {
unsafe { sfSoundStream_getPitch(self.sf_sound_stream) }
}
fn volume(&self) -> f32 {
unsafe { sfSoundStream_getVolume(self.sf_sound_stream) }
}
fn position(&self) -> Vector3f {
unsafe { Vector3f::from_raw(sfSoundStream_getPosition(self.sf_sound_stream)) }
}
fn is_relative_to_listener(&self) -> bool {
unsafe { sfSoundStream_isRelativeToListener(self.sf_sound_stream).into_bool() }
}
fn min_distance(&self) -> f32 {
unsafe { sfSoundStream_getMinDistance(self.sf_sound_stream) }
}
fn attenuation(&self) -> f32 {
unsafe { sfSoundStream_getAttenuation(self.sf_sound_stream) }
}
}
impl<'a, S: SoundStream> Drop for SoundStreamPlayer<'a, S> {
fn drop(&mut self) {
unsafe {
sfSoundStream_stop(self.sf_sound_stream);
sfSoundStream_destroy(self.sf_sound_stream);
}
}
}