Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Rust-native audio transcoding #317

Merged

Conversation

marceline-cramer
Copy link
Contributor

@marceline-cramer marceline-cramer commented Apr 16, 2023

Closes #38.

Regrettably, there are no pure-Rust ogg Vorbis encoding crates right now. However, I've added the next best thing: vorbis_rs. It has active maintainers and uses the latest ogg libraries. The other Rust ogg libraries are unmaintained and use older versions of libogg and libvorbis with known security vulnerabilities.

I also caught an issue where sound graph files were using the string literal "SOUND_GRAPH_EXTENSION" instead of the constant value SOUND_GRAPH_EXTENSION, so I've patched that.

Also, I haven't implemented the copying of tags from the input file to the output file. The reason is that vorbis_rs expects tag values to be strings but symphonia tag values can be lots of different types. I'm not sure what to do about this, so I've left tags empty in the output file for now.

@marceline-cramer marceline-cramer marked this pull request as ready for review April 17, 2023 04:09
@chaosprint
Copy link
Contributor

chaosprint commented Apr 17, 2023

Just played with the physics examples using a wav file and got this error:

[2023-04-17T07:13:58Z INFO  symphonia_core::probe] found the format marker [52, 49, 46, 46] @ 0+2 bytes.
[2023-04-17T07:13:58Z ERROR ambient_build] In pipeline pipeline.json/1, at file ping.wav
    
    Caused by:
        0: Failed to create audio decoder
        1: unsupported feature: core (codec):unsupported codec

I will now try to fix it on top of your code.

It seems that the hint is not used at all inside symphonia:

symphonia::default::get_probe().format()

The real cause is CodecType 264 is not in the CodecRegistry codecs HashMap


When I enable all default features in symphonia, I got:

[2023-04-17T08:35:07Z ERROR ambient_build] In pipeline pipeline.json/1, at file bonk.wav
    
    Caused by:
        Expected 2 channels in audio block, got 3

which is very strange...


well, you need to use "pcm" feature rather than "wav" for symphonia

Even so, I still got an error. symphonia somehow probes wav as an mp3...

[2023-04-17T08:55:51Z INFO  ambient] Building Physics
ext "wav"
[2023-04-17T08:55:51Z INFO  symphonia_core::probe] found the format marker [ff, fd] @ 0+51 bytes.
[2023-04-17T08:55:51Z WARN  symphonia_bundle_mp3::demuxer] invalid mpeg audio header
[2023-04-17T08:55:51Z WARN  symphonia_bundle_mp3::demuxer] skipping junk at 6542 bytes
[2023-04-17T08:55:51Z WARN  symphonia_bundle_mp3::demuxer] skipping junk at 18532 bytes
[2023-04-17T08:55:51Z WARN  symphonia_bundle_mp3::demuxer] skipping junk at 20470 bytes
[2023-04-17T08:55:51Z WARN  symphonia_bundle_mp3::demuxer] skipping junk at 22824 bytes

Right, three features have to be enabled: ["wav", "pcm", "mp3"] (not very intuitive but I will pr them later...)

I got the same error when all features are enabled:

When I enable all default features in `symphonia`, I got:

[2023-04-17T08:35:07Z ERROR ambient_build] In pipeline pipeline.json/1, at file bonk.wav

Caused by:
    Expected 2 channels in audio block, got 3

@chaosprint
Copy link
Contributor

chaosprint commented Apr 17, 2023

Hi,

It looks that Symphonia still has some work to do. So we have to wait or switch to other solutions.

We can use minimp3 and hound instead. Are you interested to do the switch?

@marceline-cramer
Copy link
Contributor Author

marceline-cramer commented Apr 17, 2023

I would be interested in doing the switch, but symphonia is very close to being fully working. Would the minimp3 or hound crates provide any features that symphonia wouldn't? Symphonia supports other audio codecs like FLAC and AAC, and adding support to those with symphonia is as simple as enabling the feature flag and matching the audio asset pipeline with the corresponding file extensions.

It seems like now that .wav files are being processed correctly, the problem is that the bits in the channel flags are counted to set the channel count in the Vorbis encoder. This doesn't produce the correct number of channels in all cases. This can be fixed if I use a more comprehensive method of querying which channel bits are set instead, and I'll be sure to cover more testcases with the test input files.

@chaosprint
Copy link
Contributor

For extra featues, probably not. But I do have minimp3 and hound working on my Mac while for symphonia, I cannot run its official examples. Have you tried to run the symphonia examples?

@marceline-cramer
Copy link
Contributor Author

marceline-cramer commented Apr 17, 2023

Yeah, I've ran symphonia-play from the latest master branch and it works fine on every audio file I've given it so far. For extra context I'm on Artix Linux. Symphonia also has passing CI on Mac. Do you know what's keeping you from running the examples?

@marceline-cramer
Copy link
Contributor Author

marceline-cramer commented Apr 17, 2023

I made a mistake with the channel count; counting the bits WAS the correct way to do that, but I confused the .bits() function (which returns the numeric bitfield) with the .count() function (which actually counts the bits).

After enabling the pcm Symphonia feature and counting the bits correctly, the stereo .wav files I've tested now get converted to ogg with no issue.

@philpax philpax requested a review from chaosprint April 18, 2023 11:40
@chaosprint
Copy link
Contributor

I made a mistake with the channel count; counting the bits WAS the correct way to do that, but I confused the .bits() function (which returns the numeric bitfield) with the .count() function (which actually counts the bits).

After enabling the pcm Symphonia feature and counting the bits correctly, the stereo .wav files I've tested now get converted to ogg with no issue.

Cool, I will test this ASAP

@philpax
Copy link
Contributor

philpax commented Apr 19, 2023

Sounds good to me - the log hiding is done at the top-level here:

Ambient/app/src/main.rs

Lines 28 to 40 in 40eb6eb

LevelFilter::Warn,
&[
"ambient_build",
"ambient_gpu",
"ambient_model",
"ambient_physics",
"ambient_std",
"cranelift_codegen",
"naga",
"tracing",
"wgpu_core",
"wgpu_hal",
],

We can just add a commit to this PR to do that, and merge (Marceline won't be online until later) - are you OK to do that?

Copy link
Contributor

@chaosprint chaosprint left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The latest version works perfectly on my Mac. During the conversion, some unnecessary logs are printed, but I guess it's harder to remove as this comes from the symphonia itself. We can work on the conversion mechanism later: #320

[2023-04-19T08:21:42Z INFO  symphonia_core::probe] found the format marker [49, 44, 33] @ 0+2 bytes.
[2023-04-19T08:21:42Z INFO  symphonia_core::probe] found the format marker [ff, fb] @ 1246+2 bytes.
[2023-04-19T08:21:42Z INFO  symphonia_bundle_mp3::demuxer] using xing header for duration```

@chaosprint
Copy link
Contributor

Sounds good to me - the log hiding is done at the top-level here:

Ambient/app/src/main.rs

Lines 28 to 40 in 40eb6eb

LevelFilter::Warn,
&[
"ambient_build",
"ambient_gpu",
"ambient_model",
"ambient_physics",
"ambient_std",
"cranelift_codegen",
"naga",
"tracing",
"wgpu_core",
"wgpu_hal",
],

We can just add a commit to this PR to do that, and merge (Marceline won't be online until later) - are you OK to do that?

Yes

@chaosprint chaosprint merged commit 95a6b17 into AmbientRun:main Apr 19, 2023
@marceline-cramer marceline-cramer deleted the rust-native-audio-transcoding branch April 21, 2023 13:36
philpax pushed a commit that referenced this pull request May 23, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Use Rust-native code to transcode audio in the audio pipeline
3 participants