Expand description
fmod-oxide Safe rust bindings to the FMOD sound engine. This crate tries to be as rusty and low-cost as possible, without compromising on any APIs. Certain APIs, such as loading banks from a pointer, are marked as unsafe, but are still available for use.
Supports FMOD versions >2.0.2.28 and >2.0.3.07, and Windows/Linux/MacOS/HTML5 platforms.
Any newer patch-level FMOD versions should compile but might have missing features.
§Using this crate
Due to licensing restrictions this crate can’t bundle FMOD, so you’ll need to download a copy of FMOD yourself.
Make sure to download from FMOD Engine
specifically.
After downloading FMOD, you have to tell this crate where FMOD is located. If you’re on Windows and used the FMOD installer, you don’t have to worry about this.
The easiest way is to create a cargo config in your project’s root.
# `.cargo/config.toml`
[env]
FMOD_SYS_FMOD_DIRECTORY = { value = "<absolute install path here>" }
You can also specify a relative install path like so:
# `.cargo/config.toml`
[env]
FMOD_SYS_FMOD_DIRECTORY = { value = "<install path here>", relative = true }
(not recommended because rust-analyzer won’t know this) Alternatively, you can specify FMOD_SYS_FMOD_DIRECTORY
when building your project:
FMOD_SYS_FMOD_DIRECTORY=<path> cargo run
§Cross compilation
This crate supports cross compilation and will look for a target-specific FMOD install.
The logic is quite basic at the moment, but it’ll check if <fmod install dir>/<target os>
exists and use that.
If no target specific directory was found, it’ll default to <fmod install dir>
§Using with webassembly
Currently only wasm32-unknown-emscripten
works well.
wasm32-unknown-unknown
also works in some capacity but you have to essentially reimplement parts of libc and emscripten.
Unfortunately wasm-bindgen
doesn’t work without patches right now, so your milage may vary
The setup is roughly the same, except you’ll need to add some arguments to EMCC_FLAGS
.
You can do this by editing .cargo/config.toml
:
# `.cargo/config.toml`
[env]
EMCC_CFLAGS="-s EXPORTED_RUNTIME_METHODS=ccall,cwrap,setValue,getValue" # FMOD requires this
If you’re using wasm32-unknown-unknown
, you’ll additionally need to add this until this issue is closed.
# `.cargo/config.toml`
[build]
rustflags="-Zwasm-c-abi=spec"
See web-examples/emscripten
for a more detailed example.
§Memory management & Copy types
All FMOD objects are Copy, Clone, Send and Sync because it’s possible to have multiple references to the same object. (e.g. loading a bank and then retrieving it by its path)
There are a lot of use-cases where you may want to fetch something (like a bank) and never use it again.
Implementing Drop
to automatically release things would go against that particular use-case, so this crate opts to have manual release()
methods instead.
This crate does not currently guard against use-after-frees, however using most of FMOD’s types (especially FMOD Studio’s types) after calling release()
is safe.
I’m still not 100% sure of what is and isn’t safe and I’m actively trying to test this.
§String types
fmod-oxide aims to be as zero-cost as possible, and as such, it uses UTF-8 C strings from the lanyard
crate as its string type.
This means that all FMOD functions take a &Utf8CStr
instead of a &str
or &CStr
.
&Utf8CStr
is pretty cheap to construct (and can even be done statically with the c!
macro), so this should not be a problem
When FMOD returns a string, it will always return a Utf8CString
(the owned version of Utf8CStr
) because it’s difficult to encode lifetime requirements of FMOD strings.
This applies to structs like fmod::studio::AdvancedSettings
which store C strings.
Converting structs like AdvancedSettings
to their FFI equivalents is done by reference as to not pass ownership of the string to FMOD
§Basic example
// System creation is unsafe and must be performed prior to any other FMOD operations.
let mut builder = unsafe { fmod::studio::SystemBuilder::new() }?;
let system = builder.build()?;
// Load a bank
let bank = system.load_bank_file("path/to/bank.bank", fmod::studio::LoadBankFlags::NORMAL)?;
// Query all events in the bank
for event in bank.get_event_list().unwrap() {
println!("Event: {}", event.get_path()?);
}
// Releasing Systems is unsafe because it cannot be called concurrently, and all FMOD objects are rendered invalid.
unsafe { system.release() };
§Feature flags
thread-unsafe
— Disable Send + Sync impls for FMOD’s types, making FMOD’s thread-unsafe API saferstudio
(enabled by default) — Enables FMOD’s Studio API
Re-exports§
Modules§
- core
- The low-level FMOD core API.
- coverage
Nowhere - How much of FMOD’s API fmod-oxide covers.
- debug
- Low level control over FMOD’s debug logging.
- effects
- file
- Low level control over FMOD’s filesystem access.
- memory
- Low level control over how FMOD allocates memory.
- studio
studio
- The FMOD Studio API.
- thread
- Low level control over FMOD’s threads.
Structs§
- Advanced
Settings - Advanced configuration settings.
- Async
Cancel Info - Information about cancelling a asynchronous file operation.
- Async
Read Info - Information about a single asynchronous file operation.
- Attenuation
Range - Attenuation range parameter data structure.
- Attributes3D
- Structure describing a position, velocity and orientation.
- Attributes3D
Multi - 3D attributes data structure for multiple listeners.
- Channel
- A source of audio signal that connects to the
ChannelGroup
mixing hierarchy. - Channel
Control - An interface that represents the shared APIs between
Channel
andChannelGroup
. - Channel
Group - A submix in the mixing hierarchy akin to a bus that can contain both
Channel
andChannelGroup
objects. - Channel
Mask - Flags that describe the speakers present in a given signal.
- CpuUsage
- Performance information for Core API functionality.
- Driver
State - Flags that provide additional information about a particular driver.
- Dsp
- A digital signal processor is one node within a graph that transforms input audio signals into an output stream.
- DspAttributes3D
- 3D attributes data structure.
- DspConnection
- An interface that manages Digital Signal Processor (DSP) connections
- DspMetering
Info - DSP metering info.
- DspParameter
Description - Base structure for DSP parameter descriptions.
- Dynamic
Response fmod_2_3
- Dynamic response data structure.
- Error
Callback Info - Information describing an error that has occurred.
- Fft
- FFT parameter data structure.
- File
Buffer - A mutable file buffer.
- Float
Mapping - Structure to define a mapping for a DSP unit’s float parameter.
- Geometry
- An interface that allows the setup and modification of geometry for occlusion.
- Guid
- Structure describing a globally unique identifier.
- Init
Flags - Configuration flags used when initializing the System object.
- Mode
- [
Sound
] description bitfields. - Overall
Gain - Overall gain parameter data structure.
- Piecewise
Linear Mapping - Structure to define a piecewise linear mapping.
- Reverb3D
- An interface that manages virtual 3D reverb spheres.
- Reverb
Properties - Structure defining a reverb environment.
- Sidechain
- Side chain parameter data structure.
- Sound
- Container for sample data that can be played on a Channel.
- Sound
Builder - A builder for creating a
Sound
. - Sound
Group - An interface that manages Sound Groups.
- Sound
Lock - A locked region of sound data.
- Sync
Point - Named marker for a given point in time.
- System
- Management object from which all resources are created and played.
- System
Builder - A builder for creating and initializing a
System
. - System
Callback Mask - Tag
- Tag data / metadata description.
- Thread
Affinity - Bitfield for specifying the CPU core a given thread runs on.
- Vector
- Structure describing a point in 3D space.
Enums§
- Channel
Control Type - Enum used to distinguish between
Channel
andChannelGroup
in theChannelControl
callback. - Channel
Order - Speaker ordering for multi-channel signals.
- DspConnection
Type - List of connection types between two DSP units.
- DspParameter
Data Type - Data parameter types.
- DspParameter
Type - DSP parameter types.
- DspType
- DSP types.
- Error
- An error that FMOD (or this crate) might return.
- Float
Mapping Type - DSP float parameter mappings.
- Instance
- Identifier used to represent the different types of instance in the error callback.
- Open
State - These values describe what state a sound is in after
FMOD_NONBLOCKING
has been used to open it. - Output
Type - Built-in output types that can be used to run the mixer.
- Plugin
Type - Types of plug-in used to extend functionality.
- Port
Type - Port types available for routing audio.
- Resampler
- List of interpolation types used for resampling.
- Sound
Format - These definitions describe the native format of the hardware or software buffer that will be used.
- Sound
Group Behavior - Values specifying behavior when a sound group’s max audible value is exceeded.
- Sound
Type - Recognized audio formats that can be loaded into a Sound.
- Speaker
- Assigns an enumeration for a speaker index.
- Speaker
Mode - Speaker mode types.
- TagData
- List of tag data / metadata types.
- TagType
- List of tag data / metadata types that could be stored within a sound. These include id3 tags, metadata from netstreams and vorbis/asf data.
- Thread
Type - Named constants for threads created at runtime.
- Time
Unit - Time types used for position or length.
Constants§
- BUILD_
NUMBER - The FMOD build number.
- MAX_
CHANNEL_ WIDTH - Maximum number of channels per sample of audio supported by audio files, buffers, connections and
Dsp
s. - MAX_
LISTENERS - Maximum number of listeners supported.
- MAX_
REVERB_ INSTANCES - The maximum number of global reverb instances.
- MAX_
SYSTEMS - Maximum number of System objects allowed.
- VERSION
- Current FMOD version number.
Traits§
- Channel
Control Callback - Trait for this particular FMOD callback.
- DspCallback
- Trait for this particular FMOD callback.
- File
System - The base trait for all filesystems.
- File
System Async - An async filesystem.
- File
System Sync - A synchronous filesystem.
- NonBlock
Callback - Callback to be called when a sound has finished loading, or a non blocking seek is occuring.
- PcmCallback
- Capture or provide sound data as it is decoded.
- Readable
Parameter - Trait for types that can be read from DSP parameters.
- Readable
Parameter Index - Trait for types that can be turned into a readable parameter index.
- Rolloff
Callback - Trait for this particular FMOD callback.
- System
Callback - Trait for this particular FMOD callback.
- Writable
Parameter - Trait for types that can be written to DSP parameters.
- Writable
Parameter Index - Trait for types that can be turned into a writable parameter index.
Type Aliases§
- Result
- Shorthand for
std::result::Result<T, Error>