./../../src/cm.lsp

## sc/cm [ Modules ]

[ Top ] [ Modules ]

NAME

     cm

     File:             cm.lsp

     Class Hierarchy:  none (no classes defined)

     Version:          1.0.0-beta1

     Project:          slippery chicken (algorithmic composition)

     Purpose:          Definition of common-music related and other functions
                       like transposition of notes/chords, enharmonic
                       equivalents etc.

     Author:           Michael Edwards: m@michael-edwards.org

     Creation date:    1st March 2001

     $$ Last modified: 21:06:48 Mon May 14 2012 BST

     SVN ID: $Id: cm.lsp 1764 2012-05-17 11:49:59Z medward2 $

## cm/degree-to-note [ Functions ]

[ Top ] [ cm ] [ Functions ]

FUNCTION

     Get the specified scale degree number as a note-name pitch symbol within
     the current scale. An optional argument allows the user to specify that the
     scale degree number should be used to get the note-name pitch from a
     different scale.

ARGUMENTS

     An integer that is a scale degree number.

OPTIONAL ARGUMENTS

     - The scale from which the note-name pitch symbol associated with the
       specified scale degree is to be drawn. As this is a cm function, the cm
       tuning names must be used; i.e., cm::*chromatic-scale*.

RETURN VALUE

     A note-name pitch symbol.

EXAMPLE

    (in-scale :chromatic)
    (degree-to-note 127)

    => G9

    (in-scale :twelfth-tone)
    (degree-to-note 127)

    => ATS0

    (in-scale :quarter-tone)
    (degree-to-note 127)

    => EQF4

SYNOPSIS

    (defun degree-to-note (degree &optional (scale cm::*scale*))

## cm/degrees-per-octave [ Functions ]

[ Top ] [ cm ] [ Functions ]

FUNCTION

     Return the number of scale degrees in the span of one octave within the
     current tuning system.

ARGUMENTS

     - No arguments.

RETURN VALUE

     - An integer that is the number of scale degrees in each octave.

EXAMPLE

    (in-scale :chromatic)
    (degrees-per-octave)

    => 12

    (in-scale :quarter-tone)
    (degrees-per-octave)

    => 24

SYNOPSIS

    (defun degrees-per-octave ()

## cm/degrees-per-semitone [ Functions ]

[ Top ] [ cm ] [ Functions ]

FUNCTION

     Get the number of scale degrees per equal-tempered semitone in the current
     tuning scale.

ARGUMENTS

     - No arguments

OPTIONAL ARGUMENTS

     - The scale for which the number of degrees per semitone is to be
       retrieved.

RETURN VALUE

     An integer.

EXAMPLE

    (in-scale :chromatic)
    (degrees-per-semitone)

    => 1

    (in-scale :twelfth-tone)
    (degrees-per-semitone)

    => 6

    (in-scale :quarter-tone)
    (degrees-per-semitone)

    => 2

SYNOPSIS

    (defun degrees-per-semitone (&optional (scale cm::*scale*))

## cm/degrees-to-notes [ Functions ]

[ Top ] [ cm ] [ Functions ]

FUNCTION

     NB: If the specified scale-degree number within the current scale would
         result in pitch outside of the maximum MIDI pitch range for that tuning
         (chromatic: C-1 to B10; quarter-tone: C-1 to BQS10; twelfth-tone: C-1
         to CTF11), the function will return an error.

ARGUMENTS

     An integer that is a scale degree number in the current tuning.

RETURN VALUE

     A list of note-name pitch symbols.

EXAMPLE

    (in-scale :chromatic)
    (degrees-to-notes '(0 143 116 127 38))

    => (C-1 B10 AF8 G9 D2)

    (in-scale :twelfth-tone)
    (degrees-to-notes '(0 144 116 127 38 287 863))

    => (C-1 C1 GSS0 ATS0 FSSS-1 CTF3 CTF11)

    (in-scale :quarter-tone)
    (degrees-to-notes '(0 144 116 127 38 287))

    => (C-1 C5 BF3 EQF4 G0 BQS10)

SYNOPSIS

    (defun degrees-to-notes (degrees)

## cm/freq-to-degree [ Functions ]

[ Top ] [ cm ] [ Functions ]

FUNCTION

     Get the scale degree of the specified frequency in Hertz within the current
     scale.

     NB: This method will return fractional scale degrees.

ARGUMENTS

     A frequncy in Hertz.

OPTIONAL ARGUMENTS

     - The scale in which to find the corresponding scale degree.

RETURN VALUE

     A scale degree number. This may be a decimal number.

EXAMPLE

    (in-scale :chromatic)
    (freq-to-degree 423)

    => 68.317856

    (in-scale :twelfth-tone)
    (freq-to-degree 423)

    => 409.9071

    (in-scale :quarter-tone)
    (freq-to-degree 423)

    => 136.63571

SYNOPSIS

    (defun freq-to-degree (degree &optional (chromatic-scale nil))

## cm/freq-to-note [ Functions ]

[ Top ] [ cm ] [ Functions ]

FUNCTION

     Get the note-name pitch equivalent of the specified frequency, rounded to
     the nearest scale degree of the current scale.

ARGUMENTS

     A number that is a frequency in Hertz.

OPTIONAL ARGUMENTS

     - The scale in which the note-name pitch equivalent is to be sought.

RETURN VALUE

     A note-name pitch symbol.

EXAMPLE

    (in-scale :chromatic)
    (freq-to-note 423)

    => AF4

    (in-scale :twelfth-tone)
    (freq-to-note 423)

    => GSSS4

    (in-scale :quarter-tone)
    (freq-to-note 423)

    => AQF4

SYNOPSIS

    (defun freq-to-note (freq &optional (scale cm::*scale*))

## cm/get-pitch-bend [ Functions ]

[ Top ] [ cm ] [ Functions ]

FUNCTION

     Get the MIDI pitch-bend value necessary for application to a MIDI pitch in
     order to achieve the specified frequency.

     NB: This will always return a positive value between 0.0 and 1.0, as
         slippery-chicken always applies pitch-bends upwards from the nearest
         chromatic note below the specified frequency.

     NB: This value will be the same in all tuning scales.

ARGUMENTS

     A frequency in Hertz.

RETURN VALUE

     A two-digit decimal number that is the pitch-bend value required to achieve
     the specified frequency in MIDI.

EXAMPLE

    (get-pitch-bend 423)

    => 0.32

SYNOPSIS

    (defun get-pitch-bend (freq)

## cm/in-scale [ Functions ]

[ Top ] [ cm ] [ Functions ]

FUNCTION

     Set the global scale (tuning) for the current slippery-chicken
     environment. Current options are :chromatic, :quarter-tone or
     :twelfth-tone. See the file cm-load.lsp for specifications and the html
     manual page "More about note-names and scales" for more details on use.

ARGUMENTS

     - A scale (tuning) designation.

RETURN VALUE

     Lisp REPL feedback on the tuning now set.

EXAMPLE

    (in-scale :chromatic)

    => #<tuning "chromatic-scale">

    (in-scale :quarter-tone)

    => #<tuning "quarter-tone">

    (in-scale :twelfth-tone)

    => #<tuning "twelfth-tone">

SYNOPSIS

    (defun in-scale (scale)

## cm/midi-file-high-low [ Functions ]

[ Top ] [ cm ] [ Functions ]

DATE

     30-Dec-2010

FUNCTION

     Print the highest and lowest pitch in a specified MIDI file as a MIDI note
     number.

     NB: This is a Common Music function and as such must be called with the
         package qualifier cm:: if used within slippery chicken.

ARGUMENTS

     - The path (including the name) to the MIDI file.

OPTIONAL ARGUMENTS

     - An integer or NIL to indicate which track in the specified MIDI file is
       to be accessed. If NIL, all tracks will be accessed. NB: CM (and
       therefore slippery-chicken too) generates some MIDI files by writing each
       channel to a different track, so the "track" would seem synonymous with
       "channel" here.

RETURN VALUE

     Two integer values (using the values function) that are the highest and
     lowest pitches in the specified MIDI file.

EXAMPLE

    (cm::midi-file-high-low "/tmp/multi-ps.mid")

    => 72, 60

SYNOPSIS

    (defun midi-file-high-low (file &optional track)

## cm/midi-file-one-note [ Functions ]

[ Top ] [ cm ] [ Functions ]

FUNCTION

     write all midi notes in the file out to a new file using the single note
     <note> and <channel>.

ARGUMENTS

     - the file path as a string
     - the note to write (symbol or midi note number)
     - the new channel to write note to (counting from 1)

OPTIONAL ARGUMENTS

     - the old channel: if given, only notes on this channel will be written
       (counting from 1).

RETURN VALUE

     the path to the new file

EXAMPLE

    ;;; (cm::midi-file-one-note
    ;;;         "/Users/medward2/mus/altogether/altogether.mid" 'c4 9)

SYNOPSIS

    (defun midi-file-one-note (file note channel &optional old-channel)

## cm/midi-to-degree [ Functions ]

[ Top ] [ cm ] [ Functions ]

FUNCTION

     Convert the specified MIDI note number to the degree number of the current
     scale.

ARGUMENTS

     - A MIDI note number.

RETURN VALUE

     - An integer that is the scale-degree equivalent of the specified MIDI note
       number in the current scale.

EXAMPLE

    (in-scale :chromatic)
    (midi-to-degree 64)

    => 64

    (in-scale :twelfth-tone)
    (midi-to-degree 64)

    => 384

    (in-scale :quarter-tone)
    (midi-to-degree 64)

    => 128

SYNOPSIS

    (defun midi-to-degree (midi-note)

## cm/midi-to-freq [ Functions ]

[ Top ] [ cm ] [ Functions ]

FUNCTION

     Get the frequency equivalent in Hertz to the specified MIDI note number.

ARGUMENTS

     - A number (can be a decimal) that is a MIDI note number.

RETURN VALUE

     A decimal number that is a frequency in Hertz.

EXAMPLE

    (midi-to-freq 67)

    => 391.99542

    (midi-to-freq 67.9)

    => 412.91272

SYNOPSIS

    (defun midi-to-freq (midi-note)

## cm/midi-to-note [ Functions ]

[ Top ] [ cm ] [ Functions ]

FUNCTION

     Get the note-name pitch symbol equivalent of the specified MIDI note
     number.

ARGUMENTS

     - An integer that is a MIDI note number.

RETURN VALUE

     A note-name pitch symbol.

EXAMPLE

    (midi-to-note 67)

    => G4

SYNOPSIS

    (defun midi-to-note (midi-note)

## cm/note-to-degree [ Functions ]

[ Top ] [ cm ] [ Functions ]

FUNCTION

     Get the scale degree number of the specified note-name pitch symbol within
     the current scale.

ARGUMENTS

     - A note-name pitch symbol.

OPTIONAL ARGUMENTS

     - The scale in which to find the scale-degree of the specified pitch.

RETURN VALUE

     An integer that is a scale degree in the current scale.

EXAMPLE

    (in-scale :chromatic)
    (note-to-degree 'AF4)

    => 68

    (in-scale :twelfth-tone)
    (note-to-degree 'AF4)

    => 408

    (in-scale :quarter-tone)
    (note-to-degree 'AF4)

    => 136

SYNOPSIS

    (defun note-to-degree (note &optional (scale cm::*scale*))

## cm/note-to-freq [ Functions ]

[ Top ] [ cm ] [ Functions ]

FUNCTION

     Get the frequency in Hertz of the specified note-name pitch symbol.

ARGUMENTS

     - A note-name pitch symbol.

RETURN VALUE

     A frequency in Hertz.

EXAMPLE

    (in-scale :chromatic)
    (note-to-freq 'AF4)

    => 415.3047

    (in-scale :twelfth-tone)
    (note-to-freq 'GSSS4)

    => 423.37845

    (in-scale :quarter-tone)
    (note-to-freq 'AQF4)

    => 427.47403

SYNOPSIS

    (defun note-to-freq (note)

## cm/note-to-midi [ Functions ]

[ Top ] [ cm ] [ Functions ]

FUNCTION

     Get the MIDI note number equivalent for a chromatic note-name pitch
     symbol.

ARGUMENTS

     - A chromatic note-name pitch symbol.

RETURN VALUE

     An integer.

EXAMPLE

    (note-to-midi 'g4)

    => 67

SYNOPSIS

    (defun note-to-midi (midi-note)

## cm/parse-midi-file [ Functions ]

[ Top ] [ cm ] [ Functions ]

FUNCTION

     Print the MIDI event slots in the specified file to the Lisp listener.

     NB: This is a Common Music function and as such must be called with the
         package qualifier cm:: if used within slippery chicken.

ARGUMENTS

     - The path (including the file name) to the MIDI file.

OPTIONAL ARGUMENTS

     - An integer or NIL to indicate which track in the specified MIDI file is
       to be accessed. If NIL, all tracks will be accessed. NB: CM (and
       therefore slippery-chicken too) generates some MIDI files by writing each
       channel to a different track, so the "track" would seem synonymous with
       "channel" here.

RETURN VALUE

     The CM data for the MIDI events in the specified file.

EXAMPLE

    (cm::parse-midi-file "/tmp/multi-ps.mid")

    =>
    Event #i(midi-tempo-change time 0.0 usecs 1000000)
    Event #i(midi time 0.0 keynum 72 duration 0.5 amplitude 0.6929134 channel 0)
    Event #i(midi time 0.0 keynum 65 duration 0.5 amplitude 0.6929134 channel 1)
    Event #i(midi time 0.0 keynum 60 duration 0.5 amplitude 0.6929134 channel 2)
    Event #i(midi-time-signature time 0.0 numerator 4 denominator 4 clocks 24 32nds 8)
    Event #i(midi-time-signature time 0.0 numerator 4 denominator 4 clocks 24 32nds 8)
    Event #i(midi-time-signature time 0.0 numerator 4 denominator 4 clocks 24 32nds 8)
    Event #i(midi-tempo-change time 0.0 usecs 1000000)
    Event #i(midi-tempo-change time 0.0 usecs 1000000)
    Event #i(midi-tempo-change time 0.0 usecs 1000000)
    Event #i(midi time 0.5 keynum 67 duration 0.5 amplitude 0.6929134 channel 2)
    Event #i(midi time 0.5 keynum 71 duration 0.5 amplitude 0.6929134 channel 0)
    Event #i(midi time 0.5 keynum 64 duration 0.5 amplitude 0.6929134 channel 1)
    Event #i(midi time 1.0 keynum 60 duration 0.5 amplitude 0.6929134 channel 2)
    Event #i(midi time 1.0 keynum 72 duration 0.5 amplitude 0.6929134 channel 0)
    Event #i(midi time 1.0 keynum 62 duration 0.5 amplitude 0.6929134 channel 1)
    Event #i(midi time 1.5 keynum 67 duration 0.5 amplitude 0.6929134 channel 2)
    Event #i(midi time 1.5 keynum 71 duration 0.5 amplitude 0.6929134 channel 0)
    Event #i(midi time 1.5 keynum 64 duration 0.5 amplitude 0.6929134 channel 1)
    Event #i(midi time 2.0 keynum 60 duration 0.5 amplitude 0.6929134 channel 2)
    Event #i(midi time 2.0 keynum 72 duration 0.5 amplitude 0.6929134 channel 0)
    Event #i(midi time 2.0 keynum 65 duration 0.5 amplitude 0.6929134 channel 1)
    Event #i(midi time 2.5 keynum 67 duration 0.5 amplitude 0.6929134 channel 2)
    Event #i(midi time 2.5 keynum 71 duration 0.5 amplitude 0.6929134 channel 0)
    Event #i(midi time 2.5 keynum 64 duration 0.5 amplitude 0.6929134 channel 1)
    Event #i(midi time 3.0 keynum 60 duration 0.5 amplitude 0.6929134 channel 2)
    Event #i(midi time 3.0 keynum 72 duration 0.5 amplitude 0.6929134 channel 0)
    Event #i(midi time 3.0 keynum 62 duration 0.5 amplitude 0.6929134 channel 1)
    Event #i(midi time 3.5 keynum 67 duration 0.5 amplitude 0.6929134 channel 2)
    Event #i(midi time 3.5 keynum 71 duration 0.5 amplitude 0.6929134 channel 0)
    Event #i(midi time 3.5 keynum 64 duration 0.5 amplitude 0.6929134 channel 1)
    31 events total

SYNOPSIS

    (defun parse-midi-file (file &optional track)