Drop task with decoder

This commit is contained in:
Silas Bartha 2024-09-09 21:14:56 -04:00
parent 40fa831b47
commit 128573aa26
Signed by: soaos
GPG Key ID: 9BD3DCC0D56A09B2
2 changed files with 6 additions and 5 deletions

View File

@ -1,7 +1,7 @@
[package] [package]
name = "bevy_rustysynth" name = "bevy_rustysynth"
description = "A plugin which adds MIDI file and soundfont audio support to the bevy engine via rustysynth." description = "A plugin which adds MIDI file and soundfont audio support to the bevy engine via rustysynth."
version = "0.1.0" version = "0.1.1"
edition = "2021" edition = "2021"
license = "0BSD OR MIT OR Apache-2.0" license = "0BSD OR MIT OR Apache-2.0"

View File

@ -8,7 +8,7 @@ use bevy::{
asset::{io::Reader, AssetLoader, AsyncReadExt, LoadContext}, asset::{io::Reader, AssetLoader, AsyncReadExt, LoadContext},
audio::Source, audio::Source,
prelude::*, prelude::*,
tasks::AsyncComputeTaskPool, tasks::{AsyncComputeTaskPool, Task},
}; };
use itertools::Itertools; use itertools::Itertools;
use rustysynth::{MidiFile, MidiFileSequencer, SoundFont, Synthesizer, SynthesizerSettings}; use rustysynth::{MidiFile, MidiFileSequencer, SoundFont, Synthesizer, SynthesizerSettings};
@ -47,6 +47,7 @@ impl AssetLoader for MidiAssetLoader {
pub struct MidiDecoder { pub struct MidiDecoder {
sample_rate: usize, sample_rate: usize,
stream: Receiver<f32>, stream: Receiver<f32>,
_task: Task<()>,
} }
impl MidiDecoder { impl MidiDecoder {
@ -58,7 +59,7 @@ impl MidiDecoder {
let mut midi = Cursor::new(midi); let mut midi = Cursor::new(midi);
let sample_rate = 44100_usize; let sample_rate = 44100_usize;
let (tx, rx) = async_channel::bounded::<f32>(sample_rate * 2); let (tx, rx) = async_channel::bounded::<f32>(sample_rate * 2);
AsyncComputeTaskPool::get() let task = AsyncComputeTaskPool::get()
.spawn(async move { .spawn(async move {
let midi = Arc::new(MidiFile::new(&mut midi).expect("Failed to read midi file.")); let midi = Arc::new(MidiFile::new(&mut midi).expect("Failed to read midi file."));
let settings = SynthesizerSettings::new(sample_rate as i32); let settings = SynthesizerSettings::new(sample_rate as i32);
@ -78,9 +79,9 @@ impl MidiDecoder {
} }
} }
tx.close(); tx.close();
}) });
.detach();
Self { Self {
_task: task,
sample_rate, sample_rate,
stream: rx, stream: rx,
} }