drumbeat

package module
v0.0.0-...-a0bce63 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Apr 13, 2019 License: Apache-2.0 Imports: 14 Imported by: 0

README

drumbeat

Drumbeat is a Go library to create/parse drum beat patterns.

GoDoc Go Report Card Coverage Status Build Status

Example

More information and documentation available at GoDoc.

// define a pattern using text:
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.
`)

// convert to MIDI
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()

// generate a PNG visualization
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()

png output

Documentation

Overview

Package drumbeat is a Go library to create/parse drum beat patterns.

Index

Examples

Constants

View Source
const (
	// DefaultPPQN is the default amount of ticks per quarter notes.
	DefaultPPQN = uint16(96)
)

Variables

This section is empty.

Functions

func SaveAsPNG

func SaveAsPNG(w io.Writer, patterns []*Pattern) error

SaveAsPNG converts the patterns into an image.

func ToMIDI

func ToMIDI(w io.WriteSeeker, patterns ...*Pattern) error

ToMIDI converts the passed patterns to a single MIDI file.

func WriteTo

func WriteTo(w io.Writer, patterns ...*Pattern) error

WriteTo serializes the passed patterns and write them to writer.

Types

type GridRes

type GridRes string

GridRes is the resolution of the grid for the pattern

const (
	One4  GridRes = "1/4"
	One8  GridRes = "1/8"
	One16 GridRes = "1/16"
	One32 GridRes = "1/32"
	One64 GridRes = "1/64"
)

func (GridRes) StepsInBeat

func (g GridRes) StepsInBeat() uint64

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

func FromMIDI(r io.Reader) ([]*Pattern, error)

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

func NewFromString(grid GridRes, str string) []*Pattern

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 ReadFrom

func ReadFrom(r io.Reader) ([]*Pattern, error)

ReadFrom reads a serialize drumbeat and returns the patterns

func (*Pattern) ActivePulses

func (p *Pattern) ActivePulses() int

func (*Pattern) Offset

func (p *Pattern) Offset(n int)

Offset offsets the slice of pulses by moving the pulses to the right by n positions.

func (*Pattern) ReAlign

func (p *Pattern) ReAlign()

ReAlign adds the nil steps if the pulses are unbalanced and reorder the steps if needed. This also makes sure we have the right number of pulses to fill full bars.

func (*Pattern) StepSize

func (p *Pattern) StepSize() uint64

StepSize returns the size of a pattern step in ticks given its grid resolution

type Pulse

type Pulse struct {
	Ticks    uint64
	Duration uint16
	Velocity uint8
}

Pulse indicates a drum hit

type Pulses

type Pulses []*Pulse

Pulses is a collection of ordered pulses

func (Pulses) String

func (pulses Pulses) String() string

String implements the stringer interface

Directories

Path Synopsis
cmd
gen

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL