pub struct Song {
pub bpm: BPMChoice,
pub notes: Vec<Note>,
}Expand description
This struct represents a song.
It contains a list of notes and a BPM (beats per minute) setting.
§Usage:
use rs_audio::*;
let song = Song::default(); // creates a default song with one note (A4, 440Hz, 3 seconds, 0.20 volume, sine wave)Fields§
§bpm: BPMChoice§notes: Vec<Note>Implementations§
Source§impl Song
impl Song
Sourcepub fn save_to_json(song: &Song, filename: &str) -> Result<(), Error>
pub fn save_to_json(song: &Song, filename: &str) -> Result<(), Error>
Saves a song to a JSON file. This can be useful if you want to save your songs somewhere.
Note that this can return an error if it fails to:
- Convert the song to JSON
- Write to the file
§Usage
use rs_audio::*;
let song = Song::default();
match Song::save_to_json(&song, "song.json") {
Ok(_) => (),
Err(e) => eprintln!("{}", e.to_string()),
}Sourcepub fn load_from_json(filename: &str) -> Result<Song, Error>
pub fn load_from_json(filename: &str) -> Result<Song, Error>
Loads a Song struct from a JSON file. This can be useful if you want to load existing songs from JSONs.
Note that this will return an error if it fails to:
- Open the file (it may not exist or it could not read it)
- Read from the file.
§Usage
use rs_audio::*;
let loaded_song: Song = match Song::load_from_json("song.json") {
Ok(s) => s,
Err(e) => {
eprintln!("{}", e.to_string());
std::process::exit(1);
}
};Source§impl Song
impl Song
Sourcepub fn export_to_wav(&self, filename: String) -> Result<(), Box<dyn Error>>
pub fn export_to_wav(&self, filename: String) -> Result<(), Box<dyn Error>>
Exports a Song struct to a .wav file.
Usage:
use rs_audio::*;
use rs_audio::{player::Song};
let song = Song::default();
song.export_to_wav("test.wav".to_string());§Panics
This function will panic if the file could not be created due to some error.
Sourcepub fn play_wav(file_path: &str) -> Result<(), Error>
pub fn play_wav(file_path: &str) -> Result<(), Error>
Plays a .wav file directly.
Note that .wav’s are not converted to Songs in this function due to complexity.
Usage:
use rs_audio::*;
use rs_audio::{player::Song};
Song::play_wav("test.wav").unwrap();This function will return an Error if it encounters an error.
The recommended way to use it is the following:
use rs_audio::*;
use rs_audio::{player::Song};
match Song::play_wav("test.wav") {
Ok(_) => (),
Err(e) => {
eprintln!("Error: {e}");
std::process::exit(1);
}
}Trait Implementations§
Source§impl<'de> Deserialize<'de> for Song
impl<'de> Deserialize<'de> for Song
Source§fn deserialize<__D>(__deserializer: __D) -> Result<Self, __D::Error>where
__D: Deserializer<'de>,
fn deserialize<__D>(__deserializer: __D) -> Result<Self, __D::Error>where
__D: Deserializer<'de>,
Deserialize this value from the given Serde deserializer. Read more
Auto Trait Implementations§
impl Freeze for Song
impl RefUnwindSafe for Song
impl Send for Song
impl Sync for Song
impl Unpin for Song
impl UnwindSafe for Song
Blanket Implementations§
Source§impl<T> BorrowMut<T> for Twhere
T: ?Sized,
impl<T> BorrowMut<T> for Twhere
T: ?Sized,
Source§fn borrow_mut(&mut self) -> &mut T
fn borrow_mut(&mut self) -> &mut T
Mutably borrows from an owned value. Read more