Most Used Commands

Todd Millecam tyggna at gmail.com
Wed Jun 26 13:08:32 MDT 2013


On Wed, Jun 26, 2013 at 12:56 PM, Ryan Simpkins <plug at ryansimpkins.com>
 wrote:

> On Wed, June 26, 2013 09:58, Todd Millecam wrote:
> [snip]
> >  2  33 beep  <-  I was tuning a piano, lost my kit, tried this as a
> > replacement.  Ended up writing a python script instead
> [snip]
>
> Am I the only one wanting to know how you tuned a piano using Python?
> Sounds
> interesting. The Google only returns Monty Python skits when I search
> "python
> piano."
>
> -Ryan
>

since you asked. . .  It's not perfect or anything, but works well enough
for me.

#!/usr/bin/env python2
import pygame, numpy, math

duration = 3.0          # in seconds
sample_rate = 44100
bits = 16
n_samples = int(round(duration*sample_rate))

def makeAndPlay(hertz):
  try:
    pygame.mixer.init(frequency = sample_rate, size = -bits, channels = 2)
    buf = numpy.zeros((n_samples, 2), dtype = numpy.int16)
    max_sample = 2**(bits - 1) - 1
    for s in range(n_samples):
        t = float(s)/sample_rate        # time in seconds
        buf[s][0] = int(round(max_sample*math.sin(2*math.pi*hertz*t)))
        buf[s][1] = int(round(max_sample*0.5*math.sin(2*math.pi*hertz*t)))
    sound = pygame.sndarray.make_sound(buf)
    while True:
      try:
        sound.play()
      except KeyboardInterrupt:
        break
  except KeyboardInterrupt:
    pass
  finally:
    pygame.mixer.quit()

def main():
  while True:
    print("enter a frequency: ")
    freq = raw_input()
    if freq == "q":
      break
    else:
      try:
        pitch = notes[freq[0].upper() + freq[1:]]
      except:
        pitch = float(freq)
      makeAndPlay(pitch)

notes = {
"C0"  : 16.35,
"C#0" : 17.32,
"Db0"  : 17.32,
<trimmed />
}


-- 
Todd Millecam


More information about the PLUG mailing list