[PD] Creating music notation with GEM

Frank Barknecht fbar at footils.org
Fri Jul 18 10:57:33 CEST 2008


Hallo,
David Powers hat gesagt: // David Powers wrote:

> I'm wondering if I should use something like PyExt to do some of the
> mapping, as I don't know of any simple way to do hash dictionaries in
> PD itself. As a test last night, I built a simple abstraction to take
> notes C C# D etc. and output pitch numbers 0-11, and i found it quite
> tedius to do compared to a function in code such as (this is in PHP
> because that is what I do all day long at my day job):
> function note2number($note) {
>  $num = array ('C'=>0,'C#'=>1,'Db'=>1);
>  return $num[$note];
> }

Well, Pd doesn't have proper hash dictionaries. OTOH Lua has hash
dictionaries as its *only* data structure (called "tables" in Lua), so
to me it's an ideal  complement to Pd.

Attached is a quick [note2num] object for pdlua. It also handles
multiple modifiers like "Gbbbb" or mixed ones like "Gb#bbb#b". ;)

Ciao
-- 
 Frank Barknecht                                     _ ______footils.org__
-------------- next part --------------
A non-text attachment was scrubbed...
Name: note2num-help.pd
Type: application/puredata
Size: 433 bytes
Desc: not available
URL: <http://lists.puredata.info/pipermail/pd-list/attachments/20080718/d1421d75/attachment.bin>
-------------- next part --------------
--[[

note2num: convert note names like "C#" or "Bb" or "Ab#bb" to midi note
numbers

-- fbar 2008

--]]

local M = pd.Class:new():register("note2num")

local n2n = {
    c = 0,
    d = 2,
    e = 4,
    f = 5,
    g = 7,
    a = 9,
    b = 11,
}

function M:initialize(name, atoms)
    self.inlets = 1
    self.outlets = 1 
    return true
end

function M:in_1(sel, atoms)
    local s
    if not atoms[1] then
        s = sel
    else
        s = atoms[1]
    end
    assert(type(s) == "string", "only symbols allowed")
    s = string.lower(s)

    local n, mods = s:match("^([cdefgab])([#b]*)$")
    local val = n2n[n]
    
    if mods and val then
        local _, b = string.gsub(mods, "b", "b")
        local _, h = string.gsub(mods, "#", "#")
        val = val + h - b
    end

    if val then
        self:outlet(1, "float", {val})
    end
end



More information about the Pd-list mailing list