Music Synthesizer written in 100% Python

Warren Postma embed at NOSPAM.geocities.com
Mon Aug 28 11:48:30 EDT 2000


One bit in particular could really use some improvement:

> def pitchhz(note):
>  if note=="A0":
>   return 13.75
>  if note=="A1":
>   return 27.5
[.... snipped tonnes of lines...]
>  if note=="A-10":
>   return 26579.488

Replace pitchhz function with a dictionary.

pitchhz = { "A0": 13.75,
                  "A1": 27.5,
                ....
                    "A-10": 26579.488
                }

Your if/if/if/if/if/if/if repetition is only slightly sub-optimal in C, but
in Python it is wickedly slow.

Then instead of calling pitchhz(note), use pitchhz[note]. Much faster.

Instead, use dictionaries. Very very fast lookups via a hash table. In fact,
I would probably load the pitch table from a file rather than integrating it
into the source code, since it's really a large data table.  You could also
generate the various {well/even/???}-tempered scales using some functions
rather than hard-coding them, and you could then use the program to generate
sounds in non-diatonic scales.

Warren







More information about the Python-list mailing list