@@ -79,8 +79,11 @@ void AudioChannel::mix(float *buffer, size_t frames) {
79
79
m_position = static_cast <size_t >(readPosition);
80
80
}
81
81
82
- void AudioChannel::play (std::shared_ptr<AudioFile> file, float vol) {
83
- m_currentFile = std::move (file);
82
+ void AudioChannel::play (AudioFile *file, float vol) {
83
+ if (!file)
84
+ return ;
85
+
86
+ m_currentFile = file;
84
87
m_position = 0 ;
85
88
m_volume = vol;
86
89
m_targetVolume = vol;
@@ -90,8 +93,10 @@ void AudioChannel::play(std::shared_ptr<AudioFile> file, float vol) {
90
93
91
94
void AudioChannel::stop () {
92
95
m_active = false ;
93
- m_currentFile. reset () ;
96
+ m_currentFile = nullptr ;
94
97
m_position = 0 ;
98
+ m_currentSpeed = 1 .0f ;
99
+ m_targetSpeed = 1 .0f ;
95
100
}
96
101
97
102
void AudioChannel::setVolume (float vol) {
@@ -217,37 +222,34 @@ AudioEngine::~AudioEngine() {
217
222
stopAll ();
218
223
}
219
224
220
- void AudioEngine::playSound (const std::string &path, float m_volume) {
221
- try {
222
- auto file = std::make_shared<AudioFile>(path);
223
- int channel = findFreeChannel ();
224
- if (channel != -1 ) {
225
- m_commandQueue.pushPlay (file, m_volume, channel);
226
- }
227
- } catch (const AudioFileException &e) {
228
- std::cerr << " Failed to load sound file: " << e.what () << std::endl;
229
- // Handle or propagate error
230
- // Could log error or throw depending on your error handling strategy
225
+ void AudioEngine::playSound (AssetHandle<AudioFile> sound, float volume) {
226
+ if (!sound.isValid ()) {
227
+ std::cerr << " Attempted to play invalid sound asset" << std::endl;
228
+ return ;
229
+ }
230
+
231
+ int channel = findFreeChannel ();
232
+ if (channel != -1 ) {
233
+ m_commandQueue.pushPlay (sound.operator ->(), volume, channel);
231
234
}
232
235
}
233
236
234
- void AudioEngine::playMusic (const std::string &path, bool loop) {
235
- try {
236
- auto file = std::make_shared<AudioFile>(path);
237
- file->setLooping (loop);
237
+ void AudioEngine::playMusic (AssetHandle<AudioFile> music, bool loop) {
238
+ if (!music.isValid ()) {
239
+ std::cerr << " Attempted to play invalid music asset" << std::endl;
240
+ return ;
241
+ }
238
242
239
- // Stop any currently playing music
240
- stopChannel ( 0 );
243
+ // Set looping state on the audio file
244
+ music-> setLooping (loop );
241
245
242
- // Queue the new music
243
- m_commandQueue.pushPlay (file, 1 .0f , 0 ); // Channel 0 reserved for music
244
- } catch (const AudioFileException &e) {
245
- std::cerr << " Failed to load sound file: " << e.what () << std::endl;
246
+ // Stop any currently playing music
247
+ stopChannel (0 );
246
248
247
- // Handle or propagate error
248
- }
249
+ // Queue the new music
250
+ m_commandQueue.pushPlay (music.operator ->(), 1 .0f ,
251
+ 0 ); // Channel 0 reserved for music
249
252
}
250
-
251
253
void AudioEngine::stopChannel (int channelId) {
252
254
if (channelId >= 0 && channelId < static_cast <int >(MAX_CHANNELS)) {
253
255
m_commandQueue.pushStop (channelId);
@@ -379,10 +381,12 @@ void AudioEngine::audioCallback(float *buffer, size_t frames) {
379
381
}
380
382
}
381
383
382
- int AudioEngine::findFreeChannel () const {
384
+ int AudioEngine::findFreeChannel () {
383
385
// Skip channel 0 (reserved for music)
384
386
for (size_t i = 1 ; i < MAX_CHANNELS; ++i) {
385
387
if (!m_channels[i].isActive ()) {
388
+ // Optionally force a stop to ensure clean state
389
+ m_channels[i].stop ();
386
390
return static_cast <int >(i);
387
391
}
388
392
}
0 commit comments