You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
I have a function that takes a path and loads the whole audio file into a Vec<Vec; 2>. I hope to be able to make it faster, as currently it takes couple of seconds to load a file.
To avoid resizing of the Vec<> I'm trying to allocate it with the correct size
I simply loop over the packets and decode it into SampleBuffer, the copy it to the right point in the Vec<>.
Currently it looks like this:
#[derive(Clone)]pubstructSamples{pubsamples:Arc<Vec<Vec<f32>>>,pubrate:usize,pubcount_samples:usize,}/// Load a whole audio file into memorypubfnload_samples_from_audio_file(path:&str) -> Result<Samples, symphonia::core::errors::Error>{// Open the media source.let path = Path::new(&path);let src = File::open(path).expect("failed to open media");// Create the media source stream.let mss = MediaSourceStream::new(Box::new(src),Default::default());// Create a probe hint using the file's extension. [Optional]letmut hint = Hint::new();ifletSome(extension) = path.extension(){
hint.with_extension(&extension.to_string_lossy());};// Probe the media source.let probed = symphonia::default::get_probe().format(&hint, mss,&Default::default(),&Default::default()).expect("unsupported format");// Get the instantiated format reader.letmut format = probed.format;// Find the first audio track with a known (decodeable) codec.let track = format
.tracks().iter().find(|t| t.codec_params.codec != CODEC_TYPE_NULL).expect("no supported audio tracks");// Use the default options for the decoder.let dec_opts:DecoderOptions = Default::default();// Calculate the needed size for our sample vector// We do this now as we will borrow track in the decoderlet samples_capacity:usize = ifletSome(n_frames) = track.codec_params.n_frames{
n_frames asusize}else{0};// Create a decoder for the track.letmut decoder = symphonia::default::get_codecs().make(&track.codec_params,&dec_opts).expect("unsupported codec");// Create sample buffer and retrieve sample ratelet(mut read_buffer, rate) = {// Read first packet and determine sample buffer sizelet packet = format.next_packet()?;let decoded_packet = decoder.decode(&packet)?;let spec = *decoded_packet.spec();let rate = spec.rateasusize;// Get the capacity of the decoded buffer. Note: This is capacity, not length!let duration = decoded_packet.capacity()asu64;letmut read_buffer = SampleBuffer::<f32>::new(duration, spec);// As we loaded the first packet, let's also put it into the sample buffer
read_buffer.copy_planar_ref(decoded_packet);(read_buffer, rate)};// Create vector that will hold all samplesletmut samples:Vec<Vec<f32>> = vec![Vec::with_capacity(samples_capacity); 2];loop{// extract left & right channellet(left, right) = read_buffer.samples().split_at(read_buffer.len() / 2);
samples[0].extend_from_slice(left);
samples[1].extend_from_slice(right);// Decode next packetlet packet = match format.next_packet(){Ok(packet) => packet,Err(_err) => {error!("Err loading next packet: {}", _err);break;}};let decoded_packet = match decoder.decode(&packet){Ok(decoded_packet) => decoded_packet,Err(_err) => {error!("Err decoding next packet: {}", _err);break;}};// Copy decoded packet into read buffer
read_buffer.copy_planar_ref(decoded_packet);}debug!("Precalculated sample count: {}", samples_capacity);debug!("Sample count: {}", samples[0].len());let count_samples = samples[0].len();Ok(Samples{samples:Arc::new(samples),
rate,
count_samples,})}
reacted with thumbs up emoji reacted with thumbs down emoji reacted with laugh emoji reacted with hooray emoji reacted with confused emoji reacted with heart emoji reacted with rocket emoji reacted with eyes emoji
-
I have a function that takes a path and loads the whole audio file into a Vec<Vec; 2>. I hope to be able to make it faster, as currently it takes couple of seconds to load a file.
Currently it looks like this:
Beta Was this translation helpful? Give feedback.
All reactions