1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200
// Augmented Audio: Audio libraries and applications
// Copyright (c) 2022 Pedro Tacla Yamada
//
// The MIT License (MIT)
//
// Permission is hereby granted, free of charge, to any person obtaining a copy
// of this software and associated documentation files (the "Software"), to deal
// in the Software without restriction, including without limitation the rights
// to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
// copies of the Software, and to permit persons to whom the Software is
// furnished to do so, subject to the following conditions:
//
// The above copyright notice and this permission notice shall be included in
// all copies or substantial portions of the Software.
//
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
// THE SOFTWARE.
//! # Augmented Audio: Audio Processor Standalone
//! [](https://crates.io/crates/audio-processor-standalone)
//! [](https://docs.rs/audio-processor-standalone/)
//! - - -
//! This is part of <https://github.com/yamadapc/augmented-audio>. Please review its goals. This
//! crate builds upon [`audio_processor_traits::AudioProcessor`](https://docs.rs/audio-processor-traits/latest/audio_processor_traits/trait.AudioProcessor.html).
//!
//! Provides a stand-alone audio-processor runner for [`AudioProcessor`](https://docs.rs/audio-processor-traits/latest/audio_processor_traits/trait.AudioProcessor.html)
//! implementations.
//!
//! ## Navigating the documentation
//! * Look at exported functions & macros; the structs/traits are for more advanced/internal usage.
//! * Start with [`audio_processor_main`] and [`audio_processor_main_with_midi`]
//! * There are plenty examples in the `augmented-audio` repository
//!
//! The gist of it is:
//!
//! 1. Implement [`AudioProcessor`](https://docs.rs/audio-processor-traits/latest/audio_processor_traits/trait.AudioProcessor.html)
//! or [`SimpleAudioProcessor`](https://docs.rs/audio-processor-traits/latest/audio_processor_traits/trait.SimpleAudioProcessor.html)
//! from [`audio_processor_traits`](https://docs.rs/audio-processor-traits)
//! 2. Call `audio_processor_main(processor)`
//! 3. You now have a CLI for rendering online (CPAL, use your mic) or offline (pass a file through your processor & write
//! the results to a `.wav`)
//!
//! A VST may also be generated through the `standalone_vst` module and by enabling the `vst`
//! feature flag.
//!
//! ## Example usage
//!
//! Declare the `AudioProcessor`:
//!
//! ```rust
//! use audio_processor_traits::{AudioBuffer, AudioProcessor};
//!
//! struct GainProcessor {}
//!
//! impl GainProcessor { fn new() -> Self { GainProcessor {} }}
//!
//! impl AudioProcessor for GainProcessor {
//! type SampleType = f32;
//! fn process<BufferType: AudioBuffer<SampleType=Self::SampleType>>(&mut self, data: &mut BufferType) {
//! for sample in data.slice_mut() {
//! *sample = *sample * 0.4;
//! }
//! }
//! }
//! ```
//!
//! Declare the main function:
//!
//! ```ignore
//! fn main() {
//! let processor = GainProcessor::new();
//! audio_processor_standalone::audio_processor_main(processor);
//! }
//! ```
//!
//! ## Usage of the command-line
//! ```ignore
//! audio-processor-standalone
//!
//! USAGE:
//! my-crate [OPTIONS]
//!
//! FLAGS:
//! -h, --help Prints help information
//! -V, --version Prints version information
//!
//! OPTIONS:
//! -i, --input-file <INPUT_PATH> An input audio file to process
//! --midi-input-file <MIDI_INPUT_FILE> If specified, this MIDI file will be passed through the processor
//! -o, --output-file <OUTPUT_PATH> If specified, will render offline into this file (WAV)
//! ```
use basedrop::Handle;
use audio_processor_traits::{AudioProcessor, MidiEventHandler};
use options::{ParseOptionsParams, RenderingOptions};
#[doc(inline)]
pub use standalone_cpal::{
audio_processor_start, audio_processor_start_with_midi, standalone_start, StandaloneHandles,
};
#[doc(inline)]
pub use standalone_processor::{
StandaloneAudioOnlyProcessor, StandaloneProcessor, StandaloneProcessorImpl,
};
/// Options handling for standalone processor
pub mod options;
/// Online standalone implementation using CPAL
pub mod standalone_cpal;
/// Internal wrapper types
pub mod standalone_processor;
/// Offline rendering implementation (offline functionality will not work on iOS for now)
#[cfg(not(target_os = "ios"))]
pub mod offline;
/// VST support (VST is not compiled for iOS)
#[cfg(not(target_os = "ios"))]
pub mod standalone_vst;
/// A default main function for an [`AudioProcessor`] and [`MidiEventHandler`].
///
/// Run an [`AudioProcessor`] / [`MidiEventHandler`] as a stand-alone cpal app and forward MIDI
/// messages received on all inputs to it. Same as `audio_processor_main`, but requires
/// [`MidiEventHandler`] to support MIDI.
///
/// Will internally create [`cpal::Stream`], [`audio_processor_standalone_midi::MidiHost`] and park the current thread. If the thread
/// is unparked the function will exit and the audio/MIDI threads will stop once these structures
/// are dropped.
pub fn audio_processor_main_with_midi<
Processor: AudioProcessor<SampleType = f32> + MidiEventHandler + Send + 'static,
>(
audio_processor: Processor,
handle: &Handle,
) {
let app = StandaloneProcessorImpl::new(audio_processor);
standalone_main(app, Some(handle));
}
/// A default main function for an [`AudioProcessor`].
///
/// Will support running it, based on CLI options, as:
///
/// * CPAL audio app processing audio from default input device and outputting it
/// * CPAL audio app processing an audio input file (MP3)
/// * Offline rendering into a WAV file
///
/// Returns the [`cpal::Stream`] streams. The audio-thread will keep running until these are dropped.
pub fn audio_processor_main<Processor: AudioProcessor<SampleType = f32> + Send + 'static>(
audio_processor: Processor,
) {
let app = StandaloneAudioOnlyProcessor::new(audio_processor, Default::default());
standalone_main(app, None);
}
/// Internal main function used by `audio_processor_main`.
fn standalone_main(mut app: impl StandaloneProcessor, handle: Option<&Handle>) {
let options = options::parse_options(ParseOptionsParams {
supports_midi: app.supports_midi(),
});
match options.rendering() {
RenderingOptions::Online { .. } => {
log::info!("Starting stand-alone online rendering with default IO config");
let _handles = standalone_start(app, handle);
std::thread::park();
}
#[cfg(not(target_os = "ios"))]
RenderingOptions::Offline {
input_file: input_path,
output_file: output_path,
} => {
let midi_input_file = options.midi().input_file.as_ref().map(|midi_input_file| {
let file_contents =
std::fs::read(midi_input_file).expect("Failed to read input MIDI file");
let (_, midi_file) =
augmented_midi::parse_midi_file::<String, Vec<u8>>(&file_contents)
.expect("Failed to parse input MIDI file");
midi_file
});
offline::run_offline_render(offline::OfflineRenderOptions {
app,
handle,
input_path,
output_path,
midi_input_file,
});
}
#[cfg(target_os = "ios")]
_ => {
log::error!("Offline rendering is unsupported on iOS");
std::process::exit(1)
}
}
}