GithubHelp home page GithubHelp logo

mbillingr / ambisonic Goto Github PK

View Code? Open in Web Editor NEW
84.0 4.0 12.0 180 KB

Compose and play 3D audio in pure Rust

Home Page: https://crates.io/crates/ambisonic

License: Apache License 2.0

Rust 98.37% Shell 0.08% Python 1.55%
rust ambisonics game-development

ambisonic's Introduction

ambisonic

Compose and play 3D audio.

License Cargo Documentation

Tests

The ambisonic library provides 3D sound scene support on top of rodio. It allows positioning and moving sound sources freely in 3D space around a virtual listener, and playing the resulting spatial mix in real-time over a sound card.

Features

  • Realistic directional audio
  • Take rodio sound sources and place them in space
  • Doppler effect on moving sounds

Gallery

  • Video that demonstrates spatial audio in a 3D graphics scene by @bjadamson

Usage

Add this to your Cargo.toml:

[dependencies]
ambisonic = "0.4"

Usage Example

Ambisonic can generate a sine wave for you:

use std::thread::sleep;
use std::time::Duration;
use ambisonic::{rodio, AmbisonicBuilder};

let scene = AmbisonicBuilder::default().build();
let source = rodio::source::SineWave::new(440);
let mut sound = scene.play_at(source, [50.0, 1.0, 0.0]);

// move sound from right to left
sound.set_velocity([-10.0, 0.0, 0.0]);
for i in 0..1000 {
    sound.adjust_position([50.0 - i as f32 / 10.0, 1.0, 0.0]);
    sleep(Duration::from_millis(10));
}
sound.set_velocity([0.0, 0.0, 0.0]);

Since Ambisonic is built ontop of Rodio, any file format that Rodio supports can also be loaded and positioned in 3D space.

let file = std::fs::File::open("path/to/your/file.wav").unwrap();
let source = rodio::Decoder::new(std::io::BufReader::new(file)).unwrap();
let source = source.repeat_infinite();

let mut sound = scene.play_at(source.convert_samples(), [50.0, 1.0, 0.0]);

// move sound from right to left
sound.set_velocity([-10.0, 0.0, 0.0]);
for i in 0..1000 {
    sound.adjust_position([50.0 - i as f32 / 10.0, 1.0, 0.0]);
    sleep(Duration::from_millis(10));
}
sound.set_velocity([0.0, 0.0, 0.0]);

Technical Details

ambisonic is built around the concept of an intermediate representation of the sound field, called B-format. The B-format describes what the listener should hear, independent of their audio playback equipment. This leads to a clear separation of audio scene composition and rendering. For details, see Wikipedia.

In its current state, the library allows spatial composition of single-channel rodio sources into a first-order B-format stream. The chosen renderer then decodes the B-format stream into audio signals for playback.

Currently, the following renderers are available:

  • Stereo: simple and efficient playback on two stereo speakers or headphones
  • HRTF: realistic 3D sound over headphones using head related transfer functions

Although at the moment only stereo output is supported, the B-format abstraction should make it easy to implement arbitrary speaker configurations in the future.

Learning Resources

https://github.com/mbillingr/ambisonic/blob/master/documents/info.md

License

Licensed under either of

at your option.

Contribution

Unless you explicitly state otherwise, any contribution intentionally submitted for inclusion in the work by you, as defined in the Apache-2.0 license, shall be dual licensed as above, without any additional terms or conditions.

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.