diff --git a/Cargo.lock b/Cargo.lock index b954fcc..b1912ea 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -22,13 +22,13 @@ dependencies = [ [[package]] name = "bitflags" -version = "1.2.1" +version = "1.3.2" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "cf1de2fe8c75bc145a2f577add951f8134889b4795d47466a54a5c846d691693" +checksum = "bef38d45163c2f1dde094a7dfd33ccf595c92905c8f8f4fdc18d06fb1037718a" [[package]] name = "bwavfile" -version = "0.9.3" +version = "1.1.0" dependencies = [ "byteorder", "clap", @@ -39,9 +39,9 @@ dependencies = [ [[package]] name = "byteorder" -version = "1.3.4" +version = "1.4.3" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "08c48aae112d48ed9f069b33538ea9e3e90aa263cfa3d1c24309612b1f7472de" +checksum = "14c189c53d098945499cdfa7ecc63567cf3886b3332b312a5b4585d8d3a6a610" [[package]] name = "clap" @@ -124,24 +124,24 @@ checksum = "a246d82be1c9d791c5dfde9a2bd045fc3cbba3fa2b11ad558f27d01712f00569" [[package]] name = "hermit-abi" -version = "0.1.17" +version = "0.1.19" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "5aca5565f760fb5b220e499d72710ed156fdb74e631659e99377d9ebfbd13ae8" +checksum = "62b467343b94ba476dcb2500d242dadbb39557df889310ac77c5d99100aaac33" dependencies = [ "libc", ] [[package]] name = "itoa" -version = "0.4.7" +version = "0.4.8" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "dd25036021b0de88a0aff6b850051563c6516d0bf53f8638938edbb9de732736" +checksum = "b71991ff56294aa922b450139ee08b3bfc70982c6b2c7562771375cf73542dd4" [[package]] name = "libc" -version = "0.2.81" +version = "0.2.103" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "1482821306169ec4d07f6aca392a4681f66c75c9918aa49641a2595db64053cb" +checksum = "dd8f7255a17a627354f321ef0055d63b898c6fb27eff628af4d1b66b7331edf6" [[package]] name = "ryu" @@ -151,15 +151,15 @@ checksum = "71d301d4193d031abdd79ff7e3dd721168a9572ef3fe51a1517aba235bd8f86e" [[package]] name = "serde" -version = "1.0.118" +version = "1.0.130" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "06c64263859d87aa2eb554587e2d23183398d617427327cf2b3d0ed8c69e4800" +checksum = "f12d06de37cf59146fbdecab66aa99f9fe4f78722e3607577a5375d66bd0c913" [[package]] name = "serde_json" -version = "1.0.61" +version = "1.0.68" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "4fceb2595057b6891a4ee808f70054bd2d12f0e97f1cbb78689b59f676df325a" +checksum = "0f690853975602e1bfe1ccbf50504d67174e3bcf340f23b5ea9992e0587a52d8" dependencies = [ "itoa", "ryu", @@ -183,15 +183,15 @@ dependencies = [ [[package]] name = "unicode-width" -version = "0.1.8" +version = "0.1.9" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "9337591893a19b88d8d87f2cec1e73fad5cdfd10e5a6f349f498ad6ea2ffb1e3" +checksum = "3ed742d4ea2bd1176e236172c8429aaf54486e7ac098db29ffe6529e0ce50973" [[package]] name = "uuid" -version = "0.8.1" +version = "0.8.2" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "9fde2f6a4bea1d6e007c4ad38c6839fa71cbb63b6dbf5b595aa38dc9b1093c11" +checksum = "bc5cf98d8186244414c848017f0e2676b3fcb46807f6668a97dfe67359a3c4b7" [[package]] name = "vec_map" diff --git a/Cargo.toml b/Cargo.toml index 835e089..5ed401f 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -1,6 +1,6 @@ [package] name = "bwavfile" -version = "0.9.3" +version = "1.1.0" authors = ["Jamie Hardt "] edition = "2018" license = "MIT" diff --git a/README.md b/README.md index 8688f74..57a9b9f 100644 --- a/README.md +++ b/README.md @@ -4,72 +4,45 @@ [![GitHub Workflow Status](https://img.shields.io/github/workflow/status/iluvcapra/bwavfile/Rust)](https://github.com/iluvcapra/bwavfile/actions?query=workflow%3ARust) # bwavfile -Rust Wave File Reader/Writer with Broadcast-WAV, MBWF and RF64 Support +Wave File Reader/Writer library in Rust, with Broadcast-WAV, MBWF and RF64 Support -### Features +## Features -This is currently a work-in-progress! However many features presently work: +__bwavfile__ provides a reader `WaveReader` and writer type `WaveWriter` for +reading and creating new audio files respectively. -| Feature |Read |Write| -|---------------------------------------|:---:|:-----:| -| Standard .wav files | ☑️ | ☑️ | -| Transparent promotion to RF64/BW64 | ☑️ | ☑️ | -| Unified interface for regular and extended Wave format | ☑️ | ☑️ | -| Channel/speaker map metadata | ☑️ | ☑️ | -| Ambisonic B-format metadata | ☑️ | ☑️ | -| EBU Broadcast-WAVE metadata | ☑️ | ☑️ | -| Basic iXML/ADM metadata | ☑️ | ☑️ | -| Enhanced iXML metadata support | | | -| ADM `chna` channel metadata | | | -| Broadcast-WAVE Level overview `levl` metadata | | | -| Cue list metadata | ☑️ | | -| Sampler and instrument metadata | | | -| Enhanced Wave file form validation | ☑️ | | +`WaveReader` and `WaveWriter` support: + * A unified interface for standard RIFF and RF64/BW64 64-bit Wave files. + * When using `WaveWriter`, wave files are transparently upgraded from RIFF + to RF64 when required. + * Unpacked reading and writing of Integer PCM and IEEE float audio data + formats. + * A unified interface for standard `WaveFormat` and extended `WaveFormatEx` + wave data format specification. + * Multichannel, surround, and ambisonic audio data description including + surround channel maps, ADM `AudioTrackFormat`, `AudioChannelFormatRef` and + `AudioPackRef` data structures. + * Broadcast-Wave metdata extension, including long description, originator + information, SMPTE UMID and coding history. + * Reading and writing of embedded iXML and axml/ADM metadata. + * Reading and writing of timed cues and and timed cue regions. + +### Feature Roadmap + +Some features that may be included in the future include: + * Broadcast-Wave `levl` waveform overview data reading and writing. + * Sampler and Instrument metadata. + * Performance improvements. ## Use Examples -### Examples Directory - -Check out the [examples](examples) directory for some practical use cases: - * [blits](examples/blits.rs) shows how to use `WaveWriter` to create a new file with BLITS alignment tones. - -### Reading Audio Frames From a File - -```rust - - use bwavfile::WaveReader; - let mut r = WaveReader::open("tests/media/ff_silence.wav").unwrap(); - - let format = r.format().unwrap(); - assert_eq!(format.sample_rate, 44100); - assert_eq!(format.channel_count, 1); - - let mut buffer = format.create_frame_buffer(); - let mut frame_reader = r.audio_frame_reader().unwrap(); - - let read = frame_reader.read_integer_frame(&mut buffer).unwrap(); - - assert_eq!(buffer, [0i32]); - assert_eq!(read, 1); -``` - -### Accessing Channel Descriptions - -```rust - use bwavfile::{WaveReader, ChannelMask}; - - let mut f = WaveReader::open("tests/media/pt_24bit_51.wav").unwrap(); - - let chans = f.channels().unwrap(); - assert_eq!(chans[0].index, 0); - assert_eq!(chans[0].speaker, ChannelMask::FrontLeft); - assert_eq!(chans[3].index, 3); - assert_eq!(chans[3].speaker, ChannelMask::LowFrequency); - assert_eq!(chans[4].speaker, ChannelMask::BackLeft); -``` + * [wave-inter](examples/wave-inter.rs) uses `WaveReader` and `WaveWriter` to + interleave several input Wave files into a single polyphonic Wave file. + * [wave-deinter](examples/wave-deinter.rs) uses `WaveReader` and `WaveWriter` + to de-interleave an input Wave file into several monoarual Wave files. ## Note on Testing diff --git a/src/cue.rs b/src/cue.rs index 9a84076..74b1aec 100644 --- a/src/cue.rs +++ b/src/cue.rs @@ -281,7 +281,16 @@ pub struct Cue { pub label : Option, /// The text "note"/comment of this marker if provided - pub note : Option + pub note : Option, + + /// The offser of this marker + /// + /// **Note:** Applications use the `frame` and `offset` fields + /// in different ways. iZotope RX Audio Editor writes the + /// marker position to *both* fields, while a Sound Devices + /// recorder writes the marker position to *only* the `offset` + /// field. + pub offset : u32 } @@ -306,7 +315,7 @@ impl Cue { chunk_id: DATA_SIG, chunk_start: 0, block_start: 0, - frame_offset: cue.frame + frame_offset: cue.offset }; let raw_label = cue.label.as_ref().map(|val| { @@ -380,10 +389,11 @@ impl Cue { //.filter_map(|x| str::from_utf8(&x.text).ok()) .map(|s| convert_to_cue_string(&s.text)) .next() - } + }, + offset: i.frame_offset } }).collect() ) } -} \ No newline at end of file +} diff --git a/src/errors.rs b/src/errors.rs index 291812e..fe2eff5 100644 --- a/src/errors.rs +++ b/src/errors.rs @@ -1,4 +1,5 @@ -use std::io; +use std::{fmt::{Debug,Display}, io}; +use std::error::Error as StdError; use super::fourcc::FourCC; use uuid; @@ -43,6 +44,14 @@ pub enum Error { } +impl StdError for Error {} + +impl Display for Error { + fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result { + Debug::fmt(self, f) + } +} + impl From for Error { fn from(error: io::Error) -> Error { diff --git a/src/fmt.rs b/src/fmt.rs index e18326c..027c8f3 100644 --- a/src/fmt.rs +++ b/src/fmt.rs @@ -18,6 +18,7 @@ use byteorder::{WriteBytesExt, ReadBytesExt}; /// `AudioProgramme`. /// /// See BS.2088-1 § 8, also BS.2094, also blahblahblah... +#[derive(Debug)] pub struct ADMAudioID { pub track_uid: [char; 12], pub channel_format_ref: [char; 14], @@ -28,6 +29,7 @@ pub struct ADMAudioID { /// /// This information is correlated from the Wave format ChannelMap field and /// the `chna` chunk, if present. +#[derive(Debug)] pub struct ChannelDescriptor { /// Index, the offset of this channel's samples in one frame. pub index: u16, diff --git a/src/lib.rs b/src/lib.rs index 2f2103f..0ecb58c 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -31,16 +31,11 @@ Apps we test against: - iZotope RX Audio Editor - FFMpeg - Audacity +- Sound Devices field recorders: 702T, MixPre-10 II [github]: https://github.com/iluvcapra/bwavfile */ -// #![feature(external_doc)] - -// #[doc(include="../README.md")] -// #[cfg(doctest)] -// pub struct ReadmeDoctests; - extern crate encoding; extern crate byteorder; extern crate uuid; diff --git a/src/wavebuffer.rs b/src/wavebuffer.rs new file mode 100644 index 0000000..a1d19ec --- /dev/null +++ b/src/wavebuffer.rs @@ -0,0 +1,3 @@ +pub struct WaveBuffer { + pub format : WaveFmt +} \ No newline at end of file diff --git a/src/wavereader.rs b/src/wavereader.rs index 977d798..5fa7b2e 100644 --- a/src/wavereader.rs +++ b/src/wavereader.rs @@ -50,7 +50,7 @@ impl AudioFrameReader { "Unable to read audio frames from packed formats: block alignment is {}, should be {}", format.block_alignment, (format.bits_per_sample / 8 ) * format.channel_count); - assert!(format.common_format() == CommonFormat::IntegerPCM , + assert!(format.common_format() == CommonFormat::IntegerPCM || format.common_format() == CommonFormat::IeeeFloatPCM, "Unsupported format tag {:?}", format.tag); inner.seek(Start(start))?; @@ -120,6 +120,29 @@ impl AudioFrameReader { Ok( 0 ) } } + + pub fn read_float_frame(&mut self, buffer: &mut [f32]) -> Result { + assert!(buffer.len() as u16 == self.format.channel_count, + "read_float_frame was called with a mis-sized buffer, expected {}, was {}", + self.format.channel_count, buffer.len()); + + let framed_bits_per_sample = self.format.block_alignment * 8 / self.format.channel_count; + + let tell = self.inner.seek(Current(0))?; + + if (tell - self.start) < self.length { + for n in 0..(self.format.channel_count as usize) { + buffer[n] = match (self.format.bits_per_sample, framed_bits_per_sample) { + (25..=32,32) => self.inner.read_f32::()?, + (b,_)=> panic!("Unrecognized integer format, bits per sample {}, channels {}, block_alignment {}", + b, self.format.channel_count, self.format.block_alignment) + } + } + Ok( 1 ) + } else { + Ok( 0 ) + } + } } /// Wave, Broadcast-WAV and RF64/BW64 parser/reader. diff --git a/tests/integration_test.rs b/tests/integration_test.rs index 2560fca..17b954e 100644 --- a/tests/integration_test.rs +++ b/tests/integration_test.rs @@ -158,7 +158,7 @@ fn test_channels_stereo_no_fmt_extended() { assert_eq!(channels[1].speaker,ChannelMask::FrontRight); } -//See issue 6 and 7 +///See issue 6 and 7 #[test] fn test_frame_reader_consumes_reader() { // Issue #6 @@ -175,4 +175,48 @@ fn test_frame_reader_consumes_reader() { } let _result = from_wav_filename("tests/media/pt_24bit_stereo.wav").unwrap(); +} + +///See to PR#10 +#[test] +fn test_cue_read_sounddevices() { + let mut f = WaveReader::open("tests/media/sounddevices_6_cue_points.wav").unwrap(); + let cue_points = f.cue_points().unwrap(); + assert_eq!(cue_points.len(), 6); + + assert_eq!(cue_points[0].frame, 0); + assert_eq!(cue_points[0].length, None); + assert_eq!(cue_points[0].label, None); + assert_eq!(cue_points[0].note, None); + assert_eq!(cue_points[0].offset, 90112); + + assert_eq!(cue_points[1].frame, 0); + assert_eq!(cue_points[1].length, None); + assert_eq!(cue_points[1].label, None); + assert_eq!(cue_points[1].note, None); + assert_eq!(cue_points[1].offset, 176128); + + assert_eq!(cue_points[2].frame, 0); + assert_eq!(cue_points[2].length, None); + assert_eq!(cue_points[2].label, None); + assert_eq!(cue_points[2].note, None); + assert_eq!(cue_points[2].offset, 237568); + + assert_eq!(cue_points[3].frame, 0); + assert_eq!(cue_points[3].length, None); + assert_eq!(cue_points[3].label, None); + assert_eq!(cue_points[3].note, None); + assert_eq!(cue_points[3].offset, 294912); + + assert_eq!(cue_points[4].frame, 0); + assert_eq!(cue_points[4].length, None); + assert_eq!(cue_points[4].label, None); + assert_eq!(cue_points[4].note, None); + assert_eq!(cue_points[4].offset, 380928); + + assert_eq!(cue_points[5].frame, 0); + assert_eq!(cue_points[5].length, None); + assert_eq!(cue_points[5].label, None); + assert_eq!(cue_points[5].note, None); + assert_eq!(cue_points[5].offset, 385024); } \ No newline at end of file diff --git a/tests/test_media.tgz b/tests/test_media.tgz index 3bfecca..4ab0005 100644 Binary files a/tests/test_media.tgz and b/tests/test_media.tgz differ