mirror of
https://github.com/iluvcapra/bwavfile.git
synced 2026-01-01 09:20:45 +00:00
Compare commits
185 Commits
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
a625b485d9 | ||
|
|
d17c6badfa | ||
|
|
bc6be79715 | ||
|
|
763e9a10a3 | ||
|
|
319845ae49 | ||
|
|
53fcecfc45 | ||
|
|
6776820e16 | ||
|
|
eee3c3f625 | ||
|
|
6375567af1 | ||
|
|
1a169da48f | ||
|
|
50b7b7b640 | ||
|
|
72d6be406e | ||
|
|
814b54a6e8 | ||
|
|
e826628096 | ||
|
|
0a307fbf05 | ||
|
|
e941358c8d | ||
|
|
c2cff2a893 | ||
|
|
94eb36bbb5 | ||
|
|
f2c53061dc | ||
|
|
190e8bc677 | ||
|
|
80be74c8fb | ||
|
|
5057862258 | ||
|
|
75fc40d638 | ||
|
|
2780bfb31b | ||
|
|
b160284e21 | ||
|
|
8d55b126ae | ||
|
|
b1c806598e | ||
|
|
58e43902cd | ||
|
|
48517c22bc | ||
|
|
634c21dc9a | ||
|
|
b7f82a9d63 | ||
|
|
3b88b95065 | ||
|
|
a21e17ee78 | ||
|
|
e495ad704e | ||
|
|
bbab81746f | ||
|
|
ca1d5eb714 | ||
|
|
e652722250 | ||
|
|
d0b4582dca | ||
|
|
4a0a9b7ec1 | ||
|
|
a851ed7a7f | ||
|
|
fc21173312 | ||
|
|
350599411f | ||
|
|
ec462a0b96 | ||
|
|
f0b1c51bd8 | ||
|
|
e14bcd8c76 | ||
|
|
1cb7174861 | ||
|
|
a855410d6f | ||
|
|
28b9272456 | ||
|
|
62c9aa7262 | ||
|
|
1f56e0f380 | ||
|
|
ef8a3adf69 | ||
|
|
daeb69c08c | ||
|
|
aca56558bc | ||
|
|
b11e1d7354 | ||
|
|
dfaf55955d | ||
|
|
7ea7ac5ce7 | ||
|
|
41977adb83 | ||
|
|
f63d6279c3 | ||
|
|
84942a4186 | ||
|
|
78ad1ae114 | ||
|
|
5e4c2c7da9 | ||
|
|
d43ddf6338 | ||
|
|
84366089ba | ||
|
|
cbfcce235c | ||
|
|
087d98b228 | ||
|
|
f978eb95ed | ||
|
|
1f8542a7ef | ||
|
|
25589ea848 | ||
|
|
d242dff686 | ||
|
|
155a26ace0 | ||
|
|
1d2edcb675 | ||
|
|
213a856e41 | ||
|
|
70bf402776 | ||
|
|
3ab3a28d0e | ||
|
|
620ca8a968 | ||
|
|
bb6390a95c | ||
|
|
15b4ccf851 | ||
|
|
ea9a0b6cbe | ||
|
|
95700b642d | ||
|
|
f3b646868f | ||
|
|
b303a74d45 | ||
|
|
73d2cf8cd9 | ||
|
|
e4fc4732b5 | ||
|
|
e8b030bd1e | ||
|
|
1219a4162f | ||
|
|
9a275a69c3 | ||
|
|
3d7c74fc94 | ||
|
|
e6e42f1c09 | ||
|
|
465bad40fc | ||
|
|
388c46d0ee | ||
|
|
5dba30fe23 | ||
|
|
84e4ed1e50 | ||
|
|
23dccecedb | ||
|
|
e2e029d3a3 | ||
|
|
1edfa7384d | ||
|
|
c73c9ad81d | ||
|
|
1edce82525 | ||
|
|
f4b27f8545 | ||
|
|
4cb6105073 | ||
|
|
ad0736dcc6 | ||
|
|
45311ad748 | ||
|
|
e502913f4f | ||
|
|
4890483dcd | ||
|
|
8519854596 | ||
|
|
c947904d0f | ||
|
|
2323d61ee9 | ||
|
|
60af2f43ff | ||
|
|
eb431f865f | ||
|
|
8b049e4245 | ||
|
|
dfe90fba4a | ||
|
|
b2d4bff28b | ||
|
|
79173b0576 | ||
|
|
e15e36d65d | ||
|
|
1dc5f153b0 | ||
|
|
7a9f9c8bb3 | ||
|
|
0dd7ee4d18 | ||
|
|
a213e748db | ||
|
|
d221629b3e | ||
|
|
2fcb211a67 | ||
|
|
dfe513b596 | ||
|
|
7e1c368862 | ||
|
|
8985361029 | ||
|
|
e23529e7b6 | ||
|
|
538aedd413 | ||
|
|
09a9413ff2 | ||
|
|
016f5e3e3b | ||
|
|
11b834be76 | ||
|
|
a4ded50112 | ||
|
|
9b2a9783a0 | ||
|
|
1e53436d4b | ||
|
|
a9d9081fad | ||
|
|
c43144c9db | ||
|
|
082a8596af | ||
|
|
208aa7f064 | ||
|
|
69da33b3dc | ||
|
|
9cbb2664d4 | ||
|
|
a4c4936665 | ||
|
|
5f69ec8c61 | ||
|
|
ac6bd9c1e8 | ||
|
|
010f261598 | ||
|
|
b40e8edf23 | ||
|
|
3c221bce40 | ||
|
|
883b6c73e8 | ||
|
|
5bd292e964 | ||
|
|
34e473dc49 | ||
|
|
358aa06f7c | ||
|
|
8191eedf14 | ||
|
|
521e9d0670 | ||
|
|
dea68662f8 | ||
|
|
0685e7baca | ||
|
|
123d971624 | ||
|
|
f410cda9ed | ||
|
|
cf06b50fea | ||
|
|
e8d679b725 | ||
|
|
e63ab6bffd | ||
|
|
271da75f90 | ||
|
|
6c393d9958 | ||
|
|
99e5d6fe29 | ||
|
|
bebfa235e6 | ||
|
|
a5c55dbcf1 | ||
|
|
02a91f2b1d | ||
|
|
781e8769b0 | ||
|
|
e637dc86d3 | ||
|
|
d9f8855f84 | ||
|
|
0da6594081 | ||
|
|
a3920a922e | ||
|
|
afbeb7d737 | ||
|
|
bfbe0f0b9d | ||
|
|
d242e33de3 | ||
|
|
da229c7396 | ||
|
|
5b480c919b | ||
|
|
5cd58c236b | ||
|
|
048f58d856 | ||
|
|
79f146a222 | ||
|
|
5384ebfdc5 | ||
|
|
c6ffbc4559 | ||
|
|
46fe38e3d6 | ||
|
|
6d2223d8c2 | ||
|
|
84f5a955ab | ||
|
|
013145d267 | ||
|
|
63ee997eb3 | ||
|
|
c5072c76b0 | ||
|
|
acd0fe6793 | ||
|
|
9178e48a5b | ||
|
|
044d1327c0 |
28
.github/workflows/rust.yml
vendored
Normal file
28
.github/workflows/rust.yml
vendored
Normal file
@@ -0,0 +1,28 @@
|
|||||||
|
name: Rust
|
||||||
|
|
||||||
|
on:
|
||||||
|
push:
|
||||||
|
branches: [ master ]
|
||||||
|
pull_request:
|
||||||
|
branches: [ master ]
|
||||||
|
|
||||||
|
env:
|
||||||
|
CARGO_TERM_COLOR: always
|
||||||
|
|
||||||
|
jobs:
|
||||||
|
build:
|
||||||
|
|
||||||
|
runs-on: ubuntu-latest
|
||||||
|
|
||||||
|
steps:
|
||||||
|
- uses: actions/checkout@v2
|
||||||
|
# - name: Install ffmpeg
|
||||||
|
# run: sudo apt-get install ffmpeg
|
||||||
|
- name: Create Test Media
|
||||||
|
run: cd tests; sh create_test_media.sh
|
||||||
|
- name: Build
|
||||||
|
run: cargo build --verbose
|
||||||
|
- name: Run tests
|
||||||
|
run: cargo test --verbose
|
||||||
|
# - name: rust-tarpaulin
|
||||||
|
# uses: actions-rs/tarpaulin@v0.1.0
|
||||||
3
.gitignore
vendored
3
.gitignore
vendored
@@ -1,4 +1,5 @@
|
|||||||
/target
|
/target
|
||||||
.DS_Store
|
.DS_Store
|
||||||
target/*
|
target/*
|
||||||
tests/media/*
|
tests/media/*
|
||||||
|
tests/.DS_Store
|
||||||
|
|||||||
149
Cargo.lock
generated
149
Cargo.lock
generated
@@ -1,18 +1,62 @@
|
|||||||
# This file is automatically @generated by Cargo.
|
# This file is automatically @generated by Cargo.
|
||||||
# It is not intended for manual editing.
|
# It is not intended for manual editing.
|
||||||
|
[[package]]
|
||||||
|
name = "ansi_term"
|
||||||
|
version = "0.11.0"
|
||||||
|
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||||
|
checksum = "ee49baf6cb617b853aa8d93bf420db2383fab46d314482ca2803b40d5fde979b"
|
||||||
|
dependencies = [
|
||||||
|
"winapi",
|
||||||
|
]
|
||||||
|
|
||||||
|
[[package]]
|
||||||
|
name = "atty"
|
||||||
|
version = "0.2.14"
|
||||||
|
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||||
|
checksum = "d9b39be18770d11421cdb1b9947a45dd3f37e93092cbf377614828a319d5fee8"
|
||||||
|
dependencies = [
|
||||||
|
"hermit-abi",
|
||||||
|
"libc",
|
||||||
|
"winapi",
|
||||||
|
]
|
||||||
|
|
||||||
|
[[package]]
|
||||||
|
name = "bitflags"
|
||||||
|
version = "1.2.1"
|
||||||
|
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||||
|
checksum = "cf1de2fe8c75bc145a2f577add951f8134889b4795d47466a54a5c846d691693"
|
||||||
|
|
||||||
[[package]]
|
[[package]]
|
||||||
name = "bwavfile"
|
name = "bwavfile"
|
||||||
version = "0.1.1"
|
version = "1.0.0"
|
||||||
dependencies = [
|
dependencies = [
|
||||||
"byteorder",
|
"byteorder",
|
||||||
|
"clap",
|
||||||
"encoding",
|
"encoding",
|
||||||
|
"serde_json",
|
||||||
|
"uuid",
|
||||||
]
|
]
|
||||||
|
|
||||||
[[package]]
|
[[package]]
|
||||||
name = "byteorder"
|
name = "byteorder"
|
||||||
version = "1.3.4"
|
version = "1.4.3"
|
||||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||||
checksum = "08c48aae112d48ed9f069b33538ea9e3e90aa263cfa3d1c24309612b1f7472de"
|
checksum = "14c189c53d098945499cdfa7ecc63567cf3886b3332b312a5b4585d8d3a6a610"
|
||||||
|
|
||||||
|
[[package]]
|
||||||
|
name = "clap"
|
||||||
|
version = "2.33.3"
|
||||||
|
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||||
|
checksum = "37e58ac78573c40708d45522f0d80fa2f01cc4f9b4e2bf749807255454312002"
|
||||||
|
dependencies = [
|
||||||
|
"ansi_term",
|
||||||
|
"atty",
|
||||||
|
"bitflags",
|
||||||
|
"strsim",
|
||||||
|
"textwrap",
|
||||||
|
"unicode-width",
|
||||||
|
"vec_map",
|
||||||
|
]
|
||||||
|
|
||||||
[[package]]
|
[[package]]
|
||||||
name = "encoding"
|
name = "encoding"
|
||||||
@@ -77,3 +121,102 @@ name = "encoding_index_tests"
|
|||||||
version = "0.1.4"
|
version = "0.1.4"
|
||||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||||
checksum = "a246d82be1c9d791c5dfde9a2bd045fc3cbba3fa2b11ad558f27d01712f00569"
|
checksum = "a246d82be1c9d791c5dfde9a2bd045fc3cbba3fa2b11ad558f27d01712f00569"
|
||||||
|
|
||||||
|
[[package]]
|
||||||
|
name = "hermit-abi"
|
||||||
|
version = "0.1.18"
|
||||||
|
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||||
|
checksum = "322f4de77956e22ed0e5032c359a0f1273f1f7f0d79bfa3b8ffbc730d7fbcc5c"
|
||||||
|
dependencies = [
|
||||||
|
"libc",
|
||||||
|
]
|
||||||
|
|
||||||
|
[[package]]
|
||||||
|
name = "itoa"
|
||||||
|
version = "0.4.7"
|
||||||
|
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||||
|
checksum = "dd25036021b0de88a0aff6b850051563c6516d0bf53f8638938edbb9de732736"
|
||||||
|
|
||||||
|
[[package]]
|
||||||
|
name = "libc"
|
||||||
|
version = "0.2.97"
|
||||||
|
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||||
|
checksum = "12b8adadd720df158f4d70dfe7ccc6adb0472d7c55ca83445f6a5ab3e36f8fb6"
|
||||||
|
|
||||||
|
[[package]]
|
||||||
|
name = "ryu"
|
||||||
|
version = "1.0.5"
|
||||||
|
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||||
|
checksum = "71d301d4193d031abdd79ff7e3dd721168a9572ef3fe51a1517aba235bd8f86e"
|
||||||
|
|
||||||
|
[[package]]
|
||||||
|
name = "serde"
|
||||||
|
version = "1.0.126"
|
||||||
|
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||||
|
checksum = "ec7505abeacaec74ae4778d9d9328fe5a5d04253220a85c4ee022239fc996d03"
|
||||||
|
|
||||||
|
[[package]]
|
||||||
|
name = "serde_json"
|
||||||
|
version = "1.0.64"
|
||||||
|
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||||
|
checksum = "799e97dc9fdae36a5c8b8f2cae9ce2ee9fdce2058c57a93e6099d919fd982f79"
|
||||||
|
dependencies = [
|
||||||
|
"itoa",
|
||||||
|
"ryu",
|
||||||
|
"serde",
|
||||||
|
]
|
||||||
|
|
||||||
|
[[package]]
|
||||||
|
name = "strsim"
|
||||||
|
version = "0.8.0"
|
||||||
|
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||||
|
checksum = "8ea5119cdb4c55b55d432abb513a0429384878c15dde60cc77b1c99de1a95a6a"
|
||||||
|
|
||||||
|
[[package]]
|
||||||
|
name = "textwrap"
|
||||||
|
version = "0.11.0"
|
||||||
|
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||||
|
checksum = "d326610f408c7a4eb6f51c37c330e496b08506c9457c9d34287ecc38809fb060"
|
||||||
|
dependencies = [
|
||||||
|
"unicode-width",
|
||||||
|
]
|
||||||
|
|
||||||
|
[[package]]
|
||||||
|
name = "unicode-width"
|
||||||
|
version = "0.1.8"
|
||||||
|
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||||
|
checksum = "9337591893a19b88d8d87f2cec1e73fad5cdfd10e5a6f349f498ad6ea2ffb1e3"
|
||||||
|
|
||||||
|
[[package]]
|
||||||
|
name = "uuid"
|
||||||
|
version = "0.8.2"
|
||||||
|
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||||
|
checksum = "bc5cf98d8186244414c848017f0e2676b3fcb46807f6668a97dfe67359a3c4b7"
|
||||||
|
|
||||||
|
[[package]]
|
||||||
|
name = "vec_map"
|
||||||
|
version = "0.8.2"
|
||||||
|
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||||
|
checksum = "f1bddf1187be692e79c5ffeab891132dfb0f236ed36a43c7ed39f1165ee20191"
|
||||||
|
|
||||||
|
[[package]]
|
||||||
|
name = "winapi"
|
||||||
|
version = "0.3.9"
|
||||||
|
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||||
|
checksum = "5c839a674fcd7a98952e593242ea400abe93992746761e38641405d28b00f419"
|
||||||
|
dependencies = [
|
||||||
|
"winapi-i686-pc-windows-gnu",
|
||||||
|
"winapi-x86_64-pc-windows-gnu",
|
||||||
|
]
|
||||||
|
|
||||||
|
[[package]]
|
||||||
|
name = "winapi-i686-pc-windows-gnu"
|
||||||
|
version = "0.4.0"
|
||||||
|
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||||
|
checksum = "ac3b87c63620426dd9b991e5ce0329eff545bccbbb34f3be09ff6fb6ab51b7b6"
|
||||||
|
|
||||||
|
[[package]]
|
||||||
|
name = "winapi-x86_64-pc-windows-gnu"
|
||||||
|
version = "0.4.0"
|
||||||
|
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||||
|
checksum = "712e227841d057c1ee1cd2fb22fa7e5a5461ae8e48fa2ca79ec42cfc1931183f"
|
||||||
|
|||||||
12
Cargo.toml
12
Cargo.toml
@@ -1,10 +1,9 @@
|
|||||||
[package]
|
[package]
|
||||||
name = "bwavfile"
|
name = "bwavfile"
|
||||||
version = "0.1.1"
|
version = "1.0.0"
|
||||||
authors = ["Jamie Hardt <jamiehardt@me.com>"]
|
authors = ["Jamie Hardt <jamiehardt@me.com>"]
|
||||||
edition = "2018"
|
edition = "2018"
|
||||||
license = "MIT"
|
license = "MIT"
|
||||||
license-file = "LICENSE"
|
|
||||||
description = "Rust Wave File Reader/Writer with Broadcast-WAV, MBWF and RF64 Support"
|
description = "Rust Wave File Reader/Writer with Broadcast-WAV, MBWF and RF64 Support"
|
||||||
homepage = "https://github.com/iluvcapra/bwavfile"
|
homepage = "https://github.com/iluvcapra/bwavfile"
|
||||||
readme = "README.md"
|
readme = "README.md"
|
||||||
@@ -17,3 +16,12 @@ keywords = ["audio", "broadcast", "multimedia","smpte"]
|
|||||||
[dependencies]
|
[dependencies]
|
||||||
byteorder = "1.3.4"
|
byteorder = "1.3.4"
|
||||||
encoding = "0.2.33"
|
encoding = "0.2.33"
|
||||||
|
uuid = "0.8.1"
|
||||||
|
clap = "2.33.3"
|
||||||
|
|
||||||
|
[dev-dependencies]
|
||||||
|
serde_json = "1.0.61"
|
||||||
|
|
||||||
|
[profile.release]
|
||||||
|
debug = true
|
||||||
|
|
||||||
|
|||||||
2
LICENSE
2
LICENSE
@@ -1,6 +1,6 @@
|
|||||||
MIT License
|
MIT License
|
||||||
|
|
||||||
Copyright (c) 2020 Jamie Hardt
|
Copyright (c) 2021 Jamie Hardt
|
||||||
|
|
||||||
Permission is hereby granted, free of charge, to any person obtaining a copy
|
Permission is hereby granted, free of charge, to any person obtaining a copy
|
||||||
of this software and associated documentation files (the "Software"), to deal
|
of this software and associated documentation files (the "Software"), to deal
|
||||||
|
|||||||
90
README.md
90
README.md
@@ -1,69 +1,53 @@
|
|||||||

|
[](LICENSE)
|
||||||

|
[](https://crates.io/crates/bwavfile/)
|
||||||

|

|
||||||
|
[](https://github.com/iluvcapra/bwavfile/actions?query=workflow%3ARust)
|
||||||
|
|
||||||
# bwavfile
|
# 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
|
||||||
|
|
||||||
This is currently a work-in-progress!
|
## Features
|
||||||
|
|
||||||
## Use
|
__bwavfile__ provides a reader `WaveReader` and writer type `WaveWriter` for
|
||||||
|
reading and creating new audio files respectively.
|
||||||
|
|
||||||
```rust
|
`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 region.
|
||||||
|
|
||||||
let path = "tests/media/ff_silence.wav";
|
### Feature Roadmap
|
||||||
|
|
||||||
let mut w = WaveReader::open(path)?;
|
Some features that may be included in the future include:
|
||||||
let length = w.frame_length()?;
|
* Broadcast-Wave `levl` waveform overview data reading and writing.
|
||||||
let format = w.format()?;
|
* Sampler and Instrument mentadata.
|
||||||
|
|
||||||
let bext = w.broadcast_extension()?;
|
|
||||||
println!("Description field: {}", &bext.description);
|
|
||||||
println!("Originator field: {}", &bext.originator);
|
|
||||||
|
|
||||||
let frame_reader = w.audio_frame_reader()?;
|
## Use Examples
|
||||||
|
|
||||||
let mut buffer: Vec<i32> = w.create_frame_buffer();
|
* [blits](examples/blits.rs) shows how to use `WaveWriter` to create a new
|
||||||
while( frame_reader.read_integer_frame(&mut buffer) > 0) {
|
file with BLITS alignment tones.
|
||||||
println!("Read frames {:?}", &buffer);
|
* [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
|
## Note on Testing
|
||||||
|
|
||||||
All of the media for the integration tests is committed to the respository
|
All of the media for the integration tests is committed to the respository
|
||||||
in either zipped form or is created by ffmpeg. Before you can run tests, you
|
in zipped form. Before you can run tests, you need to `cd` into the `tests`
|
||||||
will need to have ffmpeg installed, and you need to `cd` into the `tests`
|
directory and run the `create_test_media.sh` script. Note that one of the
|
||||||
directory and run the `create_test_media.sh` script.
|
test files (the RF64 test case) is over four gigs in size.
|
||||||
|
|
||||||
## Resources
|
[rf64test]: https://github.com/iluvcapra/bwavfile/blob/1f8542a7efb481da076120bf8107032c5b48889d/src/wavewriter.rs#L399
|
||||||
|
|
||||||
|
|
||||||
### Implementation of Broadcast Wave Files
|
|
||||||
- [EBU Tech 3285][ebu3285] (May 2011), "Specification of the Broadcast Wave Format (BWF)"
|
|
||||||
|
|
||||||
|
|
||||||
### Implementation of 64-bit Wave Files
|
|
||||||
- [ITU-R 2088][itu2088] (October 2019), "Long-form file format for the international exchange of audio programme materials with metadata"
|
|
||||||
- Presently in force, adopted by the EBU in [EBU Tech 3306v2][ebu3306v2] (June 2018).
|
|
||||||
- [EBU Tech 3306v1][ebu3306v1] (July 2009), "MBWF / RF64: An extended File Format for Audio"
|
|
||||||
- No longer in force, however long-established.
|
|
||||||
|
|
||||||
|
|
||||||
### Implementation of Wave format `fmt` chunk
|
|
||||||
- [MSDN WAVEFORMATEX](https://docs.microsoft.com/en-us/windows/win32/api/mmeapi/ns-mmeapi-waveformatex)
|
|
||||||
- [MSDN WAVEFORMATEXTENSIBLE](https://docs.microsoft.com/en-us/windows/win32/api/mmreg/ns-mmreg-waveformatextensible)
|
|
||||||
|
|
||||||
|
|
||||||
### Other resources
|
|
||||||
- [RFC 3261][rfc3261] (June 1998) "WAVE and AVI Codec Registries"
|
|
||||||
- [Peter Kabal, McGill University](http://www-mmsp.ece.mcgill.ca/Documents/AudioFormats/WAVE/WAVE.html)
|
|
||||||
- [Multimedia Programming Interface and Data Specifications 1.0](http://www-mmsp.ece.mcgill.ca/Documents/AudioFormats/WAVE/Docs/riffmci.pdf)
|
|
||||||
IBM Corporation and Microsoft Corporation, (August 1991)
|
|
||||||
|
|
||||||
[ebu3285]: https://tech.ebu.ch/docs/tech/tech3285.pdf
|
|
||||||
[ebu3306v1]: https://tech.ebu.ch/docs/tech/tech3306v1_1.pdf
|
|
||||||
[ebu3306v2]: https://tech.ebu.ch/docs/tech/tech3306.pdf
|
|
||||||
[itu2088]: https://www.itu.int/dms_pubrec/itu-r/rec/bs/R-REC-BS.2088-1-201910-I!!PDF-E.pdf
|
|
||||||
[rfc3261]: https://tools.ietf.org/html/rfc2361
|
|
||||||
|
|||||||
8
bwavefile.code-workspace
Normal file
8
bwavefile.code-workspace
Normal file
@@ -0,0 +1,8 @@
|
|||||||
|
{
|
||||||
|
"folders": [
|
||||||
|
{
|
||||||
|
"path": "."
|
||||||
|
}
|
||||||
|
],
|
||||||
|
"settings": {}
|
||||||
|
}
|
||||||
266
examples/blits.rs
Normal file
266
examples/blits.rs
Normal file
@@ -0,0 +1,266 @@
|
|||||||
|
//! bilts.rs
|
||||||
|
//! (c) 2021 Jamie Hardt. All rights reserved.
|
||||||
|
//!
|
||||||
|
//! This program demonstrates the creation of a wave file with a BLITS
|
||||||
|
//! ("Black and Lanes' Ident Tones for Surround") channel identification and
|
||||||
|
//! alignment signal.
|
||||||
|
//!
|
||||||
|
//! TODO: Pre-calculate the sine waves to speed up generation
|
||||||
|
//! TODO: Make tone onsets less snappy
|
||||||
|
|
||||||
|
use std::f64;
|
||||||
|
use std::io;
|
||||||
|
|
||||||
|
extern crate bwavfile;
|
||||||
|
use bwavfile::{WaveWriter, WaveFmt, Error};
|
||||||
|
|
||||||
|
#[macro_use]
|
||||||
|
extern crate clap;
|
||||||
|
use clap::{Arg, App};
|
||||||
|
|
||||||
|
|
||||||
|
fn sine_wave(t: u64, amplitude : i32, wavelength : u32) -> i32 {
|
||||||
|
//I did it this way because I'm weird
|
||||||
|
Some(t).map(|i| (i as f64) * 2f64 * f64::consts::PI / wavelength as f64 )
|
||||||
|
.map(|f| f.sin() )
|
||||||
|
.map(|s| (s * amplitude as f64) as i32)
|
||||||
|
.unwrap()
|
||||||
|
}
|
||||||
|
|
||||||
|
/// Return the corresponding f32 gain for a dbfs.
|
||||||
|
///
|
||||||
|
/// Retval will always be positive
|
||||||
|
fn dbfs_to_f32(dbfs : f32) -> f32 {
|
||||||
|
10f32.powf(dbfs / 20f32)
|
||||||
|
}
|
||||||
|
|
||||||
|
fn dbfs_to_signed_int(dbfs: f32, bit_depth: u16) -> i32 {
|
||||||
|
let full_code : i32 = (1i32 << bit_depth - 1) - 1;
|
||||||
|
((full_code as f32) * dbfs_to_f32(dbfs)) as i32
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
#[derive(Clone, Copy, PartialEq)]
|
||||||
|
enum ToneBurst {
|
||||||
|
/// Tone of .0 frequency (hz) for .1 duration (ms) at .2 dBfs
|
||||||
|
Tone(f32, u64, f32),
|
||||||
|
/// Silence of .0 Duration (ms)
|
||||||
|
Silence(u64),
|
||||||
|
}
|
||||||
|
|
||||||
|
impl ToneBurst {
|
||||||
|
fn duration(&self, sample_rate : u32) -> u64 {
|
||||||
|
match self {
|
||||||
|
Self::Tone(_, dur, _) => *dur * sample_rate as u64 / 1000,
|
||||||
|
Self::Silence(dur) => *dur * sample_rate as u64 / 1000
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
trait ToneBurstSignal {
|
||||||
|
|
||||||
|
fn duration(&self, sample_rate: u32) -> u64;
|
||||||
|
|
||||||
|
fn signal(&self, t: u64, sample_rate: u32, bit_depth: u16) -> i32;
|
||||||
|
}
|
||||||
|
|
||||||
|
impl ToneBurstSignal for Vec<ToneBurst> {
|
||||||
|
|
||||||
|
fn duration(&self, sample_rate: u32) -> u64 {
|
||||||
|
self.iter().fold(0u64, |accum, &item| {
|
||||||
|
accum + &item.duration(sample_rate)
|
||||||
|
})
|
||||||
|
}
|
||||||
|
|
||||||
|
fn signal(&self, t: u64, sample_rate: u32, bit_depth: u16) -> i32 {
|
||||||
|
self.iter()
|
||||||
|
.scan(0u64, |accum, &item| {
|
||||||
|
let dur = item.duration(sample_rate);
|
||||||
|
let this_time_range = *accum..(*accum + dur);
|
||||||
|
*accum = *accum + dur;
|
||||||
|
Some( (this_time_range, item) )
|
||||||
|
})
|
||||||
|
.find(|(range, _)| range.contains(&t))
|
||||||
|
.map(|(_, item)| {
|
||||||
|
match item {
|
||||||
|
ToneBurst::Tone(freq, _, dbfs) => {
|
||||||
|
let gain = dbfs_to_signed_int(dbfs, bit_depth);
|
||||||
|
sine_wave(t, gain, (sample_rate as f32 / freq) as u32)
|
||||||
|
},
|
||||||
|
ToneBurst::Silence(_) => {
|
||||||
|
0
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}).unwrap_or(0i32)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
fn create_blits_file(file_name: &str, sample_rate : u32, bits_per_sample : u16) -> Result<(),Error> {
|
||||||
|
|
||||||
|
// BLITS Tone signal format
|
||||||
|
// From EBU Tech 3304 §4 - https://tech.ebu.ch/docs/tech/tech3304.pdf
|
||||||
|
let left_channel_sequence : Vec<ToneBurst> = vec![
|
||||||
|
// channel ident
|
||||||
|
ToneBurst::Tone(880.0, 600, -18.0),
|
||||||
|
ToneBurst::Silence(200),
|
||||||
|
ToneBurst::Silence(4000),
|
||||||
|
|
||||||
|
// LR ident
|
||||||
|
ToneBurst::Tone(1000.0, 1000, -18.0),
|
||||||
|
ToneBurst::Silence(300),
|
||||||
|
ToneBurst::Tone(1000.0, 300, -18.0),
|
||||||
|
ToneBurst::Silence(300),
|
||||||
|
ToneBurst::Tone(1000.0, 300, -18.0),
|
||||||
|
ToneBurst::Silence(300),
|
||||||
|
ToneBurst::Tone(1000.0, 300, -18.0),
|
||||||
|
ToneBurst::Silence(300),
|
||||||
|
ToneBurst::Tone(1000.0, 2000, -18.0),
|
||||||
|
ToneBurst::Silence(300),
|
||||||
|
|
||||||
|
// Phase check,
|
||||||
|
ToneBurst::Tone(2000.0, 3000, -24.0),
|
||||||
|
ToneBurst::Silence(200)
|
||||||
|
];
|
||||||
|
|
||||||
|
let right_channel_sequence : Vec<ToneBurst> = vec![
|
||||||
|
// channel ident
|
||||||
|
ToneBurst::Silence(800),
|
||||||
|
ToneBurst::Tone(880.0, 600, -18.0),
|
||||||
|
ToneBurst::Silence(200),
|
||||||
|
ToneBurst::Silence(3200),
|
||||||
|
|
||||||
|
// LR ident
|
||||||
|
ToneBurst::Tone(1000.0, 5100, -18.0),
|
||||||
|
ToneBurst::Silence(300),
|
||||||
|
|
||||||
|
// Phase check,
|
||||||
|
ToneBurst::Tone(2000.0, 3000, -24.0),
|
||||||
|
ToneBurst::Silence(200)
|
||||||
|
];
|
||||||
|
|
||||||
|
let center_channel_sequence : Vec<ToneBurst> = vec![
|
||||||
|
// channel ident
|
||||||
|
ToneBurst::Silence(1600),
|
||||||
|
ToneBurst::Tone(1320.0, 600, -18.0),
|
||||||
|
ToneBurst::Silence(200),
|
||||||
|
ToneBurst::Silence(2400),
|
||||||
|
|
||||||
|
// LR ident
|
||||||
|
ToneBurst::Silence(5400),
|
||||||
|
|
||||||
|
// Phase check,
|
||||||
|
ToneBurst::Tone(2000.0, 3000, -24.0),
|
||||||
|
ToneBurst::Silence(200)
|
||||||
|
];
|
||||||
|
|
||||||
|
let lfe_channel_sequence : Vec<ToneBurst> = vec![
|
||||||
|
// channel ident
|
||||||
|
ToneBurst::Silence(2400),
|
||||||
|
ToneBurst::Tone(82.5, 600, -18.0),
|
||||||
|
ToneBurst::Silence(200),
|
||||||
|
ToneBurst::Silence(1600),
|
||||||
|
|
||||||
|
// LR ident
|
||||||
|
ToneBurst::Silence(5400),
|
||||||
|
|
||||||
|
// Phase check,
|
||||||
|
ToneBurst::Tone(2000.0, 3000, -24.0),
|
||||||
|
ToneBurst::Silence(200)
|
||||||
|
];
|
||||||
|
|
||||||
|
let ls_channel_sequence : Vec<ToneBurst> = vec![
|
||||||
|
// channel ident
|
||||||
|
ToneBurst::Silence(3200),
|
||||||
|
ToneBurst::Tone(660.0, 600, -18.0),
|
||||||
|
ToneBurst::Silence(200),
|
||||||
|
ToneBurst::Silence(800),
|
||||||
|
|
||||||
|
// LR ident
|
||||||
|
ToneBurst::Silence(5400),
|
||||||
|
|
||||||
|
// Phase check,
|
||||||
|
ToneBurst::Tone(2000.0, 3000, -24.0),
|
||||||
|
ToneBurst::Silence(200)
|
||||||
|
];
|
||||||
|
|
||||||
|
let rs_channel_sequence : Vec<ToneBurst> = vec![
|
||||||
|
// channel ident
|
||||||
|
ToneBurst::Silence(4000),
|
||||||
|
ToneBurst::Tone(660.0, 600, -18.0),
|
||||||
|
ToneBurst::Silence(200),
|
||||||
|
|
||||||
|
// LR ident
|
||||||
|
ToneBurst::Silence(5400),
|
||||||
|
|
||||||
|
// Phase check,
|
||||||
|
ToneBurst::Tone(2000.0, 3000, -24.0),
|
||||||
|
ToneBurst::Silence(200)
|
||||||
|
];
|
||||||
|
|
||||||
|
let length = [&left_channel_sequence, &right_channel_sequence,
|
||||||
|
¢er_channel_sequence, &lfe_channel_sequence,
|
||||||
|
&ls_channel_sequence, &rs_channel_sequence].iter()
|
||||||
|
.map(|i| i.duration(sample_rate))
|
||||||
|
.max().unwrap_or(0);
|
||||||
|
|
||||||
|
let frames = (0..=length).map(|frame| {
|
||||||
|
(left_channel_sequence.signal(frame, sample_rate, bits_per_sample),
|
||||||
|
right_channel_sequence.signal(frame, sample_rate, bits_per_sample),
|
||||||
|
center_channel_sequence.signal(frame, sample_rate, bits_per_sample),
|
||||||
|
lfe_channel_sequence.signal(frame, sample_rate, bits_per_sample),
|
||||||
|
ls_channel_sequence.signal(frame, sample_rate, bits_per_sample),
|
||||||
|
rs_channel_sequence.signal(frame, sample_rate, bits_per_sample))
|
||||||
|
});
|
||||||
|
|
||||||
|
let format = WaveFmt::new_pcm_multichannel(sample_rate, bits_per_sample, 0b111111);
|
||||||
|
|
||||||
|
let file = WaveWriter::create(file_name, format)?;
|
||||||
|
|
||||||
|
let mut fw = file.audio_frame_writer()?;
|
||||||
|
for frame in frames {
|
||||||
|
let buf = vec![frame.0, frame.1, frame.2, frame.3, frame.4, frame.5];
|
||||||
|
fw.write_integer_frames(&buf)?;
|
||||||
|
}
|
||||||
|
fw.end()?;
|
||||||
|
|
||||||
|
Ok(())
|
||||||
|
}
|
||||||
|
|
||||||
|
fn main() -> io::Result<()> {
|
||||||
|
|
||||||
|
let matches = App::new("blits")
|
||||||
|
.version(crate_version!())
|
||||||
|
.author(crate_authors!())
|
||||||
|
.about("Generate a BLITS 5.1 alignment tone.")
|
||||||
|
.arg(Arg::with_name("sample_rate")
|
||||||
|
.long("sample-rate")
|
||||||
|
.short("s")
|
||||||
|
.help("Sample rate of output")
|
||||||
|
.default_value("48000")
|
||||||
|
)
|
||||||
|
.arg(Arg::with_name("bit_depth")
|
||||||
|
.long("bit-depth")
|
||||||
|
.short("b")
|
||||||
|
.help("Bit depth of output")
|
||||||
|
.default_value("24")
|
||||||
|
)
|
||||||
|
.arg(Arg::with_name("OUTPUT")
|
||||||
|
.help("Output wave file")
|
||||||
|
.default_value("blits.wav")
|
||||||
|
)
|
||||||
|
.get_matches();
|
||||||
|
|
||||||
|
let sample_rate = matches.value_of("sample_rate").unwrap().parse::<u32>()
|
||||||
|
.expect("Failed to read sample rate");
|
||||||
|
|
||||||
|
let bits_per_sample = matches.value_of("bit_depth").unwrap().parse::<u16>()
|
||||||
|
.expect("Failed to read bit depth");
|
||||||
|
|
||||||
|
let filename = matches.value_of("OUTPUT").unwrap();
|
||||||
|
|
||||||
|
match create_blits_file(&filename, sample_rate, bits_per_sample) {
|
||||||
|
Err( Error::IOError(x) ) => panic!("IO Error: {:?}", x),
|
||||||
|
Err( err ) => panic!("Error: {:?}", err),
|
||||||
|
Ok(()) => Ok(())
|
||||||
|
}
|
||||||
|
}
|
||||||
119
examples/wave-deinter.rs
Normal file
119
examples/wave-deinter.rs
Normal file
@@ -0,0 +1,119 @@
|
|||||||
|
//! wave-inter.rs
|
||||||
|
//! (c) 2021 Jamie Hardt. All rights reserved.
|
||||||
|
//!
|
||||||
|
//! This program demonstrats combining several wave files into a single
|
||||||
|
//! polyphonic wave file.
|
||||||
|
|
||||||
|
use std::io;
|
||||||
|
use std::path::Path;
|
||||||
|
|
||||||
|
extern crate bwavfile;
|
||||||
|
use bwavfile::{Error,WaveReader, WaveWriter, ChannelDescriptor, ChannelMask, WaveFmt};
|
||||||
|
|
||||||
|
#[macro_use]
|
||||||
|
extern crate clap;
|
||||||
|
use clap::{Arg, App};
|
||||||
|
|
||||||
|
fn name_suffix(force_numeric : bool, delim : &str, index: usize, channel_descriptor : &ChannelDescriptor) -> String {
|
||||||
|
if force_numeric || channel_descriptor.speaker == ChannelMask::DirectOut {
|
||||||
|
format!("{}A{:02}", delim, index)
|
||||||
|
} else {
|
||||||
|
let chan_name = match channel_descriptor.speaker {
|
||||||
|
ChannelMask::FrontLeft => "L",
|
||||||
|
ChannelMask::FrontCenter => "C",
|
||||||
|
ChannelMask::FrontRight => "R",
|
||||||
|
ChannelMask::BackLeft => "Ls",
|
||||||
|
ChannelMask::BackRight => "Rs",
|
||||||
|
ChannelMask::BackCenter => "S",
|
||||||
|
ChannelMask::TopCenter => "Tc",
|
||||||
|
ChannelMask::LowFrequency => "Lfe",
|
||||||
|
ChannelMask::SideLeft => "Lss",
|
||||||
|
ChannelMask::SideRight => "Rss",
|
||||||
|
ChannelMask::FrontCenterLeft => "Lc",
|
||||||
|
ChannelMask::FrontCenterRight => "Rc",
|
||||||
|
ChannelMask::TopFrontLeft => "Ltf",
|
||||||
|
ChannelMask::TopFrontCenter => "Ctf",
|
||||||
|
ChannelMask::TopFrontRight => "Rtf",
|
||||||
|
ChannelMask::TopBackLeft => "Ltb",
|
||||||
|
ChannelMask::TopBackCenter => "Ctb",
|
||||||
|
ChannelMask::TopBackRight => "Rtb",
|
||||||
|
ChannelMask::DirectOut => panic!("Error, can't get here")
|
||||||
|
};
|
||||||
|
format!("{}{}", delim, chan_name)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
fn process_file(infile: &str, delim : &str, numeric_channel_names : bool) -> Result<(), Error> {
|
||||||
|
let mut input_file = WaveReader::open(infile)?;
|
||||||
|
let channel_desc = input_file.channels()?;
|
||||||
|
let input_format = input_file.format()?;
|
||||||
|
|
||||||
|
if channel_desc.len() == 1 {
|
||||||
|
println!("Input file in monoaural, exiting.");
|
||||||
|
return Ok(());
|
||||||
|
}
|
||||||
|
|
||||||
|
let infile_path = Path::new(infile);
|
||||||
|
let basename = infile_path.file_stem().expect("Unable to extract file basename").to_str().unwrap();
|
||||||
|
let output_dir = infile_path.parent().expect("Unable to derive parent directory");
|
||||||
|
|
||||||
|
let ouptut_format = WaveFmt::new_pcm_mono(input_format.sample_rate, input_format.bits_per_sample);
|
||||||
|
let mut input_wave_reader = input_file.audio_frame_reader()?;
|
||||||
|
|
||||||
|
for (n, channel) in channel_desc.iter().enumerate() {
|
||||||
|
let suffix = name_suffix(numeric_channel_names, delim, n + 1, channel);
|
||||||
|
let outfile_name = output_dir.join(format!("{}{}.wav", basename, suffix))
|
||||||
|
.into_os_string().into_string().unwrap();
|
||||||
|
|
||||||
|
println!("Will create file {}", outfile_name);
|
||||||
|
|
||||||
|
let output_file = WaveWriter::create(&outfile_name, ouptut_format).expect("Failed to create new file");
|
||||||
|
|
||||||
|
let mut output_wave_writer = output_file.audio_frame_writer()?;
|
||||||
|
let mut buffer = input_format.create_frame_buffer(1);
|
||||||
|
|
||||||
|
while input_wave_reader.read_integer_frame(&mut buffer)? > 0 {
|
||||||
|
output_wave_writer.write_integer_frames(&buffer[n..=n])?;
|
||||||
|
}
|
||||||
|
|
||||||
|
output_wave_writer.end()?;
|
||||||
|
input_wave_reader.locate(0)?;
|
||||||
|
}
|
||||||
|
|
||||||
|
Ok(())
|
||||||
|
}
|
||||||
|
|
||||||
|
fn main() -> io::Result<()> {
|
||||||
|
let matches = App::new("wave-deinter")
|
||||||
|
.version(crate_version!())
|
||||||
|
.author(crate_authors!())
|
||||||
|
.about("Extract each channel of a polyphonic wave file as a new monoaural wave file.")
|
||||||
|
.arg(Arg::with_name("numeric_names")
|
||||||
|
.long("numeric")
|
||||||
|
.short("n")
|
||||||
|
.help("Use numeric channel names \"01\" \"02\" etc.")
|
||||||
|
.takes_value(false)
|
||||||
|
)
|
||||||
|
.arg(Arg::with_name("channel_delimiter")
|
||||||
|
.long("delim")
|
||||||
|
.short("d")
|
||||||
|
.help("Channel label delimiter.")
|
||||||
|
.default_value(".")
|
||||||
|
)
|
||||||
|
.arg(Arg::with_name("INPUT")
|
||||||
|
.help("Input wave file")
|
||||||
|
.required(true)
|
||||||
|
.multiple(true)
|
||||||
|
)
|
||||||
|
.get_matches();
|
||||||
|
|
||||||
|
let delimiter = matches.value_of("channel_delimiter").unwrap();
|
||||||
|
let use_numeric_names = matches.is_present("numeric_names");
|
||||||
|
let infile = matches.value_of("INPUT").unwrap();
|
||||||
|
|
||||||
|
match process_file(infile, delimiter, use_numeric_names) {
|
||||||
|
Err(Error::IOError(io)) => Err(io),
|
||||||
|
Err(e) => panic!("Error: {:?}", e),
|
||||||
|
Ok(()) => Ok(())
|
||||||
|
}
|
||||||
|
}
|
||||||
35
examples/wave-inter.rs
Normal file
35
examples/wave-inter.rs
Normal file
@@ -0,0 +1,35 @@
|
|||||||
|
//! wave-inter.rs
|
||||||
|
//! (c) 2021 Jamie Hardt. All rights reserved.
|
||||||
|
//!
|
||||||
|
//! This program demonstrates combining several wave files into a single
|
||||||
|
//! polyphonic wave file.
|
||||||
|
|
||||||
|
use std::io;
|
||||||
|
|
||||||
|
extern crate bwavfile;
|
||||||
|
|
||||||
|
#[macro_use]
|
||||||
|
extern crate clap;
|
||||||
|
use clap::{Arg, App};
|
||||||
|
|
||||||
|
fn main() -> io::Result<()> {
|
||||||
|
let matches = App::new("wave-inter")
|
||||||
|
.version(crate_version!())
|
||||||
|
.author(crate_authors!())
|
||||||
|
.about("Combine several wave files into a single polyphonic wave file.")
|
||||||
|
.arg(Arg::with_name("OUTPUT")
|
||||||
|
.long("output")
|
||||||
|
.short("o")
|
||||||
|
.help("Output file name. If absent, will be basename, minus any channel extension, of first INPUT.")
|
||||||
|
)
|
||||||
|
.arg(Arg::with_name("INPUT")
|
||||||
|
.help("Input wave file")
|
||||||
|
.required(true)
|
||||||
|
.multiple(true)
|
||||||
|
)
|
||||||
|
.get_matches();
|
||||||
|
|
||||||
|
println!("Command line opts: {:?}", matches);
|
||||||
|
|
||||||
|
todo!("Finish implementation");
|
||||||
|
}
|
||||||
@@ -1,58 +0,0 @@
|
|||||||
use std::io::{Read, Seek};
|
|
||||||
use std::io::SeekFrom::{Start,};
|
|
||||||
|
|
||||||
use byteorder::LittleEndian;
|
|
||||||
use byteorder::ReadBytesExt;
|
|
||||||
|
|
||||||
use super::chunks::WaveFmt;
|
|
||||||
use super::errors::Error;
|
|
||||||
|
|
||||||
#[derive(Debug)]
|
|
||||||
pub struct AudioFrameReader<R: Read + Seek> {
|
|
||||||
inner : R,
|
|
||||||
format: WaveFmt
|
|
||||||
}
|
|
||||||
|
|
||||||
impl<R: Read + Seek> AudioFrameReader<R> {
|
|
||||||
/// Create a new AudioFrameReader, taking possession of a reader.
|
|
||||||
pub fn new(inner: R, format: WaveFmt) -> Self {
|
|
||||||
assert!(format.block_alignment * 8 == format.bits_per_sample * format.channel_count,
|
|
||||||
"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.tag == 1, "Unsupported format tag {}", format.tag);
|
|
||||||
AudioFrameReader { inner , format }
|
|
||||||
}
|
|
||||||
|
|
||||||
pub fn locate(&mut self, to :u64) -> Result<u64,Error> {
|
|
||||||
let position = to * self.format.block_alignment as u64;
|
|
||||||
let seek_result = self.inner.seek(Start(position))?;
|
|
||||||
Ok( seek_result / self.format.block_alignment as u64 )
|
|
||||||
}
|
|
||||||
|
|
||||||
pub fn create_frame_buffer(&self) -> Vec<i32> {
|
|
||||||
vec![0i32; self.format.channel_count as usize]
|
|
||||||
}
|
|
||||||
|
|
||||||
pub fn read_integer_frame(&mut self, buffer:&mut [i32]) -> Result<u64,Error> {
|
|
||||||
assert!(buffer.len() as u16 == self.format.channel_count,
|
|
||||||
"read_integer_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;
|
|
||||||
|
|
||||||
for n in 0..(self.format.channel_count as usize) {
|
|
||||||
buffer[n] = match (self.format.bits_per_sample, framed_bits_per_sample) {
|
|
||||||
(0..=8,8) => self.inner.read_u8()? as i32 - 0x80_i32, // EBU 3285 §A2.2
|
|
||||||
(9..=16,16) => self.inner.read_i16::<LittleEndian>()? as i32,
|
|
||||||
(10..=24,24) => self.inner.read_i24::<LittleEndian>()?,
|
|
||||||
(25..=32,32) => self.inner.read_i32::<LittleEndian>()?,
|
|
||||||
(b,_)=> panic!("Unrecognized integer format, bits per sample {}, channels {}, block_alignment {}",
|
|
||||||
b, self.format.channel_count, self.format.block_alignment)
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
Ok( 1 )
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
88
src/bext.rs
Normal file
88
src/bext.rs
Normal file
@@ -0,0 +1,88 @@
|
|||||||
|
|
||||||
|
pub type LU = f32;
|
||||||
|
pub type LUFS = f32;
|
||||||
|
pub type Decibels = f32;
|
||||||
|
|
||||||
|
|
||||||
|
/// Broadcast-WAV metadata record.
|
||||||
|
///
|
||||||
|
/// The `bext` record contains information about the original recording of the
|
||||||
|
/// Wave file, including a longish (256 ASCII chars) description field,
|
||||||
|
/// originator identification fields, creation calendar date and time, a
|
||||||
|
/// sample-accurate recording time field, and a SMPTE UMID.
|
||||||
|
///
|
||||||
|
/// For a Wave file to be a complaint "Broadcast-WAV" file, it must contain
|
||||||
|
/// a `bext` metadata record.
|
||||||
|
///
|
||||||
|
/// ## Resources
|
||||||
|
/// - [EBU Tech 3285](https://tech.ebu.ch/docs/tech/tech3285.pdf).
|
||||||
|
/// - [EBU Tech R098](https://tech.ebu.ch/docs/r/r098.pdf) (1999) "Format for the <CodingHistory> field in Broadcast Wave Format files, BWF"
|
||||||
|
/// - [EBU Tech R099](https://tech.ebu.ch/docs/r/r099.pdf) (October 2011) "‘Unique’ Source Identifier (USID) for use in the
|
||||||
|
/// <OriginatorReference> field of the Broadcast Wave Format"
|
||||||
|
|
||||||
|
// Note for me later:
|
||||||
|
// get env values: https://doc.rust-lang.org/std/macro.option_env.html
|
||||||
|
// Cargo env values: https://doc.rust-lang.org/cargo/reference/environment-variables.html
|
||||||
|
|
||||||
|
#[derive(Debug)]
|
||||||
|
pub struct Bext {
|
||||||
|
|
||||||
|
/// 256 ASCII character field with free text.
|
||||||
|
pub description: String,
|
||||||
|
|
||||||
|
/// Originating application.
|
||||||
|
pub originator: String,
|
||||||
|
|
||||||
|
/// Application-specific UID.
|
||||||
|
pub originator_reference: String,
|
||||||
|
|
||||||
|
/// Creation date in format `YYYY-MM-DD`.
|
||||||
|
pub origination_date: String,
|
||||||
|
|
||||||
|
/// Creation time in format `HH:MM:SS`.
|
||||||
|
pub origination_time: String,
|
||||||
|
|
||||||
|
/// Time of the start of this wave file, expressed as the number of samples
|
||||||
|
/// since local midnight.
|
||||||
|
pub time_reference: u64,
|
||||||
|
|
||||||
|
/// Bext chunk version.
|
||||||
|
///
|
||||||
|
/// Version 1 contains a UMID, version 2 contains a UMID and
|
||||||
|
/// loudness metadata.
|
||||||
|
pub version: u16,
|
||||||
|
|
||||||
|
/// SMPTE 330M UMID
|
||||||
|
///
|
||||||
|
/// This field is `None` if the version is less than 1.
|
||||||
|
pub umid: Option<[u8; 64]>,
|
||||||
|
|
||||||
|
/// Integrated loudness in LUFS.
|
||||||
|
///
|
||||||
|
/// This field is `None` if the version is less than 2.
|
||||||
|
pub loudness_value: Option<LUFS>,
|
||||||
|
|
||||||
|
/// Loudness range in LU.
|
||||||
|
///
|
||||||
|
/// This field is `None` if the version is less than 2.
|
||||||
|
pub loudness_range: Option<LU>,
|
||||||
|
|
||||||
|
/// Maximum True Peak Level in decibels True Peak.
|
||||||
|
///
|
||||||
|
/// This field is `None` if the version is less than 2.
|
||||||
|
pub max_true_peak_level: Option<Decibels>,
|
||||||
|
|
||||||
|
/// Maximum momentary loudness in LUFS.
|
||||||
|
///
|
||||||
|
/// This field is `None` if the version is less than 2.
|
||||||
|
pub max_momentary_loudness: Option<LUFS>,
|
||||||
|
|
||||||
|
/// Maximum short-term loudness in LUFS.
|
||||||
|
///
|
||||||
|
/// This field is `None` if the version is less than 2.
|
||||||
|
pub max_short_term_loudness: Option<LUFS>,
|
||||||
|
// 180 bytes of nothing
|
||||||
|
|
||||||
|
/// Coding History.
|
||||||
|
pub coding_history: String
|
||||||
|
}
|
||||||
217
src/chunks.rs
217
src/chunks.rs
@@ -1,7 +1,5 @@
|
|||||||
use std::io::{Read, Write};
|
use std::io::{Read, Write};
|
||||||
|
|
||||||
use super::errors::Error as ParserError;
|
|
||||||
|
|
||||||
use encoding::{DecoderTrap, EncoderTrap};
|
use encoding::{DecoderTrap, EncoderTrap};
|
||||||
use encoding::{Encoding};
|
use encoding::{Encoding};
|
||||||
use encoding::all::ASCII;
|
use encoding::all::ASCII;
|
||||||
@@ -9,156 +7,11 @@ use encoding::all::ASCII;
|
|||||||
use byteorder::LittleEndian;
|
use byteorder::LittleEndian;
|
||||||
use byteorder::{ReadBytesExt, WriteBytesExt};
|
use byteorder::{ReadBytesExt, WriteBytesExt};
|
||||||
|
|
||||||
/**
|
use uuid::Uuid;
|
||||||
* References:
|
|
||||||
* - http://www-mmsp.ece.mcgill.ca/Documents/AudioFormats/WAVE/Docs/multichaudP.pdf
|
|
||||||
*/
|
|
||||||
#[derive(PartialEq)]
|
|
||||||
enum FormatTags {
|
|
||||||
Integer = 0x0001,
|
|
||||||
Float = 0x0003,
|
|
||||||
Extensible = 0xFFFE
|
|
||||||
}
|
|
||||||
|
|
||||||
const PCM_SUBTYPE_UUID: [u8; 16] = [0x00, 0x00, 0x00, 0x01,
|
use super::errors::Error as ParserError;
|
||||||
0x00, 0x00, 0x00, 0x10,
|
use super::fmt::{WaveFmt, WaveFmtExtended};
|
||||||
0x80, 0x00, 0x00, 0xaa,
|
use super::bext::Bext;
|
||||||
0x00, 0x38, 0x9b, 0x71];
|
|
||||||
|
|
||||||
const FLOAT_SUBTYPE_UUID: [u8; 16] = [0x00, 0x00, 0x00, 0x03,
|
|
||||||
0x00, 0x00, 0x00, 0x10,
|
|
||||||
0x80, 0x00, 0x00, 0xaa,
|
|
||||||
0x00, 0x38, 0x9b, 0x71];
|
|
||||||
|
|
||||||
/*
|
|
||||||
https://docs.microsoft.com/en-us/windows-hardware/drivers/audio/subformat-guids-for-compressed-audio-formats
|
|
||||||
http://dream.cs.bath.ac.uk/researchdev/wave-ex/bformat.html
|
|
||||||
|
|
||||||
These are from http://dream.cs.bath.ac.uk/researchdev/wave-ex/mulchaud.rtf
|
|
||||||
*/
|
|
||||||
|
|
||||||
#[derive(Debug)]
|
|
||||||
enum WaveFmtExtendedChannelMask {
|
|
||||||
FrontLeft = 0x1,
|
|
||||||
FrontRight = 0x2,
|
|
||||||
FrontCenter = 0x4,
|
|
||||||
LowFrequency = 0x8,
|
|
||||||
BackLeft = 0x10,
|
|
||||||
BackRight = 0x20,
|
|
||||||
FrontCenterLeft = 0x40,
|
|
||||||
FrontCenterRight = 0x80,
|
|
||||||
BackCenter = 0x100,
|
|
||||||
SideLeft = 0x200,
|
|
||||||
SideRight = 0x400,
|
|
||||||
TopCenter = 0x800,
|
|
||||||
TopFrontLeft = 0x1000,
|
|
||||||
TopFrontCenter = 0x2000,
|
|
||||||
TopFrontRight = 0x4000,
|
|
||||||
TopBackLeft = 0x8000,
|
|
||||||
TopBackCenter = 0x10000,
|
|
||||||
TopBackRight = 0x20000
|
|
||||||
}
|
|
||||||
|
|
||||||
|
|
||||||
/**
|
|
||||||
* Extended Wave Format
|
|
||||||
*
|
|
||||||
* https://docs.microsoft.com/en-us/windows/win32/api/mmreg/ns-mmreg-waveformatextensible
|
|
||||||
*/
|
|
||||||
#[derive(Debug)]
|
|
||||||
pub struct WaveFmtExtended {
|
|
||||||
valid_bits_per_sample : u16,
|
|
||||||
channel_mask : WaveFmtExtendedChannelMask,
|
|
||||||
type_guid : [u8; 16],
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
|
||||||
* WAV file data format record.
|
|
||||||
*
|
|
||||||
* The `fmt` record contains essential information describing the binary
|
|
||||||
* structure of the data segment of the WAVE file, such as sample
|
|
||||||
* rate, sample binary format, channel count, etc.
|
|
||||||
*
|
|
||||||
*/
|
|
||||||
#[derive(Debug)]
|
|
||||||
pub struct WaveFmt {
|
|
||||||
pub tag: u16,
|
|
||||||
pub channel_count: u16,
|
|
||||||
pub sample_rate: u32,
|
|
||||||
pub bytes_per_second: u32,
|
|
||||||
pub block_alignment: u16,
|
|
||||||
pub bits_per_sample: u16,
|
|
||||||
pub extended_format: Option<WaveFmtExtended>
|
|
||||||
}
|
|
||||||
|
|
||||||
|
|
||||||
impl WaveFmt {
|
|
||||||
|
|
||||||
pub fn new_pcm(sample_rate: u32, bits_per_sample: u16, channel_count: u16) -> Self {
|
|
||||||
let container_bits_per_sample = bits_per_sample + (bits_per_sample % 8);
|
|
||||||
let container_bytes_per_sample= container_bits_per_sample / 8;
|
|
||||||
|
|
||||||
let tag :u16 = match channel_count {
|
|
||||||
0 => panic!("Error"),
|
|
||||||
1..=2 => FormatTags::Integer as u16,
|
|
||||||
_ => FormatTags::Extensible as u16,
|
|
||||||
};
|
|
||||||
|
|
||||||
WaveFmt {
|
|
||||||
tag,
|
|
||||||
channel_count,
|
|
||||||
sample_rate,
|
|
||||||
bytes_per_second: container_bytes_per_sample as u32 * sample_rate * channel_count as u32,
|
|
||||||
block_alignment: container_bytes_per_sample * channel_count,
|
|
||||||
bits_per_sample: container_bits_per_sample,
|
|
||||||
extended_format: None
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
pub fn bytes_per_frame(&self) -> u16 {
|
|
||||||
let bits_per_byte = 8;
|
|
||||||
let bits_per_sample_with_pad = self.bits_per_sample + (self.bits_per_sample % 8);
|
|
||||||
bits_per_sample_with_pad * self.channel_count / bits_per_byte
|
|
||||||
}
|
|
||||||
|
|
||||||
pub fn valid_broadcast_wave_format(&self) -> bool {
|
|
||||||
let real_alignment = self.block_alignment;
|
|
||||||
self.bytes_per_frame() == real_alignment
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
|
||||||
* Broadcast-WAV metadata record.
|
|
||||||
*
|
|
||||||
* The `bext` record contains information about the original recording of the
|
|
||||||
* Wave file, including a longish (256 ASCII chars) description field,
|
|
||||||
* originator identification fields, creation calendar date and time, a
|
|
||||||
* sample-accurate recording time field, and a SMPTE UMID.
|
|
||||||
*
|
|
||||||
* For a Wave file to be a complaint "Broadcast-WAV" file, it must contain
|
|
||||||
* a `bext` metadata record.
|
|
||||||
*
|
|
||||||
* For reference on the structure and use of the BEXT record
|
|
||||||
* check out [EBU Tech 3285](https://tech.ebu.ch/docs/tech/tech3285.pdf).
|
|
||||||
*/
|
|
||||||
#[derive(Debug)]
|
|
||||||
pub struct Bext {
|
|
||||||
pub description: String,
|
|
||||||
pub originator: String,
|
|
||||||
pub originator_reference: String,
|
|
||||||
pub origination_date: String,
|
|
||||||
pub origination_time: String,
|
|
||||||
pub time_reference: u64,
|
|
||||||
pub version: u16,
|
|
||||||
pub umid: Option<[u8; 64]>,
|
|
||||||
pub loudness_value: Option<f32>,
|
|
||||||
pub loudness_range: Option<f32>,
|
|
||||||
pub max_true_peak_level: Option<f32>,
|
|
||||||
pub max_momentary_loudness: Option<f32>,
|
|
||||||
pub max_short_term_loudness: Option<f32>,
|
|
||||||
// 180 bytes of nothing
|
|
||||||
pub coding_history: String
|
|
||||||
}
|
|
||||||
|
|
||||||
pub trait ReadBWaveChunks: Read {
|
pub trait ReadBWaveChunks: Read {
|
||||||
fn read_bext(&mut self) -> Result<Bext, ParserError>;
|
fn read_bext(&mut self) -> Result<Bext, ParserError>;
|
||||||
@@ -174,13 +27,20 @@ pub trait WriteBWaveChunks: Write {
|
|||||||
|
|
||||||
impl<T> WriteBWaveChunks for T where T: Write {
|
impl<T> WriteBWaveChunks for T where T: Write {
|
||||||
fn write_wave_fmt(&mut self, format : &WaveFmt) -> Result<(), ParserError> {
|
fn write_wave_fmt(&mut self, format : &WaveFmt) -> Result<(), ParserError> {
|
||||||
self.write_u16::<LittleEndian>(format.tag)?;
|
self.write_u16::<LittleEndian>(format.tag as u16 )?;
|
||||||
self.write_u16::<LittleEndian>(format.channel_count)?;
|
self.write_u16::<LittleEndian>(format.channel_count)?;
|
||||||
self.write_u32::<LittleEndian>(format.sample_rate)?;
|
self.write_u32::<LittleEndian>(format.sample_rate)?;
|
||||||
self.write_u32::<LittleEndian>(format.bytes_per_second)?;
|
self.write_u32::<LittleEndian>(format.bytes_per_second)?;
|
||||||
self.write_u16::<LittleEndian>(format.block_alignment)?;
|
self.write_u16::<LittleEndian>(format.block_alignment)?;
|
||||||
self.write_u16::<LittleEndian>(format.bits_per_sample)?;
|
self.write_u16::<LittleEndian>(format.bits_per_sample)?;
|
||||||
// self.write_u8(0)?;
|
if let Some(ext) = format.extended_format {
|
||||||
|
let cb_size = 24u16;
|
||||||
|
self.write_u16::<LittleEndian>(cb_size)?;
|
||||||
|
self.write_u16::<LittleEndian>(ext.valid_bits_per_sample)?;
|
||||||
|
self.write_u32::<LittleEndian>(ext.channel_mask)?;
|
||||||
|
let uuid = ext.type_guid.as_bytes();
|
||||||
|
self.write(uuid)?;
|
||||||
|
}
|
||||||
Ok(())
|
Ok(())
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -234,14 +94,34 @@ impl<T> WriteBWaveChunks for T where T: Write {
|
|||||||
impl<T> ReadBWaveChunks for T where T: Read {
|
impl<T> ReadBWaveChunks for T where T: Read {
|
||||||
|
|
||||||
fn read_wave_fmt(&mut self) -> Result<WaveFmt, ParserError> {
|
fn read_wave_fmt(&mut self) -> Result<WaveFmt, ParserError> {
|
||||||
|
let tag_value : u16;
|
||||||
Ok(WaveFmt {
|
Ok(WaveFmt {
|
||||||
tag: self.read_u16::<LittleEndian>()?,
|
tag: {
|
||||||
|
tag_value = self.read_u16::<LittleEndian>()?;
|
||||||
|
tag_value
|
||||||
|
},
|
||||||
channel_count: self.read_u16::<LittleEndian>()?,
|
channel_count: self.read_u16::<LittleEndian>()?,
|
||||||
sample_rate: self.read_u32::<LittleEndian>()?,
|
sample_rate: self.read_u32::<LittleEndian>()?,
|
||||||
bytes_per_second: self.read_u32::<LittleEndian>()?,
|
bytes_per_second: self.read_u32::<LittleEndian>()?,
|
||||||
block_alignment: self.read_u16::<LittleEndian>()?,
|
block_alignment: self.read_u16::<LittleEndian>()?,
|
||||||
bits_per_sample: self.read_u16::<LittleEndian>()?,
|
bits_per_sample: self.read_u16::<LittleEndian>()?,
|
||||||
extended_format: None
|
extended_format: {
|
||||||
|
if tag_value == 0xFFFE {
|
||||||
|
let cb_size = self.read_u16::<LittleEndian>()?;
|
||||||
|
assert!(cb_size >= 22, "Format extension is not correct size");
|
||||||
|
Some(WaveFmtExtended {
|
||||||
|
valid_bits_per_sample: self.read_u16::<LittleEndian>()?,
|
||||||
|
channel_mask: self.read_u32::<LittleEndian>()?,
|
||||||
|
type_guid: {
|
||||||
|
let mut buf : [u8; 16] = [0; 16];
|
||||||
|
self.read_exact(&mut buf)?;
|
||||||
|
Uuid::from_slice(&buf)?
|
||||||
|
}
|
||||||
|
})
|
||||||
|
} else {
|
||||||
|
None
|
||||||
|
}
|
||||||
|
}
|
||||||
})
|
})
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -291,11 +171,36 @@ impl<T> ReadBWaveChunks for T where T: Read {
|
|||||||
if version > 1 { Some(val) } else { None }
|
if version > 1 { Some(val) } else { None }
|
||||||
},
|
},
|
||||||
coding_history: {
|
coding_history: {
|
||||||
for _ in 0..=180 { self.read_u8()?; }
|
for _ in 0..180 { self.read_u8()?; }
|
||||||
let mut buf = vec![];
|
let mut buf = vec![];
|
||||||
self.read_to_end(&mut buf)?;
|
self.read_to_end(&mut buf)?;
|
||||||
ASCII.decode(&buf, DecoderTrap::Ignore).expect("Error decoding text")
|
ASCII.decode(&buf, DecoderTrap::Ignore).expect("Error decoding text")
|
||||||
}
|
}
|
||||||
})
|
})
|
||||||
}
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
#[test]
|
||||||
|
fn test_read_51_wav() {
|
||||||
|
use super::fmt::ChannelMask;
|
||||||
|
use super::common_format::CommonFormat;
|
||||||
|
|
||||||
|
let path = "tests/media/pt_24bit_51.wav";
|
||||||
|
|
||||||
|
let mut w = super::wavereader::WaveReader::open(path).unwrap();
|
||||||
|
let format = w.format().unwrap();
|
||||||
|
assert_eq!(format.tag, 0xFFFE);
|
||||||
|
assert_eq!(format.channel_count, 6);
|
||||||
|
assert_eq!(format.sample_rate, 48000);
|
||||||
|
let extended = format.extended_format.unwrap();
|
||||||
|
|
||||||
|
assert_eq!(extended.valid_bits_per_sample, 24);
|
||||||
|
|
||||||
|
let channels = ChannelMask::channels(extended.channel_mask, format.channel_count);
|
||||||
|
|
||||||
|
assert_eq!(channels, [ChannelMask::FrontLeft, ChannelMask::FrontRight,
|
||||||
|
ChannelMask::FrontCenter, ChannelMask::LowFrequency,
|
||||||
|
ChannelMask::BackLeft, ChannelMask::BackRight]);
|
||||||
|
|
||||||
|
assert_eq!(format.common_format(), CommonFormat::IntegerPCM);
|
||||||
}
|
}
|
||||||
97
src/common_format.rs
Normal file
97
src/common_format.rs
Normal file
@@ -0,0 +1,97 @@
|
|||||||
|
use uuid::Uuid;
|
||||||
|
|
||||||
|
const BASIC_PCM: u16 = 0x0001;
|
||||||
|
const BASIC_FLOAT: u16 = 0x0003;
|
||||||
|
const BASIC_MPEG: u16 = 0x0050;
|
||||||
|
const BASIC_EXTENDED: u16 = 0xFFFE;
|
||||||
|
|
||||||
|
/* RC 2361 §4:
|
||||||
|
|
||||||
|
WAVE Format IDs are converted to GUIDs by inserting the hexadecimal
|
||||||
|
value of the WAVE Format ID into the XXXXXXXX part of the following
|
||||||
|
template: {XXXXXXXX-0000-0010-8000-00AA00389B71}. For example, a WAVE
|
||||||
|
Format ID of 123 has the GUID value of {00000123-0000-0010-8000-
|
||||||
|
00AA00389B71}.
|
||||||
|
|
||||||
|
*/
|
||||||
|
|
||||||
|
pub const UUID_PCM: Uuid = Uuid::from_bytes([0x01, 0x00, 0x00, 0x00, 0x00, 0x00, 0x10, 0x00,
|
||||||
|
0x80, 0x00, 0x00, 0xaa, 0x00, 0x38, 0x9b, 0x71]);
|
||||||
|
|
||||||
|
pub const UUID_FLOAT: Uuid = Uuid::from_bytes([0x03, 0x00, 0x00, 0x00, 0x00, 0x00, 0x10, 0x00,
|
||||||
|
0x80, 0x00, 0x00, 0xaa, 0x00, 0x38, 0x9b, 0x71]);
|
||||||
|
|
||||||
|
pub const UUID_MPEG: Uuid = Uuid::from_bytes([0x50, 0x00, 0x00, 0x00, 0x00, 0x00, 0x10, 0x00,
|
||||||
|
0x80, 0x00, 0x00, 0xaa, 0x00, 0x38, 0x9b, 0x71]);
|
||||||
|
|
||||||
|
pub const UUID_BFORMAT_PCM: Uuid = Uuid::from_bytes([0x01, 0x00, 0x00, 0x00, 0x21, 0x07, 0xd3, 0x11,
|
||||||
|
0x86, 0x44, 0xc8, 0xc1, 0xca, 0x00, 0x00, 0x00]);
|
||||||
|
|
||||||
|
pub const UUID_BFORMAT_FLOAT: Uuid = Uuid::from_bytes([0x03, 0x00, 0x00, 0x00, 0x21, 0x07, 0xd3, 0x11,
|
||||||
|
0x86, 0x44, 0xc8, 0xc1, 0xca, 0x00, 0x00, 0x00]);
|
||||||
|
|
||||||
|
|
||||||
|
fn uuid_from_basic_tag(tag: u16) -> Uuid {
|
||||||
|
let tail : [u8; 6] = [0x00,0xaa,0x00,0x38,0x9b,0x71];
|
||||||
|
Uuid::from_fields_le(tag as u32, 0x0000, 0x0010, &tail).unwrap()
|
||||||
|
}
|
||||||
|
|
||||||
|
/// Sample format of the Wave file.
|
||||||
|
///
|
||||||
|
///
|
||||||
|
#[derive(Debug, Copy, Clone, PartialEq)]
|
||||||
|
pub enum CommonFormat {
|
||||||
|
/// Integer linear PCM
|
||||||
|
IntegerPCM,
|
||||||
|
|
||||||
|
/// IEEE Floating-point Linear PCM
|
||||||
|
IeeeFloatPCM,
|
||||||
|
|
||||||
|
/// MPEG
|
||||||
|
Mpeg,
|
||||||
|
|
||||||
|
/// Ambisonic B-Format Linear PCM
|
||||||
|
AmbisonicBFormatIntegerPCM,
|
||||||
|
|
||||||
|
/// Ambisonic B-Format Float PCM
|
||||||
|
AmbisonicBFormatIeeeFloatPCM,
|
||||||
|
|
||||||
|
/// An unknown format identified by a basic format tag.
|
||||||
|
UnknownBasic(u16),
|
||||||
|
|
||||||
|
/// An unknown format identified by an extension UUID.
|
||||||
|
UnknownExtended(Uuid),
|
||||||
|
}
|
||||||
|
|
||||||
|
impl CommonFormat {
|
||||||
|
/// Resolve a tag and Uuid to a `CommonFormat`.
|
||||||
|
pub fn make(basic: u16, uuid: Option<Uuid>) -> Self {
|
||||||
|
match (basic, uuid) {
|
||||||
|
(BASIC_PCM, _) => Self::IntegerPCM,
|
||||||
|
(BASIC_FLOAT, _) => Self::IeeeFloatPCM,
|
||||||
|
(BASIC_MPEG, _) => Self::Mpeg,
|
||||||
|
(BASIC_EXTENDED, Some(UUID_PCM)) => Self::IntegerPCM,
|
||||||
|
(BASIC_EXTENDED, Some(UUID_FLOAT))=> Self::IeeeFloatPCM,
|
||||||
|
(BASIC_EXTENDED, Some(UUID_BFORMAT_PCM)) => Self::AmbisonicBFormatIntegerPCM,
|
||||||
|
(BASIC_EXTENDED, Some(UUID_BFORMAT_FLOAT)) => Self::AmbisonicBFormatIeeeFloatPCM,
|
||||||
|
(BASIC_EXTENDED, Some(x)) => CommonFormat::UnknownExtended(x),
|
||||||
|
(x, _) => CommonFormat::UnknownBasic(x)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
/// Get the appropriate tag and `Uuid` for the callee.
|
||||||
|
///
|
||||||
|
/// If there is no appropriate tag for the format of the callee, the
|
||||||
|
/// returned tag will be 0xFFFE and the `Uuid` will describe the format.
|
||||||
|
pub fn take(self) -> (u16, Uuid) {
|
||||||
|
match self {
|
||||||
|
Self::IntegerPCM => (BASIC_PCM, UUID_PCM),
|
||||||
|
Self::IeeeFloatPCM => (BASIC_FLOAT, UUID_FLOAT),
|
||||||
|
Self::Mpeg => (BASIC_MPEG, UUID_MPEG),
|
||||||
|
Self::AmbisonicBFormatIntegerPCM => (BASIC_EXTENDED, UUID_BFORMAT_PCM),
|
||||||
|
Self::AmbisonicBFormatIeeeFloatPCM => (BASIC_EXTENDED, UUID_BFORMAT_FLOAT),
|
||||||
|
Self::UnknownBasic(x) => ( x, uuid_from_basic_tag(x) ),
|
||||||
|
Self::UnknownExtended(x) => ( BASIC_EXTENDED, x)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
389
src/cue.rs
Normal file
389
src/cue.rs
Normal file
@@ -0,0 +1,389 @@
|
|||||||
|
use super::fourcc::{FourCC,ReadFourCC, WriteFourCC, LABL_SIG, NOTE_SIG,
|
||||||
|
ADTL_SIG, LTXT_SIG, DATA_SIG};
|
||||||
|
use super::list_form::collect_list_form;
|
||||||
|
|
||||||
|
use byteorder::{WriteBytesExt, ReadBytesExt, LittleEndian};
|
||||||
|
|
||||||
|
use encoding::{DecoderTrap,EncoderTrap};
|
||||||
|
use encoding::{Encoding};
|
||||||
|
use encoding::all::ASCII;
|
||||||
|
|
||||||
|
use std::io::{Cursor, Error, Read, Write};
|
||||||
|
|
||||||
|
#[derive(Copy,Clone, Debug)]
|
||||||
|
struct RawCue {
|
||||||
|
cue_point_id : u32,
|
||||||
|
frame : u32,
|
||||||
|
chunk_id : FourCC,
|
||||||
|
chunk_start : u32,
|
||||||
|
block_start : u32,
|
||||||
|
frame_offset : u32
|
||||||
|
}
|
||||||
|
|
||||||
|
impl RawCue {
|
||||||
|
|
||||||
|
fn write_to(cues : Vec<Self>) -> Vec<u8> {
|
||||||
|
let mut writer = Cursor::new(vec![0u8; 0]);
|
||||||
|
|
||||||
|
writer.write_u32::<LittleEndian>(cues.len() as u32).unwrap();
|
||||||
|
for cue in cues.iter() {
|
||||||
|
writer.write_u32::<LittleEndian>(cue.cue_point_id).unwrap();
|
||||||
|
writer.write_u32::<LittleEndian>(cue.frame).unwrap();
|
||||||
|
writer.write_fourcc(cue.chunk_id).unwrap();
|
||||||
|
writer.write_u32::<LittleEndian>(cue.chunk_start).unwrap();
|
||||||
|
writer.write_u32::<LittleEndian>(cue.block_start).unwrap();
|
||||||
|
writer.write_u32::<LittleEndian>(cue.frame_offset).unwrap();
|
||||||
|
}
|
||||||
|
|
||||||
|
writer.into_inner()
|
||||||
|
}
|
||||||
|
|
||||||
|
fn read_from(data : &[u8]) -> Result<Vec<Self>,Error> {
|
||||||
|
let mut rdr = Cursor::new(data);
|
||||||
|
let count = rdr.read_u32::<LittleEndian>()?;
|
||||||
|
let mut retval : Vec<Self> = vec![];
|
||||||
|
|
||||||
|
for _ in 0..count {
|
||||||
|
retval.push( Self {
|
||||||
|
cue_point_id : rdr.read_u32::<LittleEndian>()?,
|
||||||
|
frame : rdr.read_u32::<LittleEndian>()?,
|
||||||
|
chunk_id : rdr.read_fourcc()?,
|
||||||
|
chunk_start : rdr.read_u32::<LittleEndian>()?,
|
||||||
|
block_start : rdr.read_u32::<LittleEndian>()?,
|
||||||
|
frame_offset : rdr.read_u32::<LittleEndian>()?
|
||||||
|
})
|
||||||
|
}
|
||||||
|
|
||||||
|
Ok( retval )
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
#[derive(Clone, Debug)]
|
||||||
|
struct RawLabel {
|
||||||
|
cue_point_id : u32,
|
||||||
|
text : Vec<u8>
|
||||||
|
}
|
||||||
|
|
||||||
|
impl RawLabel {
|
||||||
|
|
||||||
|
fn write_to(&self) -> Vec<u8> {
|
||||||
|
let mut writer = Cursor::new(vec![0u8;0]);
|
||||||
|
writer.write_u32::<LittleEndian>(self.cue_point_id as u32).unwrap();
|
||||||
|
writer.write(&self.text).unwrap();
|
||||||
|
writer.into_inner()
|
||||||
|
}
|
||||||
|
|
||||||
|
fn read_from(data : &[u8]) -> Result<Self, Error> {
|
||||||
|
let mut rdr = Cursor::new(data);
|
||||||
|
let length = data.len();
|
||||||
|
|
||||||
|
Ok( Self {
|
||||||
|
cue_point_id : rdr.read_u32::<LittleEndian>()?,
|
||||||
|
text : {
|
||||||
|
let mut buf = vec![0u8; (length - 4) as usize ];
|
||||||
|
rdr.read_exact(&mut buf)?;
|
||||||
|
buf
|
||||||
|
}
|
||||||
|
})
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
#[derive(Clone, Debug)]
|
||||||
|
struct RawNote {
|
||||||
|
cue_point_id : u32,
|
||||||
|
text : Vec<u8>
|
||||||
|
}
|
||||||
|
|
||||||
|
impl RawNote {
|
||||||
|
|
||||||
|
fn write_to(&self) -> Vec<u8> {
|
||||||
|
let mut writer = Cursor::new(vec![0u8; 0]);
|
||||||
|
writer.write_u32::<LittleEndian>(self.cue_point_id).unwrap();
|
||||||
|
writer.write(&self.text).unwrap();
|
||||||
|
writer.into_inner()
|
||||||
|
}
|
||||||
|
|
||||||
|
fn read_from(data : &[u8]) -> Result<Self, Error> {
|
||||||
|
let mut rdr = Cursor::new(data);
|
||||||
|
let length = data.len();
|
||||||
|
|
||||||
|
Ok( Self {
|
||||||
|
cue_point_id : rdr.read_u32::<LittleEndian>()?,
|
||||||
|
text : {
|
||||||
|
let mut buf = vec![0u8; (length - 4) as usize ];
|
||||||
|
rdr.read_exact(&mut buf)?;
|
||||||
|
buf
|
||||||
|
}
|
||||||
|
})
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
#[derive(Clone, Debug)]
|
||||||
|
struct RawLtxt {
|
||||||
|
cue_point_id : u32,
|
||||||
|
frame_length : u32,
|
||||||
|
purpose : FourCC,
|
||||||
|
country : u16,
|
||||||
|
language : u16,
|
||||||
|
dialect : u16,
|
||||||
|
code_page : u16,
|
||||||
|
text: Option<Vec<u8>>
|
||||||
|
}
|
||||||
|
|
||||||
|
impl RawLtxt {
|
||||||
|
|
||||||
|
fn write_to(&self) -> Vec<u8> {
|
||||||
|
let mut writer = Cursor::new(vec![0u8; 0]);
|
||||||
|
writer.write_u32::<LittleEndian>(self.cue_point_id).unwrap();
|
||||||
|
writer.write_u32::<LittleEndian>(self.frame_length).unwrap();
|
||||||
|
writer.write_fourcc(self.purpose).unwrap();
|
||||||
|
writer.write_u16::<LittleEndian>(self.country).unwrap();
|
||||||
|
writer.write_u16::<LittleEndian>(self.language).unwrap();
|
||||||
|
writer.write_u16::<LittleEndian>(self.dialect).unwrap();
|
||||||
|
writer.write_u16::<LittleEndian>(self.code_page).unwrap();
|
||||||
|
if let Some(ext_text) = &self.text {
|
||||||
|
writer.write(ext_text).unwrap();
|
||||||
|
}
|
||||||
|
writer.into_inner()
|
||||||
|
}
|
||||||
|
|
||||||
|
fn read_from(data : &[u8]) -> Result<Self, Error> {
|
||||||
|
let mut rdr = Cursor::new(data);
|
||||||
|
let length = data.len();
|
||||||
|
|
||||||
|
Ok( Self {
|
||||||
|
cue_point_id : rdr.read_u32::<LittleEndian>()?,
|
||||||
|
frame_length : rdr.read_u32::<LittleEndian>()?,
|
||||||
|
purpose : rdr.read_fourcc()?,
|
||||||
|
country : rdr.read_u16::<LittleEndian>()?,
|
||||||
|
language : rdr.read_u16::<LittleEndian>()?,
|
||||||
|
dialect : rdr.read_u16::<LittleEndian>()?,
|
||||||
|
code_page : rdr.read_u16::<LittleEndian>()?,
|
||||||
|
text : {
|
||||||
|
if length - 20 > 0 {
|
||||||
|
let mut buf = vec![0u8; (length - 20) as usize];
|
||||||
|
rdr.read_exact(&mut buf)?;
|
||||||
|
Some( buf )
|
||||||
|
} else {
|
||||||
|
None
|
||||||
|
}
|
||||||
|
}
|
||||||
|
})
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
#[derive(Clone, Debug)]
|
||||||
|
enum RawAdtlMember {
|
||||||
|
Label(RawLabel),
|
||||||
|
Note(RawNote),
|
||||||
|
LabeledText(RawLtxt),
|
||||||
|
Unrecognized(FourCC)
|
||||||
|
}
|
||||||
|
|
||||||
|
impl RawAdtlMember {
|
||||||
|
fn compile_adtl(members : &[Self]) -> Vec<u8> {
|
||||||
|
let mut w = Cursor::new(vec![0u8; 0]);
|
||||||
|
// It seems like all this casing could be done with traits
|
||||||
|
for member in members.iter() {
|
||||||
|
let (fcc, buf) = match member {
|
||||||
|
RawAdtlMember::Label(l) => ( (LABL_SIG, l.write_to()) ),
|
||||||
|
RawAdtlMember::Note(n) => ( (NOTE_SIG, n.write_to()) ),
|
||||||
|
RawAdtlMember::LabeledText(t) => ( (LTXT_SIG, t.write_to()) ),
|
||||||
|
RawAdtlMember::Unrecognized(f) => (*f, vec![0u8;0] ) // <-- this is a dopey case but here for completeness
|
||||||
|
};
|
||||||
|
w.write_fourcc(fcc).unwrap();
|
||||||
|
w.write_u32::<LittleEndian>(buf.len() as u32).unwrap();
|
||||||
|
w.write(&buf).unwrap();
|
||||||
|
if buf.len() % 2 == 1 { w.write_u8(0).unwrap(); }
|
||||||
|
}
|
||||||
|
|
||||||
|
let chunk_content = w.into_inner();
|
||||||
|
let mut writer = Cursor::new(vec![0u8; 0]);
|
||||||
|
writer.write_fourcc(ADTL_SIG).unwrap();
|
||||||
|
writer.write_u32::<LittleEndian>(chunk_content.len() as u32).unwrap();
|
||||||
|
writer.write(&chunk_content).unwrap();
|
||||||
|
writer.into_inner()
|
||||||
|
}
|
||||||
|
|
||||||
|
fn collect_from(chunk : &[u8]) -> Result<Vec<RawAdtlMember>,Error> {
|
||||||
|
let chunks = collect_list_form(chunk)?;
|
||||||
|
let mut retval : Vec<RawAdtlMember> = vec![];
|
||||||
|
|
||||||
|
for chunk in chunks.iter() {
|
||||||
|
retval.push(
|
||||||
|
match chunk.signature {
|
||||||
|
LABL_SIG => RawAdtlMember::Label( RawLabel::read_from(&chunk.contents)? ),
|
||||||
|
NOTE_SIG => RawAdtlMember::Note( RawNote::read_from(&chunk.contents)? ),
|
||||||
|
LTXT_SIG => RawAdtlMember::LabeledText( RawLtxt::read_from(&chunk.contents)? ),
|
||||||
|
x => RawAdtlMember::Unrecognized(x)
|
||||||
|
}
|
||||||
|
)
|
||||||
|
}
|
||||||
|
Ok( retval )
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
trait AdtlMemberSearch {
|
||||||
|
fn labels_for_cue_point(&self, id: u32) -> Vec<&RawLabel>;
|
||||||
|
fn notes_for_cue_point(&self, id : u32) -> Vec<&RawNote>;
|
||||||
|
fn ltxt_for_cue_point(&self, id: u32) -> Vec<&RawLtxt>;
|
||||||
|
}
|
||||||
|
|
||||||
|
impl AdtlMemberSearch for Vec<RawAdtlMember> {
|
||||||
|
|
||||||
|
fn labels_for_cue_point(&self, id: u32) -> Vec<&RawLabel> {
|
||||||
|
self.iter().filter_map(|item| {
|
||||||
|
match item {
|
||||||
|
RawAdtlMember::Label(x) if x.cue_point_id == id => Some(x),
|
||||||
|
_ => None
|
||||||
|
}
|
||||||
|
})
|
||||||
|
.collect()
|
||||||
|
}
|
||||||
|
|
||||||
|
fn notes_for_cue_point(&self, id: u32) -> Vec<&RawNote> {
|
||||||
|
self.iter().filter_map(|item| {
|
||||||
|
match item {
|
||||||
|
RawAdtlMember::Note(x) if x.cue_point_id == id => Some(x),
|
||||||
|
_ => None
|
||||||
|
}
|
||||||
|
})
|
||||||
|
.collect()
|
||||||
|
}
|
||||||
|
|
||||||
|
fn ltxt_for_cue_point(&self, id: u32) -> Vec<&RawLtxt> {
|
||||||
|
self.iter().filter_map(|item| {
|
||||||
|
match item {
|
||||||
|
RawAdtlMember::LabeledText(x) if x.cue_point_id == id => Some(x),
|
||||||
|
_ => None
|
||||||
|
}
|
||||||
|
})
|
||||||
|
.collect()
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
/// A cue point recorded in the `cue` and `adtl` metadata.
|
||||||
|
///
|
||||||
|
/// ## Resources
|
||||||
|
/// - [Cue list, label and other metadata](https://sites.google.com/site/musicgapi/technical-documents/wav-file-format#smpl)
|
||||||
|
///
|
||||||
|
/// ### Not Implemented
|
||||||
|
/// - [EBU 3285 Supplement 2](https://tech.ebu.ch/docs/tech/tech3285s2.pdf) (July 2001): Quality chunk and cuesheet
|
||||||
|
pub struct Cue {
|
||||||
|
|
||||||
|
/// The time of this marker
|
||||||
|
pub frame : u32,
|
||||||
|
|
||||||
|
/// The length of this marker, if it is a range
|
||||||
|
pub length : Option<u32>,
|
||||||
|
|
||||||
|
/// The text "label"/name of this marker if provided
|
||||||
|
pub label : Option<String>,
|
||||||
|
|
||||||
|
/// The text "note"/comment of this marker if provided
|
||||||
|
pub note : Option<String>
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
fn convert_to_cue_string(buffer : &[u8]) -> String {
|
||||||
|
let trimmed : Vec<u8> = buffer.iter().take_while(|c| **c != 0 as u8).cloned().collect();
|
||||||
|
ASCII.decode(&trimmed, DecoderTrap::Ignore).expect("Error decoding text")
|
||||||
|
}
|
||||||
|
|
||||||
|
fn convert_from_cue_string(val : &str) -> Vec<u8> {
|
||||||
|
ASCII.encode(&val, EncoderTrap::Ignore).expect("Error encoding text")
|
||||||
|
}
|
||||||
|
|
||||||
|
impl Cue {
|
||||||
|
|
||||||
|
/// Take a list of `Cue`s and convert it into `RawCue` and `RawAdtlMember`s
|
||||||
|
fn compile_to(cues : &[Cue]) -> (Vec<RawCue>, Vec<RawAdtlMember>) {
|
||||||
|
cues.iter().enumerate()
|
||||||
|
.map(|(n, cue)| {
|
||||||
|
let raw_cue = RawCue {
|
||||||
|
cue_point_id: n as u32,
|
||||||
|
frame : cue.frame,
|
||||||
|
chunk_id: DATA_SIG,
|
||||||
|
chunk_start: 0,
|
||||||
|
block_start: 0,
|
||||||
|
frame_offset: cue.frame
|
||||||
|
};
|
||||||
|
|
||||||
|
let raw_label = cue.label.as_ref().map(|val| {
|
||||||
|
RawLabel {
|
||||||
|
cue_point_id : n as u32,
|
||||||
|
text: convert_from_cue_string(&val)
|
||||||
|
}
|
||||||
|
});
|
||||||
|
|
||||||
|
let raw_note = cue.note.as_ref().map(|val| {
|
||||||
|
RawNote {
|
||||||
|
cue_point_id: n as u32,
|
||||||
|
text : convert_from_cue_string(&val)
|
||||||
|
}
|
||||||
|
});
|
||||||
|
|
||||||
|
let raw_ltxt = cue.length.map(|val| {
|
||||||
|
RawLtxt {
|
||||||
|
cue_point_id : n as u32,
|
||||||
|
frame_length : val,
|
||||||
|
purpose : FourCC::make(b"rgn "),
|
||||||
|
country : 0,
|
||||||
|
language : 0,
|
||||||
|
dialect : 0,
|
||||||
|
code_page : 0,
|
||||||
|
text : None
|
||||||
|
}
|
||||||
|
});
|
||||||
|
|
||||||
|
(raw_cue, raw_label, raw_note, raw_ltxt)
|
||||||
|
})
|
||||||
|
.fold((Vec::<RawCue>::new(), Vec::<RawAdtlMember>::new()),
|
||||||
|
|(mut cues, mut adtls) , (cue, label, note, ltxt)| {
|
||||||
|
cues.push(cue);
|
||||||
|
label.map(|l| adtls.push( RawAdtlMember::Label(l)));
|
||||||
|
note.map(|n| adtls.push(RawAdtlMember::Note(n)));
|
||||||
|
ltxt.map(|m| adtls.push(RawAdtlMember::LabeledText(m)));
|
||||||
|
(cues, adtls)
|
||||||
|
})
|
||||||
|
}
|
||||||
|
|
||||||
|
pub fn collect_from(cue_chunk : &[u8], adtl_chunk : Option<&[u8]>) -> Result<Vec<Cue>, Error> {
|
||||||
|
let raw_cues = RawCue::read_from(cue_chunk)?;
|
||||||
|
let raw_adtl : Vec<RawAdtlMember>;
|
||||||
|
|
||||||
|
if let Some(adtl) = adtl_chunk {
|
||||||
|
raw_adtl = RawAdtlMember::collect_from(adtl)?;
|
||||||
|
} else {
|
||||||
|
raw_adtl = vec![];
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
Ok(
|
||||||
|
raw_cues.iter()
|
||||||
|
.map(|i| {
|
||||||
|
Cue {
|
||||||
|
//ident : i.cue_point_id,
|
||||||
|
frame : i.frame,
|
||||||
|
length: {
|
||||||
|
raw_adtl.ltxt_for_cue_point(i.cue_point_id).first()
|
||||||
|
.filter(|x| x.purpose == FourCC::make(b"rgn "))
|
||||||
|
.map(|x| x.frame_length)
|
||||||
|
},
|
||||||
|
label: {
|
||||||
|
raw_adtl.labels_for_cue_point(i.cue_point_id).iter()
|
||||||
|
.map(|s| convert_to_cue_string(&s.text))
|
||||||
|
.next()
|
||||||
|
},
|
||||||
|
note : {
|
||||||
|
raw_adtl.notes_for_cue_point(i.cue_point_id).iter()
|
||||||
|
//.filter_map(|x| str::from_utf8(&x.text).ok())
|
||||||
|
.map(|s| convert_to_cue_string(&s.text))
|
||||||
|
.next()
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}).collect()
|
||||||
|
)
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
||||||
@@ -1,17 +1,46 @@
|
|||||||
use std::io;
|
use std::io;
|
||||||
use super::fourcc::FourCC;
|
use super::fourcc::FourCC;
|
||||||
|
|
||||||
|
use uuid;
|
||||||
|
|
||||||
|
/// Errors returned by methods in this crate.
|
||||||
#[derive(Debug)]
|
#[derive(Debug)]
|
||||||
pub enum Error {
|
pub enum Error {
|
||||||
|
|
||||||
|
/// An `io::Error` occurred
|
||||||
IOError(io::Error),
|
IOError(io::Error),
|
||||||
|
|
||||||
|
/// An error occured reading a tag UUID
|
||||||
|
UuidError(uuid::Error),
|
||||||
|
|
||||||
|
/// The file does not begin with a recognized WAVE header
|
||||||
HeaderNotRecognized,
|
HeaderNotRecognized,
|
||||||
|
|
||||||
|
/// A wave file with a 64-bit header does not contain
|
||||||
|
/// the required `ds64` metadata element
|
||||||
MissingRequiredDS64,
|
MissingRequiredDS64,
|
||||||
|
|
||||||
|
/// A data chunk required to complete the operation
|
||||||
|
/// is not present in the file
|
||||||
ChunkMissing { signature : FourCC },
|
ChunkMissing { signature : FourCC },
|
||||||
|
|
||||||
|
/// The file is formatted improperly
|
||||||
FmtChunkAfterData,
|
FmtChunkAfterData,
|
||||||
|
|
||||||
|
/// The file did not validate as a minimal WAV file
|
||||||
NotMinimalWaveFile,
|
NotMinimalWaveFile,
|
||||||
|
|
||||||
|
/// The `data` chunk is not aligned to the desired page
|
||||||
|
/// boundary
|
||||||
DataChunkNotAligned,
|
DataChunkNotAligned,
|
||||||
|
|
||||||
|
/// The file cannot be converted into an RF64 file due
|
||||||
|
/// to its internal structure
|
||||||
InsufficientDS64Reservation {expected: u64, actual: u64},
|
InsufficientDS64Reservation {expected: u64, actual: u64},
|
||||||
DataChunkNotPreparedForAppend
|
|
||||||
|
/// The file is not optimized for writing new data
|
||||||
|
DataChunkNotPreparedForAppend,
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
@@ -19,4 +48,10 @@ impl From<io::Error> for Error {
|
|||||||
fn from(error: io::Error) -> Error {
|
fn from(error: io::Error) -> Error {
|
||||||
Error::IOError(error)
|
Error::IOError(error)
|
||||||
}
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
impl From <uuid::Error> for Error {
|
||||||
|
fn from(error: uuid::Error) -> Error {
|
||||||
|
Error::UuidError(error)
|
||||||
|
}
|
||||||
}
|
}
|
||||||
427
src/fmt.rs
Normal file
427
src/fmt.rs
Normal file
@@ -0,0 +1,427 @@
|
|||||||
|
use uuid::Uuid;
|
||||||
|
use super::common_format::{CommonFormat, UUID_PCM,UUID_BFORMAT_PCM};
|
||||||
|
use std::io::Cursor;
|
||||||
|
|
||||||
|
use byteorder::LittleEndian;
|
||||||
|
use byteorder::{WriteBytesExt, ReadBytesExt};
|
||||||
|
|
||||||
|
// Need more test cases for ADMAudioID
|
||||||
|
#[allow(dead_code)]
|
||||||
|
|
||||||
|
/// ADM Audio ID record.
|
||||||
|
///
|
||||||
|
/// This structure relates a channel in the wave file to either a common ADM
|
||||||
|
/// channel definition or further definition in the WAV file's ADM metadata
|
||||||
|
/// chunk.
|
||||||
|
///
|
||||||
|
/// An individual channel in a WAV file can have multiple Audio IDs in an ADM
|
||||||
|
/// `AudioProgramme`.
|
||||||
|
///
|
||||||
|
/// See BS.2088-1 § 8, also BS.2094, also blahblahblah...
|
||||||
|
pub struct ADMAudioID {
|
||||||
|
pub track_uid: [char; 12],
|
||||||
|
pub channel_format_ref: [char; 14],
|
||||||
|
pub pack_ref: [char; 11]
|
||||||
|
}
|
||||||
|
|
||||||
|
/// Describes a single channel in a WAV file.
|
||||||
|
///
|
||||||
|
/// This information is correlated from the Wave format ChannelMap field and
|
||||||
|
/// the `chna` chunk, if present.
|
||||||
|
pub struct ChannelDescriptor {
|
||||||
|
/// Index, the offset of this channel's samples in one frame.
|
||||||
|
pub index: u16,
|
||||||
|
|
||||||
|
/// Channel assignment
|
||||||
|
///
|
||||||
|
/// This is either implied (in the case of mono or stereo wave files) or
|
||||||
|
/// explicitly given in `WaveFormatExtentended` for files with more tracks.
|
||||||
|
pub speaker: ChannelMask,
|
||||||
|
|
||||||
|
/// ADM audioTrackUIDs
|
||||||
|
pub adm_track_audio_ids: Vec<ADMAudioID>,
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
/// A bitmask indicating which channels are present in
|
||||||
|
/// the file.
|
||||||
|
///
|
||||||
|
#[derive(Debug, Clone, Copy, PartialEq)]
|
||||||
|
pub enum ChannelMask {
|
||||||
|
DirectOut = 0x0,
|
||||||
|
FrontLeft = 0x1,
|
||||||
|
FrontRight = 0x2,
|
||||||
|
FrontCenter = 0x4,
|
||||||
|
LowFrequency = 0x8,
|
||||||
|
BackLeft = 0x10,
|
||||||
|
BackRight = 0x20,
|
||||||
|
FrontCenterLeft = 0x40,
|
||||||
|
FrontCenterRight = 0x80,
|
||||||
|
BackCenter = 0x100,
|
||||||
|
SideLeft = 0x200,
|
||||||
|
SideRight = 0x400,
|
||||||
|
TopCenter = 0x800,
|
||||||
|
TopFrontLeft = 0x1000,
|
||||||
|
TopFrontCenter = 0x2000,
|
||||||
|
TopFrontRight = 0x4000,
|
||||||
|
TopBackLeft = 0x8000,
|
||||||
|
TopBackCenter = 0x10000,
|
||||||
|
TopBackRight = 0x20000,
|
||||||
|
}
|
||||||
|
|
||||||
|
impl From<u32> for ChannelMask {
|
||||||
|
|
||||||
|
fn from(value: u32) -> Self {
|
||||||
|
match value {
|
||||||
|
0x1 => Self::FrontLeft,
|
||||||
|
0x2 => Self::FrontRight,
|
||||||
|
0x4 => Self::FrontCenter,
|
||||||
|
0x8 => Self::LowFrequency,
|
||||||
|
0x10 => Self::BackLeft,
|
||||||
|
0x20 => Self::BackRight,
|
||||||
|
0x40 => Self::FrontCenterLeft,
|
||||||
|
0x80 => Self::FrontCenterRight,
|
||||||
|
0x100 => Self::BackCenter,
|
||||||
|
0x200 => Self::SideLeft,
|
||||||
|
0x400 => Self::SideRight,
|
||||||
|
0x800 => Self::TopCenter,
|
||||||
|
0x1000 => Self::TopFrontLeft,
|
||||||
|
0x2000 => Self::TopFrontCenter,
|
||||||
|
0x4000 => Self::TopFrontRight,
|
||||||
|
0x8000 => Self::TopBackLeft,
|
||||||
|
0x10000 => Self::TopBackCenter,
|
||||||
|
0x20000 => Self::TopBackRight,
|
||||||
|
_ => Self::DirectOut
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
impl ChannelMask {
|
||||||
|
pub fn channels(input_mask : u32, channel_count: u16) -> Vec<ChannelMask> {
|
||||||
|
let reserved_mask = 0xfff2_0000_u32;
|
||||||
|
if (input_mask & reserved_mask) > 0 {
|
||||||
|
vec![ ChannelMask::DirectOut ; channel_count as usize ]
|
||||||
|
} else {
|
||||||
|
(0..18).map(|i| 1 << i )
|
||||||
|
.filter(|mask| mask & input_mask > 0)
|
||||||
|
.map(|mask| Into::<ChannelMask>::into(mask))
|
||||||
|
.collect()
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Extended Wave Format
|
||||||
|
*
|
||||||
|
* https://docs.microsoft.com/en-us/windows/win32/api/mmreg/ns-mmreg-waveformatextensible
|
||||||
|
*/
|
||||||
|
#[derive(Debug, Copy, Clone)]
|
||||||
|
pub struct WaveFmtExtended {
|
||||||
|
|
||||||
|
/// Valid bits per sample
|
||||||
|
pub valid_bits_per_sample : u16,
|
||||||
|
|
||||||
|
/// Channel mask
|
||||||
|
///
|
||||||
|
/// Identifies the speaker assignment for each channel in the file
|
||||||
|
pub channel_mask : u32,
|
||||||
|
|
||||||
|
/// Codec GUID
|
||||||
|
///
|
||||||
|
/// Identifies the codec of the audio stream
|
||||||
|
pub type_guid : Uuid,
|
||||||
|
}
|
||||||
|
|
||||||
|
///
|
||||||
|
/// WAV file data format record.
|
||||||
|
///
|
||||||
|
/// The `fmt` record contains essential information describing the binary
|
||||||
|
/// structure of the data segment of the WAVE file, such as sample
|
||||||
|
/// rate, sample binary format, channel count, etc.
|
||||||
|
///
|
||||||
|
///
|
||||||
|
/// ## Resources
|
||||||
|
///
|
||||||
|
/// ### Implementation of Wave format `fmt` chunk
|
||||||
|
/// - [MSDN WAVEFORMATEX](https://docs.microsoft.com/en-us/windows/win32/api/mmeapi/ns-mmeapi-waveformatex)
|
||||||
|
/// - [MSDN WAVEFORMATEXTENSIBLE](https://docs.microsoft.com/en-us/windows/win32/api/mmreg/ns-mmreg-waveformatextensible)
|
||||||
|
///
|
||||||
|
/// ### Other resources
|
||||||
|
/// - [RFC 3261][rfc3261] (June 1998) "WAVE and AVI Codec Registries"
|
||||||
|
/// - [Sampler Metadata](http://www.piclist.com/techref/io/serial/midi/wave.html)
|
||||||
|
/// - [Peter Kabal, McGill University](http://www-mmsp.ece.mcgill.ca/Documents/AudioFormats/WAVE/WAVE.html)
|
||||||
|
/// - [Multimedia Programming Interface and Data Specifications 1.0](http://www-mmsp.ece.mcgill.ca/Documents/AudioFormats/WAVE/Docs/riffmci.pdf)
|
||||||
|
/// (August 1991), IBM Corporation and Microsoft Corporation
|
||||||
|
///
|
||||||
|
/// [rfc3261]: https://tools.ietf.org/html/rfc2361
|
||||||
|
|
||||||
|
|
||||||
|
#[derive(Debug, Copy, Clone)]
|
||||||
|
pub struct WaveFmt {
|
||||||
|
|
||||||
|
/// A tag identifying the codec in use.
|
||||||
|
///
|
||||||
|
/// If this is 0xFFFE, the codec will be identified by a GUID
|
||||||
|
/// in `extended_format`
|
||||||
|
pub tag: u16,
|
||||||
|
|
||||||
|
/// Count of audio channels in each frame
|
||||||
|
pub channel_count: u16,
|
||||||
|
|
||||||
|
/// Playback rate of the audio data
|
||||||
|
///
|
||||||
|
/// In frames per second.
|
||||||
|
pub sample_rate: u32,
|
||||||
|
|
||||||
|
/// Count of bytes per second
|
||||||
|
///
|
||||||
|
/// By rule, this is `block_alignment * sample_rate`
|
||||||
|
pub bytes_per_second: u32,
|
||||||
|
|
||||||
|
/// Count of bytes per audio frame
|
||||||
|
///
|
||||||
|
/// By rule, this is `channel_count * bits_per_sample / 8`
|
||||||
|
pub block_alignment: u16,
|
||||||
|
|
||||||
|
/// Count of bits stored in the file per sample
|
||||||
|
///
|
||||||
|
/// By rule, `bits_per_sample % 8 == 0` for Broadcast-Wave files.
|
||||||
|
///
|
||||||
|
/// Modern clients will encode
|
||||||
|
/// unusual sample sizes in normal byte sizes but will set the valid_bits
|
||||||
|
/// flag in extended format record.
|
||||||
|
///
|
||||||
|
/// Generally speaking this will be true for all modern wave files, though
|
||||||
|
/// there was an historical "packed" stereo format of 20 bits per sample,
|
||||||
|
/// 5 bytes per frame, 5 bytes block alignment.
|
||||||
|
pub bits_per_sample: u16,
|
||||||
|
|
||||||
|
/// Extended format description
|
||||||
|
///
|
||||||
|
/// Additional format metadata if `channel_count` is greater than 2,
|
||||||
|
/// or if certain codecs are used.
|
||||||
|
pub extended_format: Option<WaveFmtExtended>
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
impl WaveFmt {
|
||||||
|
|
||||||
|
pub fn valid_bits_per_sample(&self) -> u16 {
|
||||||
|
if let Some(ext) = self.extended_format {
|
||||||
|
ext.valid_bits_per_sample
|
||||||
|
} else {
|
||||||
|
self.bits_per_sample
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
/// Create a new integer PCM format for a monoaural audio stream.
|
||||||
|
pub fn new_pcm_mono(sample_rate: u32, bits_per_sample: u16) -> Self {
|
||||||
|
Self::new_pcm_multichannel(sample_rate, bits_per_sample, 0x4)
|
||||||
|
}
|
||||||
|
|
||||||
|
/// Create a new integer PCM format for a standard Left-Right stereo audio
|
||||||
|
/// stream.
|
||||||
|
pub fn new_pcm_stereo(sample_rate: u32, bits_per_sample: u16) -> Self {
|
||||||
|
Self::new_pcm_multichannel(sample_rate, bits_per_sample, 0x3)
|
||||||
|
}
|
||||||
|
|
||||||
|
/// Create a new integer PCM format for ambisonic b-format.
|
||||||
|
pub fn new_pcm_ambisonic(sample_rate: u32, bits_per_sample: u16, channel_count: u16) -> Self {
|
||||||
|
let container_bits_per_sample = bits_per_sample + (bits_per_sample % 8);
|
||||||
|
let container_bytes_per_sample= container_bits_per_sample / 8;
|
||||||
|
|
||||||
|
WaveFmt {
|
||||||
|
tag : 0xFFFE,
|
||||||
|
channel_count,
|
||||||
|
sample_rate,
|
||||||
|
bytes_per_second: container_bytes_per_sample as u32 * sample_rate * channel_count as u32,
|
||||||
|
block_alignment: container_bytes_per_sample * channel_count,
|
||||||
|
bits_per_sample: container_bits_per_sample,
|
||||||
|
extended_format: Some(WaveFmtExtended {
|
||||||
|
valid_bits_per_sample: bits_per_sample,
|
||||||
|
channel_mask: ChannelMask::DirectOut as u32,
|
||||||
|
type_guid: UUID_BFORMAT_PCM
|
||||||
|
})
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
/// Create a new integer PCM format `WaveFmt` with a custom channel bitmap.
|
||||||
|
///
|
||||||
|
/// The order of `channels` is not important. When reading or writing
|
||||||
|
/// audio frames you must use the standard multichannel order for Wave
|
||||||
|
/// files, the numerical order of the cases of `ChannelMask`.
|
||||||
|
pub fn new_pcm_multichannel(sample_rate: u32, bits_per_sample: u16, channel_bitmap: u32) -> Self {
|
||||||
|
let container_bits_per_sample = bits_per_sample + (bits_per_sample % 8);
|
||||||
|
let container_bytes_per_sample= container_bits_per_sample / 8;
|
||||||
|
|
||||||
|
let channel_count: u16 = (0..=31).fold(0u16, |accum, n| accum + (0x1 & (channel_bitmap >> n) as u16) );
|
||||||
|
|
||||||
|
let result : (u16, Option<WaveFmtExtended>) = match channel_bitmap {
|
||||||
|
ch if bits_per_sample != container_bits_per_sample => (
|
||||||
|
(0xFFFE, Some(WaveFmtExtended { valid_bits_per_sample: bits_per_sample, channel_mask: ch,
|
||||||
|
type_guid: UUID_PCM }) )
|
||||||
|
),
|
||||||
|
0b0100 => (0x0001, None),
|
||||||
|
0b0011 => (0x0001, None),
|
||||||
|
ch => (
|
||||||
|
(0xFFFE, Some( WaveFmtExtended { valid_bits_per_sample: bits_per_sample, channel_mask: ch,
|
||||||
|
type_guid: UUID_PCM}))
|
||||||
|
)
|
||||||
|
};
|
||||||
|
|
||||||
|
let (tag, extformat) = result;
|
||||||
|
|
||||||
|
WaveFmt {
|
||||||
|
tag,
|
||||||
|
channel_count,
|
||||||
|
sample_rate,
|
||||||
|
bytes_per_second: container_bytes_per_sample as u32 * sample_rate * channel_count as u32,
|
||||||
|
block_alignment: container_bytes_per_sample * channel_count,
|
||||||
|
bits_per_sample: container_bits_per_sample,
|
||||||
|
extended_format: extformat
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
/// Format or codec of the file's audio data.
|
||||||
|
///
|
||||||
|
/// The `CommonFormat` unifies the format tag and the format extension GUID. Use this
|
||||||
|
/// method to determine the codec.
|
||||||
|
pub fn common_format(&self) -> CommonFormat {
|
||||||
|
CommonFormat::make( self.tag, self.extended_format.map(|ext| ext.type_guid))
|
||||||
|
}
|
||||||
|
|
||||||
|
/// Create a frame buffer sized to hold `length` frames for a reader or
|
||||||
|
/// writer
|
||||||
|
///
|
||||||
|
/// This is a conveneince method that creates a `Vec<i32>` with
|
||||||
|
/// as many elements as there are channels in the underlying stream.
|
||||||
|
pub fn create_frame_buffer(&self, length : usize) -> Vec<i32> {
|
||||||
|
vec![0i32; self.channel_count as usize * length]
|
||||||
|
}
|
||||||
|
|
||||||
|
/// Create a raw byte buffer to hold `length` blocks from a reader or
|
||||||
|
/// writer
|
||||||
|
pub fn create_raw_buffer(&self, length : usize) -> Vec<u8> {
|
||||||
|
vec![0u8; self.block_alignment as usize * length]
|
||||||
|
}
|
||||||
|
|
||||||
|
/// Write frames into a byte vector
|
||||||
|
pub fn pack_frames(&self, from_frames: &[i32], into_bytes: &mut [u8]) -> () {
|
||||||
|
let mut write_cursor = Cursor::new(into_bytes);
|
||||||
|
|
||||||
|
assert!(from_frames.len() % self.channel_count as usize == 0,
|
||||||
|
"frames buffer does not contain a number of samples % channel_count == 0");
|
||||||
|
|
||||||
|
for n in 0..from_frames.len() {
|
||||||
|
match (self.valid_bits_per_sample(), self.bits_per_sample) {
|
||||||
|
(0..=8,8) => write_cursor.write_u8((from_frames[n] + 0x80) as u8 ).unwrap(), // EBU 3285 §A2.2
|
||||||
|
(9..=16,16) => write_cursor.write_i16::<LittleEndian>(from_frames[n] as i16).unwrap(),
|
||||||
|
(10..=24,24) => write_cursor.write_i24::<LittleEndian>(from_frames[n]).unwrap(),
|
||||||
|
(25..=32,32) => write_cursor.write_i32::<LittleEndian>(from_frames[n]).unwrap(),
|
||||||
|
(b,_)=> panic!("Unrecognized integer format, bits per sample {}, channels {}, block_alignment {}",
|
||||||
|
b, self.channel_count, self.block_alignment)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
()
|
||||||
|
}
|
||||||
|
|
||||||
|
/// Read bytes into frames
|
||||||
|
pub fn unpack_frames(&self, from_bytes: &[u8], into_frames: &mut [i32]) -> () {
|
||||||
|
let mut rdr = Cursor::new(from_bytes);
|
||||||
|
for n in 0..(into_frames.len()) {
|
||||||
|
into_frames[n] = match (self.valid_bits_per_sample(), self.bits_per_sample) {
|
||||||
|
(0..=8,8) => rdr.read_u8().unwrap() as i32 - 0x80_i32, // EBU 3285 §A2.2
|
||||||
|
(9..=16,16) => rdr.read_i16::<LittleEndian>().unwrap() as i32,
|
||||||
|
(10..=24,24) => rdr.read_i24::<LittleEndian>().unwrap(),
|
||||||
|
(25..=32,32) => rdr.read_i32::<LittleEndian>().unwrap(),
|
||||||
|
(b,_)=> panic!("Unrecognized integer format, bits per sample {}, channels {}, block_alignment {}",
|
||||||
|
b, self.channel_count, self.block_alignment)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
/// Channel descriptors for each channel.
|
||||||
|
pub fn channels(&self) -> Vec<ChannelDescriptor> {
|
||||||
|
match self.channel_count {
|
||||||
|
1 => vec![
|
||||||
|
ChannelDescriptor {
|
||||||
|
index: 0,
|
||||||
|
speaker: ChannelMask::FrontCenter,
|
||||||
|
adm_track_audio_ids: vec![]
|
||||||
|
}
|
||||||
|
],
|
||||||
|
2 => vec![
|
||||||
|
ChannelDescriptor {
|
||||||
|
index: 0,
|
||||||
|
speaker: ChannelMask::FrontLeft,
|
||||||
|
adm_track_audio_ids: vec![]
|
||||||
|
},
|
||||||
|
ChannelDescriptor {
|
||||||
|
index: 1,
|
||||||
|
speaker: ChannelMask::FrontRight,
|
||||||
|
adm_track_audio_ids: vec![]
|
||||||
|
}
|
||||||
|
],
|
||||||
|
x if x > 2 => {
|
||||||
|
let channel_mask = self.extended_format.map(|x| x.channel_mask).unwrap_or(0);
|
||||||
|
let channels = ChannelMask::channels(channel_mask, self.channel_count);
|
||||||
|
let channels_expanded = channels.iter().chain(std::iter::repeat(&ChannelMask::DirectOut));
|
||||||
|
|
||||||
|
(0..self.channel_count)
|
||||||
|
.zip(channels_expanded)
|
||||||
|
.map(|(n,chan)| ChannelDescriptor {
|
||||||
|
index: n,
|
||||||
|
speaker: *chan,
|
||||||
|
adm_track_audio_ids: vec![]
|
||||||
|
}).collect()
|
||||||
|
},
|
||||||
|
x => panic!("Channel count ({}) was illegal!", x),
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
trait ReadWavAudioData {
|
||||||
|
fn read_i32_frames(&mut self, format: WaveFmt, into: &mut [i32]) -> Result<usize,std::io::Error>;
|
||||||
|
fn read_f32_frames(&mut self, format: WaveFmt, into: &mut [f32]) -> Result<usize,std::io::Error>;
|
||||||
|
}
|
||||||
|
|
||||||
|
impl<T> ReadWavAudioData for T where T: std::io::Read {
|
||||||
|
|
||||||
|
fn read_i32_frames(&mut self, format: WaveFmt, into: &mut [i32]) -> Result<usize, std::io::Error> {
|
||||||
|
assert!(into.len() % format.channel_count as usize == 0);
|
||||||
|
|
||||||
|
for n in 0..(into.len()) {
|
||||||
|
into[n] = match (format.valid_bits_per_sample(), format.bits_per_sample) {
|
||||||
|
(0..=8,8) => self.read_u8().unwrap() as i32 - 0x80_i32, // EBU 3285 §A2.2
|
||||||
|
(9..=16,16) => self.read_i16::<LittleEndian>().unwrap() as i32,
|
||||||
|
(10..=24,24) => self.read_i24::<LittleEndian>().unwrap(),
|
||||||
|
(25..=32,32) => self.read_i32::<LittleEndian>().unwrap(),
|
||||||
|
(b,_)=> panic!("Unrecognized integer format, bits per sample {}, channels {}, block_alignment {}",
|
||||||
|
b, format.channel_count, format.block_alignment)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
todo!()
|
||||||
|
}
|
||||||
|
fn read_f32_frames(&mut self, format: WaveFmt, into: &mut [f32]) -> Result<usize, std::io::Error> {
|
||||||
|
assert!(into.len() % format.channel_count as usize == 0);
|
||||||
|
todo!()
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
|
trait WriteWavAudioData {
|
||||||
|
fn write_i32_frames(&mut self, format: WaveFmt, from: &[i32]) -> Result<usize, std::io::Error>;
|
||||||
|
fn write_f32_frames(&mut self, format: WaveFmt, from: &[f32]) -> Result<usize, std::io::Error>;
|
||||||
|
}
|
||||||
|
|
||||||
|
impl<T> WriteWavAudioData for T where T: std::io::Write {
|
||||||
|
|
||||||
|
fn write_i32_frames(&mut self, format: WaveFmt, _: &[i32]) -> Result<usize, std::io::Error> {
|
||||||
|
todo!()
|
||||||
|
}
|
||||||
|
fn write_f32_frames(&mut self, format: WaveFmt, _: &[f32]) -> Result<usize, std::io::Error> {
|
||||||
|
todo!()
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -102,9 +102,20 @@ pub const DATA_SIG: FourCC = FourCC::make(b"data");
|
|||||||
pub const FMT__SIG: FourCC = FourCC::make(b"fmt ");
|
pub const FMT__SIG: FourCC = FourCC::make(b"fmt ");
|
||||||
|
|
||||||
pub const BEXT_SIG: FourCC = FourCC::make(b"bext");
|
pub const BEXT_SIG: FourCC = FourCC::make(b"bext");
|
||||||
|
//pub const FACT_SIG: FourCC = FourCC::make(b"fact");
|
||||||
|
pub const IXML_SIG: FourCC = FourCC::make(b"iXML");
|
||||||
|
pub const AXML_SIG: FourCC = FourCC::make(b"axml");
|
||||||
|
|
||||||
pub const JUNK_SIG: FourCC = FourCC::make(b"JUNK");
|
pub const JUNK_SIG: FourCC = FourCC::make(b"JUNK");
|
||||||
pub const FLLR_SIG: FourCC = FourCC::make(b"FLLR");
|
pub const FLLR_SIG: FourCC = FourCC::make(b"FLLR");
|
||||||
|
pub const ELM1_SIG: FourCC = FourCC::make(b"elm1");
|
||||||
|
pub const LIST_SIG: FourCC = FourCC::make(b"LIST");
|
||||||
|
|
||||||
|
pub const CUE__SIG: FourCC = FourCC::make(b"cue ");
|
||||||
|
pub const ADTL_SIG: FourCC = FourCC::make(b"adtl");
|
||||||
|
pub const LABL_SIG: FourCC = FourCC::make(b"labl");
|
||||||
|
pub const NOTE_SIG: FourCC = FourCC::make(b"note");
|
||||||
|
pub const LTXT_SIG: FourCC = FourCC::make(b"ltxt");
|
||||||
|
|
||||||
|
|
||||||
#[cfg(test)]
|
#[cfg(test)]
|
||||||
|
|||||||
3
src/levl.rs
Normal file
3
src/levl.rs
Normal file
@@ -0,0 +1,3 @@
|
|||||||
|
/// Resources
|
||||||
|
///
|
||||||
|
/// [EBU 3285 Supplement 3](https://tech.ebu.ch/docs/tech/tech3285s3.pdf) (July 2001): Peak Metadata
|
||||||
69
src/lib.rs
69
src/lib.rs
@@ -1,30 +1,63 @@
|
|||||||
|
/*!
|
||||||
|
# bwavfile
|
||||||
|
|
||||||
|
Rust Wave File Reader/Writer with Broadcast-WAV, MBWF and RF64 Support
|
||||||
|
|
||||||
|
## Interfaces
|
||||||
|
|
||||||
|
### `WaveReader`
|
||||||
|
|
||||||
|
`WaveReader` can open and parse a Wave, Broadcast-Wave, or RF64/BW64 64-bit
|
||||||
|
wave file. Metadata can be accessed and parsed in arbitrary order and audio
|
||||||
|
samples can be accessed using the `AudioFrameReader` type, created by an
|
||||||
|
accessor method of `WaveReader`.
|
||||||
|
|
||||||
|
### `WaveWriter`
|
||||||
|
|
||||||
|
`WaveWriter` can create a new Wave, Broadcast-Wave, or RF64/BW64 64-bit wave
|
||||||
|
file. Metadata chunks and audio samples are added sequentially, write-only, to
|
||||||
|
a Wave file which is automatically promoted from standard Wave to RF64 wave
|
||||||
|
when the total WAVE form size exceeds 0xFFFFFFFF bytes.
|
||||||
|
|
||||||
|
|
||||||
|
## Objectives and Roadmap
|
||||||
|
|
||||||
|
This package aims to support read and writing any kind of WAV file you are likely
|
||||||
|
to encounter in a professional audio, motion picture production, broadcast, or music
|
||||||
|
production.
|
||||||
|
|
||||||
|
Apps we test against:
|
||||||
|
- Avid Pro Tools
|
||||||
|
- iZotope RX Audio Editor
|
||||||
|
- FFMpeg
|
||||||
|
- Audacity
|
||||||
|
|
||||||
|
[github]: https://github.com/iluvcapra/bwavfile
|
||||||
|
*/
|
||||||
|
|
||||||
extern crate encoding;
|
extern crate encoding;
|
||||||
extern crate byteorder;
|
extern crate byteorder;
|
||||||
|
extern crate uuid;
|
||||||
|
|
||||||
/**!
|
|
||||||
* bwavfile
|
|
||||||
*
|
|
||||||
* Rust Wave File Reader/Writer with Broadcast-WAV, MBWF and RF64 Support
|
|
||||||
*
|
|
||||||
* This crate is currently a work-in-progress (I'm using it to teach myself
|
|
||||||
* rust) so the interface may change dramatically and not all features work.
|
|
||||||
*
|
|
||||||
!*/
|
|
||||||
|
|
||||||
|
|
||||||
mod parser;
|
|
||||||
mod fourcc;
|
mod fourcc;
|
||||||
mod errors;
|
mod errors;
|
||||||
|
mod common_format;
|
||||||
|
|
||||||
mod validation;
|
mod parser;
|
||||||
|
mod list_form;
|
||||||
|
|
||||||
mod raw_chunk_reader;
|
|
||||||
mod audio_frame_reader;
|
|
||||||
mod chunks;
|
mod chunks;
|
||||||
|
mod cue;
|
||||||
|
mod bext;
|
||||||
|
mod fmt;
|
||||||
|
|
||||||
mod wavereader;
|
mod wavereader;
|
||||||
mod wavewriter;
|
mod wavewriter;
|
||||||
|
|
||||||
pub use wavereader::{WaveReader};
|
pub use errors::Error;
|
||||||
pub use chunks::{WaveFmt,Bext};
|
pub use wavereader::{WaveReader, AudioFrameReader};
|
||||||
pub use errors::Error;
|
pub use wavewriter::{WaveWriter, AudioFrameWriter};
|
||||||
|
pub use bext::Bext;
|
||||||
|
pub use fmt::{WaveFmt, WaveFmtExtended, ChannelDescriptor, ChannelMask, ADMAudioID};
|
||||||
|
pub use common_format::CommonFormat;
|
||||||
|
pub use cue::Cue;
|
||||||
40
src/list_form.rs
Normal file
40
src/list_form.rs
Normal file
@@ -0,0 +1,40 @@
|
|||||||
|
use super::fourcc::{FourCC, ReadFourCC};
|
||||||
|
use byteorder::{ReadBytesExt, LittleEndian};
|
||||||
|
use std::io::{Cursor, Error, Read};
|
||||||
|
|
||||||
|
pub struct ListFormItem {
|
||||||
|
pub signature : FourCC,
|
||||||
|
pub contents : Vec<u8>
|
||||||
|
}
|
||||||
|
|
||||||
|
/// A helper that will accept a LIST chunk as a [u8]
|
||||||
|
/// and give you back each segment
|
||||||
|
///
|
||||||
|
pub fn collect_list_form(list_contents :& [u8]) -> Result<Vec<ListFormItem>, Error> {
|
||||||
|
let mut cursor = Cursor::new(list_contents);
|
||||||
|
let mut remain = list_contents.len();
|
||||||
|
let _ = cursor.read_fourcc()?; // skip signature
|
||||||
|
|
||||||
|
remain -= 4;
|
||||||
|
let mut retval : Vec<ListFormItem> = vec![];
|
||||||
|
|
||||||
|
while remain > 0 {
|
||||||
|
let this_sig = cursor.read_fourcc()?;
|
||||||
|
let this_size = cursor.read_u32::<LittleEndian>()? as usize;
|
||||||
|
remain -= 8;
|
||||||
|
let mut content_buf = vec![0u8; this_size];
|
||||||
|
cursor.read_exact(&mut content_buf)?;
|
||||||
|
remain -= this_size;
|
||||||
|
|
||||||
|
retval.push( ListFormItem { signature : this_sig, contents : content_buf } );
|
||||||
|
|
||||||
|
if this_size % 2 == 1 {
|
||||||
|
cursor.read_u8()?;
|
||||||
|
//panic!("Got this far!");
|
||||||
|
remain -= 1;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
Ok( retval )
|
||||||
|
}
|
||||||
@@ -45,6 +45,7 @@ pub struct Parser<R: Read + Seek> {
|
|||||||
ds64state: HashMap<FourCC,u64>
|
ds64state: HashMap<FourCC,u64>
|
||||||
}
|
}
|
||||||
|
|
||||||
|
#[derive(Debug, PartialEq, Eq)]
|
||||||
pub struct ChunkIteratorItem {
|
pub struct ChunkIteratorItem {
|
||||||
pub signature: FourCC,
|
pub signature: FourCC,
|
||||||
pub start: u64,
|
pub start: u64,
|
||||||
@@ -103,7 +104,6 @@ impl<R: Read + Seek> Iterator for Parser<R> {
|
|||||||
|
|
||||||
fn next(&mut self) -> Option<Event> {
|
fn next(&mut self) -> Option<Event> {
|
||||||
let (event, next_state) = self.advance();
|
let (event, next_state) = self.advance();
|
||||||
println!("{:?}", event);
|
|
||||||
self.state = next_state;
|
self.state = next_state;
|
||||||
return event;
|
return event;
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -1,73 +0,0 @@
|
|||||||
use std::cmp::min;
|
|
||||||
|
|
||||||
use std::io::SeekFrom;
|
|
||||||
use std::io::SeekFrom::{Start, Current, End};
|
|
||||||
use std::io::{Seek,Read,Error,ErrorKind};
|
|
||||||
|
|
||||||
// I'm not sure this hasn't already been written somewhere in
|
|
||||||
// std but I'm just doing this here as an exercise.
|
|
||||||
#[derive(Debug)]
|
|
||||||
pub struct RawChunkReader<'a, R: Read + Seek> {
|
|
||||||
reader: &'a mut R,
|
|
||||||
start: u64,
|
|
||||||
length: u64,
|
|
||||||
position: u64
|
|
||||||
}
|
|
||||||
|
|
||||||
impl<'a,R: Read + Seek> RawChunkReader<'a, R> {
|
|
||||||
pub fn new(reader: &'a mut R, start: u64, length: u64) -> Self {
|
|
||||||
return Self {
|
|
||||||
reader: reader,
|
|
||||||
start: start,
|
|
||||||
length: length,
|
|
||||||
position: 0
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
pub fn length(&self) -> u64 {
|
|
||||||
self.length
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
impl<'a, R:Read + Seek> Read for RawChunkReader<'_, R> {
|
|
||||||
fn read(&mut self, buf: &mut [u8]) -> Result<usize, Error> {
|
|
||||||
if self.position >= self.length {
|
|
||||||
Err(Error::new(ErrorKind::UnexpectedEof, "RawChunkReader encountered end-of-file"))
|
|
||||||
} else {
|
|
||||||
self.reader.seek(Start(self.start + self.position))?;
|
|
||||||
let to_read = min(self.length - self.position, buf.len() as u64);
|
|
||||||
self.reader.take(to_read).read(buf)?;
|
|
||||||
self.position += to_read;
|
|
||||||
Ok(to_read as usize)
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
impl<'a, R:Read + Seek> Seek for RawChunkReader<'_, R> {
|
|
||||||
fn seek(&mut self, seek: SeekFrom) -> Result<u64, std::io::Error> {
|
|
||||||
match seek {
|
|
||||||
Start(s) => {
|
|
||||||
self.position = s;
|
|
||||||
Ok(self.position)
|
|
||||||
},
|
|
||||||
Current(s) => {
|
|
||||||
let new_position = s + self.position as i64;
|
|
||||||
if new_position < 0 {
|
|
||||||
Err( Error::new(ErrorKind::Other, "Attempted seek before beginning of chunk") )
|
|
||||||
} else {
|
|
||||||
self.position = new_position as u64;
|
|
||||||
Ok(self.position)
|
|
||||||
}
|
|
||||||
},
|
|
||||||
End(s) => {
|
|
||||||
let new_position = s + self.length as i64;
|
|
||||||
if new_position < 0 {
|
|
||||||
Err( Error::new(ErrorKind::Other, "Attempted seek before beginning of chunk") )
|
|
||||||
} else {
|
|
||||||
self.position = new_position as u64;
|
|
||||||
Ok(self.position)
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
2
src/sampler.rs
Normal file
2
src/sampler.rs
Normal file
@@ -0,0 +1,2 @@
|
|||||||
|
/// ## Resources
|
||||||
|
/// - [Sampler Metadata](http://www.piclist.com/techref/io/serial/midi/wave.html)
|
||||||
@@ -1,144 +0,0 @@
|
|||||||
use super::parser::{Parser};
|
|
||||||
use super::fourcc::{FourCC, FMT__SIG,DATA_SIG, BEXT_SIG, JUNK_SIG, FLLR_SIG};
|
|
||||||
use super::errors::Error as ParserError;
|
|
||||||
use super::wavereader::{WaveReader};
|
|
||||||
|
|
||||||
use std::io::{Read,Seek};
|
|
||||||
|
|
||||||
impl<R:Read + Seek> WaveReader<R> {
|
|
||||||
/**
|
|
||||||
* Returns without `Err` if the source meets the minimum standard of
|
|
||||||
* readability by a permissive client:
|
|
||||||
* 1. `fmt` chunk and `data` chunk are present
|
|
||||||
* 1. `fmt` chunk appears before `data` chunk
|
|
||||||
*/
|
|
||||||
pub fn validate_readable(&mut self) -> Result<(), ParserError> {
|
|
||||||
let (fmt_pos, _) = self.get_chunk_extent_at_index(FMT__SIG, 0)?;
|
|
||||||
let (data_pos, _) = self.get_chunk_extent_at_index(DATA_SIG, 0)?;
|
|
||||||
|
|
||||||
if fmt_pos < data_pos {
|
|
||||||
Ok(())
|
|
||||||
} else {
|
|
||||||
Err( ParserError::FmtChunkAfterData)
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
|
||||||
* Validate minimal WAVE file
|
|
||||||
*
|
|
||||||
* Returns without `Err` the source is `validate_readable` AND
|
|
||||||
*
|
|
||||||
* - Contains _only_ a `fmt` chunk and `data` chunk, with no other chunks present
|
|
||||||
* - is not an RF64/BW64
|
|
||||||
*
|
|
||||||
* Some clients require a WAVE file to only contain format and data without any other
|
|
||||||
* metadata and this function is provided to validate this condition.
|
|
||||||
*
|
|
||||||
* ```
|
|
||||||
* # use bwavfile::WaveReader;
|
|
||||||
*
|
|
||||||
* let mut w = WaveReader::open("tests/media/ff_minimal.wav").unwrap();
|
|
||||||
* w.validate_minimal().expect("Minimal wav did not validate not minimal!");
|
|
||||||
* ```
|
|
||||||
*
|
|
||||||
* ```
|
|
||||||
* # use bwavfile::WaveReader;
|
|
||||||
*
|
|
||||||
* let mut x = WaveReader::open("tests/media/pt_24bit_51.wav").unwrap();
|
|
||||||
* x.validate_minimal().expect_err("Complex WAV validated minimal!");
|
|
||||||
* ```
|
|
||||||
*/
|
|
||||||
pub fn validate_minimal(&mut self) -> Result<(), ParserError> {
|
|
||||||
self.validate_readable()?;
|
|
||||||
|
|
||||||
let chunk_fourccs : Vec<FourCC> = Parser::make(&mut self.inner)?
|
|
||||||
.into_chunk_list()?.iter().map(|c| c.signature ).collect();
|
|
||||||
|
|
||||||
if chunk_fourccs == vec![FMT__SIG, DATA_SIG] {
|
|
||||||
Ok(())
|
|
||||||
} else {
|
|
||||||
Err( ParserError::NotMinimalWaveFile )
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
|
||||||
* Validate Broadcast-WAVE file format
|
|
||||||
*
|
|
||||||
* Returns without `Err` if `validate_readable()` and file contains a
|
|
||||||
* Broadcast-WAV metadata record (a `bext` chunk).
|
|
||||||
*
|
|
||||||
* ```
|
|
||||||
* # use bwavfile::WaveReader;
|
|
||||||
*
|
|
||||||
* let mut w = WaveReader::open("tests/media/ff_bwav_stereo.wav").unwrap();
|
|
||||||
* w.validate_broadcast_wave().expect("BWAVE file did not validate BWAVE");
|
|
||||||
*
|
|
||||||
* let mut x = WaveReader::open("tests/media/pt_24bit.wav").unwrap();
|
|
||||||
* x.validate_broadcast_wave().expect("BWAVE file did not validate BWAVE");
|
|
||||||
*
|
|
||||||
* let mut y = WaveReader::open("tests/media/audacity_16bit.wav").unwrap();
|
|
||||||
* y.validate_broadcast_wave().expect_err("Plain WAV file DID validate BWAVE");
|
|
||||||
* ```
|
|
||||||
*/
|
|
||||||
pub fn validate_broadcast_wave(&mut self) -> Result<(), ParserError> {
|
|
||||||
self.validate_readable()?;
|
|
||||||
let (_, _) = self.get_chunk_extent_at_index(BEXT_SIG, 0)?;
|
|
||||||
Ok(())
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
|
||||||
* Verify data is aligned to a block boundary
|
|
||||||
*
|
|
||||||
* Returns without `Err` if `validate_readable()` and the start of the
|
|
||||||
* `data` chunk's content begins at 0x4000.
|
|
||||||
*/
|
|
||||||
pub fn validate_data_chunk_alignment(&mut self) -> Result<() , ParserError> {
|
|
||||||
self.validate_readable()?;
|
|
||||||
let (start, _) = self.get_chunk_extent_at_index(DATA_SIG, 0)?;
|
|
||||||
if start == 0x4000 {
|
|
||||||
Ok(())
|
|
||||||
} else {
|
|
||||||
Err(ParserError::DataChunkNotAligned)
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
|
||||||
* Returns without `Err` if:
|
|
||||||
* - `validate_readable()`
|
|
||||||
* - there is a `JUNK` or `FLLR` immediately at the beginning of the chunk
|
|
||||||
* list adequately large enough to be overwritten by a `ds64` (96 bytes)
|
|
||||||
* - `data` is the final chunk
|
|
||||||
*/
|
|
||||||
pub fn validate_prepared_for_append(&mut self) -> Result<(), ParserError> {
|
|
||||||
self.validate_readable()?;
|
|
||||||
|
|
||||||
let chunks = Parser::make(&mut self.inner)?.into_chunk_list()?;
|
|
||||||
let ds64_space_required = 92;
|
|
||||||
|
|
||||||
let eligible_filler_chunks = chunks.iter()
|
|
||||||
.take_while(|c| c.signature == JUNK_SIG || c.signature == FLLR_SIG);
|
|
||||||
|
|
||||||
let filler = eligible_filler_chunks
|
|
||||||
.enumerate()
|
|
||||||
.fold(0, |accum, (n, item)| if n == 0 { accum + item.length } else {accum + item.length + 8});
|
|
||||||
|
|
||||||
if filler < ds64_space_required {
|
|
||||||
Err(ParserError::InsufficientDS64Reservation {expected: ds64_space_required, actual: filler})
|
|
||||||
} else {
|
|
||||||
let data_pos = chunks.iter().position(|c| c.signature == DATA_SIG);
|
|
||||||
|
|
||||||
match data_pos {
|
|
||||||
Some(p) if p == chunks.len() - 1 => Ok(()),
|
|
||||||
_ => Err(ParserError::DataChunkNotPreparedForAppend)
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
@@ -1,151 +1,584 @@
|
|||||||
|
|
||||||
use std::fs::File;
|
use std::fs::File;
|
||||||
|
|
||||||
|
use std::io::SeekFrom;
|
||||||
|
use std::io::Cursor;
|
||||||
|
use std::io::{Read, Seek, BufReader};
|
||||||
|
use std::io::SeekFrom::{Start,Current,};
|
||||||
|
|
||||||
use super::parser::Parser;
|
use super::parser::Parser;
|
||||||
use super::fourcc::{FourCC, FMT__SIG, BEXT_SIG, DATA_SIG};
|
use super::fourcc::{FourCC, ReadFourCC, FMT__SIG, DATA_SIG, BEXT_SIG, LIST_SIG,
|
||||||
|
JUNK_SIG, FLLR_SIG, CUE__SIG, ADTL_SIG, AXML_SIG, IXML_SIG};
|
||||||
use super::errors::Error as ParserError;
|
use super::errors::Error as ParserError;
|
||||||
use super::raw_chunk_reader::RawChunkReader;
|
use super::fmt::{WaveFmt, ChannelDescriptor, ChannelMask};
|
||||||
use super::chunks::{WaveFmt, Bext};
|
use super::bext::Bext;
|
||||||
use super::audio_frame_reader::AudioFrameReader;
|
|
||||||
use super::chunks::ReadBWaveChunks;
|
use super::chunks::ReadBWaveChunks;
|
||||||
//use super::validation;
|
use super::cue::Cue;
|
||||||
//use std::io::SeekFrom::{Start};
|
use super::errors::Error;
|
||||||
use std::io::{Read, Seek};
|
use super::CommonFormat;
|
||||||
|
|
||||||
|
use byteorder::LittleEndian;
|
||||||
|
use byteorder::ReadBytesExt;
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
/// Read audio frames
|
||||||
|
///
|
||||||
|
/// The inner reader is interpreted as a raw audio data
|
||||||
|
/// bitstream having a format specified by `format`.
|
||||||
|
///
|
||||||
|
#[derive(Debug)]
|
||||||
|
pub struct AudioFrameReader<R: Read + Seek> {
|
||||||
|
inner : R,
|
||||||
|
format: WaveFmt,
|
||||||
|
start: u64,
|
||||||
|
length: u64
|
||||||
|
}
|
||||||
|
|
||||||
|
impl<R: Read + Seek> AudioFrameReader<R> {
|
||||||
|
|
||||||
|
/// Create a new `AudioFrameReader`
|
||||||
|
///
|
||||||
|
/// ### Panics
|
||||||
|
///
|
||||||
|
/// This method does a few sanity checks on the provided format
|
||||||
|
/// parameter to confirm the `block_alignment` law is fulfilled
|
||||||
|
/// and the format tag is readable by this implementation (only
|
||||||
|
/// format 0x01 is supported at this time.)
|
||||||
|
pub fn new(mut inner: R, format: WaveFmt, start: u64, length: u64) -> Result<Self, Error> {
|
||||||
|
assert!(format.block_alignment * 8 == format.bits_per_sample * format.channel_count,
|
||||||
|
"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 ,
|
||||||
|
"Unsupported format tag {:?}", format.tag);
|
||||||
|
|
||||||
|
inner.seek(Start(start))?;
|
||||||
|
Ok( AudioFrameReader { inner , format , start, length} )
|
||||||
|
}
|
||||||
|
|
||||||
|
/// Unwrap the inner reader.
|
||||||
|
pub fn into_inner(self) -> R {
|
||||||
|
self.inner
|
||||||
|
}
|
||||||
|
|
||||||
|
/// Locate the read position to a different frame
|
||||||
|
///
|
||||||
|
/// Seeks within the audio stream.
|
||||||
|
///
|
||||||
|
/// Returns the new location of the read position.
|
||||||
|
///
|
||||||
|
/// locate() behaves similarly to Read methods in that
|
||||||
|
/// seeking after the end of the audio data is not an error.
|
||||||
|
pub fn locate(&mut self, to :u64) -> Result<u64,Error> {
|
||||||
|
let position = to * self.format.block_alignment as u64;
|
||||||
|
let seek_result = self.inner.seek(Start(self.start + position))?;
|
||||||
|
Ok( (seek_result - self.start) / self.format.block_alignment as u64 )
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
/// Read a frame
|
||||||
|
///
|
||||||
|
/// A single frame is read from the audio stream and the read location
|
||||||
|
/// is advanced one frame.
|
||||||
|
///
|
||||||
|
/// Regardless of the number of bits in the audio sample, this method
|
||||||
|
/// always writes `i32` samples back to the buffer. These samples are
|
||||||
|
/// written back "right-aligned" so samples that are shorter than i32
|
||||||
|
/// will leave the MSB bits empty.
|
||||||
|
///
|
||||||
|
/// For example: A full-code sample in 16 bit (0xFFFF) will be written
|
||||||
|
/// back to the buffer as 0x0000FFFF.
|
||||||
|
///
|
||||||
|
///
|
||||||
|
/// ### Panics
|
||||||
|
///
|
||||||
|
/// The `buffer` must have a number of elements equal to the number of
|
||||||
|
/// channels and this method will panic if this is not the case.
|
||||||
|
pub fn read_integer_frame(&mut self, buffer:&mut [i32]) -> Result<u64,Error> {
|
||||||
|
assert!(buffer.len() as u16 == self.format.channel_count,
|
||||||
|
"read_integer_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) {
|
||||||
|
(0..=8,8) => self.inner.read_u8()? as i32 - 0x80_i32, // EBU 3285 §A2.2
|
||||||
|
(9..=16,16) => self.inner.read_i16::<LittleEndian>()? as i32,
|
||||||
|
(10..=24,24) => self.inner.read_i24::<LittleEndian>()?,
|
||||||
|
(25..=32,32) => self.inner.read_i32::<LittleEndian>()?,
|
||||||
|
(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 )
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
pub fn read_float_frame(&mut self, buffer:&mut [f32]) -> Result<u64, Error> {
|
||||||
|
todo!()
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
/// Wave, Broadcast-WAV and RF64/BW64 parser/reader.
|
||||||
|
///
|
||||||
|
/// ```
|
||||||
|
/// 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 frame_reader = r.audio_frame_reader().unwrap();
|
||||||
|
/// let mut buffer = format.create_frame_buffer(1);
|
||||||
|
///
|
||||||
|
/// let read = frame_reader.read_integer_frame(&mut buffer).unwrap();
|
||||||
|
///
|
||||||
|
/// assert_eq!(buffer, [0i32]);
|
||||||
|
/// assert_eq!(read, 1);
|
||||||
|
///
|
||||||
|
/// ```
|
||||||
|
///
|
||||||
|
/// ## Resources
|
||||||
|
///
|
||||||
|
/// ### Implementation of Wave Files
|
||||||
|
/// - [Peter Kabal, McGill University](http://www-mmsp.ece.mcgill.ca/Documents/AudioFormats/WAVE/WAVE.html)
|
||||||
|
/// - [Multimedia Programming Interface and Data Specifications 1.0](http://www-mmsp.ece.mcgill.ca/Documents/AudioFormats/WAVE/Docs/riffmci.pdf)
|
||||||
|
/// (August 1991), IBM Corporation and Microsoft Corporation
|
||||||
|
///
|
||||||
|
/// ### Implementation of Broadcast Wave Files
|
||||||
|
/// - [EBU Tech 3285][ebu3285] (May 2011), "Specification of the Broadcast Wave Format (BWF)"
|
||||||
|
/// - [Supplement 1](https://tech.ebu.ch/docs/tech/tech3285s1.pdf) (July 1997): MPEG Audio
|
||||||
|
/// - [EBU Rec 68](https://tech.ebu.ch/docs/r/r068.pdf): Signal modulation and format constraints
|
||||||
|
///
|
||||||
|
/// ### Implementation of 64-bit Wave Files
|
||||||
|
/// - [ITU-R 2088][itu2088] (October 2019), "Long-form file format for the international exchange of audio programme materials with metadata"
|
||||||
|
/// - Presently in force, adopted by the EBU in [EBU Tech 3306v2][ebu3306v2] (June 2018).
|
||||||
|
/// - [EBU Tech 3306v1][ebu3306v1] (July 2009), "MBWF / RF64: An extended File Format for Audio"
|
||||||
|
/// - No longer in force, however long-established.
|
||||||
|
///
|
||||||
|
///
|
||||||
|
/// [ebu3285]: https://tech.ebu.ch/docs/tech/tech3285.pdf
|
||||||
|
/// [ebu3306v1]: https://tech.ebu.ch/docs/tech/tech3306v1_1.pdf
|
||||||
|
/// [ebu3306v2]: https://tech.ebu.ch/docs/tech/tech3306.pdf
|
||||||
|
/// [itu2088]: https://www.itu.int/dms_pubrec/itu-r/rec/bs/R-REC-BS.2088-1-201910-I!!PDF-E.pdf
|
||||||
|
/// [rfc3261]: https://tools.ietf.org/html/rfc2361
|
||||||
|
|
||||||
|
|
||||||
/**
|
|
||||||
* Wave, Broadcast-WAV and RF64/BW64 parser/reader.
|
|
||||||
*
|
|
||||||
* ```
|
|
||||||
* 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 frame_reader = r.audio_frame_reader().unwrap();
|
|
||||||
* let mut buffer = frame_reader.create_frame_buffer();
|
|
||||||
*
|
|
||||||
* let read = frame_reader.read_integer_frame(&mut buffer).unwrap();
|
|
||||||
*
|
|
||||||
* assert_eq!(buffer, [0i32]);
|
|
||||||
* assert_eq!(read, 1);
|
|
||||||
*
|
|
||||||
* ```
|
|
||||||
*/
|
|
||||||
#[derive(Debug)]
|
#[derive(Debug)]
|
||||||
pub struct WaveReader<R: Read + Seek> {
|
pub struct WaveReader<R: Read + Seek> {
|
||||||
pub inner: R,
|
pub inner: R,
|
||||||
}
|
}
|
||||||
|
|
||||||
impl WaveReader<File> {
|
impl WaveReader<BufReader<File>> {
|
||||||
/**
|
|
||||||
* Open a file for reading.
|
|
||||||
*
|
|
||||||
* A convenience that opens `path` and calls `Self::new()`
|
|
||||||
*
|
|
||||||
*/
|
|
||||||
pub fn open(path: &str) -> Result<Self, ParserError> {
|
pub fn open(path: &str) -> Result<Self, ParserError> {
|
||||||
|
let f = File::open(path)?;
|
||||||
|
let inner = BufReader::new(f);
|
||||||
|
Ok( Self::new(inner)? )
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
impl WaveReader<File> {
|
||||||
|
|
||||||
|
/// Open a file for reading with unbuffered IO.
|
||||||
|
///
|
||||||
|
/// A convenience that opens `path` and calls `Self::new()`
|
||||||
|
|
||||||
|
pub fn open_unbuffered(path: &str) -> Result<Self, ParserError> {
|
||||||
let inner = File::open(path)?;
|
let inner = File::open(path)?;
|
||||||
return Ok( Self::new(inner)? )
|
return Ok( Self::new(inner)? )
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
impl<R: Read + Seek> WaveReader<R> {
|
impl<R: Read + Seek> WaveReader<R> {
|
||||||
/**
|
|
||||||
* Wrap a `Read` struct in a new `WaveReader`.
|
/// Wrap a `Read` struct in a new `WaveReader`.
|
||||||
*
|
///
|
||||||
* This is the primary entry point into the `WaveReader` interface. The
|
/// This is the primary entry point into the `WaveReader` interface. The
|
||||||
* stream passed as `inner` must be at the beginning of the header of the
|
/// stream passed as `inner` must be at the beginning of the header of the
|
||||||
* WAVE data. For a .wav file, this means it must be at the start of the
|
/// WAVE data. For a .wav file, this means it must be at the start of the
|
||||||
* file.
|
/// file.
|
||||||
*
|
///
|
||||||
* This function does a minimal validation on the provided stream and
|
/// This function does a minimal validation on the provided stream and
|
||||||
* will return an `Err(errors::Error)` immediately if there is a structural
|
/// will return an `Err(errors::Error)` immediately if there is a structural
|
||||||
* inconsistency that makes the stream unreadable or if it's missing
|
/// inconsistency that makes the stream unreadable or if it's missing
|
||||||
* essential components that make interpreting the audio data impoossible.
|
/// essential components that make interpreting the audio data impossible.
|
||||||
*
|
|
||||||
* ```rust
|
/// ```rust
|
||||||
* use std::fs::File;
|
/// use std::fs::File;
|
||||||
* use std::io::{Error,ErrorKind};
|
/// use std::io::{Error,ErrorKind};
|
||||||
* use bwavfile::{WaveReader, Error as WavError};
|
/// use bwavfile::{WaveReader, Error as WavError};
|
||||||
*
|
///
|
||||||
* let f = File::open("tests/media/error.wav").unwrap();
|
/// let f = File::open("tests/media/error.wav").unwrap();
|
||||||
*
|
///
|
||||||
* let reader = WaveReader::new(f);
|
/// let reader = WaveReader::new(f);
|
||||||
*
|
///
|
||||||
* match reader {
|
/// match reader {
|
||||||
* Ok(_) => panic!("error.wav should not be openable"),
|
/// Ok(_) => panic!("error.wav should not be openable"),
|
||||||
* Err( WavError::IOError( e ) ) => {
|
/// Err( WavError::IOError( e ) ) => {
|
||||||
* assert_eq!(e.kind(), ErrorKind::UnexpectedEof)
|
/// assert_eq!(e.kind(), ErrorKind::UnexpectedEof)
|
||||||
* }
|
/// }
|
||||||
* Err(e) => panic!("Unexpected error was returned {:?}", e)
|
/// Err(e) => panic!("Unexpected error was returned {:?}", e)
|
||||||
* }
|
/// }
|
||||||
*
|
///
|
||||||
* ```
|
/// ```
|
||||||
*
|
|
||||||
*/
|
|
||||||
pub fn new(inner: R) -> Result<Self,ParserError> {
|
pub fn new(inner: R) -> Result<Self,ParserError> {
|
||||||
let mut retval = Self { inner };
|
let mut retval = Self { inner };
|
||||||
retval.validate_readable()?;
|
retval.validate_readable()?;
|
||||||
Ok(retval)
|
Ok(retval)
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
|
||||||
* Unwrap the inner reader.
|
/// Unwrap the inner reader.
|
||||||
*/
|
|
||||||
pub fn into_inner(self) -> R {
|
pub fn into_inner(self) -> R {
|
||||||
return self.inner;
|
return self.inner;
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
///
|
||||||
* Create an `AudioFrameReader` for reading each audio frame.
|
/// Create an `AudioFrameReader` for reading each audio frame and consume the `WaveReader`.
|
||||||
*/
|
///
|
||||||
pub fn audio_frame_reader(&mut self) -> Result<AudioFrameReader<RawChunkReader<R>>, ParserError> {
|
pub fn audio_frame_reader(mut self) -> Result<AudioFrameReader<R>, ParserError> {
|
||||||
let format = self.format()?;
|
let format = self.format()?;
|
||||||
let audio_chunk_reader = self.chunk_reader(DATA_SIG, 0)?;
|
let audio_chunk_reader = self.get_chunk_extent_at_index(DATA_SIG, 0)?;
|
||||||
Ok(AudioFrameReader::new(audio_chunk_reader, format))
|
Ok(AudioFrameReader::new(self.inner, format, audio_chunk_reader.0, audio_chunk_reader.1)?)
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
|
||||||
* The count of audio frames in the file.
|
/// The count of audio frames in the file.
|
||||||
*/
|
|
||||||
pub fn frame_length(&mut self) -> Result<u64, ParserError> {
|
pub fn frame_length(&mut self) -> Result<u64, ParserError> {
|
||||||
let (_, data_length ) = self.get_chunk_extent_at_index(DATA_SIG, 0)?;
|
let (_, data_length ) = self.get_chunk_extent_at_index(DATA_SIG, 0)?;
|
||||||
let format = self.format()?;
|
let format = self.format()?;
|
||||||
Ok( data_length / (format.block_alignment as u64) )
|
Ok( data_length / (format.block_alignment as u64) )
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
|
||||||
* Sample and frame format of this wave file.
|
/// Sample and frame format of this wave file.
|
||||||
*/
|
///
|
||||||
pub fn format(&mut self) -> Result<WaveFmt, ParserError> {
|
pub fn format(&mut self) -> Result<WaveFmt, ParserError> {
|
||||||
self.chunk_reader(FMT__SIG, 0)?.read_wave_fmt()
|
let (start, _) = self.get_chunk_extent_at_index(FMT__SIG, 0)?;
|
||||||
|
self.inner.seek(SeekFrom::Start(start))?;
|
||||||
|
self.inner.read_wave_fmt()
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/// The Broadcast-WAV metadata record for this file, if present.
|
||||||
|
///
|
||||||
|
pub fn broadcast_extension(&mut self) -> Result<Option<Bext>, ParserError> {
|
||||||
|
let mut bext_buff : Vec<u8> = vec![ ];
|
||||||
|
let result = self.read_chunk(BEXT_SIG, 0, &mut bext_buff)?;
|
||||||
|
if result > 0 {
|
||||||
|
let mut bext_cursor = Cursor::new(bext_buff);
|
||||||
|
Ok( Some( bext_cursor.read_bext()? ) )
|
||||||
|
} else {
|
||||||
|
Ok( None)
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
|
/// Describe the channels in this file
|
||||||
|
///
|
||||||
|
/// Returns a vector of channel descriptors, one for each channel
|
||||||
|
///
|
||||||
|
/// ```rust
|
||||||
|
/// use bwavfile::WaveReader;
|
||||||
|
/// use bwavfile::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);
|
||||||
|
/// ```
|
||||||
|
pub fn channels(&mut self) -> Result<Vec<ChannelDescriptor>, ParserError> {
|
||||||
|
|
||||||
|
let format = self.format()?;
|
||||||
|
let channel_masks : Vec<ChannelMask> = match (format.channel_count, format.extended_format) {
|
||||||
|
(1,_) => vec![ChannelMask::FrontCenter],
|
||||||
|
(2,_) => vec![ChannelMask::FrontLeft, ChannelMask::FrontRight],
|
||||||
|
(n,Some(x)) => ChannelMask::channels(x.channel_mask, n),
|
||||||
|
(n,_) => vec![ChannelMask::DirectOut; n as usize]
|
||||||
|
};
|
||||||
|
|
||||||
|
Ok( (0..format.channel_count).zip(channel_masks)
|
||||||
|
.map(|(i,m)| ChannelDescriptor { index: i, speaker:m, adm_track_audio_ids: vec![] } )
|
||||||
|
.collect() )
|
||||||
|
}
|
||||||
|
|
||||||
|
/// Read cue points.
|
||||||
|
///
|
||||||
|
/// ```rust
|
||||||
|
/// use bwavfile::WaveReader;
|
||||||
|
/// use bwavfile::Cue;
|
||||||
|
///
|
||||||
|
/// let mut f = WaveReader::open("tests/media/izotope_test.wav").unwrap();
|
||||||
|
/// let cue_points = f.cue_points().unwrap();
|
||||||
|
///
|
||||||
|
/// assert_eq!(cue_points.len(), 3);
|
||||||
|
/// assert_eq!(cue_points[0].frame, 12532);
|
||||||
|
/// assert_eq!(cue_points[0].length, None);
|
||||||
|
/// assert_eq!(cue_points[0].label, Some(String::from("Marker 1")));
|
||||||
|
/// assert_eq!(cue_points[0].note, Some(String::from("Marker 1 Comment")));
|
||||||
|
///
|
||||||
|
/// assert_eq!(cue_points[1].frame, 20997);
|
||||||
|
/// assert_eq!(cue_points[1].length, None);
|
||||||
|
/// assert_eq!(cue_points[1].label, Some(String::from("Marker 2")));
|
||||||
|
/// assert_eq!(cue_points[1].note, Some(String::from("Marker 2 Comment")));
|
||||||
|
///
|
||||||
|
/// assert_eq!(cue_points[2].frame, 26711);
|
||||||
|
/// assert_eq!(cue_points[2].length, Some(6465));
|
||||||
|
/// assert_eq!(cue_points[2].label, Some(String::from("Timed Region")));
|
||||||
|
/// assert_eq!(cue_points[2].note, Some(String::from("Region Comment")));
|
||||||
|
///
|
||||||
|
/// ```
|
||||||
|
pub fn cue_points(&mut self) -> Result<Vec<Cue>,ParserError> {
|
||||||
|
let mut cue_buffer : Vec<u8> = vec![];
|
||||||
|
let mut adtl_buffer : Vec<u8> = vec![];
|
||||||
|
|
||||||
|
let cue_read = self.read_chunk(CUE__SIG, 0, &mut cue_buffer)?;
|
||||||
|
let adtl_read = self.read_list(ADTL_SIG, &mut adtl_buffer)?;
|
||||||
|
|
||||||
|
match (cue_read, adtl_read) {
|
||||||
|
(0,_) => Ok( vec![] ),
|
||||||
|
(_,0) => Ok( Cue::collect_from(&cue_buffer, None)? ),
|
||||||
|
(_,_) => Ok( Cue::collect_from(&cue_buffer, Some(&adtl_buffer) )? )
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
/// Read iXML data.
|
||||||
|
///
|
||||||
|
/// The iXML data will be appended to `buffer`.
|
||||||
|
/// If there are no iXML metadata present in the file,
|
||||||
|
/// Ok(0) will be returned.
|
||||||
|
pub fn read_ixml(&mut self, buffer: &mut Vec<u8>) -> Result<usize, ParserError> {
|
||||||
|
self.read_chunk(IXML_SIG, 0, buffer)
|
||||||
|
}
|
||||||
|
|
||||||
|
/// Read AXML data.
|
||||||
|
///
|
||||||
|
/// The axml data will be appended to `buffer`. By convention this will
|
||||||
|
/// generally be ADM metadata.
|
||||||
|
///
|
||||||
|
/// If there are no axml metadata present in the file,
|
||||||
|
/// Ok(0) will be returned
|
||||||
|
pub fn read_axml(&mut self, buffer: &mut Vec<u8>) -> Result<usize, ParserError> {
|
||||||
|
self.read_chunk(AXML_SIG, 0, buffer)
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* The Broadcast-WAV metadata record for this file.
|
* Validate file is readable.
|
||||||
*/
|
*
|
||||||
pub fn broadcast_extension(&mut self) -> Result<Bext, ParserError> {
|
* `Ok(())` if the source meets the minimum standard of
|
||||||
self.chunk_reader(BEXT_SIG, 0)?.read_bext()
|
* readability by a permissive client:
|
||||||
|
* - `fmt` chunk and `data` chunk are present
|
||||||
|
* - `fmt` chunk appears before `data` chunk
|
||||||
|
*/
|
||||||
|
pub fn validate_readable(&mut self) -> Result<(), ParserError> {
|
||||||
|
let (fmt_pos, _) = self.get_chunk_extent_at_index(FMT__SIG, 0)?;
|
||||||
|
let (data_pos, _) = self.get_chunk_extent_at_index(DATA_SIG, 0)?;
|
||||||
|
|
||||||
|
if fmt_pos < data_pos {
|
||||||
|
Ok(())
|
||||||
|
} else {
|
||||||
|
Err( ParserError::FmtChunkAfterData)
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
|
||||||
|
|
||||||
impl<R:Read+Seek> WaveReader<R> { /* Private Implementation */
|
/// Validate minimal WAVE file.
|
||||||
|
///
|
||||||
|
/// `Ok(())` if the source is `validate_readable()` AND
|
||||||
|
///
|
||||||
|
/// - Contains _only_ a `fmt` chunk and `data` chunk, with no other chunks present
|
||||||
|
/// - `fmt` chunk is exactly 16 bytes long and begins _exactly_ at file offset 12
|
||||||
|
/// - `data` content begins _exactly_ at file offset 36
|
||||||
|
/// - is not an RF64/BW64
|
||||||
|
///
|
||||||
|
/// Some clients require a WAVE file to only contain format and data without any other
|
||||||
|
/// metadata and this function is provided to validate this condition.
|
||||||
|
///
|
||||||
|
/// ### Examples
|
||||||
|
///
|
||||||
|
/// ```
|
||||||
|
/// # use bwavfile::WaveReader;
|
||||||
|
///
|
||||||
|
/// let mut w = WaveReader::open("tests/media/ff_minimal.wav").unwrap();
|
||||||
|
/// w.validate_minimal().expect("Minimal wav did not validate not minimal!");
|
||||||
|
/// ```
|
||||||
|
///
|
||||||
|
/// ```
|
||||||
|
/// # use bwavfile::WaveReader;
|
||||||
|
///
|
||||||
|
/// let mut x = WaveReader::open("tests/media/pt_24bit_51.wav").unwrap();
|
||||||
|
/// x.validate_minimal().expect_err("Complex WAV validated minimal!");
|
||||||
|
/// ```
|
||||||
|
pub fn validate_minimal(&mut self) -> Result<(), ParserError> {
|
||||||
|
self.validate_readable()?;
|
||||||
|
|
||||||
fn chunk_reader(&mut self, signature: FourCC, at_index: u32) -> Result<RawChunkReader<R>, ParserError> {
|
let chunk_fourccs : Vec<FourCC> = Parser::make(&mut self.inner)?
|
||||||
let (start, length) = self.get_chunk_extent_at_index(signature, at_index)?;
|
.into_chunk_list()?.iter().map(|c| c.signature ).collect();
|
||||||
Ok( RawChunkReader::new(&mut self.inner, start, length) )
|
|
||||||
|
if chunk_fourccs == vec![FMT__SIG, DATA_SIG] {
|
||||||
|
Ok(()) /* FIXME: finish implementation */
|
||||||
|
} else {
|
||||||
|
Err( ParserError::NotMinimalWaveFile )
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
/// Validate Broadcast-WAVE file format
|
||||||
|
///
|
||||||
|
/// Returns `Ok(())` if `validate_readable()` and file contains a
|
||||||
|
/// Broadcast-WAV metadata record (a `bext` chunk).
|
||||||
|
///
|
||||||
|
/// ### Examples
|
||||||
|
///
|
||||||
|
/// ```
|
||||||
|
/// # use bwavfile::WaveReader;
|
||||||
|
///
|
||||||
|
/// let mut w = WaveReader::open("tests/media/ff_bwav_stereo.wav").unwrap();
|
||||||
|
/// w.validate_broadcast_wave().expect("BWAVE file did not validate BWAVE");
|
||||||
|
///
|
||||||
|
/// let mut x = WaveReader::open("tests/media/pt_24bit.wav").unwrap();
|
||||||
|
/// x.validate_broadcast_wave().expect("BWAVE file did not validate BWAVE");
|
||||||
|
///
|
||||||
|
/// let mut y = WaveReader::open("tests/media/audacity_16bit.wav").unwrap();
|
||||||
|
/// y.validate_broadcast_wave().expect_err("Plain WAV file DID validate BWAVE");
|
||||||
|
/// ```
|
||||||
|
///
|
||||||
|
pub fn validate_broadcast_wave(&mut self) -> Result<(), ParserError> {
|
||||||
|
self.validate_readable()?;
|
||||||
|
let (_, _) = self.get_chunk_extent_at_index(BEXT_SIG, 0)?;
|
||||||
|
Ok(())
|
||||||
}
|
}
|
||||||
|
|
||||||
pub fn get_chunk_extent_at_index(&mut self, fourcc: FourCC, index: u32) -> Result<(u64,u64), ParserError> {
|
///
|
||||||
let p = Parser::make(&mut self.inner)?.into_chunk_list()?;
|
/// Verify data is aligned to a block boundary.
|
||||||
|
///
|
||||||
if let Some(chunk) = p.iter().filter(|item| item.signature == fourcc).nth(index as usize) {
|
/// Returns `Ok(())` if `validate_readable()` and the start of the
|
||||||
Ok ((chunk.start, chunk.length))
|
/// `data` chunk's content begins at 0x4000.
|
||||||
|
pub fn validate_data_chunk_alignment(&mut self) -> Result<() , ParserError> {
|
||||||
|
self.validate_readable()?;
|
||||||
|
let (start, _) = self.get_chunk_extent_at_index(DATA_SIG, 0)?;
|
||||||
|
if start == 0x4000 {
|
||||||
|
Ok(())
|
||||||
} else {
|
} else {
|
||||||
Err( ParserError::ChunkMissing { signature : fourcc })
|
Err(ParserError::DataChunkNotAligned)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
/// Verify audio data can be appended immediately to this file.
|
||||||
|
///
|
||||||
|
/// Returns `Ok(())` if:
|
||||||
|
/// - `validate_readable()`
|
||||||
|
/// - there is a `JUNK` or `FLLR` immediately at the beginning of the chunk
|
||||||
|
/// list adequately large enough to be overwritten by a `ds64` (92 bytes)
|
||||||
|
/// - `data` is the final chunk
|
||||||
|
pub fn validate_prepared_for_append(&mut self) -> Result<(), ParserError> {
|
||||||
|
self.validate_readable()?;
|
||||||
|
|
||||||
|
let chunks = Parser::make(&mut self.inner)?.into_chunk_list()?;
|
||||||
|
let ds64_space_required = 92;
|
||||||
|
|
||||||
|
let eligible_filler_chunks = chunks.iter()
|
||||||
|
.take_while(|c| c.signature == JUNK_SIG || c.signature == FLLR_SIG);
|
||||||
|
|
||||||
|
let filler = eligible_filler_chunks
|
||||||
|
.enumerate()
|
||||||
|
.fold(0, |accum, (n, item)| if n == 0 { accum + item.length } else {accum + item.length + 8});
|
||||||
|
|
||||||
|
if filler < ds64_space_required {
|
||||||
|
Err(ParserError::InsufficientDS64Reservation {expected: ds64_space_required, actual: filler})
|
||||||
|
} else {
|
||||||
|
let data_pos = chunks.iter().position(|c| c.signature == DATA_SIG);
|
||||||
|
|
||||||
|
match data_pos {
|
||||||
|
Some(p) if p == chunks.len() - 1 => Ok(()),
|
||||||
|
_ => Err(ParserError::DataChunkNotPreparedForAppend)
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
impl<R:Read+Seek> WaveReader<R> {
|
||||||
|
|
||||||
|
// Private implementation
|
||||||
|
//
|
||||||
|
// As time passes thi get smore obnoxious because I haven't implemented recursive chunk
|
||||||
|
// parsing in the raw parser and I'm working around it
|
||||||
|
|
||||||
|
// fn chunk_reader(&mut self, signature: FourCC, at_index: u32) -> Result<RawChunkReader<R>, ParserError> {
|
||||||
|
// let (start, length) = self.get_chunk_extent_at_index(signature, at_index)?;
|
||||||
|
// Ok( RawChunkReader::new(&mut self.inner, start, length) )
|
||||||
|
// }
|
||||||
|
|
||||||
|
fn read_list(&mut self, ident: FourCC, buffer: &mut Vec<u8>) -> Result<usize, ParserError> {
|
||||||
|
if let Some(index) = self.get_list_form(ident)? {
|
||||||
|
self.read_chunk(LIST_SIG, index, buffer)
|
||||||
|
} else {
|
||||||
|
Ok( 0 )
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
fn read_chunk(&mut self, ident: FourCC, at: u32, mut buffer: &mut Vec<u8>) -> Result<usize, ParserError> {
|
||||||
|
|
||||||
|
match self.get_chunk_extent_at_index(ident, at) {
|
||||||
|
Ok((start, length)) => {
|
||||||
|
buffer.resize(length as usize, 0x0);
|
||||||
|
self.inner.seek(SeekFrom::Start(start))?;
|
||||||
|
self.inner.read(&mut buffer).map_err(|e| ParserError::IOError(e))
|
||||||
|
},
|
||||||
|
Err(ParserError::ChunkMissing { signature : _} ) => Ok(0),
|
||||||
|
Err( any ) => Err(any.into())
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
/// Extent of every chunk with the given fourcc
|
||||||
|
fn get_chunks_extents(&mut self, fourcc: FourCC) -> Result<Vec<(u64,u64)>, ParserError> {
|
||||||
|
let p = Parser::make(&mut self.inner)?.into_chunk_list()?;
|
||||||
|
|
||||||
|
Ok( p.iter().filter(|item| item.signature == fourcc)
|
||||||
|
.map(|item| (item.start, item.length)).collect() )
|
||||||
|
}
|
||||||
|
|
||||||
|
/// Index of first LIST for with the given FORM fourcc
|
||||||
|
fn get_list_form(&mut self, fourcc: FourCC) -> Result<Option<u32>, ParserError> {
|
||||||
|
for (n, (start, _)) in self.get_chunks_extents(LIST_SIG)?.iter().enumerate() {
|
||||||
|
self.inner.seek(SeekFrom::Start(*start as u64))?;
|
||||||
|
let this_fourcc = self.inner.read_fourcc()?;
|
||||||
|
if this_fourcc == fourcc {
|
||||||
|
return Ok( Some( n as u32 ) );
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
Ok( None )
|
||||||
|
}
|
||||||
|
|
||||||
|
fn get_chunk_extent_at_index(&mut self, fourcc: FourCC, index: u32) -> Result<(u64,u64), ParserError> {
|
||||||
|
if let Some((start, length)) = self.get_chunks_extents(fourcc)?.iter().nth(index as usize) {
|
||||||
|
Ok ((*start, *length))
|
||||||
|
} else {
|
||||||
|
Err( ParserError::ChunkMissing { signature : fourcc } )
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
#[test]
|
||||||
|
fn test_list_form() {
|
||||||
|
let mut f = WaveReader::open("tests/media/izotope_test.wav").unwrap();
|
||||||
|
let mut buf : Vec<u8> = vec![];
|
||||||
|
|
||||||
|
f.read_list(ADTL_SIG, &mut buf).unwrap();
|
||||||
|
|
||||||
|
assert_ne!(buf.len(), 0);
|
||||||
|
|
||||||
|
}
|
||||||
@@ -1,102 +1,524 @@
|
|||||||
use std::io::{Write, Seek, SeekFrom};
|
|
||||||
use std::fs::File;
|
use std::fs::File;
|
||||||
use std::io::Cursor;
|
use std::io::{Write,Seek,SeekFrom,Cursor,BufWriter};
|
||||||
|
|
||||||
use super::errors::Error;
|
use super::Error;
|
||||||
use super::chunks::{WaveFmt, Bext, WriteBWaveChunks};
|
use super::fourcc::{FourCC, WriteFourCC, RIFF_SIG, RF64_SIG, DS64_SIG,
|
||||||
use super::fourcc::{FourCC, RIFF_SIG, WAVE_SIG, FMT__SIG, JUNK_SIG, BEXT_SIG, DATA_SIG, WriteFourCC};
|
WAVE_SIG, FMT__SIG, DATA_SIG, ELM1_SIG, JUNK_SIG, BEXT_SIG,AXML_SIG,
|
||||||
|
IXML_SIG};
|
||||||
|
use super::fmt::WaveFmt;
|
||||||
|
//use super::common_format::CommonFormat;
|
||||||
|
use super::chunks::WriteBWaveChunks;
|
||||||
|
use super::bext::Bext;
|
||||||
|
|
||||||
use byteorder::LittleEndian;
|
use byteorder::LittleEndian;
|
||||||
use byteorder::WriteBytesExt;
|
use byteorder::WriteBytesExt;
|
||||||
|
|
||||||
struct WaveWriter<W> where W: Write + Seek {
|
/// Write audio frames to a `WaveWriter`.
|
||||||
inner : W
|
///
|
||||||
|
///
|
||||||
|
pub struct AudioFrameWriter<W> where W: Write + Seek {
|
||||||
|
inner : WaveChunkWriter<W>
|
||||||
}
|
}
|
||||||
|
|
||||||
impl WaveWriter<File> {
|
impl<W> AudioFrameWriter<W> where W: Write + Seek {
|
||||||
pub fn create(path : &str, format:WaveFmt, broadcast_extension: Option<Bext>) -> Result<Self,Error> {
|
|
||||||
let inner = File::create(path)?;
|
fn new(inner: WaveChunkWriter<W>) -> Self {
|
||||||
Self::make(inner, format, broadcast_extension)
|
AudioFrameWriter { inner }
|
||||||
|
}
|
||||||
|
|
||||||
|
fn write_integer_frames_to_buffer(&self, from_frames :&[i32], to_buffer : &mut [u8]) -> () {
|
||||||
|
assert!(from_frames.len() % self.inner.inner.format.channel_count as usize == 0,
|
||||||
|
"frames buffer does not contain a number of samples % channel_count == 0");
|
||||||
|
self.inner.inner.format.pack_frames(&from_frames, to_buffer);
|
||||||
|
()
|
||||||
|
}
|
||||||
|
|
||||||
|
/// Write interleaved samples in `buffer`
|
||||||
|
///
|
||||||
|
/// # Panics
|
||||||
|
///
|
||||||
|
/// This function will panic if `buffer.len()` modulo the Wave file's channel count
|
||||||
|
/// is not zero.
|
||||||
|
pub fn write_integer_frames(&mut self, buffer: &[i32]) -> Result<u64,Error> {
|
||||||
|
let mut write_buffer = self.inner.inner.format
|
||||||
|
.create_raw_buffer(buffer.len() / self.inner.inner.format.channel_count as usize);
|
||||||
|
|
||||||
|
self.write_integer_frames_to_buffer(&buffer, &mut write_buffer);
|
||||||
|
|
||||||
|
self.inner.write(&write_buffer)?;
|
||||||
|
self.inner.flush()?;
|
||||||
|
Ok(write_buffer.len() as u64 / self.inner.inner.format.channel_count as u64)
|
||||||
|
}
|
||||||
|
|
||||||
|
/// Finish writing audio frames and unwrap the inner `WaveWriter`.
|
||||||
|
///
|
||||||
|
/// This method must be called when the client has finished writing audio
|
||||||
|
/// data. This will finalize the audio data chunk.
|
||||||
|
pub fn end(self) -> Result<WaveWriter<W>, Error> {
|
||||||
|
self.inner.end()
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
impl<W:Write + Seek> WaveWriter<W> {
|
/// Write a wave data chunk.
|
||||||
|
///
|
||||||
|
/// `WaveChunkWriter` implements `Write` and as bytes are written to it,
|
||||||
|
///
|
||||||
|
/// ### Important!
|
||||||
|
///
|
||||||
|
/// When you are done writing to a chunk you must call `end()` in order to
|
||||||
|
/// finalize the chunk for storage.
|
||||||
|
pub struct WaveChunkWriter<W> where W: Write + Seek {
|
||||||
|
ident : FourCC,
|
||||||
|
inner : WaveWriter<W>,
|
||||||
|
content_start_pos : u64,
|
||||||
|
length : u64
|
||||||
|
}
|
||||||
|
|
||||||
pub fn make(inner : W, format: WaveFmt, broadcast_extension: Option<Bext>) -> Result<Self, Error> {
|
impl<W> WaveChunkWriter<W> where W: Write + Seek {
|
||||||
let mut retval = Self { inner };
|
|
||||||
retval.prepare_created(format, broadcast_extension)?;
|
fn begin(mut inner : WaveWriter<W>, ident : FourCC) -> Result<Self,Error> {
|
||||||
Ok(retval)
|
let length : u64 = 0;
|
||||||
|
inner.inner.write_fourcc(ident)?;
|
||||||
|
inner.inner.write_u32::<LittleEndian>(length as u32)?;
|
||||||
|
inner.increment_form_length(8)?;
|
||||||
|
let content_start_pos = inner.inner.seek(SeekFrom::End(0))?;
|
||||||
|
Ok( WaveChunkWriter { ident, inner , content_start_pos, length } )
|
||||||
}
|
}
|
||||||
|
|
||||||
fn prepare_created(&mut self, format : WaveFmt, broadcast_extension: Option<Bext>) -> Result<(),Error> {
|
fn end(mut self) -> Result<WaveWriter<W>, Error> {
|
||||||
self.inner.write_fourcc(RIFF_SIG)?;
|
if self.length % 2 == 1 {
|
||||||
self.inner.write_u32::<LittleEndian>(4)?;
|
self.inner.inner.seek(SeekFrom::End(0))?;
|
||||||
self.inner.write_fourcc(WAVE_SIG)?;
|
self.inner.inner.write(&[0u8])?;
|
||||||
let mut written : u64 = 4;
|
self.inner.increment_form_length(1)?;
|
||||||
|
|
||||||
let ds64_reservation = [0u8; 92];
|
|
||||||
|
|
||||||
written += self.primitive_append_chunk(JUNK_SIG, &ds64_reservation)?;
|
|
||||||
|
|
||||||
let fmt_data : Vec<u8> = {
|
|
||||||
let mut c = Cursor::new(vec![]);
|
|
||||||
c.write_wave_fmt(&format)?;
|
|
||||||
c.into_inner()
|
|
||||||
};
|
|
||||||
|
|
||||||
written += self.primitive_append_chunk(FMT__SIG, &fmt_data)?;
|
|
||||||
|
|
||||||
if let Some(bext) = broadcast_extension {
|
|
||||||
let mut b = Cursor::new(vec![]);
|
|
||||||
b.write_bext(&bext)?;
|
|
||||||
let data = b.into_inner();
|
|
||||||
written += self.primitive_append_chunk(BEXT_SIG, &data)?;
|
|
||||||
}
|
}
|
||||||
|
Ok( self.inner )
|
||||||
// show our work
|
}
|
||||||
let desired_data_alignment = 0x4000;
|
|
||||||
let data_fourcc_start = desired_data_alignment - 8;
|
|
||||||
let current_position_from_start = written + 8;
|
|
||||||
let data_pad_length = data_fourcc_start - current_position_from_start;
|
|
||||||
|
|
||||||
let data_padding = vec![0u8; data_pad_length as usize];
|
fn increment_chunk_length(&mut self, amount: u64) -> Result<(), std::io::Error> {
|
||||||
|
self.length = self.length + amount;
|
||||||
|
if !self.inner.is_rf64 {
|
||||||
|
self.inner.inner.seek(SeekFrom::Start(self.content_start_pos - 4))?;
|
||||||
|
self.inner.inner.write_u32::<LittleEndian>(self.length as u32)?;
|
||||||
|
} else {
|
||||||
|
if self.ident == DATA_SIG {
|
||||||
|
let data_chunk_64bit_field_offset = 8 + 4 + 8 + 8;
|
||||||
|
self.inner.inner.seek(SeekFrom::Start(self.content_start_pos - 4))?;
|
||||||
|
self.inner.inner.write_u32::<LittleEndian>(0xFFFF_FFFF)?;
|
||||||
|
// this only need to happen once, not every time we increment
|
||||||
|
|
||||||
written += self.primitive_append_chunk(JUNK_SIG, &data_padding)?;
|
self.inner.inner.seek(SeekFrom::Start(data_chunk_64bit_field_offset))?;
|
||||||
|
self.inner.inner.write_u64::<LittleEndian>(self.length)?;
|
||||||
self.inner.write_fourcc(DATA_SIG)?;
|
} else {
|
||||||
self.inner.write_u32::<LittleEndian>(0)?;
|
todo!("FIXME RF64 wave writing is not yet supported for chunks other than `data`")
|
||||||
|
}
|
||||||
written += 8;
|
|
||||||
self.inner.seek(SeekFrom::Start(4))?;
|
}
|
||||||
self.inner.write_u32::<LittleEndian>(written as u32)?;
|
|
||||||
|
|
||||||
Ok(())
|
Ok(())
|
||||||
}
|
}
|
||||||
|
}
|
||||||
|
|
||||||
fn primitive_append_chunk(&mut self, signature: FourCC, data: &[u8]) -> Result<u64,Error> {
|
impl<W> Write for WaveChunkWriter<W> where W: Write + Seek {
|
||||||
assert!((data.len() as u32) < u32::MAX,
|
|
||||||
"primitive_append_chunk called with a long data buffer");
|
|
||||||
|
|
||||||
self.inner.write_fourcc(signature)?;
|
fn write(&mut self, buffer: &[u8]) -> Result<usize, std::io::Error> {
|
||||||
|
self.inner.inner.seek(SeekFrom::End(0))?;
|
||||||
|
let written = self.inner.inner.write(buffer)?;
|
||||||
|
self.inner.increment_form_length(written as u64)?;
|
||||||
|
self.increment_chunk_length(written as u64)?;
|
||||||
|
|
||||||
|
Ok( written )
|
||||||
|
}
|
||||||
|
|
||||||
|
fn flush(&mut self) -> Result<(), std::io::Error> {
|
||||||
|
self.inner.inner.flush()
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
/// Wave, Broadcast-WAV and RF64/BW64 writer.
|
||||||
|
///
|
||||||
|
/// A `WaveWriter` creates a new wave file at the given path (with `create()`)
|
||||||
|
/// or into the given `Write`- and `Seek`-able inner writer.
|
||||||
|
///
|
||||||
|
/// Audio is added to the wave file by starting the audio data chunk with
|
||||||
|
/// `WaveWriter::audio_frame_writer()`. All of the functions that add chunks
|
||||||
|
/// move the WaveWriter and return it to the host when complete.
|
||||||
|
///
|
||||||
|
/// # Structure of New Wave Files
|
||||||
|
///
|
||||||
|
/// `WaveWriter` will create a Wave file with two chunks automatically: a 96
|
||||||
|
/// byte `JUNK` chunk and a standard `fmt ` chunk, which has the extended
|
||||||
|
/// length if the format your provided requires it. The first `JUNK` chunk is
|
||||||
|
/// a reservation for a `ds64` record which will be written over it if
|
||||||
|
/// the file needs to be upgraded to RF64 format.
|
||||||
|
///
|
||||||
|
/// Chunks are added to the file in the order the client adds them.
|
||||||
|
/// `audio_file_writer()` will add a `data` chunk for the audio data, and will
|
||||||
|
/// also add an `elm1` filler chunk prior to the data chunk to ensure that the
|
||||||
|
/// first byte of the data chunk's content is aligned with 0x4000.
|
||||||
|
///
|
||||||
|
/// ```
|
||||||
|
/// use bwavfile::{WaveWriter,WaveFmt};
|
||||||
|
/// # use std::io::Cursor;
|
||||||
|
///
|
||||||
|
/// // Write a three-sample wave file to a cursor
|
||||||
|
/// let mut cursor = Cursor::new(vec![0u8;0]);
|
||||||
|
/// let format = WaveFmt::new_pcm_mono(48000, 24);
|
||||||
|
/// let w = WaveWriter::new(&mut cursor, format).unwrap();
|
||||||
|
///
|
||||||
|
/// let mut frame_writer = w.audio_frame_writer().unwrap();
|
||||||
|
///
|
||||||
|
/// frame_writer.write_integer_frames(&[0i32]).unwrap();
|
||||||
|
/// frame_writer.write_integer_frames(&[0i32]).unwrap();
|
||||||
|
/// frame_writer.write_integer_frames(&[0i32]).unwrap();
|
||||||
|
/// frame_writer.end().unwrap();
|
||||||
|
/// ```
|
||||||
|
///
|
||||||
|
/// ## Resources
|
||||||
|
///
|
||||||
|
/// ### Implementation of Wave Files
|
||||||
|
/// - [Peter Kabal, McGill University](http://www-mmsp.ece.mcgill.ca/Documents/AudioFormats/WAVE/WAVE.html)
|
||||||
|
/// - [Multimedia Programming Interface and Data Specifications 1.0](http://www-mmsp.ece.mcgill.ca/Documents/AudioFormats/WAVE/Docs/riffmci.pdf)
|
||||||
|
/// (August 1991), IBM Corporation and Microsoft Corporation
|
||||||
|
///
|
||||||
|
/// ### Implementation of Broadcast Wave Files
|
||||||
|
/// - [EBU Tech 3285][ebu3285] (May 2011), "Specification of the Broadcast Wave Format (BWF)"
|
||||||
|
/// - [Supplement 1](https://tech.ebu.ch/docs/tech/tech3285s1.pdf) (July 1997): MPEG Audio
|
||||||
|
/// - [EBU Rec 68](https://tech.ebu.ch/docs/r/r068.pdf): Signal modulation and format constraints
|
||||||
|
///
|
||||||
|
/// ### Implementation of 64-bit Wave Files
|
||||||
|
/// - [ITU-R 2088][itu2088] (October 2019), "Long-form file format for the international exchange of audio programme materials with metadata"
|
||||||
|
/// - Presently in force, adopted by the EBU in [EBU Tech 3306v2][ebu3306v2] (June 2018).
|
||||||
|
/// - [EBU Tech 3306v1][ebu3306v1] (July 2009), "MBWF / RF64: An extended File Format for Audio"
|
||||||
|
/// - No longer in force, however long-established.
|
||||||
|
///
|
||||||
|
///
|
||||||
|
/// [ebu3285]: https://tech.ebu.ch/docs/tech/tech3285.pdf
|
||||||
|
/// [ebu3306v1]: https://tech.ebu.ch/docs/tech/tech3306v1_1.pdf
|
||||||
|
/// [ebu3306v2]: https://tech.ebu.ch/docs/tech/tech3306.pdf
|
||||||
|
/// [itu2088]: https://www.itu.int/dms_pubrec/itu-r/rec/bs/R-REC-BS.2088-1-201910-I!!PDF-E.pdf
|
||||||
|
/// [rfc3261]: https://tools.ietf.org/html/rfc2361
|
||||||
|
pub struct WaveWriter<W> where W: Write + Seek {
|
||||||
|
inner : W,
|
||||||
|
form_length: u64,
|
||||||
|
|
||||||
|
/// True if file is RF64
|
||||||
|
pub is_rf64: bool,
|
||||||
|
|
||||||
|
/// Format of the wave file.
|
||||||
|
pub format: WaveFmt
|
||||||
|
}
|
||||||
|
|
||||||
|
const DS64_RESERVATION_LENGTH : u32 = 96;
|
||||||
|
|
||||||
|
impl WaveWriter<BufWriter<File>> {
|
||||||
|
|
||||||
|
/// Create a new Wave file at `path`.
|
||||||
|
pub fn create(path : &str, format : WaveFmt) -> Result<Self, Error> {
|
||||||
|
let f = File::create(path)?;
|
||||||
|
let b = BufWriter::new(f);
|
||||||
|
Ok( Self::new(b, format)? )
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
impl WaveWriter<File> {
|
||||||
|
/// Creare a new Wave file with unbuffered IO at `path`
|
||||||
|
pub fn create_unbuffered(path : &str, format : WaveFmt) -> Result<Self, Error> {
|
||||||
|
let f = File::create(path)?;
|
||||||
|
Ok( Self::new(f, format)? )
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
impl<W> WaveWriter<W> where W: Write + Seek {
|
||||||
|
|
||||||
|
/// Wrap a writer in a Wave writer.
|
||||||
|
///
|
||||||
|
/// The inner writer will immediately have a RIFF WAVE file header
|
||||||
|
/// written to it along with the format descriptor (and possibly a `fact`
|
||||||
|
/// chunk if appropriate).
|
||||||
|
pub fn new(mut inner : W, format: WaveFmt) -> Result<Self, Error> {
|
||||||
|
inner.write_fourcc(RIFF_SIG)?;
|
||||||
|
inner.write_u32::<LittleEndian>(0)?;
|
||||||
|
inner.write_fourcc(WAVE_SIG)?;
|
||||||
|
|
||||||
|
let mut retval = WaveWriter { inner, form_length: 0, is_rf64: false, format};
|
||||||
|
|
||||||
|
retval.increment_form_length(4)?;
|
||||||
|
|
||||||
|
// write ds64_reservation
|
||||||
|
retval.write_junk(DS64_RESERVATION_LENGTH)?;
|
||||||
|
|
||||||
|
let mut chunk = retval.chunk(FMT__SIG)?;
|
||||||
|
chunk.write_wave_fmt(&format)?;
|
||||||
|
let retval = chunk.end()?;
|
||||||
|
|
||||||
|
Ok( retval )
|
||||||
|
}
|
||||||
|
|
||||||
|
fn write_chunk(&mut self, ident: FourCC, data : &[u8]) -> Result<(),Error> {
|
||||||
|
self.inner.seek(SeekFrom::End(0))?;
|
||||||
|
self.inner.write_fourcc(ident)?;
|
||||||
|
assert!(data.len() < u32::MAX as usize);
|
||||||
self.inner.write_u32::<LittleEndian>(data.len() as u32)?;
|
self.inner.write_u32::<LittleEndian>(data.len() as u32)?;
|
||||||
self.inner.write_all(&data)?;
|
self.inner.write(data)?;
|
||||||
let padding : u64 = data.len() as u64 % 2;
|
if data.len() % 2 == 0 {
|
||||||
if padding == 1 {
|
self.increment_form_length(8 + data.len() as u64)?;
|
||||||
self.inner.write_u8(0)?;
|
} else {
|
||||||
|
self.inner.write(&[0u8])?;
|
||||||
|
self.increment_form_length(8 + data.len() as u64 + 1)?;
|
||||||
}
|
}
|
||||||
|
Ok(())
|
||||||
|
}
|
||||||
|
|
||||||
Ok(8 + data.len() as u64 + padding)
|
/// Write Broadcast-Wave metadata to the file.
|
||||||
|
///
|
||||||
|
/// This function will write the metadata chunk immediately to the end of
|
||||||
|
/// the file; if you have already written and closed the audio data the
|
||||||
|
/// bext chunk will be positioned after it.
|
||||||
|
pub fn write_broadcast_metadata(&mut self, bext: &Bext) -> Result<(),Error> {
|
||||||
|
//FIXME Implement re-writing
|
||||||
|
let mut c = Cursor::new(vec![0u8; 0]);
|
||||||
|
c.write_bext(&bext)?;
|
||||||
|
let buf = c.into_inner();
|
||||||
|
self.write_chunk(BEXT_SIG, &buf )?;
|
||||||
|
Ok(())
|
||||||
|
}
|
||||||
|
|
||||||
|
/// Write iXML metadata
|
||||||
|
pub fn write_ixml(&mut self, ixml: &[u8]) -> Result<(),Error> {
|
||||||
|
//FIXME Implement re-writing
|
||||||
|
self.write_chunk(IXML_SIG, &ixml)
|
||||||
|
}
|
||||||
|
|
||||||
|
/// Write axml/ADM metadata
|
||||||
|
pub fn write_axml(&mut self, axml: &[u8]) -> Result<(), Error> {
|
||||||
|
//FIXME Implement re-writing
|
||||||
|
self.write_chunk(AXML_SIG, &axml)
|
||||||
|
}
|
||||||
|
|
||||||
|
/// Write a `JUNK` filler chunk
|
||||||
|
pub fn write_junk(&mut self, length: u32) -> Result<(), Error> {
|
||||||
|
let filler = vec![0u8; length as usize];
|
||||||
|
self.write_chunk(JUNK_SIG, &filler)
|
||||||
|
}
|
||||||
|
|
||||||
|
/// Create an audio frame writer, which takes possession of the callee
|
||||||
|
/// `WaveWriter`.
|
||||||
|
///
|
||||||
|
pub fn audio_frame_writer(mut self) -> Result<AudioFrameWriter<W>, Error> {
|
||||||
|
// append elm1 chunk
|
||||||
|
|
||||||
|
let framing = 0x4000;
|
||||||
|
|
||||||
|
let lip = self.inner.seek(SeekFrom::End(0))?;
|
||||||
|
let to_add = framing - (lip % framing) - 16;
|
||||||
|
let mut chunk = self.chunk(ELM1_SIG)?;
|
||||||
|
let buf = vec![0u8; to_add as usize];
|
||||||
|
chunk.write(&buf)?;
|
||||||
|
let closed = chunk.end()?;
|
||||||
|
let inner = closed.chunk(DATA_SIG)?;
|
||||||
|
Ok( AudioFrameWriter::new(inner) )
|
||||||
|
}
|
||||||
|
|
||||||
|
/// Open a wave chunk writer here
|
||||||
|
fn chunk(mut self, ident: FourCC) -> Result<WaveChunkWriter<W>,Error> {
|
||||||
|
self.inner.seek(SeekFrom::End(0))?;
|
||||||
|
WaveChunkWriter::begin(self, ident)
|
||||||
|
}
|
||||||
|
|
||||||
|
/// Upgrade this file to RF64
|
||||||
|
fn promote_to_rf64(&mut self) -> Result<(), std::io::Error> {
|
||||||
|
if !self.is_rf64 {
|
||||||
|
self.inner.seek(SeekFrom::Start(0))?;
|
||||||
|
self.inner.write_fourcc(RF64_SIG)?;
|
||||||
|
self.inner.write_u32::<LittleEndian>(0xFFFF_FFFF)?;
|
||||||
|
self.inner.seek(SeekFrom::Start(12))?;
|
||||||
|
|
||||||
|
self.inner.write_fourcc(DS64_SIG)?;
|
||||||
|
self.inner.seek(SeekFrom::Current(4))?;
|
||||||
|
self.inner.write_u64::<LittleEndian>(self.form_length)?;
|
||||||
|
self.is_rf64 = true;
|
||||||
|
}
|
||||||
|
Ok(())
|
||||||
|
}
|
||||||
|
|
||||||
|
/// Add `amount` to the RIFF/RF64 form length
|
||||||
|
fn increment_form_length(&mut self, amount: u64) -> Result<(), std::io::Error> {
|
||||||
|
self.form_length = self.form_length + amount;
|
||||||
|
if self.is_rf64 {
|
||||||
|
self.inner.seek(SeekFrom::Start(8 + 4 + 8))?;
|
||||||
|
self.inner.write_u64::<LittleEndian>(self.form_length)?;
|
||||||
|
} else if self.form_length < u32::MAX as u64 {
|
||||||
|
self.inner.seek(SeekFrom::Start(4))?;
|
||||||
|
self.inner.write_u32::<LittleEndian>(self.form_length as u32)?;
|
||||||
|
} else {
|
||||||
|
self.promote_to_rf64()?;
|
||||||
|
|
||||||
|
}
|
||||||
|
Ok(())
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
#[test]
|
#[test]
|
||||||
fn test_chunk_append() -> Result<(), Error> {
|
fn test_new() {
|
||||||
let mut test :Vec<u8> = vec![];
|
use std::io::Cursor;
|
||||||
let mut cursor = Cursor::new(test);
|
use super::fourcc::ReadFourCC;
|
||||||
let f = WaveFmt::new_pcm(48000, 16, 1);
|
use byteorder::ReadBytesExt;
|
||||||
let mut w = WaveWriter::make(cursor, f, None)?;
|
|
||||||
|
|
||||||
|
let mut cursor = Cursor::new(vec![0u8;0]);
|
||||||
|
let format = WaveFmt::new_pcm_mono(4800, 24);
|
||||||
|
WaveWriter::new(&mut cursor, format).unwrap();
|
||||||
|
|
||||||
Ok(())
|
cursor.seek(SeekFrom::Start(0)).unwrap();
|
||||||
|
|
||||||
|
assert_eq!(cursor.read_fourcc().unwrap(), RIFF_SIG);
|
||||||
|
let form_size = cursor.read_u32::<LittleEndian>().unwrap();
|
||||||
|
assert_eq!(cursor.read_fourcc().unwrap(), WAVE_SIG);
|
||||||
|
|
||||||
|
assert_eq!(cursor.read_fourcc().unwrap(), JUNK_SIG);
|
||||||
|
let junk_size = cursor.read_u32::<LittleEndian>().unwrap();
|
||||||
|
assert_eq!(junk_size,96);
|
||||||
|
cursor.seek(SeekFrom::Current(junk_size as i64)).unwrap();
|
||||||
|
|
||||||
|
assert_eq!(cursor.read_fourcc().unwrap(), FMT__SIG);
|
||||||
|
let fmt_size = cursor.read_u32::<LittleEndian>().unwrap();
|
||||||
|
assert_eq!(form_size, 4 + 8 + junk_size + 8 + fmt_size);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
#[test]
|
||||||
|
fn test_write_audio() {
|
||||||
|
use std::io::Cursor;
|
||||||
|
use super::fourcc::ReadFourCC;
|
||||||
|
use byteorder::ReadBytesExt;
|
||||||
|
|
||||||
|
let mut cursor = Cursor::new(vec![0u8;0]);
|
||||||
|
let format = WaveFmt::new_pcm_mono(48000, 24);
|
||||||
|
let w = WaveWriter::new(&mut cursor, format).unwrap();
|
||||||
|
|
||||||
|
let mut frame_writer = w.audio_frame_writer().unwrap();
|
||||||
|
|
||||||
|
frame_writer.write_integer_frames(&[0i32]).unwrap();
|
||||||
|
frame_writer.write_integer_frames(&[0i32]).unwrap();
|
||||||
|
frame_writer.write_integer_frames(&[0i32]).unwrap();
|
||||||
|
|
||||||
|
frame_writer.end().unwrap();
|
||||||
|
|
||||||
|
cursor.seek(SeekFrom::Start(0)).unwrap();
|
||||||
|
|
||||||
|
cursor.seek(SeekFrom::Start(0)).unwrap();
|
||||||
|
assert_eq!(cursor.read_fourcc().unwrap(), RIFF_SIG);
|
||||||
|
let form_size = cursor.read_u32::<LittleEndian>().unwrap();
|
||||||
|
|
||||||
|
assert_eq!(cursor.read_fourcc().unwrap(), WAVE_SIG); //4
|
||||||
|
|
||||||
|
assert_eq!(cursor.read_fourcc().unwrap(), JUNK_SIG); //4
|
||||||
|
let junk_size = cursor.read_u32::<LittleEndian>().unwrap(); //4
|
||||||
|
cursor.seek(SeekFrom::Current(junk_size as i64)).unwrap();
|
||||||
|
|
||||||
|
assert_eq!(cursor.read_fourcc().unwrap(), FMT__SIG); //4
|
||||||
|
let fmt_size = cursor.read_u32::<LittleEndian>().unwrap(); //4
|
||||||
|
cursor.seek(SeekFrom::Current(fmt_size as i64)).unwrap();
|
||||||
|
|
||||||
|
assert_eq!(cursor.read_fourcc().unwrap(), ELM1_SIG); //4
|
||||||
|
let elm1_size = cursor.read_u32::<LittleEndian>().unwrap(); //4
|
||||||
|
cursor.seek(SeekFrom::Current(elm1_size as i64)).unwrap();
|
||||||
|
|
||||||
|
assert_eq!(cursor.read_fourcc().unwrap(), DATA_SIG); //4
|
||||||
|
let data_size = cursor.read_u32::<LittleEndian>().unwrap(); //4
|
||||||
|
assert_eq!(data_size, 9);
|
||||||
|
|
||||||
|
let tell = cursor.seek(SeekFrom::Current(0)).unwrap();
|
||||||
|
assert!(tell % 0x4000 == 0);
|
||||||
|
|
||||||
|
assert_eq!(form_size, 4 + 8 + junk_size + 8 + fmt_size + 8 + elm1_size + 8 + data_size + data_size % 2)
|
||||||
|
}
|
||||||
|
|
||||||
|
#[test]
|
||||||
|
fn test_write_bext() {
|
||||||
|
use std::io::Cursor;
|
||||||
|
|
||||||
|
let mut cursor = Cursor::new(vec![0u8;0]);
|
||||||
|
let format = WaveFmt::new_pcm_mono(48000, 24);
|
||||||
|
let mut w = WaveWriter::new(&mut cursor, format).unwrap();
|
||||||
|
|
||||||
|
let bext = Bext {
|
||||||
|
description: String::from("Test description"),
|
||||||
|
originator: String::from(""),
|
||||||
|
originator_reference: String::from(""),
|
||||||
|
origination_date: String::from("2020-01-01"),
|
||||||
|
origination_time: String::from("12:34:56"),
|
||||||
|
time_reference: 0,
|
||||||
|
version: 0,
|
||||||
|
umid: None,
|
||||||
|
loudness_value: None,
|
||||||
|
loudness_range: None,
|
||||||
|
max_true_peak_level: None,
|
||||||
|
max_momentary_loudness: None,
|
||||||
|
max_short_term_loudness: None,
|
||||||
|
coding_history: String::from(""),
|
||||||
|
};
|
||||||
|
|
||||||
|
w.write_broadcast_metadata(&bext).unwrap();
|
||||||
|
|
||||||
|
let mut frame_writer = w.audio_frame_writer().unwrap();
|
||||||
|
|
||||||
|
frame_writer.write_integer_frames(&[0i32]).unwrap();
|
||||||
|
frame_writer.write_integer_frames(&[0i32]).unwrap();
|
||||||
|
frame_writer.write_integer_frames(&[0i32]).unwrap();
|
||||||
|
|
||||||
|
frame_writer.end().unwrap();
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
// NOTE! This test of RF64 writing takes several minutes to complete.
|
||||||
|
#[test]
|
||||||
|
fn test_create_rf64() {
|
||||||
|
use super::fourcc::ReadFourCC;
|
||||||
|
use byteorder::ReadBytesExt;
|
||||||
|
|
||||||
|
let mut cursor = Cursor::new(vec![0u8;0]);
|
||||||
|
let format = WaveFmt::new_pcm_stereo(48000, 24);
|
||||||
|
let w = WaveWriter::new(&mut cursor, format).unwrap();
|
||||||
|
|
||||||
|
let buflen = 16000 as u64;
|
||||||
|
|
||||||
|
let buf = vec![0i32; buflen as usize];
|
||||||
|
|
||||||
|
let four_and_a_half_hours_of_frames = 48000 * 16_200;
|
||||||
|
|
||||||
|
let mut af = w.audio_frame_writer().unwrap();
|
||||||
|
|
||||||
|
for _ in 0..(four_and_a_half_hours_of_frames * format.channel_count as u64 / buflen) {
|
||||||
|
af.write_integer_frames(&buf).unwrap();
|
||||||
|
}
|
||||||
|
af.end().unwrap();
|
||||||
|
|
||||||
|
assert!(cursor.seek(SeekFrom::End(0)).unwrap() > 0xFFFF_FFFFu64, "internal test error, Created file is not long enough to be RF64" );
|
||||||
|
let expected_data_length = four_and_a_half_hours_of_frames * format.block_alignment as u64;
|
||||||
|
|
||||||
|
cursor.seek(SeekFrom::Start(0)).unwrap();
|
||||||
|
assert_eq!(cursor.read_fourcc().unwrap(), RF64_SIG);
|
||||||
|
assert_eq!(cursor.read_u32::<LittleEndian>().unwrap(), 0xFFFF_FFFF);
|
||||||
|
assert_eq!(cursor.read_fourcc().unwrap(), WAVE_SIG);
|
||||||
|
|
||||||
|
assert_eq!(cursor.read_fourcc().unwrap(), DS64_SIG);
|
||||||
|
let ds64_size = cursor.read_u32::<LittleEndian>().unwrap();
|
||||||
|
let form_size = cursor.read_u64::<LittleEndian>().unwrap();
|
||||||
|
let data_size = cursor.read_u64::<LittleEndian>().unwrap();
|
||||||
|
assert_eq!(data_size, expected_data_length);
|
||||||
|
cursor.seek(SeekFrom::Current(ds64_size as i64 - 16)).unwrap();
|
||||||
|
|
||||||
|
assert_eq!(cursor.read_fourcc().unwrap(), FMT__SIG);
|
||||||
|
let fmt_size = cursor.read_u32::<LittleEndian>().unwrap();
|
||||||
|
cursor.seek(SeekFrom::Current((fmt_size + fmt_size % 2) as i64)).unwrap();
|
||||||
|
|
||||||
|
assert_eq!(cursor.read_fourcc().unwrap(), ELM1_SIG);
|
||||||
|
let elm1_size = cursor.read_u32::<LittleEndian>().unwrap();
|
||||||
|
let data_start = cursor.seek(SeekFrom::Current((elm1_size + elm1_size % 2) as i64)).unwrap();
|
||||||
|
|
||||||
|
assert!((data_start + 8) % 0x4000 == 0, "data content start is not aligned, starts at {}", data_start + 8);
|
||||||
|
assert_eq!(cursor.read_fourcc().unwrap(), DATA_SIG);
|
||||||
|
assert_eq!(cursor.read_u32::<LittleEndian>().unwrap(), 0xFFFF_FFFF);
|
||||||
|
cursor.seek(SeekFrom::Current(data_size as i64)).unwrap();
|
||||||
|
|
||||||
|
assert_eq!(4 + 8 + ds64_size as u64 + 8 + data_size + 8 + fmt_size as u64 + 8 + elm1_size as u64, form_size)
|
||||||
|
}
|
||||||
BIN
tests/.DS_Store
vendored
BIN
tests/.DS_Store
vendored
Binary file not shown.
Binary file not shown.
Binary file not shown.
@@ -1,7 +1,10 @@
|
|||||||
#!/bin/zsh
|
#!/bin/zsh
|
||||||
|
|
||||||
mkdir -p media
|
mkdir -p media
|
||||||
cd media
|
|
||||||
|
touch media/error.wav
|
||||||
|
|
||||||
|
tar xzf test_media.tgz
|
||||||
|
|
||||||
# create a silent bext wave file with fixture metadata and a time refernce starting at
|
# create a silent bext wave file with fixture metadata and a time refernce starting at
|
||||||
# one minute
|
# one minute
|
||||||
@@ -9,40 +12,34 @@ cd media
|
|||||||
# Keywords for bext metadata are here...
|
# Keywords for bext metadata are here...
|
||||||
# https://github.com/FFmpeg/FFmpeg/blob/17a0dfebf55f67653c29a607545a799f12bc0c01/libavformat/wavenc.c#L110
|
# https://github.com/FFmpeg/FFmpeg/blob/17a0dfebf55f67653c29a607545a799f12bc0c01/libavformat/wavenc.c#L110
|
||||||
#
|
#
|
||||||
ffmpeg -y -f lavfi -i "aevalsrc=0|0:c=stereo" -to 0.1 -ar 48000 -c:a pcm_s24le -write_bext 1 \
|
# ffmpeg -y -f lavfi -i "aevalsrc=0|0:c=stereo" -to 0.1 -ar 48000 -c:a pcm_s24le -write_bext 1 \
|
||||||
-metadata "description=FFMPEG-generated stereo WAV file with bext metadata" \
|
# -metadata "description=FFMPEG-generated stereo WAV file with bext metadata" \
|
||||||
-metadata "originator=ffmpeg" \
|
# -metadata "originator=ffmpeg" \
|
||||||
-metadata "originator_reference=STEREO_WAVE_TEST" \
|
# -metadata "originator_reference=STEREO_WAVE_TEST" \
|
||||||
-metadata "time_reference=2880000" \
|
# -metadata "time_reference=2880000" \
|
||||||
-metadata "origination_date=2020-11-18" \
|
# -metadata "origination_date=2020-11-18" \
|
||||||
-metadata "origination_time=12:00:00" \
|
# -metadata "origination_time=12:00:00" \
|
||||||
-metadata "umid=0xFF00FF00FF00FF00FF00FF00FF00FF00" \
|
# -metadata "umid=0xFF00FF00FF00FF00FF00FF00FF00FF00" \
|
||||||
-metadata "coding_history=A:PCM,48K" ff_bwav_stereo.wav
|
# -metadata "coding_history=A:PCM,48K" ff_bwav_stereo.wav
|
||||||
|
|
||||||
ffmpeg -y -f lavfi -i "aevalsrc=0|0|0|0|0|0:c=5.1" -to 0.1 -ar 48000 -c:a pcm_s24le -write_bext 1 \
|
# ffmpeg -y -f lavfi -i "aevalsrc=0|0|0|0|0|0:c=5.1" -to 0.1 -ar 48000 -c:a pcm_s24le -write_bext 1 \
|
||||||
-metadata "description=FFMPEG-generated 5.1 WAV file with bext metadata" \
|
# -metadata "description=FFMPEG-generated 5.1 WAV file with bext metadata" \
|
||||||
-metadata "originator=ffmpeg" \
|
# -metadata "originator=ffmpeg" \
|
||||||
-metadata "originator_reference=5_1_WAVE_TEST" \
|
# -metadata "originator_reference=5_1_WAVE_TEST" \
|
||||||
-metadata "time_reference=0" \
|
# -metadata "time_reference=0" \
|
||||||
-metadata "origination_date=2020-11-18" \
|
# -metadata "origination_date=2020-11-18" \
|
||||||
-metadata "origination_time=13:00:00" \
|
# -metadata "origination_time=13:00:00" \
|
||||||
-metadata "umid=0xFF00FF00FF00FF00FF00FF00FF00FF01" \
|
# -metadata "umid=0xFF00FF00FF00FF00FF00FF00FF00FF01" \
|
||||||
-metadata "coding_history=A:PCM,48K" ff_bwav_51.wav
|
# -metadata "coding_history=A:PCM,48K" ff_bwav_51.wav
|
||||||
|
|
||||||
ffmpeg -y -f lavfi -i "aevalsrc=0" -to 1 -ar 44100 ff_silence.wav
|
# ffmpeg -y -f lavfi -i "aevalsrc=0" -to 1 -ar 44100 ff_silence.wav
|
||||||
|
|
||||||
ffmpeg -y -f lavfi -i "aevalsrc=0" -to 1 -ar 44100 -fflags bitexact ff_minimal.wav
|
# ffmpeg -y -f lavfi -i "aevalsrc=0" -to 1 -ar 44100 -fflags bitexact ff_minimal.wav
|
||||||
|
|
||||||
# ffmpeg -y -f lavfi -i "aevalsrc=0|0|0|0|0|0:c=5.1" -to 0:45:00 -ar 96000 -c:a pcm_s24le -rf64 1 \
|
# ffmpeg -y -f lavfi -i "aevalsrc=0|0|0|0|0|0:c=5.1" -to 0:45:00 -ar 96000 -c:a pcm_s24le -rf64 1 \
|
||||||
# -write_bext 1 \
|
# -write_bext 1 \
|
||||||
# -metadata "description=rf64 test file" ff_longfile.wav
|
# -metadata "description=rf64 test file" ff_longfile.wav
|
||||||
|
|
||||||
ffmpeg -y -f lavfi -i "anoisesrc=r=48000:a=0.5:c=pink:s=41879" -to 0.1 -ar 48000 -c:a pcm_f32le \
|
# ffmpeg -y -f lavfi -i "anoisesrc=r=48000:a=0.5:c=pink:s=41879" -to 0.1 -ar 48000 -c:a pcm_f32le \
|
||||||
-write_bext 1 \
|
# -write_bext 1 \
|
||||||
-metadata "description=float test file" ff_float.wav
|
# -metadata "description=float test file" ff_float.wav
|
||||||
|
|
||||||
touch error.wav
|
|
||||||
|
|
||||||
unzip ../arch_pt_media.zip
|
|
||||||
unzip ../arch_audacity_media.zip
|
|
||||||
rm -rf __MACOSX
|
|
||||||
|
|||||||
836
tests/ffprobe_media_tests.json
Normal file
836
tests/ffprobe_media_tests.json
Normal file
@@ -0,0 +1,836 @@
|
|||||||
|
[
|
||||||
|
{
|
||||||
|
"streams": [
|
||||||
|
{
|
||||||
|
"index": 0,
|
||||||
|
"codec_name": "pcm_s24le",
|
||||||
|
"codec_long_name": "PCM signed 24-bit little-endian",
|
||||||
|
"codec_type": "audio",
|
||||||
|
"codec_time_base": "1/48000",
|
||||||
|
"codec_tag_string": "[1][0][0][0]",
|
||||||
|
"codec_tag": "0x0001",
|
||||||
|
"sample_fmt": "s32",
|
||||||
|
"sample_rate": "48000",
|
||||||
|
"channels": 2,
|
||||||
|
"channel_layout": "stereo",
|
||||||
|
"bits_per_sample": 24,
|
||||||
|
"r_frame_rate": "0/0",
|
||||||
|
"avg_frame_rate": "0/0",
|
||||||
|
"time_base": "1/48000",
|
||||||
|
"duration_ts": 4800,
|
||||||
|
"duration": "0.100000",
|
||||||
|
"bit_rate": "2304000",
|
||||||
|
"bits_per_raw_sample": "24",
|
||||||
|
"disposition": {
|
||||||
|
"default": 0,
|
||||||
|
"dub": 0,
|
||||||
|
"original": 0,
|
||||||
|
"comment": 0,
|
||||||
|
"lyrics": 0,
|
||||||
|
"karaoke": 0,
|
||||||
|
"forced": 0,
|
||||||
|
"hearing_impaired": 0,
|
||||||
|
"visual_impaired": 0,
|
||||||
|
"clean_effects": 0,
|
||||||
|
"attached_pic": 0,
|
||||||
|
"timed_thumbnails": 0
|
||||||
|
}
|
||||||
|
}
|
||||||
|
],
|
||||||
|
"format": {
|
||||||
|
"filename": "tests/media/ff_pink.wav",
|
||||||
|
"nb_streams": 1,
|
||||||
|
"nb_programs": 0,
|
||||||
|
"format_name": "wav",
|
||||||
|
"format_long_name": "WAV / WAVE (Waveform Audio)",
|
||||||
|
"duration": "0.100000",
|
||||||
|
"size": "28902",
|
||||||
|
"bit_rate": "2312160",
|
||||||
|
"probe_score": 99,
|
||||||
|
"tags": {
|
||||||
|
"encoder": "Lavf58.45.100"
|
||||||
|
}
|
||||||
|
}
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"streams": [
|
||||||
|
{
|
||||||
|
"index": 0,
|
||||||
|
"codec_name": "pcm_f32le",
|
||||||
|
"codec_long_name": "PCM 32-bit floating point little-endian",
|
||||||
|
"codec_type": "audio",
|
||||||
|
"codec_time_base": "1/48000",
|
||||||
|
"codec_tag_string": "[3][0][0][0]",
|
||||||
|
"codec_tag": "0x0003",
|
||||||
|
"sample_fmt": "flt",
|
||||||
|
"sample_rate": "48000",
|
||||||
|
"channels": 1,
|
||||||
|
"bits_per_sample": 32,
|
||||||
|
"r_frame_rate": "0/0",
|
||||||
|
"avg_frame_rate": "0/0",
|
||||||
|
"time_base": "1/48000",
|
||||||
|
"duration_ts": 24000,
|
||||||
|
"duration": "0.500000",
|
||||||
|
"bit_rate": "1536000",
|
||||||
|
"disposition": {
|
||||||
|
"default": 0,
|
||||||
|
"dub": 0,
|
||||||
|
"original": 0,
|
||||||
|
"comment": 0,
|
||||||
|
"lyrics": 0,
|
||||||
|
"karaoke": 0,
|
||||||
|
"forced": 0,
|
||||||
|
"hearing_impaired": 0,
|
||||||
|
"visual_impaired": 0,
|
||||||
|
"clean_effects": 0,
|
||||||
|
"attached_pic": 0,
|
||||||
|
"timed_thumbnails": 0
|
||||||
|
}
|
||||||
|
}
|
||||||
|
],
|
||||||
|
"format": {
|
||||||
|
"filename": "tests/media/pt_float.wav",
|
||||||
|
"nb_streams": 1,
|
||||||
|
"nb_programs": 0,
|
||||||
|
"format_name": "wav",
|
||||||
|
"format_long_name": "WAV / WAVE (Waveform Audio)",
|
||||||
|
"duration": "0.500000",
|
||||||
|
"size": "113344",
|
||||||
|
"bit_rate": "1813504",
|
||||||
|
"probe_score": 99,
|
||||||
|
"tags": {
|
||||||
|
"encoded_by": "Pro Tools",
|
||||||
|
"originator_reference": "aaijNFpjYOIk",
|
||||||
|
"date": "2020-11-18",
|
||||||
|
"creation_time": "19:57:21",
|
||||||
|
"time_reference": "1036416000",
|
||||||
|
"umid": "0x060A2B340101010501010F10130000002A3224F7E7248000F0B04F71BFE13E00"
|
||||||
|
}
|
||||||
|
}
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"streams": [
|
||||||
|
{
|
||||||
|
"index": 0,
|
||||||
|
"codec_name": "pcm_s16le",
|
||||||
|
"codec_long_name": "PCM signed 16-bit little-endian",
|
||||||
|
"codec_type": "audio",
|
||||||
|
"codec_time_base": "1/44100",
|
||||||
|
"codec_tag_string": "[1][0][0][0]",
|
||||||
|
"codec_tag": "0x0001",
|
||||||
|
"sample_fmt": "s16",
|
||||||
|
"sample_rate": "44100",
|
||||||
|
"channels": 1,
|
||||||
|
"bits_per_sample": 16,
|
||||||
|
"r_frame_rate": "0/0",
|
||||||
|
"avg_frame_rate": "0/0",
|
||||||
|
"time_base": "1/44100",
|
||||||
|
"duration_ts": 4418,
|
||||||
|
"duration": "0.100181",
|
||||||
|
"bit_rate": "705600",
|
||||||
|
"disposition": {
|
||||||
|
"default": 0,
|
||||||
|
"dub": 0,
|
||||||
|
"original": 0,
|
||||||
|
"comment": 0,
|
||||||
|
"lyrics": 0,
|
||||||
|
"karaoke": 0,
|
||||||
|
"forced": 0,
|
||||||
|
"hearing_impaired": 0,
|
||||||
|
"visual_impaired": 0,
|
||||||
|
"clean_effects": 0,
|
||||||
|
"attached_pic": 0,
|
||||||
|
"timed_thumbnails": 0
|
||||||
|
}
|
||||||
|
}
|
||||||
|
],
|
||||||
|
"format": {
|
||||||
|
"filename": "tests/media/audacity_16bit.wav",
|
||||||
|
"nb_streams": 1,
|
||||||
|
"nb_programs": 0,
|
||||||
|
"format_name": "wav",
|
||||||
|
"format_long_name": "WAV / WAVE (Waveform Audio)",
|
||||||
|
"duration": "0.100181",
|
||||||
|
"size": "9046",
|
||||||
|
"bit_rate": "722372",
|
||||||
|
"probe_score": 99,
|
||||||
|
"tags": {
|
||||||
|
"title": "Pink Noise 16bit",
|
||||||
|
"album": "Test Media",
|
||||||
|
"artist": "Jamie Hardt"
|
||||||
|
}
|
||||||
|
}
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"streams": [
|
||||||
|
{
|
||||||
|
"index": 0,
|
||||||
|
"codec_name": "pcm_s24le",
|
||||||
|
"codec_long_name": "PCM signed 24-bit little-endian",
|
||||||
|
"codec_type": "audio",
|
||||||
|
"codec_time_base": "1/48000",
|
||||||
|
"codec_tag_string": "[1][0][0][0]",
|
||||||
|
"codec_tag": "0x0001",
|
||||||
|
"sample_fmt": "s32",
|
||||||
|
"sample_rate": "48000",
|
||||||
|
"channels": 6,
|
||||||
|
"channel_layout": "5.1",
|
||||||
|
"bits_per_sample": 24,
|
||||||
|
"r_frame_rate": "0/0",
|
||||||
|
"avg_frame_rate": "0/0",
|
||||||
|
"time_base": "1/48000",
|
||||||
|
"duration_ts": 24000,
|
||||||
|
"duration": "0.500000",
|
||||||
|
"bit_rate": "6912000",
|
||||||
|
"bits_per_raw_sample": "24",
|
||||||
|
"disposition": {
|
||||||
|
"default": 0,
|
||||||
|
"dub": 0,
|
||||||
|
"original": 0,
|
||||||
|
"comment": 0,
|
||||||
|
"lyrics": 0,
|
||||||
|
"karaoke": 0,
|
||||||
|
"forced": 0,
|
||||||
|
"hearing_impaired": 0,
|
||||||
|
"visual_impaired": 0,
|
||||||
|
"clean_effects": 0,
|
||||||
|
"attached_pic": 0,
|
||||||
|
"timed_thumbnails": 0
|
||||||
|
}
|
||||||
|
}
|
||||||
|
],
|
||||||
|
"format": {
|
||||||
|
"filename": "tests/media/pt_24bit_51.wav",
|
||||||
|
"nb_streams": 1,
|
||||||
|
"nb_programs": 0,
|
||||||
|
"format_name": "wav",
|
||||||
|
"format_long_name": "WAV / WAVE (Waveform Audio)",
|
||||||
|
"duration": "0.500000",
|
||||||
|
"size": "452910",
|
||||||
|
"bit_rate": "7246560",
|
||||||
|
"probe_score": 99,
|
||||||
|
"tags": {
|
||||||
|
"encoded_by": "Pro Tools",
|
||||||
|
"originator_reference": "aaijNdlHDPIk",
|
||||||
|
"date": "2020-11-18",
|
||||||
|
"creation_time": "19:58:18",
|
||||||
|
"time_reference": "1036416000",
|
||||||
|
"umid": "0x060A2B340101010501010F10130000002A5D84B0E724800060654F71BFE13E00"
|
||||||
|
}
|
||||||
|
}
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"streams": [
|
||||||
|
{
|
||||||
|
"index": 0,
|
||||||
|
"codec_name": "pcm_f32le",
|
||||||
|
"codec_long_name": "PCM 32-bit floating point little-endian",
|
||||||
|
"codec_type": "audio",
|
||||||
|
"codec_time_base": "1/48000",
|
||||||
|
"codec_tag_string": "[3][0][0][0]",
|
||||||
|
"codec_tag": "0x0003",
|
||||||
|
"sample_fmt": "flt",
|
||||||
|
"sample_rate": "48000",
|
||||||
|
"channels": 2,
|
||||||
|
"bits_per_sample": 32,
|
||||||
|
"r_frame_rate": "0/0",
|
||||||
|
"avg_frame_rate": "0/0",
|
||||||
|
"time_base": "1/48000",
|
||||||
|
"duration_ts": 24000,
|
||||||
|
"duration": "0.500000",
|
||||||
|
"bit_rate": "3072000",
|
||||||
|
"disposition": {
|
||||||
|
"default": 0,
|
||||||
|
"dub": 0,
|
||||||
|
"original": 0,
|
||||||
|
"comment": 0,
|
||||||
|
"lyrics": 0,
|
||||||
|
"karaoke": 0,
|
||||||
|
"forced": 0,
|
||||||
|
"hearing_impaired": 0,
|
||||||
|
"visual_impaired": 0,
|
||||||
|
"clean_effects": 0,
|
||||||
|
"attached_pic": 0,
|
||||||
|
"timed_thumbnails": 0
|
||||||
|
}
|
||||||
|
}
|
||||||
|
],
|
||||||
|
"format": {
|
||||||
|
"filename": "tests/media/pt_float_stereo.wav",
|
||||||
|
"nb_streams": 1,
|
||||||
|
"nb_programs": 0,
|
||||||
|
"format_name": "wav",
|
||||||
|
"format_long_name": "WAV / WAVE (Waveform Audio)",
|
||||||
|
"duration": "0.500000",
|
||||||
|
"size": "210058",
|
||||||
|
"bit_rate": "3360928",
|
||||||
|
"probe_score": 99,
|
||||||
|
"tags": {
|
||||||
|
"encoded_by": "Pro Tools",
|
||||||
|
"originator_reference": "aaijN!1TjQIk",
|
||||||
|
"date": "2020-11-18",
|
||||||
|
"creation_time": "19:59:16",
|
||||||
|
"time_reference": "1036416000",
|
||||||
|
"umid": "0x060A2B340101010501010F10130000002A89B75FE724800012164F71BFE13E00"
|
||||||
|
}
|
||||||
|
}
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"streams": [
|
||||||
|
{
|
||||||
|
"index": 0,
|
||||||
|
"codec_name": "pcm_s24le",
|
||||||
|
"codec_long_name": "PCM signed 24-bit little-endian",
|
||||||
|
"codec_type": "audio",
|
||||||
|
"codec_time_base": "1/48000",
|
||||||
|
"codec_tag_string": "[1][0][0][0]",
|
||||||
|
"codec_tag": "0x0001",
|
||||||
|
"sample_fmt": "s32",
|
||||||
|
"sample_rate": "48000",
|
||||||
|
"channels": 2,
|
||||||
|
"bits_per_sample": 24,
|
||||||
|
"r_frame_rate": "0/0",
|
||||||
|
"avg_frame_rate": "0/0",
|
||||||
|
"time_base": "1/48000",
|
||||||
|
"duration_ts": 24000,
|
||||||
|
"duration": "0.500000",
|
||||||
|
"bit_rate": "2304000",
|
||||||
|
"bits_per_raw_sample": "24",
|
||||||
|
"disposition": {
|
||||||
|
"default": 0,
|
||||||
|
"dub": 0,
|
||||||
|
"original": 0,
|
||||||
|
"comment": 0,
|
||||||
|
"lyrics": 0,
|
||||||
|
"karaoke": 0,
|
||||||
|
"forced": 0,
|
||||||
|
"hearing_impaired": 0,
|
||||||
|
"visual_impaired": 0,
|
||||||
|
"clean_effects": 0,
|
||||||
|
"attached_pic": 0,
|
||||||
|
"timed_thumbnails": 0
|
||||||
|
}
|
||||||
|
}
|
||||||
|
],
|
||||||
|
"format": {
|
||||||
|
"filename": "tests/media/pt_24bit_stereo.wav",
|
||||||
|
"nb_streams": 1,
|
||||||
|
"nb_programs": 0,
|
||||||
|
"format_name": "wav",
|
||||||
|
"format_long_name": "WAV / WAVE (Waveform Audio)",
|
||||||
|
"duration": "0.500000",
|
||||||
|
"size": "162058",
|
||||||
|
"bit_rate": "2592928",
|
||||||
|
"probe_score": 99,
|
||||||
|
"tags": {
|
||||||
|
"encoded_by": "Pro Tools",
|
||||||
|
"originator_reference": "aaijNF9aoPIk",
|
||||||
|
"date": "2020-11-18",
|
||||||
|
"creation_time": "19:57:57",
|
||||||
|
"time_reference": "1036416000",
|
||||||
|
"umid": "0x060A2B340101010501010F10130000002A4E03D7E724800059E44F71BFE13E00"
|
||||||
|
}
|
||||||
|
}
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"streams": [
|
||||||
|
{
|
||||||
|
"index": 0,
|
||||||
|
"codec_name": "pcm_f32le",
|
||||||
|
"codec_long_name": "PCM 32-bit floating point little-endian",
|
||||||
|
"codec_type": "audio",
|
||||||
|
"codec_time_base": "1/48000",
|
||||||
|
"codec_tag_string": "[3][0][0][0]",
|
||||||
|
"codec_tag": "0x0003",
|
||||||
|
"sample_fmt": "flt",
|
||||||
|
"sample_rate": "48000",
|
||||||
|
"channels": 1,
|
||||||
|
"channel_layout": "mono",
|
||||||
|
"bits_per_sample": 32,
|
||||||
|
"r_frame_rate": "0/0",
|
||||||
|
"avg_frame_rate": "0/0",
|
||||||
|
"time_base": "1/48000",
|
||||||
|
"duration_ts": 4800,
|
||||||
|
"duration": "0.100000",
|
||||||
|
"bit_rate": "1536000",
|
||||||
|
"disposition": {
|
||||||
|
"default": 0,
|
||||||
|
"dub": 0,
|
||||||
|
"original": 0,
|
||||||
|
"comment": 0,
|
||||||
|
"lyrics": 0,
|
||||||
|
"karaoke": 0,
|
||||||
|
"forced": 0,
|
||||||
|
"hearing_impaired": 0,
|
||||||
|
"visual_impaired": 0,
|
||||||
|
"clean_effects": 0,
|
||||||
|
"attached_pic": 0,
|
||||||
|
"timed_thumbnails": 0
|
||||||
|
}
|
||||||
|
}
|
||||||
|
],
|
||||||
|
"format": {
|
||||||
|
"filename": "tests/media/ff_float.wav",
|
||||||
|
"nb_streams": 1,
|
||||||
|
"nb_programs": 0,
|
||||||
|
"format_name": "wav",
|
||||||
|
"format_long_name": "WAV / WAVE (Waveform Audio)",
|
||||||
|
"duration": "0.100000",
|
||||||
|
"size": "19924",
|
||||||
|
"bit_rate": "1593920",
|
||||||
|
"probe_score": 99,
|
||||||
|
"tags": {
|
||||||
|
"comment": "float test file",
|
||||||
|
"time_reference": "0",
|
||||||
|
"encoder": "Lavf58.29.100"
|
||||||
|
}
|
||||||
|
}
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"streams": [
|
||||||
|
{
|
||||||
|
"index": 0,
|
||||||
|
"codec_name": "pcm_s24le",
|
||||||
|
"codec_long_name": "PCM signed 24-bit little-endian",
|
||||||
|
"codec_type": "audio",
|
||||||
|
"codec_time_base": "1/48000",
|
||||||
|
"codec_tag_string": "[1][0][0][0]",
|
||||||
|
"codec_tag": "0x0001",
|
||||||
|
"sample_fmt": "s32",
|
||||||
|
"sample_rate": "48000",
|
||||||
|
"channels": 2,
|
||||||
|
"channel_layout": "stereo",
|
||||||
|
"bits_per_sample": 24,
|
||||||
|
"r_frame_rate": "0/0",
|
||||||
|
"avg_frame_rate": "0/0",
|
||||||
|
"time_base": "1/48000",
|
||||||
|
"duration_ts": 4800,
|
||||||
|
"duration": "0.100000",
|
||||||
|
"bit_rate": "2304000",
|
||||||
|
"bits_per_raw_sample": "24",
|
||||||
|
"disposition": {
|
||||||
|
"default": 0,
|
||||||
|
"dub": 0,
|
||||||
|
"original": 0,
|
||||||
|
"comment": 0,
|
||||||
|
"lyrics": 0,
|
||||||
|
"karaoke": 0,
|
||||||
|
"forced": 0,
|
||||||
|
"hearing_impaired": 0,
|
||||||
|
"visual_impaired": 0,
|
||||||
|
"clean_effects": 0,
|
||||||
|
"attached_pic": 0,
|
||||||
|
"timed_thumbnails": 0
|
||||||
|
}
|
||||||
|
}
|
||||||
|
],
|
||||||
|
"format": {
|
||||||
|
"filename": "tests/media/ff_bwav_stereo.wav",
|
||||||
|
"nb_streams": 1,
|
||||||
|
"nb_programs": 0,
|
||||||
|
"format_name": "wav",
|
||||||
|
"format_long_name": "WAV / WAVE (Waveform Audio)",
|
||||||
|
"duration": "0.100000",
|
||||||
|
"size": "29522",
|
||||||
|
"bit_rate": "2361760",
|
||||||
|
"probe_score": 99,
|
||||||
|
"tags": {
|
||||||
|
"comment": "FFMPEG-generated stereo WAV file with bext metadata",
|
||||||
|
"encoded_by": "ffmpeg",
|
||||||
|
"originator_reference": "STEREO_WAVE_TEST",
|
||||||
|
"date": "2020-11-18",
|
||||||
|
"creation_time": "12:00:00",
|
||||||
|
"time_reference": "2880000",
|
||||||
|
"umid": "0x7FFFFFFFFFFFFFFF7FFFFFFFFFFFFFFF00000000000000000000000000000000",
|
||||||
|
"coding_history": "A:PCM,48K",
|
||||||
|
"encoder": "Lavf58.29.100"
|
||||||
|
}
|
||||||
|
}
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"streams": [
|
||||||
|
{
|
||||||
|
"index": 0,
|
||||||
|
"codec_name": "pcm_s24le",
|
||||||
|
"codec_long_name": "PCM signed 24-bit little-endian",
|
||||||
|
"codec_type": "audio",
|
||||||
|
"codec_time_base": "1/48000",
|
||||||
|
"codec_tag_string": "[1][0][0][0]",
|
||||||
|
"codec_tag": "0x0001",
|
||||||
|
"sample_fmt": "s32",
|
||||||
|
"sample_rate": "48000",
|
||||||
|
"channels": 1,
|
||||||
|
"bits_per_sample": 24,
|
||||||
|
"r_frame_rate": "0/0",
|
||||||
|
"avg_frame_rate": "0/0",
|
||||||
|
"time_base": "1/48000",
|
||||||
|
"duration_ts": 24000,
|
||||||
|
"duration": "0.500000",
|
||||||
|
"bit_rate": "1152000",
|
||||||
|
"bits_per_raw_sample": "24",
|
||||||
|
"disposition": {
|
||||||
|
"default": 0,
|
||||||
|
"dub": 0,
|
||||||
|
"original": 0,
|
||||||
|
"comment": 0,
|
||||||
|
"lyrics": 0,
|
||||||
|
"karaoke": 0,
|
||||||
|
"forced": 0,
|
||||||
|
"hearing_impaired": 0,
|
||||||
|
"visual_impaired": 0,
|
||||||
|
"clean_effects": 0,
|
||||||
|
"attached_pic": 0,
|
||||||
|
"timed_thumbnails": 0
|
||||||
|
}
|
||||||
|
}
|
||||||
|
],
|
||||||
|
"format": {
|
||||||
|
"filename": "tests/media/pt_24bit.wav",
|
||||||
|
"nb_streams": 1,
|
||||||
|
"nb_programs": 0,
|
||||||
|
"format_name": "wav",
|
||||||
|
"format_long_name": "WAV / WAVE (Waveform Audio)",
|
||||||
|
"duration": "0.500000",
|
||||||
|
"size": "89344",
|
||||||
|
"bit_rate": "1429504",
|
||||||
|
"probe_score": 99,
|
||||||
|
"tags": {
|
||||||
|
"encoded_by": "Pro Tools",
|
||||||
|
"originator_reference": "aaijNtnVOOIk",
|
||||||
|
"date": "2020-11-18",
|
||||||
|
"creation_time": "19:57:08",
|
||||||
|
"time_reference": "1036416000",
|
||||||
|
"umid": "0x060A2B340101010501010F10130000002A28BCD4E72480004F4A4F71BFE13E00"
|
||||||
|
}
|
||||||
|
}
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"streams": [
|
||||||
|
{
|
||||||
|
"index": 0,
|
||||||
|
"codec_name": "pcm_s24le",
|
||||||
|
"codec_long_name": "PCM signed 24-bit little-endian",
|
||||||
|
"codec_type": "audio",
|
||||||
|
"codec_time_base": "1/48000",
|
||||||
|
"codec_tag_string": "[1][0][0][0]",
|
||||||
|
"codec_tag": "0x0001",
|
||||||
|
"sample_fmt": "s32",
|
||||||
|
"sample_rate": "48000",
|
||||||
|
"channels": 6,
|
||||||
|
"channel_layout": "5.1",
|
||||||
|
"bits_per_sample": 24,
|
||||||
|
"r_frame_rate": "0/0",
|
||||||
|
"avg_frame_rate": "0/0",
|
||||||
|
"time_base": "1/48000",
|
||||||
|
"duration_ts": 4800,
|
||||||
|
"duration": "0.100000",
|
||||||
|
"bit_rate": "6912000",
|
||||||
|
"bits_per_raw_sample": "24",
|
||||||
|
"disposition": {
|
||||||
|
"default": 0,
|
||||||
|
"dub": 0,
|
||||||
|
"original": 0,
|
||||||
|
"comment": 0,
|
||||||
|
"lyrics": 0,
|
||||||
|
"karaoke": 0,
|
||||||
|
"forced": 0,
|
||||||
|
"hearing_impaired": 0,
|
||||||
|
"visual_impaired": 0,
|
||||||
|
"clean_effects": 0,
|
||||||
|
"attached_pic": 0,
|
||||||
|
"timed_thumbnails": 0
|
||||||
|
}
|
||||||
|
}
|
||||||
|
],
|
||||||
|
"format": {
|
||||||
|
"filename": "tests/media/ff_bwav_51.wav",
|
||||||
|
"nb_streams": 1,
|
||||||
|
"nb_programs": 0,
|
||||||
|
"format_name": "wav",
|
||||||
|
"format_long_name": "WAV / WAVE (Waveform Audio)",
|
||||||
|
"duration": "0.100000",
|
||||||
|
"size": "87122",
|
||||||
|
"bit_rate": "6969760",
|
||||||
|
"probe_score": 99,
|
||||||
|
"tags": {
|
||||||
|
"comment": "FFMPEG-generated 5.1 WAV file with bext metadata",
|
||||||
|
"encoded_by": "ffmpeg",
|
||||||
|
"originator_reference": "5_1_WAVE_TEST",
|
||||||
|
"date": "2020-11-18",
|
||||||
|
"creation_time": "13:00:00",
|
||||||
|
"time_reference": "0",
|
||||||
|
"umid": "0x7FFFFFFFFFFFFFFF7FFFFFFFFFFFFFFF00000000000000000000000000000000",
|
||||||
|
"coding_history": "A:PCM,48K",
|
||||||
|
"encoder": "Lavf58.29.100"
|
||||||
|
}
|
||||||
|
}
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"streams": [
|
||||||
|
{
|
||||||
|
"index": 0,
|
||||||
|
"codec_name": "pcm_s24le",
|
||||||
|
"codec_long_name": "PCM signed 24-bit little-endian",
|
||||||
|
"codec_type": "audio",
|
||||||
|
"codec_time_base": "1/96000",
|
||||||
|
"codec_tag_string": "[1][0][0][0]",
|
||||||
|
"codec_tag": "0x0001",
|
||||||
|
"sample_fmt": "s32",
|
||||||
|
"sample_rate": "96000",
|
||||||
|
"channels": 6,
|
||||||
|
"channel_layout": "5.1",
|
||||||
|
"bits_per_sample": 24,
|
||||||
|
"r_frame_rate": "0/0",
|
||||||
|
"avg_frame_rate": "0/0",
|
||||||
|
"time_base": "1/96000",
|
||||||
|
"duration_ts": 259200000,
|
||||||
|
"duration": "2700.000000",
|
||||||
|
"bit_rate": "13824000",
|
||||||
|
"bits_per_raw_sample": "24",
|
||||||
|
"disposition": {
|
||||||
|
"default": 0,
|
||||||
|
"dub": 0,
|
||||||
|
"original": 0,
|
||||||
|
"comment": 0,
|
||||||
|
"lyrics": 0,
|
||||||
|
"karaoke": 0,
|
||||||
|
"forced": 0,
|
||||||
|
"hearing_impaired": 0,
|
||||||
|
"visual_impaired": 0,
|
||||||
|
"clean_effects": 0,
|
||||||
|
"attached_pic": 0,
|
||||||
|
"timed_thumbnails": 0
|
||||||
|
}
|
||||||
|
}
|
||||||
|
],
|
||||||
|
"format": {
|
||||||
|
"filename": "tests/media/ff_longfile.wav",
|
||||||
|
"nb_streams": 1,
|
||||||
|
"nb_programs": 0,
|
||||||
|
"format_name": "wav",
|
||||||
|
"format_long_name": "WAV / WAVE (Waveform Audio)",
|
||||||
|
"duration": "2700.000000",
|
||||||
|
"size": "4665600748",
|
||||||
|
"bit_rate": "13824002",
|
||||||
|
"probe_score": 100,
|
||||||
|
"tags": {
|
||||||
|
"comment": "rf64 test file",
|
||||||
|
"time_reference": "0",
|
||||||
|
"encoder": "Lavf58.29.100"
|
||||||
|
}
|
||||||
|
}
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"streams": [
|
||||||
|
{
|
||||||
|
"index": 0,
|
||||||
|
"codec_name": "pcm_f32le",
|
||||||
|
"codec_long_name": "PCM 32-bit floating point little-endian",
|
||||||
|
"codec_type": "audio",
|
||||||
|
"codec_time_base": "1/48000",
|
||||||
|
"codec_tag_string": "[3][0][0][0]",
|
||||||
|
"codec_tag": "0x0003",
|
||||||
|
"sample_fmt": "flt",
|
||||||
|
"sample_rate": "48000",
|
||||||
|
"channels": 6,
|
||||||
|
"channel_layout": "5.1",
|
||||||
|
"bits_per_sample": 32,
|
||||||
|
"r_frame_rate": "0/0",
|
||||||
|
"avg_frame_rate": "0/0",
|
||||||
|
"time_base": "1/48000",
|
||||||
|
"duration_ts": 24000,
|
||||||
|
"duration": "0.500000",
|
||||||
|
"bit_rate": "9216000",
|
||||||
|
"disposition": {
|
||||||
|
"default": 0,
|
||||||
|
"dub": 0,
|
||||||
|
"original": 0,
|
||||||
|
"comment": 0,
|
||||||
|
"lyrics": 0,
|
||||||
|
"karaoke": 0,
|
||||||
|
"forced": 0,
|
||||||
|
"hearing_impaired": 0,
|
||||||
|
"visual_impaired": 0,
|
||||||
|
"clean_effects": 0,
|
||||||
|
"attached_pic": 0,
|
||||||
|
"timed_thumbnails": 0
|
||||||
|
}
|
||||||
|
}
|
||||||
|
],
|
||||||
|
"format": {
|
||||||
|
"filename": "tests/media/pt_float_51.wav",
|
||||||
|
"nb_streams": 1,
|
||||||
|
"nb_programs": 0,
|
||||||
|
"format_name": "wav",
|
||||||
|
"format_long_name": "WAV / WAVE (Waveform Audio)",
|
||||||
|
"duration": "0.500000",
|
||||||
|
"size": "596910",
|
||||||
|
"bit_rate": "9550560",
|
||||||
|
"probe_score": 99,
|
||||||
|
"tags": {
|
||||||
|
"encoded_by": "Pro Tools",
|
||||||
|
"originator_reference": "aaijNViK8PIk",
|
||||||
|
"date": "2020-11-18",
|
||||||
|
"creation_time": "19:58:59",
|
||||||
|
"time_reference": "1036416000",
|
||||||
|
"umid": "0x060A2B340101010501010F10130000002A7C908BE724800047E94F71BFE13E00"
|
||||||
|
}
|
||||||
|
}
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"streams": [
|
||||||
|
{
|
||||||
|
"index": 0,
|
||||||
|
"codec_name": "pcm_s16le",
|
||||||
|
"codec_long_name": "PCM signed 16-bit little-endian",
|
||||||
|
"codec_type": "audio",
|
||||||
|
"codec_time_base": "1/44100",
|
||||||
|
"codec_tag_string": "[1][0][0][0]",
|
||||||
|
"codec_tag": "0x0001",
|
||||||
|
"sample_fmt": "s16",
|
||||||
|
"sample_rate": "44100",
|
||||||
|
"channels": 1,
|
||||||
|
"bits_per_sample": 16,
|
||||||
|
"r_frame_rate": "0/0",
|
||||||
|
"avg_frame_rate": "0/0",
|
||||||
|
"time_base": "1/44100",
|
||||||
|
"duration_ts": 44100,
|
||||||
|
"duration": "1.000000",
|
||||||
|
"bit_rate": "705600",
|
||||||
|
"disposition": {
|
||||||
|
"default": 0,
|
||||||
|
"dub": 0,
|
||||||
|
"original": 0,
|
||||||
|
"comment": 0,
|
||||||
|
"lyrics": 0,
|
||||||
|
"karaoke": 0,
|
||||||
|
"forced": 0,
|
||||||
|
"hearing_impaired": 0,
|
||||||
|
"visual_impaired": 0,
|
||||||
|
"clean_effects": 0,
|
||||||
|
"attached_pic": 0,
|
||||||
|
"timed_thumbnails": 0
|
||||||
|
}
|
||||||
|
}
|
||||||
|
],
|
||||||
|
"format": {
|
||||||
|
"filename": "tests/media/ff_minimal.wav",
|
||||||
|
"nb_streams": 1,
|
||||||
|
"nb_programs": 0,
|
||||||
|
"format_name": "wav",
|
||||||
|
"format_long_name": "WAV / WAVE (Waveform Audio)",
|
||||||
|
"duration": "1.000000",
|
||||||
|
"size": "88244",
|
||||||
|
"bit_rate": "705952",
|
||||||
|
"probe_score": 99
|
||||||
|
}
|
||||||
|
},
|
||||||
|
{},
|
||||||
|
{
|
||||||
|
"streams": [
|
||||||
|
{
|
||||||
|
"index": 0,
|
||||||
|
"codec_name": "pcm_s16le",
|
||||||
|
"codec_long_name": "PCM signed 16-bit little-endian",
|
||||||
|
"codec_type": "audio",
|
||||||
|
"codec_time_base": "1/44100",
|
||||||
|
"codec_tag_string": "[1][0][0][0]",
|
||||||
|
"codec_tag": "0x0001",
|
||||||
|
"sample_fmt": "s16",
|
||||||
|
"sample_rate": "44100",
|
||||||
|
"channels": 1,
|
||||||
|
"bits_per_sample": 16,
|
||||||
|
"r_frame_rate": "0/0",
|
||||||
|
"avg_frame_rate": "0/0",
|
||||||
|
"time_base": "1/44100",
|
||||||
|
"duration_ts": 44100,
|
||||||
|
"duration": "1.000000",
|
||||||
|
"bit_rate": "705600",
|
||||||
|
"disposition": {
|
||||||
|
"default": 0,
|
||||||
|
"dub": 0,
|
||||||
|
"original": 0,
|
||||||
|
"comment": 0,
|
||||||
|
"lyrics": 0,
|
||||||
|
"karaoke": 0,
|
||||||
|
"forced": 0,
|
||||||
|
"hearing_impaired": 0,
|
||||||
|
"visual_impaired": 0,
|
||||||
|
"clean_effects": 0,
|
||||||
|
"attached_pic": 0,
|
||||||
|
"timed_thumbnails": 0
|
||||||
|
}
|
||||||
|
}
|
||||||
|
],
|
||||||
|
"format": {
|
||||||
|
"filename": "tests/media/ff_silence.wav",
|
||||||
|
"nb_streams": 1,
|
||||||
|
"nb_programs": 0,
|
||||||
|
"format_name": "wav",
|
||||||
|
"format_long_name": "WAV / WAVE (Waveform Audio)",
|
||||||
|
"duration": "1.000000",
|
||||||
|
"size": "88278",
|
||||||
|
"bit_rate": "706224",
|
||||||
|
"probe_score": 99,
|
||||||
|
"tags": {
|
||||||
|
"encoder": "Lavf58.29.100"
|
||||||
|
}
|
||||||
|
}
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"streams": [
|
||||||
|
{
|
||||||
|
"index": 0,
|
||||||
|
"codec_name": "pcm_s16le",
|
||||||
|
"codec_long_name": "PCM signed 16-bit little-endian",
|
||||||
|
"codec_type": "audio",
|
||||||
|
"codec_time_base": "1/48000",
|
||||||
|
"codec_tag_string": "[1][0][0][0]",
|
||||||
|
"codec_tag": "0x0001",
|
||||||
|
"sample_fmt": "s16",
|
||||||
|
"sample_rate": "48000",
|
||||||
|
"channels": 1,
|
||||||
|
"bits_per_sample": 16,
|
||||||
|
"r_frame_rate": "0/0",
|
||||||
|
"avg_frame_rate": "0/0",
|
||||||
|
"time_base": "1/48000",
|
||||||
|
"duration_ts": 24000,
|
||||||
|
"duration": "0.500000",
|
||||||
|
"bit_rate": "768000",
|
||||||
|
"disposition": {
|
||||||
|
"default": 0,
|
||||||
|
"dub": 0,
|
||||||
|
"original": 0,
|
||||||
|
"comment": 0,
|
||||||
|
"lyrics": 0,
|
||||||
|
"karaoke": 0,
|
||||||
|
"forced": 0,
|
||||||
|
"hearing_impaired": 0,
|
||||||
|
"visual_impaired": 0,
|
||||||
|
"clean_effects": 0,
|
||||||
|
"attached_pic": 0,
|
||||||
|
"timed_thumbnails": 0
|
||||||
|
}
|
||||||
|
}
|
||||||
|
],
|
||||||
|
"format": {
|
||||||
|
"filename": "tests/media/pt_16bit.wav",
|
||||||
|
"nb_streams": 1,
|
||||||
|
"nb_programs": 0,
|
||||||
|
"format_name": "wav",
|
||||||
|
"format_long_name": "WAV / WAVE (Waveform Audio)",
|
||||||
|
"duration": "0.500000",
|
||||||
|
"size": "65344",
|
||||||
|
"bit_rate": "1045504",
|
||||||
|
"probe_score": 99,
|
||||||
|
"tags": {
|
||||||
|
"encoded_by": "Pro Tools",
|
||||||
|
"originator_reference": "aaijNZdiDOIk",
|
||||||
|
"date": "2020-11-18",
|
||||||
|
"creation_time": "19:56:53",
|
||||||
|
"time_reference": "1036416000",
|
||||||
|
"umid": "0x060A2B340101010501010F10130000002A1D203CE7248000711E4F71BFE13E00"
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
]
|
||||||
55
tests/ffprobe_media_tests.rs
Normal file
55
tests/ffprobe_media_tests.rs
Normal file
@@ -0,0 +1,55 @@
|
|||||||
|
|
||||||
|
extern crate serde_json;
|
||||||
|
use core::fmt::Debug;
|
||||||
|
use serde_json::{Value, from_str};
|
||||||
|
use std::fs::File;
|
||||||
|
use std::io::Read;
|
||||||
|
|
||||||
|
use bwavfile::WaveReader;
|
||||||
|
|
||||||
|
// Media Tests
|
||||||
|
//
|
||||||
|
// These tests compare metadata and format data read by ffprobe with the same values
|
||||||
|
// as read by `WaveReader`.
|
||||||
|
|
||||||
|
// This is rickety but we're going with it
|
||||||
|
fn assert_match_stream<T>(stream_key: &str,
|
||||||
|
other: impl Fn(&mut WaveReader<File>) -> T)
|
||||||
|
where T: PartialEq + Debug,
|
||||||
|
T: Into<Value>
|
||||||
|
{
|
||||||
|
|
||||||
|
let mut json_file = File::open("tests/ffprobe_media_tests.json").unwrap();
|
||||||
|
let mut s = String::new();
|
||||||
|
json_file.read_to_string(&mut s).unwrap();
|
||||||
|
if let Value::Array(v) = from_str(&mut s).unwrap() { /* */
|
||||||
|
v.iter()
|
||||||
|
.filter(|value| {
|
||||||
|
!value["format"]["filename"].is_null()
|
||||||
|
})
|
||||||
|
.for_each(|value| {
|
||||||
|
let filen : &str = value["format"]["filename"].as_str().unwrap();
|
||||||
|
let json_value : &Value = &value["streams"][0][stream_key];
|
||||||
|
let mut wavfile = WaveReader::open_unbuffered(filen).unwrap();
|
||||||
|
let wavfile_value: T = other(&mut wavfile);
|
||||||
|
println!("asserting {} for {}",stream_key, filen);
|
||||||
|
assert_eq!(Into::<Value>::into(wavfile_value), *json_value);
|
||||||
|
|
||||||
|
})
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
#[test]
|
||||||
|
fn test_frame_count() {
|
||||||
|
assert_match_stream("duration_ts", |w| w.frame_length().unwrap() );
|
||||||
|
}
|
||||||
|
|
||||||
|
#[test]
|
||||||
|
fn test_sample_rate() {
|
||||||
|
assert_match_stream("sample_rate", |w| format!("{}", w.format().unwrap().sample_rate) );
|
||||||
|
}
|
||||||
|
|
||||||
|
#[test]
|
||||||
|
fn test_channel_count() {
|
||||||
|
assert_match_stream("channels", |w| w.format().unwrap().channel_count );
|
||||||
|
}
|
||||||
@@ -2,6 +2,7 @@ extern crate bwavfile;
|
|||||||
|
|
||||||
use bwavfile::WaveReader;
|
use bwavfile::WaveReader;
|
||||||
use bwavfile::Error;
|
use bwavfile::Error;
|
||||||
|
use bwavfile::{ ChannelMask};
|
||||||
|
|
||||||
#[test]
|
#[test]
|
||||||
fn test_open() {
|
fn test_open() {
|
||||||
@@ -27,7 +28,7 @@ fn test_format_silence() -> Result<(),Error> {
|
|||||||
|
|
||||||
assert_eq!(format.sample_rate, 44100);
|
assert_eq!(format.sample_rate, 44100);
|
||||||
assert_eq!(format.channel_count, 1);
|
assert_eq!(format.channel_count, 1);
|
||||||
assert_eq!(format.tag, 1);
|
assert_eq!(format.tag as u16, 1);
|
||||||
Ok( () )
|
Ok( () )
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -74,4 +75,104 @@ fn test_minimal_wave() {
|
|||||||
} else {
|
} else {
|
||||||
assert!(true);
|
assert!(true);
|
||||||
}
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
#[test]
|
||||||
|
fn test_read() {
|
||||||
|
let path = "tests/media/audacity_16bit.wav";
|
||||||
|
|
||||||
|
let mut w = WaveReader::open(path).expect("Failure opening test file");
|
||||||
|
let mut buffer = w.format().unwrap().create_frame_buffer(1);
|
||||||
|
|
||||||
|
let mut reader = w.audio_frame_reader().unwrap();
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
assert_eq!(reader.read_integer_frame(&mut buffer).unwrap(), 1);
|
||||||
|
assert_eq!(buffer[0], -2823_i32);
|
||||||
|
assert_eq!(reader.read_integer_frame(&mut buffer).unwrap(), 1);
|
||||||
|
assert_eq!(buffer[0], 2012_i32);
|
||||||
|
assert_eq!(reader.read_integer_frame(&mut buffer).unwrap(), 1);
|
||||||
|
assert_eq!(buffer[0], 4524_i32);
|
||||||
|
}
|
||||||
|
|
||||||
|
#[test]
|
||||||
|
fn test_locate_multichannel_read() {
|
||||||
|
let path = "tests/media/ff_pink.wav";
|
||||||
|
|
||||||
|
let mut w = WaveReader::open(path).expect("Failure opening test file");
|
||||||
|
let mut buffer = w.format().unwrap().create_frame_buffer(1);
|
||||||
|
|
||||||
|
let mut reader = w.audio_frame_reader().unwrap();
|
||||||
|
|
||||||
|
assert_eq!(reader.read_integer_frame(&mut buffer).unwrap(), 1);
|
||||||
|
assert_eq!(buffer[0], 332702_i32);
|
||||||
|
assert_eq!(buffer[1], 3258791_i32);
|
||||||
|
assert_eq!(reader.read_integer_frame(&mut buffer).unwrap(), 1);
|
||||||
|
assert_eq!(buffer[0], -258742_i32); // 0x800000 = 8388608 // 8129866 - 8388608
|
||||||
|
assert_eq!(buffer[1], 0x0D7EF9_i32);
|
||||||
|
|
||||||
|
assert_eq!(reader.locate(100).unwrap(), 100);
|
||||||
|
assert_eq!(reader.read_integer_frame(&mut buffer).unwrap(), 1);
|
||||||
|
assert_eq!(buffer[0], 0x109422_i32);
|
||||||
|
assert_eq!(buffer[1], -698901_i32); // 7689707 - 8388608
|
||||||
|
}
|
||||||
|
|
||||||
|
#[test]
|
||||||
|
fn test_channels_stereo() {
|
||||||
|
let path = "tests/media/ff_pink.wav";
|
||||||
|
|
||||||
|
let mut w = WaveReader::open(path).expect("Failure opening test file");
|
||||||
|
let channels = w.channels().unwrap();
|
||||||
|
|
||||||
|
assert_eq!(channels.len(), 2);
|
||||||
|
assert_eq!(channels[0].index,0);
|
||||||
|
assert_eq!(channels[1].index,1);
|
||||||
|
assert_eq!(channels[0].speaker,ChannelMask::FrontLeft);
|
||||||
|
assert_eq!(channels[1].speaker,ChannelMask::FrontRight);
|
||||||
|
}
|
||||||
|
|
||||||
|
#[test]
|
||||||
|
fn test_channels_mono_no_extended() {
|
||||||
|
let path = "tests/media/audacity_16bit.wav";
|
||||||
|
|
||||||
|
let mut w = WaveReader::open(path).expect("Failure opening test file");
|
||||||
|
let channels = w.channels().unwrap();
|
||||||
|
|
||||||
|
assert_eq!(channels.len(), 1);
|
||||||
|
assert_eq!(channels[0].index,0);
|
||||||
|
assert_eq!(channels[0].speaker,ChannelMask::FrontCenter);
|
||||||
|
}
|
||||||
|
|
||||||
|
#[test]
|
||||||
|
fn test_channels_stereo_no_fmt_extended() {
|
||||||
|
let path = "tests/media/pt_24bit_stereo.wav";
|
||||||
|
|
||||||
|
let mut w = WaveReader::open(path).expect("Failure opening test file");
|
||||||
|
let channels = w.channels().unwrap();
|
||||||
|
|
||||||
|
assert_eq!(channels.len(), 2);
|
||||||
|
assert_eq!(channels[0].index,0);
|
||||||
|
assert_eq!(channels[1].index,1);
|
||||||
|
assert_eq!(channels[0].speaker,ChannelMask::FrontLeft);
|
||||||
|
assert_eq!(channels[1].speaker,ChannelMask::FrontRight);
|
||||||
|
}
|
||||||
|
|
||||||
|
//See issue 6 and 7
|
||||||
|
#[test]
|
||||||
|
fn test_frame_reader_consumes_reader() {
|
||||||
|
// Issue #6
|
||||||
|
use bwavfile::{WaveFmt, AudioFrameReader};
|
||||||
|
use std::fs::File;
|
||||||
|
fn from_wav_filename(wav_filename: &str) -> Result<(WaveFmt, AudioFrameReader<std::io::BufReader<File>>), ()> {
|
||||||
|
if let Ok(mut r) = WaveReader::open(&wav_filename) {
|
||||||
|
let format = r.format().unwrap();
|
||||||
|
let frame_reader = r.audio_frame_reader().unwrap();
|
||||||
|
Ok((format, frame_reader))
|
||||||
|
} else {
|
||||||
|
Err(())
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
let _result = from_wav_filename("tests/media/pt_24bit_stereo.wav").unwrap();
|
||||||
}
|
}
|
||||||
BIN
tests/test_media.tgz
Normal file
BIN
tests/test_media.tgz
Normal file
Binary file not shown.
Reference in New Issue
Block a user