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
Hello, @pdeljanov! First of all: thanks for the great work you have done! Really appreciate.
I am trying to build a simple audio format converter using Symphonia. To be more precise: any format (or at least aac and mp3) to wav. But to be honest, I don't really know rust. So would appreciate any support.
I took this function from the example, and what I am trying to do is just store sample_buf in the output file. Unfortunately, not very successful.
Would love to hear any tips and hints on how I can achieve my goal.
use symphonia::core::audio::SampleBuffer;use symphonia::core::codecs::{DecoderOptions,CODEC_TYPE_NULL};use symphonia::core::errors::Error;use symphonia::core::formats::FormatOptions;use symphonia::core::io::MediaSourceStream;use symphonia::core::meta::MetadataOptions;use symphonia::core::probe::Hint;pubfnmain(path:&str){// Open the media source.let src = std::fs::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();
hint.with_extension("wav");// Use the default options for metadata and format readers.let meta_opts:MetadataOptions = Default::default();let fmt_opts:FormatOptions = Default::default();// Probe the media source.let probed = symphonia::default::get_probe().format(&hint, mss,&fmt_opts,&meta_opts).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();// Create a decoder for the track.letmut decoder = symphonia::default::get_codecs().make(&track.codec_params,&dec_opts).expect("unsupported codec");// Store the track identifier, it will be used to filter packets.let track_id = track.id;letmut sample_count = 0;letmut sample_buf = None;loop{// Get the next packet from the format reader.let packet_unwrap = format.next_packet();if packet_unwrap.is_err(){break;}let packet = packet_unwrap.unwrap();// If the packet does not belong to the selected track, skip it.if packet.track_id() != track_id {continue;}// Decode the packet into audio samples, ignoring any decode errors.match decoder.decode(&packet){Ok(audio_buf) => {// The decoded audio samples may now be accessed via the audio buffer if per-channel// slices of samples in their native decoded format is desired. Use-cases where// the samples need to be accessed in an interleaved order or converted into// another sample format, or a byte buffer is required, are covered by copying the// audio buffer into a sample buffer or raw sample buffer, respectively. In the// example below, we will copy the audio buffer into a sample buffer in an// interleaved order while also converting to a f32 sample format.// If this is the *first* decoded packet, create a sample buffer matching the// decoded audio buffer format.if sample_buf.is_none(){// Get the audio buffer specification.let spec = *audio_buf.spec();// Get the capacity of the decoded buffer. Note: This is capacity, not length!let duration = audio_buf.capacity()asu64;println!("{:?}", duration);// Create the f32 sample buffer.
sample_buf = Some(SampleBuffer::<f32>::new(duration, spec));}// Copy the decoded audio buffer into the sample buffer in an interleaved format.ifletSome(buf) = &mut sample_buf {
buf.copy_interleaved_ref(audio_buf);// The samples may now be access via the `samples()` function.
sample_count += buf.samples().len();print!("\rDecoded {} samples", sample_count);}}Err(Error::DecodeError(_)) => (),Err(_) => break,}}// if let Some(buf) = sample_buf {// // Create a file and open it for writing// let mut file = std::fs::File::create("sample_buf.raw").expect("failed to create file");// // Write the sample buffer to the file// buf.write(&mut file).expect("failed to write to file");// }}
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
-
Hello, @pdeljanov! First of all: thanks for the great work you have done! Really appreciate.
I am trying to build a simple audio format converter using Symphonia. To be more precise: any format (or at least aac and mp3) to wav. But to be honest, I don't really know rust. So would appreciate any support.
I took this function from the example, and what I am trying to do is just store sample_buf in the output file. Unfortunately, not very successful.
Would love to hear any tips and hints on how I can achieve my goal.
Beta Was this translation helpful? Give feedback.
All reactions