Documentation
¶
Overview ¶
Package drumbeat is a Go library to create/parse drum beat patterns.
Index ¶
Examples ¶
Constants ¶
const ( // DefaultPPQN is the default amount of ticks per quarter notes. DefaultPPQN = uint16(96) )
Variables ¶
This section is empty.
Functions ¶
Types ¶
type GridRes ¶
type GridRes string
GridRes is the resolution of the grid for the pattern
func (GridRes) StepsInBeat ¶
StepsInBeat returns the number of steps to fill a beat
type Pattern ¶
type Pattern struct { // Name of the pattern or instrument Name string // Steps are the values for each step 0.0 means no pulse, a pulse greater // than 0 indicates the duration in beats of the pulse Pulses Pulses // Key indicates the MIDI key this pattern should be triggering. Useful when // converting to MIDI Key int // PPQN is the amount of ticks per quarter note. PPQN uint16 // Grid is the resolution of the pattern Grid GridRes // contains filtered or unexported fields }
Pattern represent the content of a drum pattern/beat.
func FromMIDI ¶
FromMIDI converts the content of a MIDI file into drum beat patterns. Note that this is for drum patterns only, expect the unexpected if you use non drum sequences.
Example ¶
package main import ( "fmt" "log" "os" "github.com/mattetti/drumbeat" ) func main() { f, err := os.Open("fixtures/singlePattern.mid") if err != nil { log.Fatal(err) } defer f.Close() patterns, err := drumbeat.FromMIDI(f) if err != nil { log.Fatalf("Failed to parse the MIDI file - %v", err) } // Default to 1/16th grid fmt.Printf("%s: %s", patterns[0].Name, patterns[0].Pulses) }
Output: C1: x.......x.......
func NewFromString ¶
NewFromString converts a string where `x` are converted into active pulses. The first argument is the resolution of the grid so we can define how many steps fit in a bar. Default velocity is 0.9
Multiple patterns can be provided if separated by a semi colon: `;`.
Example ¶
package main import ( "fmt" "log" "os" "github.com/mattetti/drumbeat" ) func main() { patterns := drumbeat.NewFromString(drumbeat.One16, ` [kick] {C1} x.x.......xx...x x.x.....x......x; [snare] {D1} ....x.......x... ....x.......x...; [hihat] {F#1} x.x.x.x.x.x.x.x. x.x.x.x.x.x.x.x. `) f, err := os.Create("drumbeat.mid") if err != nil { log.Println("something wrong happened when creating the MIDI file", err) os.Exit(1) } if err := drumbeat.ToMIDI(f, patterns...); err != nil { log.Fatal(err) } f.Close() fmt.Println("drumbeat.mid generated") imgf, err := os.Create("drumbeat.png") if err != nil { log.Println("something wrong happened when creating the image file", err) os.Exit(1) } if err := drumbeat.SaveAsPNG(imgf, patterns); err != nil { log.Fatal(err) } imgf.Close() fmt.Println("drumbeat.png generated") os.Remove(f.Name()) os.Remove(imgf.Name()) }
Output: drumbeat.mid generated drumbeat.png generated
func (*Pattern) ActivePulses ¶
func (*Pattern) Offset ¶
Offset offsets the slice of pulses by moving the pulses to the right by n positions.