Advertisement
Guest User

Tone Generator with Pygame, SciKits.AudioLab and SciPy

a guest
Oct 13th, 2012
373
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 2.43 KB | None | 0 0
  1.  
  2.  
  3. # f is the frequency of the tone in Hertz,
  4. # fs is the sampling rate, and
  5. # T is the length of the tone in seconds.
  6.  
  7. import scikits.audiolab
  8. import scipy
  9. import pygame
  10. import pygame.midi
  11. from pygame.locals import *
  12. pygame.mixer.pre_init(44100,-16,2,2048)
  13. pygame.init()
  14. pygame.midi.init()
  15.  
  16.  
  17. f = 440
  18. fs = 11025 #22050
  19. T = 0.1
  20. x = scipy.cos((2*scipy.pi*f/fs)*scipy.arange(fs*T))
  21. scikits.audiolab.play(x, fs)
  22.  
  23.  
  24.  
  25. for i in range( pygame.midi.get_count() ):
  26.     r = pygame.midi.get_device_info(i)
  27.     (interf, name, input, output, opened) = r
  28.     in_out = ""
  29.     if input:
  30.         in_out = "(input)"
  31.     if output:
  32.         in_out = "(output)"
  33.  
  34.     print ("%2i: interface :%s:, name :%s:, opened :%s:  %s" % (i, interf, name, opened, in_out))
  35.  
  36. mydevicenumber=raw_input('See list above. Please enter the number for your midi input device: ')
  37.  
  38. print "you chose " + mydevicenumber + ''
  39.  
  40. reserved_channel_0 = pygame.mixer.Channel(0)
  41.  
  42. my_font = pygame.font.SysFont("Arial",104)
  43.  
  44. pygame.fastevent.init()
  45. event_get = pygame.fastevent.get
  46. event_post = pygame.fastevent.post
  47.  
  48. input_id = int(mydevicenumber)
  49. i = pygame.midi.Input( input_id )
  50.  
  51. pygame.display.set_caption("midi to sine wave sound generator")
  52. screen = pygame.display.set_mode((800, 600), RESIZABLE, 32)
  53.  
  54. print "starting"
  55.  
  56. going = True
  57.  
  58. while going:
  59.  
  60.     events = event_get()
  61.     for e in events:
  62.         if e.type in [QUIT]:
  63.             going = False
  64.  
  65.     if i.poll():
  66.         midi_events = i.read(10)
  67.         #print "full midi_events " + str(midi_events)
  68.         #print "my midi note is " + str(midi_events[0][0][1])
  69.  
  70.         if str(midi_events[0][0][2]) != "0":
  71.             mymidinote = str(midi_events[0][0][1])
  72.             mymidinumber = int(mymidinote)
  73.             mymidinumber = mymidinumber + 0.0   #types matter here!    
  74.  
  75.             #freq = 440*(2**((m-69)/12))
  76.             f = 440*(2**((mymidinumber-69)/12))
  77.             x = scipy.cos((2*scipy.pi*f/fs)*scipy.arange(fs*T))
  78.  
  79.             print "before"
  80.             scikits.audiolab.play(x, fs)
  81.             print "after"
  82.  
  83.             #print "on event"
  84.             #print "freq = " + str(f)
  85.  
  86.         if str(midi_events[0][0][2]) == "0":
  87.             mymidinote = "0"
  88.             #print "off event"
  89.  
  90.         #convert them into pygame events.
  91.         midi_evs = pygame.midi.midis2events(midi_events, i.device_id)
  92.  
  93.         for m_e in midi_evs:
  94.             event_post( m_e )
  95. del i
  96. exit()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement