Skip to main content

Note

Struct Note 

Source
pub struct Note(/* private fields */);

Implementations§

Source§

impl Note

Source

pub fn from_midi(number: u8) -> Self

Examples found in repository?
examples/sequenced_arrangement.rs (line 39)
28fn build_arrangement() -> Result<IndexedSequence, Box<dyn Error>> {
29    let mut sequence = IndexedSequence::new(4)
30        .title("Curated Sequencer Arrangement")
31        .composer("melody-bay examples");
32    sequence.tempo_at(0, 132.0);
33    sequence.tempo_at(16, 96.0);
34    sequence.tempo_at(24, 144.0);
35
36    sequence.add_track(
37        TrackId::named("lead.graph"),
38        IndexedTrack::new(Instrument::graph(lead_graph()?))
39            .note_with_velocity(0, Note::from_midi(60), 3, Velocity::new(0.55))
40            .note_with_velocity(4, Note::from_midi(64), 3, Velocity::new(0.75))
41            .note_with_velocity(8, Note::from_midi(67), 4, Velocity::new(0.85))
42            .note_with_velocity(16, Note::from_midi(72), 6, Velocity::new(0.95))
43            .automation_at(0, "voice.gain", 0.18)
44            .linear_ramp_to_value_at_index(16, "voice.gain", 0.11)
45            .value_curve_at_index(20, "filter.frequency", [700.0, 1_800.0, 900.0], 8),
46    );
47
48    sequence.add_track(
49        TrackId::named("pad.graph"),
50        IndexedTrack::new(Instrument::graph(pad_graph()?))
51            .note_with_velocity(0, Note::from_midi(48), 16, Velocity::new(0.5))
52            .note_with_velocity(16, Note::from_midi(53), 12, Velocity::new(0.55))
53            .linear_ramp_to_value_at_index(12, "pad.gain", 0.08)
54            .linear_ramp_to_value_at_index(28, "pad.gain", 0.0),
55    );
56
57    sequence.add_track(
58        TrackId::named("sample.kick"),
59        IndexedTrack::new(kick_instrument()?)
60            .note_with_velocity(0, Note::from_midi(36), 1, Velocity::new(0.95))
61            .note_with_velocity(8, Note::from_midi(36), 1, Velocity::new(0.8))
62            .note_with_velocity(16, Note::from_midi(36), 1, Velocity::new(0.95))
63            .note_with_velocity(24, Note::from_midi(36), 1, Velocity::new(0.8)),
64    );
65
66    Ok(sequence)
67}
68
69fn lead_graph() -> Result<AudioContext, Box<dyn Error>> {
70    let mut graph = AudioContext::try_new_with_sample_rate(SAMPLE_RATE)?;
71    let osc = graph.create_oscillator();
72    osc.set_type(Waveform::Sawtooth);
73    let filter = graph.create_biquad_filter();
74    filter.set_type(BiquadFilterType::Lowpass);
75    filter.frequency().set_value(1_200.0)?;
76    filter.q().set_value(0.8)?;
77    let gain = graph.create_gain();
78    gain.gain().set_value(0.16)?;
79    graph.label_node(&gain, "voice")?;
80    graph.label_node(&filter, "filter")?;
81    graph.connect(osc, &filter)?;
82    graph.connect(&filter, &gain)?;
83    graph.connect(&gain, graph.destination())?;
84    Ok(graph)
85}
86
87fn pad_graph() -> Result<AudioContext, Box<dyn Error>> {
88    let mut graph = AudioContext::try_new_with_sample_rate(SAMPLE_RATE)?;
89    let pad_gain = graph.create_gain();
90    pad_gain.gain().set_value(0.08)?;
91    graph.label_node(&pad_gain, "pad")?;
92    for (frequency, detune) in [(220.0, -7.0), (277.18, 0.0), (329.63, 5.0)] {
93        let osc = graph.create_oscillator();
94        osc.set_type(Waveform::Triangle);
95        osc.frequency().set_value(frequency)?;
96        osc.detune().set_value(detune)?;
97        graph.connect(osc, &pad_gain)?;
98    }
99    graph.connect(&pad_gain, graph.destination())?;
100    Ok(graph)
101}
102
103fn kick_instrument() -> Result<Instrument, Box<dyn Error>> {
104    let samples = (0..2_400).map(|i| {
105        let t = i as f32 / SAMPLE_RATE as f32;
106        let pitch = 90.0 - t * 120.0;
107        let env = (1.0 - t * 18.0).max(0.0);
108        (t * std::f32::consts::TAU * pitch).sin() * env
109    });
110    let buffer = AudioBuffer::try_from_mono(SAMPLE_RATE, 2_400, samples)?;
111    Ok(Instrument::sample(buffer, Note::from_midi(36))
112        .volume(0.75)
113        .envelope(SampleEnvelope {
114            points: vec![(0.0, 1.0), (0.08, 0.0)],
115        }))
116}
Source

pub fn midi_number(self) -> u8

Source

pub fn frequency(self) -> f32

Source

pub fn name(self) -> String

Trait Implementations§

Source§

impl Clone for Note

Source§

fn clone(&self) -> Note

Returns a duplicate of the value. Read more
1.0.0 (const: unstable) · Source§

fn clone_from(&mut self, source: &Self)

Performs copy-assignment from source. Read more
Source§

impl Copy for Note

Source§

impl Debug for Note

Source§

fn fmt(&self, f: &mut Formatter<'_>) -> Result

Formats the value using the given formatter. Read more
Source§

impl Default for Note

Source§

fn default() -> Self

Returns the “default value” for a type. Read more
Source§

impl Eq for Note

Source§

impl Hash for Note

Source§

fn hash<__H: Hasher>(&self, state: &mut __H)

Feeds this value into the given Hasher. Read more
1.3.0 · Source§

fn hash_slice<H>(data: &[Self], state: &mut H)
where H: Hasher, Self: Sized,

Feeds a slice of this type into the given Hasher. Read more
Source§

impl PartialEq for Note

Source§

fn eq(&self, other: &Note) -> bool

Tests for self and other values to be equal, and is used by ==.
1.0.0 (const: unstable) · Source§

fn ne(&self, other: &Rhs) -> bool

Tests for !=. The default implementation is almost always sufficient, and should not be overridden without very good reason.
Source§

impl StructuralPartialEq for Note

Auto Trait Implementations§

§

impl Freeze for Note

§

impl RefUnwindSafe for Note

§

impl Send for Note

§

impl Sync for Note

§

impl Unpin for Note

§

impl UnsafeUnpin for Note

§

impl UnwindSafe for Note

Blanket Implementations§

Source§

impl<T> Any for T
where T: 'static + ?Sized,

Source§

fn type_id(&self) -> TypeId

Gets the TypeId of self. Read more
Source§

impl<T> Borrow<T> for T
where T: ?Sized,

Source§

fn borrow(&self) -> &T

Immutably borrows from an owned value. Read more
Source§

impl<T> BorrowMut<T> for T
where T: ?Sized,

Source§

fn borrow_mut(&mut self) -> &mut T

Mutably borrows from an owned value. Read more
Source§

impl<T> CloneToUninit for T
where T: Clone,

Source§

unsafe fn clone_to_uninit(&self, dest: *mut u8)

🔬This is a nightly-only experimental API. (clone_to_uninit)
Performs copy-assignment from self to dest. Read more
Source§

impl<S, T> Duplex<S> for T
where T: FromSample<S> + ToSample<S>,

Source§

impl<T> From<T> for T

Source§

fn from(t: T) -> T

Returns the argument unchanged.

Source§

impl<S> FromSample<S> for S

Source§

fn from_sample_(s: S) -> S

Source§

impl<T, U> Into<U> for T
where U: From<T>,

Source§

fn into(self) -> U

Calls U::from(self).

That is, this conversion is whatever the implementation of From<T> for U chooses to do.

Source§

impl<F, T> IntoSample<T> for F
where T: FromSample<F>,

Source§

fn into_sample(self) -> T

Source§

impl<T> ToOwned for T
where T: Clone,

Source§

type Owned = T

The resulting type after obtaining ownership.
Source§

fn to_owned(&self) -> T

Creates owned data from borrowed data, usually by cloning. Read more
Source§

fn clone_into(&self, target: &mut T)

Uses borrowed data to replace owned data, usually by cloning. Read more
Source§

impl<T, U> ToSample<U> for T
where U: FromSample<T>,

Source§

fn to_sample_(self) -> U

Source§

impl<T, U> TryFrom<U> for T
where U: Into<T>,

Source§

type Error = Infallible

The type returned in the event of a conversion error.
Source§

fn try_from(value: U) -> Result<T, <T as TryFrom<U>>::Error>

Performs the conversion.
Source§

impl<T, U> TryInto<U> for T
where U: TryFrom<T>,

Source§

type Error = <U as TryFrom<T>>::Error

The type returned in the event of a conversion error.
Source§

fn try_into(self) -> Result<U, <U as TryFrom<T>>::Error>

Performs the conversion.