Saturday, October 13, 2012

Tone Generator with Python, PyGame, SciPy and SciKits AudioLab modules


Project: 

Generate sine wave sounds with drums triggers while changing the pitches with a MIDI controller using Python modules some fancy IF statements.

Baby Step:

Read MIDI events and output changing sine wave sounds

Progress:

Working but laggy, see code below and here http://pastebin.com/VTBQxjQm


# f is the frequency of the tone in Hertz,
# fs is the sampling rate, and
# T is the length of the tone in seconds.
import scikits.audiolab
import scipy
import pygame
import pygame.midi
from pygame.locals import *
pygame.mixer.pre_init(44100,-16,2,2048)
pygame.init()
pygame.midi.init()

f = 440
fs = 11025 #22050
T = 0.1
x = scipy.cos((2*scipy.pi*f/fs)*scipy.arange(fs*T))
scikits.audiolab.play(x, fs)


for i in range( pygame.midi.get_count() ):
    r = pygame.midi.get_device_info(i)
    (interf, name, input, output, opened) = r
    in_out = ""
    if input:
        in_out = "(input)"
    if output:
        in_out = "(output)"
    print ("%2i: interface :%s:, name :%s:, opened :%s:  %s" % (i, interf, name, opened, in_out))

mydevicenumber=raw_input('See list above. Please enter the number for your midi input device: ')
print "you chose " + mydevicenumber + ''
reserved_channel_0 = pygame.mixer.Channel(0)
my_font = pygame.font.SysFont("Arial",104)

pygame.fastevent.init()
event_get = pygame.fastevent.get
event_post = pygame.fastevent.post
input_id = int(mydevicenumber)
i = pygame.midi.Input( input_id )
pygame.display.set_caption("midi to sine wave sound generator")
screen = pygame.display.set_mode((800, 600), RESIZABLE, 32)

print "starting"
going = True
while going:
    events = event_get()
    for e in events:
        if e.type in [QUIT]:
            going = False
    if i.poll():
        midi_events = i.read(10)
        #print "full midi_events " + str(midi_events)
        #print "my midi note is " + str(midi_events[0][0][1])
        if str(midi_events[0][0][2]) != "0":
            mymidinote = str(midi_events[0][0][1])
            mymidinumber = int(mymidinote)
            mymidinumber = mymidinumber + 0.0   #types matter here!  
            #freq = 440*(2**((m-69)/12))
            f = 440*(2**((mymidinumber-69)/12))
            x = scipy.cos((2*scipy.pi*f/fs)*scipy.arange(fs*T))
            print "before (fast here)"
            scikits.audiolab.play(x, fs)
            print "after (laggy here)"
            #print "on event"
            #print "freq = " + str(f)
        if str(midi_events[0][0][2]) == "0":
            mymidinote = "0"
            #print "off event"
        #convert them into pygame events.
        midi_evs = pygame.midi.midis2events(midi_events, i.device_id)
        for m_e in midi_evs:
            event_post( m_e )
del i
exit()

2 comments:

Anonymous said...

… Unbelievable , but I just found software which can do all hard work promoting your bgryderclock.blogspot.com website on complete autopilot - building backlinks and getting your website on top of Google and other search engines 1st pages, so your site finally can get laser targeted qualified traffic, and so you can get lot more visitors for your website.

YEP, that’s right, there’s this little known website which shows you how to get to the top 10 of Google and other search engines guaranteed.

I used it and in just 7 days… got floods of traffic to my site...

…Well check out the incredible results for yourself -
http://magic-traffic-software.com

I’m not trying to be rude here, but I believe when you find something that finally works you should share it…

…so that’s what I’m doing today, sharing it with you:

http://magic-traffic-software.com

Take care - your friend Jennifer

Anonymous said...

After getting more than 10000 visitors/day to my website I thought your bgryderclock.blogspot.com website also need unstoppable flow of traffic...

Use this BRAND NEW software and get all the traffic for your website you will ever need ...

= = > > http://get-massive-autopilot-traffic.com

In testing phase it generated 867,981 visitors and $540,340.

Then another $86,299.13 in 90 days to be exact. That's $958.88 a
day!!

And all it took was 10 minutes to set up and run.

But how does it work??

You just configure the system, click the mouse button a few
times, activate the software, copy and paste a few links and
you're done!!

Click the link BELOW as you're about to witness a software that
could be a MAJOR turning point to your success.

= = > > http://get-massive-autopilot-traffic.com

bgryderclock micro-blog