Module sorceress::pattern[][src]

A module for defining patterns.

This module provides an expressive API for describing sequences of events. A Pattern is a collection of events with timing information. Patterns can be embedded into other paterns to form larger sequences or can be combined in parallel to allow complex sections of music to be decomposed into small independent sequences.

Examples

use sorceress::pattern::{sequence, Event, Pattern};

let pattern: Pattern<String> = sequence(|s| {
    s.play(1.0, "hello");
    s.rest(1.0);
    s.parallel(|p| {
        p.play(2.0, "there");
        p.sequence(|s| {
            s.play(1.0, "my");
            s.play(1.0, "friend");
        });
    });
});

let mut events = pattern.into_iter();

assert_eq!(Some(Event::new(1.0, "hello")), events.next());
assert_eq!(Some(Event::rest(1.0)), events.next());
assert_eq!(Some(Event::new(0.0, "there")), events.next());
assert_eq!(Some(Event::new(1.0, "my")), events.next());
assert_eq!(Some(Event::new(1.0, "friend")), events.next());
assert_eq!(None, events.next());

Splitting Up Patterns

If you would like to split up of a large pattern for the sake of readability or reuse, you should create a function that returns a Pattern and embed it into the larger pattern. Prefer this over creating functions that take Sequence or Parallel types as arguments.

use sorceress::pattern::{parallel, Pattern, Sequence};

fn section() -> Pattern<String> {
    parallel(|p| {
        // Do this.
        p.embed(chord());

        // Not this.
        p.sequence(bass);
    })
}

// Do this.
fn chord() -> Pattern<String> {
    parallel(|p| {
        p.play(4.0, "F4");
        p.play(4.0, "A5");
        p.play(4.0, "C5");
    })
}

// Not this.
fn bass(s: &mut Sequence<String>) {
    s.play(1.0, "E2");
    s.play(1.0, "C2");
    s.play(2.0, "F2");
}

Modules

player

Play patterns using the scheduler module.

Structs

Event

An occurrence with timing information.

IntoIter

An iterator that transforms a pattern into a flat sequence of events.

Parallel

A builder for parallel patterns.

Pattern

A collection of events with timing information.

Sequence

A builder for sequential patterns.

Enums

EventOrRest

Represents either the data of an event or a musical rest.

Functions

parallel

Play multiple sequences at the same time.

sequence

Create a new sequence of events.