Python Instrument Program – Version 1.0

import pygame
from pygame.locals import *
import sys
import math
import numpy

######## Visual variables – screen settings etc. ########
# set up a bunch of constants
WHITE = (255, 255, 255)
DARKRED = (128, 0, 0)
RED = (255, 0, 0)
BLACK = ( 0, 0, 0)
GREEN = ( 0, 255, 0)
BLUE = ( 0, 0, 255)

BGCOLOR = (97, 97, 97)

image = pygame.image.load(“KeyboardInstructions.bmp”) #Load image into the ‘image’ variable
screen = pygame.display.set_mode()

background = pygame.Surface(screen.get_size())
background = background.convert()
background.fill(BGCOLOR)

WINDOWWIDTH = screen.get_width() # width of the program’s window, in pixels
WINDOWHEIGHT = screen.get_height() # height in pixels
WIN_CENTERX = int(WINDOWWIDTH / 2) # the midpoint for the width of the window
WIN_CENTERY = int(WINDOWHEIGHT / 2) # the midpoint for the height of the window

FPS = 160 # frames per second to run at

AMPLITUDE = 80 # how many pixels tall the waves with rise/fall.

# standard pygame setup code
pygame.init()
FPSCLOCK = pygame.time.Clock()
DISPLAYSURF = pygame.display.set_mode((WINDOWWIDTH, WINDOWHEIGHT), pygame.RESIZABLE)

pygame.display.set_caption(‘For Nibbles!!’)
fontObj = pygame.font.Font(‘freesansbold.ttf’, 16)

pause = False

xPos = 0
step = 0 # the current input f

posRecord = {‘sin': [], ‘square': []} # keeps track of the ball positions for drawing the waves

DISPLAYSURF.fill(BGCOLOR)

# making text Surface and Rect objects for wavetype labels
squarelabelsurf = fontObj.render(‘Wavetype: Square’, True, WHITE,BGCOLOR)
squarelabelrect = squarelabelsurf.get_rect()
squarelabelrect.left = WINDOWWIDTH – 250
squarelabelrect.top = 50

sinelabelsurf = fontObj.render(‘Wavetype: Sine’, True, WHITE, BGCOLOR)
sinelabelrect = sinelabelsurf.get_rect()
sinelabelrect.left = WINDOWWIDTH – 250
sinelabelrect.top = 50

freqlabelsurf = fontObj.render(‘Frequency: None’, True, WHITE, BGCOLOR)
freqlabelrect = sinelabelsurf.get_rect()
freqlabelrect.left = WINDOWWIDTH – 250
freqlabelrect.top = 70

yPosSquare = AMPLITUDE # starting position

#———————————————————————-
# functions
#———————————————————————-

def SineWave(freq=1000, volume=16000, length=1):

num_steps = length * SAMPLE_RATE
s = []

for n in range(num_steps):
value = int(math.sin(n * freq * (6.28318/SAMPLE_RATE) * length) * volume)
s.append( [value, value] )

return numpy.array(s)

def SquareWave(freq=1000, volume=10000, length=1):

num_steps = length * SAMPLE_RATE
s = []

length_of_plateau = SAMPLE_RATE / (2*freq)
x = length_of_plateau

state = 1
next_plateau_end = length_of_plateau

for n in range(num_steps):
value = state * volume
s.append( [value, value] )

if n >= next_plateau_end:
next_plateau_end += length_of_plateau
state *= -1

return numpy.array(s)

def MakeSound(arr):
return pygame.sndarray.make_sound(arr)

def MakeSineWave(freq=1000):
return MakeSound(SineWave(freq))

def MakeSquareWave(freq=1000):
return MakeSound(SquareWave(freq))

#———————————————————————-
# main program
#———————————————————————-

######## Audial code – generating sound on key press ########
SAMPLE_RATE = 22050 # This many array entries == 1 second of sound.

#For changing the sound of the note (on ENTER press)
SINE_WAVE_TYPE = ‘Sine’
SQUARE_WAVE_TYPE = ‘Square’

#Creating a dictionary for the toggle
sound_types = {SINE_WAVE_TYPE:SQUARE_WAVE_TYPE, SQUARE_WAVE_TYPE:SINE_WAVE_TYPE}

#Default wave type is the sine wave
current_type = SINE_WAVE_TYPE

#Determines which frequency is assigned to which key for the sine wave
current_played_sine = { ‘z': MakeSineWave(130.81), ‘z#': MakeSineWave(138.59), \
‘x': MakeSineWave(146.83), ‘x#': MakeSineWave(155.56), ‘c': MakeSineWave(164.81), \
‘c#': MakeSineWave(174.61), ‘v': MakeSineWave(174.61), ‘v#': MakeSineWave(185.00), \
‘b': MakeSineWave(196.00), ‘b#': MakeSineWave(207.65), ‘n': MakeSineWave(220.00), \
‘n#': MakeSineWave(233.08), ‘m': MakeSineWave(246.94), ‘m#': MakeSineWave(261.63), \
‘,': MakeSineWave(261.63), ‘,#': MakeSineWave(277.18), ‘a': MakeSineWave(261.63), \
‘a#': MakeSineWave(277.18), ‘s': MakeSineWave(293.66), ‘s#': MakeSineWave(311.13), \
‘d': MakeSineWave(329.63), ‘d#': MakeSineWave(349.23), ‘f': MakeSineWave(349.23), \
‘f#': MakeSineWave(369.99), ‘g': MakeSineWave(392.00), ‘g#': MakeSineWave(415.30), \
‘h': MakeSineWave(440.00), ‘h#': MakeSineWave(466.16), ‘j': MakeSineWave(493.88), \
‘j#': MakeSineWave(523.25), ‘k': MakeSineWave(523.25), ‘k#': MakeSineWave(554.37), \
‘q': MakeSineWave(523.25), ‘q#': MakeSineWave(554.37), ‘w': MakeSineWave(587.33), \
‘w#': MakeSineWave(622.25), ‘e': MakeSineWave(659.25), ‘e#': MakeSineWave(698.46), \
‘r': MakeSineWave(698.46), ‘r#': MakeSineWave(739.99), ‘t': MakeSineWave(783.99), \
‘t#': MakeSineWave(830.61), ‘y': MakeSineWave(880.00), ‘y#': MakeSineWave(932.33), \
‘u': MakeSineWave(987.77), ‘u#': MakeSineWave(1046.50), ‘i': MakeSineWave(1046.50), \
‘i#': MakeSineWave(1108.73)}

#Determines which frequency is assigned to which key for the square wave
current_played_square = { ‘z': MakeSquareWave(130.81), ‘z#': MakeSquareWave(138.59), \
‘x': MakeSquareWave(146.83), ‘x#': MakeSquareWave(155.56), ‘c': MakeSquareWave(164.81), \
‘c#': MakeSquareWave(174.61), ‘v': MakeSquareWave(174.61), ‘v#': MakeSquareWave(185.00), \
‘b': MakeSquareWave(196.00), ‘b#': MakeSquareWave(207.65), ‘n': MakeSquareWave(220.00), \
‘n#': MakeSquareWave(233.08), ‘m': MakeSquareWave(246.94), ‘m#': MakeSquareWave(261.63), \
‘,': MakeSquareWave(261.63), ‘,#': MakeSquareWave(277.18), ‘a': MakeSquareWave(261.63), \
‘a#': MakeSquareWave(277.18), ‘s': MakeSquareWave(293.66), ‘s#': MakeSquareWave(311.13), \
‘d': MakeSquareWave(329.63), ‘d#': MakeSquareWave(349.23), ‘f': MakeSquareWave(349.23), \
‘f#': MakeSquareWave(369.99), ‘g': MakeSquareWave(392.00), ‘g#': MakeSquareWave(415.30), \
‘h': MakeSquareWave(440.00), ‘h#': MakeSquareWave(466.16), ‘j': MakeSquareWave(493.88), \
‘j#': MakeSquareWave(523.25), ‘k': MakeSquareWave(523.25), ‘k#': MakeSquareWave(554.37), \
‘q': MakeSquareWave(523.25), ‘q#': MakeSquareWave(554.37), ‘w': MakeSquareWave(587.33), \
‘w#': MakeSquareWave(622.25), ‘e': MakeSquareWave(659.25), ‘e#': MakeSquareWave(698.46), \
‘r': MakeSquareWave(698.46), ‘r#': MakeSquareWave(739.99), ‘t': MakeSquareWave(783.99), \
‘t#': MakeSquareWave(830.61), ‘y': MakeSquareWave(880.00), ‘y#': MakeSquareWave(932.33), \
‘u': MakeSquareWave(987.77), ‘u#': MakeSquareWave(1046.50), ‘i': MakeSquareWave(1046.50), \
‘i#': MakeSquareWave(1108.73)}

#Main program loop
_running = True
while _running:

if current_type == SINE_WAVE_TYPE:
DISPLAYSURF.blit(sinelabelsurf, sinelabelrect)
elif current_type == SQUARE_WAVE_TYPE:
DISPLAYSURF.blit(squarelabelsurf, squarelabelrect)

DISPLAYSURF.blit(freqlabelsurf, freqlabelrect)

#Preparing to capture key presses
for event in pygame.event.get():

#Allows user to close program safely
if event.type == pygame.QUIT:
_running = False

#Some keys don’t depend on the current wave type
elif event.type == KEYDOWN:

#Allows program to be closed on ESCAPE press
if event.key == K_ESCAPE:
_running = False

#Change wave type (sound) on ENTER/RETURN press
if event.key == K_SPACE:
current_type = sound_types[current_type] #Toggle
if current_type == SINE_WAVE_TYPE:
DISPLAYSURF.blit(sinelabelsurf, sinelabelrect)
elif current_type == SQUARE_WAVE_TYPE:
DISPLAYSURF.blit(squarelabelsurf, squarelabelrect)

print ‘new type:’, current_type

#If current wave type is sine, play sine wave sound
if current_type == SINE_WAVE_TYPE:

if event.type == KEYDOWN:

#Play sound of corresponding frequency when key is pressed
mods = pygame.key.get_mods() & KMOD_SHIFT

#Lower notes DOWN (bottom row)
if event.key == K_z:
if mods:
freqlabelsurf = fontObj.render(‘C#3 Frequency: 138.59Hz’, True, WHITE, BGCOLOR)
DISPLAYSURF.blit(freqlabelsurf, freqlabelrect)
current_played_sine[‘z#’].play(-1)
else:
freqlabelsurf = fontObj.render(‘C3 Frequency: 130.81Hz’, True, WHITE, BGCOLOR)
DISPLAYSURF.blit(freqlabelsurf, freqlabelrect)
current_played_sine[‘z’].play(-1)

elif event.key == K_x:
if mods:
freqlabelsurf = fontObj.render(‘D#3 Frequency: 155.56Hz’, True, WHITE, BGCOLOR)
DISPLAYSURF.blit(freqlabelsurf, freqlabelrect)
current_played_sine[‘x#’].play(-1)
else:
freqlabelsurf = fontObj.render(‘D3 Frequency: 146.83Hz’, True, WHITE, BGCOLOR)
DISPLAYSURF.blit(freqlabelsurf, freqlabelrect)
current_played_sine[‘x’].play(-1)

elif event.key == K_c:
if mods:
freqlabelsurf = fontObj.render(‘F3 Frequency: 174.61Hz’, True, WHITE, BGCOLOR)
DISPLAYSURF.blit(freqlabelsurf, freqlabelrect)
current_played_sine[‘c#’].play(-1)
else:
freqlabelsurf = fontObj.render(‘E3 Frequency: 164.81Hz’, True, WHITE, BGCOLOR)
DISPLAYSURF.blit(freqlabelsurf, freqlabelrect)
current_played_sine[‘c’].play(-1)

elif event.key == K_v:
if mods:
freqlabelsurf = fontObj.render(‘F#3 Frequency: 185.00Hz’, True, WHITE, BGCOLOR)
DISPLAYSURF.blit(freqlabelsurf, freqlabelrect)
current_played_sine[‘v#’].play(-1)
else:
freqlabelsurf = fontObj.render(‘F3 Frequency: 174.61Hz’, True, WHITE, BGCOLOR)
DISPLAYSURF.blit(freqlabelsurf, freqlabelrect)
current_played_sine[‘v’].play(-1)

elif event.key == K_b:
if mods:
freqlabelsurf = fontObj.render(‘G#3 Frequency: 207.65Hz’, True, WHITE, BGCOLOR)
DISPLAYSURF.blit(freqlabelsurf, freqlabelrect)
current_played_sine[‘b#’].play(-1)
else:
freqlabelsurf = fontObj.render(‘G3 Frequency: 196.00Hz’, True, WHITE, BGCOLOR)
DISPLAYSURF.blit(freqlabelsurf, freqlabelrect)
current_played_sine[‘b’].play(-1)

elif event.key == K_n:
if mods:
freqlabelsurf = fontObj.render(‘A#3 Frequency: 233.08Hz’, True, WHITE, BGCOLOR)
DISPLAYSURF.blit(freqlabelsurf, freqlabelrect)
current_played_sine[‘n#’].play(-1)
else:
freqlabelsurf = fontObj.render(‘A3 Frequency: 220.00Hz’, True, WHITE, BGCOLOR)
DISPLAYSURF.blit(freqlabelsurf, freqlabelrect)
current_played_sine[‘n’].play(-1)

elif event.key == K_m:
if mods:
freqlabelsurf = fontObj.render(‘C4 Frequency: 261.63Hz’, True, WHITE, BGCOLOR)
DISPLAYSURF.blit(freqlabelsurf, freqlabelrect)
current_played_sine[‘m#’].play(-1)
else:
freqlabelsurf = fontObj.render(‘B3 Frequency: 246.94Hz’, True, WHITE, BGCOLOR)
DISPLAYSURF.blit(freqlabelsurf, freqlabelrect)
current_played_sine[‘m’].play(-1)

elif event.key == K_COMMA:
if mods:
freqlabelsurf = fontObj.render(‘C#4 Frequency: 277.18Hz’, True, WHITE, BGCOLOR)
DISPLAYSURF.blit(freqlabelsurf, freqlabelrect)
current_played_sine[‘,#’].play(-1)
else:
freqlabelsurf = fontObj.render(‘C4 Frequency: 261.63Hz’, True, WHITE, BGCOLOR)
DISPLAYSURF.blit(freqlabelsurf, freqlabelrect)
current_played_sine[‘,’].play(-1)

#Middle notes DOWN (middle row)
elif event.key == K_a:
if mods:
freqlabelsurf = fontObj.render(‘C#4 Frequency: 277.18Hz’, True, WHITE, BGCOLOR)
DISPLAYSURF.blit(freqlabelsurf, freqlabelrect)
current_played_sine[‘a#’].play(-1)
else:
freqlabelsurf = fontObj.render(‘C4 Frequency: 261.63Hz’, True, WHITE, BGCOLOR)
DISPLAYSURF.blit(freqlabelsurf, freqlabelrect)
current_played_sine[‘a’].play(-1)

elif event.key == K_s:
if mods:
freqlabelsurf = fontObj.render(‘D#4 Frequency: 311.33Hz’, True, WHITE, BGCOLOR)
DISPLAYSURF.blit(freqlabelsurf, freqlabelrect)
current_played_sine[‘s#’].play(-1)
else:
freqlabelsurf = fontObj.render(‘D4 Frequency: 293.66Hz’, True, WHITE, BGCOLOR)
DISPLAYSURF.blit(freqlabelsurf, freqlabelrect)
current_played_sine[‘s’].play(-1)

elif event.key == K_d:
if mods:
freqlabelsurf = fontObj.render(‘F4 Frequency: 349.23Hz’, True, WHITE, BGCOLOR)
DISPLAYSURF.blit(freqlabelsurf, freqlabelrect)
current_played_sine[‘d#’].play(-1)
else:
freqlabelsurf = fontObj.render(‘E4 Frequency: 329.63Hz’, True, WHITE, BGCOLOR)
DISPLAYSURF.blit(freqlabelsurf, freqlabelrect)
current_played_sine[‘d’].play(-1)

elif event.key == K_f:
if mods:
freqlabelsurf = fontObj.render(‘F#4 Frequency: 369.99Hz’, True, WHITE, BGCOLOR)
current_played_sine[‘f#’].play(-1)
else:
freqlabelsurf = fontObj.render(‘F4 Frequency: 349.23Hz’, True, WHITE, BGCOLOR)
current_played_sine[‘f’].play(-1)

elif event.key == K_g:
if mods:
freqlabelsurf = fontObj.render(‘G#4 Frequency: 415.30Hz’, True, WHITE, BGCOLOR)
DISPLAYSURF.blit(freqlabelsurf, freqlabelrect)
current_played_sine[‘g#’].play(-1)
else:
freqlabelsurf = fontObj.render(‘G4 Frequency: 392.00Hz’, True, WHITE, BGCOLOR)
DISPLAYSURF.blit(freqlabelsurf, freqlabelrect)
current_played_sine[‘g’].play(-1)

elif event.key == K_h:
if mods:
freqlabelsurf = fontObj.render(‘A#4 Frequency: 466.16Hz’, True, WHITE, BGCOLOR)
DISPLAYSURF.blit(freqlabelsurf, freqlabelrect)
current_played_sine[‘h#’].play(-1)
else:
freqlabelsurf = fontObj.render(‘A4 Frequency: 440.00Hz’, True, WHITE, BGCOLOR)
DISPLAYSURF.blit(freqlabelsurf, freqlabelrect)
current_played_sine[‘h’].play(-1)

elif event.key == K_j:
if mods:
freqlabelsurf = fontObj.render(‘C5 Frequency: 523.25Hz’, True, WHITE, BGCOLOR)
DISPLAYSURF.blit(freqlabelsurf, freqlabelrect)
current_played_sine[‘j#’].play(-1)
else:
freqlabelsurf = fontObj.render(‘B4 Frequency: 493.88Hz’, True, WHITE, BGCOLOR)
DISPLAYSURF.blit(freqlabelsurf, freqlabelrect)
current_played_sine[‘j’].play(-1)

elif event.key == K_k:
if mods:
freqlabelsurf = fontObj.render(‘C#5 Frequency: 554.37Hz’, True, WHITE, BGCOLOR)
DISPLAYSURF.blit(freqlabelsurf, freqlabelrect)
current_played_sine[‘k#’].play(-1)
else:
freqlabelsurf = fontObj.render(‘C5 Frequency: 523.25Hz’, True, WHITE, BGCOLOR)
DISPLAYSURF.blit(freqlabelsurf, freqlabelrect)
current_played_sine[‘k’].play(-1)

#Higher notes DOWN (top row)
elif event.key == K_q:
if mods:
freqlabelsurf = fontObj.render(‘C#5 Frequency: 554.37Hz’, True, WHITE, BGCOLOR)
DISPLAYSURF.blit(freqlabelsurf, freqlabelrect)
current_played_sine[‘q#’].play(-1)
else:
freqlabelsurf = fontObj.render(‘C5 Frequency: 523.25Hz’, True, WHITE, BGCOLOR)
DISPLAYSURF.blit(freqlabelsurf, freqlabelrect)
current_played_sine[‘q’].play(-1)

elif event.key == K_w:
if mods:
freqlabelsurf = fontObj.render(‘D#5 Frequency: 622.25Hz’, True, WHITE, BGCOLOR)
DISPLAYSURF.blit(freqlabelsurf, freqlabelrect)
current_played_sine[‘w#’].play(-1)
else:
freqlabelsurf = fontObj.render(‘D5 Frequency: 587.33Hz’, True, WHITE, BGCOLOR)
DISPLAYSURF.blit(freqlabelsurf, freqlabelrect)
current_played_sine[‘w’].play(-1)

elif event.key == K_e:
if mods:
freqlabelsurf = fontObj.render(‘F5 Frequency: 698.46Hz’, True, WHITE, BGCOLOR)
DISPLAYSURF.blit(freqlabelsurf, freqlabelrect)
current_played_sine[‘e#’].play(-1)
else:
freqlabelsurf = fontObj.render(‘E5 Frequency: 659.25Hz’, True, WHITE, BGCOLOR)
DISPLAYSURF.blit(freqlabelsurf, freqlabelrect)
current_played_sine[‘e’].play(-1)

elif event.key == K_r:
if mods:
freqlabelsurf = fontObj.render(‘F#5 Frequency: 739.99Hz’, True, WHITE, BGCOLOR)
DISPLAYSURF.blit(freqlabelsurf, freqlabelrect)
current_played_sine[‘r#’].play(-1)
else:
freqlabelsurf = fontObj.render(‘F5 Frequency: 698.46Hz’, True, WHITE, BGCOLOR)
DISPLAYSURF.blit(freqlabelsurf, freqlabelrect)
current_played_sine[‘r’].play(-1)

elif event.key == K_t:
if mods:
freqlabelsurf = fontObj.render(‘G#5 Frequency: 830.61Hz’, True, WHITE, BGCOLOR)
DISPLAYSURF.blit(freqlabelsurf, freqlabelrect)
current_played_sine[‘t#’].play(-1)
else:
freqlabelsurf = fontObj.render(‘G5 Frequency: 783.99Hz’, True, WHITE, BGCOLOR)
DISPLAYSURF.blit(freqlabelsurf, freqlabelrect)
current_played_sine[‘t’].play(-1)

elif event.key == K_y:
if mods:
freqlabelsurf = fontObj.render(‘A#5 Frequency: 932.33Hz’, True, WHITE, BGCOLOR)
DISPLAYSURF.blit(freqlabelsurf, freqlabelrect)
current_played_sine[‘y#’].play(-1)
else:
freqlabelsurf = fontObj.render(‘A5 Frequency: 880.00Hz’, True, WHITE, BGCOLOR)
DISPLAYSURF.blit(freqlabelsurf, freqlabelrect)
current_played_sine[‘y’].play(-1)

elif event.key == K_u:
if mods:
freqlabelsurf = fontObj.render(‘C6 Frequency: 1046.50Hz’, True, WHITE, BGCOLOR)
DISPLAYSURF.blit(freqlabelsurf, freqlabelrect)
current_played_sine[‘u#’].play(-1)
else:
freqlabelsurf = fontObj.render(‘B5 Frequency: 987.77Hz’, True, WHITE, BGCOLOR)
DISPLAYSURF.blit(freqlabelsurf, freqlabelrect)
current_played_sine[‘u’].play(-1)

elif event.key == K_i:
if mods:
freqlabelsurf = fontObj.render(‘C#6 Frequency: 1108.73Hz’, True, WHITE, BGCOLOR)
DISPLAYSURF.blit(freqlabelsurf, freqlabelrect)
current_played_sine[‘i#’].play(-1)
else:
freqlabelsurf = fontObj.render(‘C6 Frequency: 1046.50Hz’, True, WHITE, BGCOLOR)
DISPLAYSURF.blit(freqlabelsurf, freqlabelrect)
current_played_sine[‘i’].play(-1)

elif event.type == KEYUP:

#Fade out the note on key release (0.35 seconds)

#Lower notes UP (bottom row)
if event.key == K_z:
current_played_sine[‘z’].fadeout(350) #350 milliseconds (0.35 seconds) fadeout
current_played_sine[‘z#’].fadeout(350)

elif event.key == K_x:
current_played_sine[‘x’].fadeout(350)
current_played_sine[‘x#’].fadeout(350)

elif event.key == K_c:
current_played_sine[‘c’].fadeout(350)
current_played_sine[‘c#’].fadeout(350)

elif event.key == K_v:
current_played_sine[‘v’].fadeout(350)
current_played_sine[‘v#’].fadeout(350)

elif event.key == K_b:
current_played_sine[‘b’].fadeout(350)
current_played_sine[‘b#’].fadeout(350)

elif event.key == K_n:
current_played_sine[‘n’].fadeout(350)
current_played_sine[‘n#’].fadeout(350)

elif event.key == K_m:
current_played_sine[‘m’].fadeout(350)
current_played_sine[‘m#’].fadeout(350)

elif event.key == K_COMMA:
current_played_sine[‘,’].fadeout(350)
current_played_sine[‘,#’].fadeout(350)

#Middle notes UP (middle row)

elif event.key == K_a:
current_played_sine[‘a’].fadeout(350)
current_played_sine[‘a#’].fadeout(350)

elif event.key == K_s:
current_played_sine[‘s’].fadeout(350)
current_played_sine[‘s#’].fadeout(350)

elif event.key == K_d:
current_played_sine[‘d’].fadeout(350)
current_played_sine[‘d#’].fadeout(350)

elif event.key == K_f:
current_played_sine[‘f’].fadeout(350)
current_played_sine[‘f#’].fadeout(350)

elif event.key == K_g:
current_played_sine[‘g’].fadeout(350)
current_played_sine[‘g#’].fadeout(350)

elif event.key == K_h:
current_played_sine[‘h’].fadeout(350)
current_played_sine[‘h#’].fadeout(350)

elif event.key == K_j:
current_played_sine[‘j’].fadeout(350)
current_played_sine[‘j#’].fadeout(350)

elif event.key == K_k:
current_played_sine[‘k’].fadeout(350)
current_played_sine[‘k#’].fadeout(350)

#Higher notes UP (top row)

elif event.key == K_q:
current_played_sine[‘q’].fadeout(350)
current_played_sine[‘q#’].fadeout(350)

elif event.key == K_w:
current_played_sine[‘w’].fadeout(350)
current_played_sine[‘w#’].fadeout(350)

elif event.key == K_e:
current_played_sine[‘e’].fadeout(350)
current_played_sine[‘e#’].fadeout(350)

elif event.key == K_r:
current_played_sine[‘r’].fadeout(350)
current_played_sine[‘r#’].fadeout(350)

elif event.key == K_t:
current_played_sine[‘t’].fadeout(350)
current_played_sine[‘t#’].fadeout(350)

elif event.key == K_y:
current_played_sine[‘y’].fadeout(350)
current_played_sine[‘y#’].fadeout(350)

elif event.key == K_u:
current_played_sine[‘u’].fadeout(350)
current_played_sine[‘u#’].fadeout(350)

elif event.key == K_i:
current_played_sine[‘i’].fadeout(350)
current_played_sine[‘i#’].fadeout(350)

#If the current type is square wave, play square wave sound
elif current_type == SQUARE_WAVE_TYPE:

if event.type == KEYDOWN:

#Play sound of corresponding frequency when key is pressed
mods = pygame.key.get_mods() & KMOD_SHIFT

#Lower notes DOWN (bottom row)
if event.key == K_z:
if mods:
freqlabelsurf = fontObj.render(‘C#3 Frequency: 138.59Hz’, True, WHITE, BGCOLOR)
DISPLAYSURF.blit(freqlabelsurf, freqlabelrect)
current_played_square[‘z#’].play(-1)
else:
freqlabelsurf = fontObj.render(‘C3 Frequency: 130.81Hz’, True, WHITE, BGCOLOR)
DISPLAYSURF.blit(freqlabelsurf, freqlabelrect)
current_played_square[‘z’].play(-1)

elif event.key == K_x:
if mods:
freqlabelsurf = fontObj.render(‘D#3 Frequency: 155.56Hz’, True, WHITE, BGCOLOR)
DISPLAYSURF.blit(freqlabelsurf, freqlabelrect)
current_played_square[‘x#’].play(-1)
else:
freqlabelsurf = fontObj.render(‘D3 Frequency: 146.83Hz’, True, WHITE, BGCOLOR)
DISPLAYSURF.blit(freqlabelsurf, freqlabelrect)
current_played_square[‘x’].play(-1)

elif event.key == K_c:
if mods:
freqlabelsurf = fontObj.render(‘F3 Frequency: 174.61Hz’, True, WHITE, BGCOLOR)
DISPLAYSURF.blit(freqlabelsurf, freqlabelrect)
current_played_square[‘c#’].play(-1)
else:
freqlabelsurf = fontObj.render(‘E3 Frequency: 164.81Hz’, True, WHITE, BGCOLOR)
DISPLAYSURF.blit(freqlabelsurf, freqlabelrect)
current_played_square[‘c’].play(-1)

elif event.key == K_v:
if mods:
freqlabelsurf = fontObj.render(‘F#3 Frequency: 185.00Hz’, True, WHITE, BGCOLOR)
DISPLAYSURF.blit(freqlabelsurf, freqlabelrect)
current_played_square[‘v#’].play(-1)
else:
freqlabelsurf = fontObj.render(‘F3 Frequency: 174.61Hz’, True, WHITE, BGCOLOR)
DISPLAYSURF.blit(freqlabelsurf, freqlabelrect)
current_played_square[‘v’].play(-1)

elif event.key == K_b:
if mods:
freqlabelsurf = fontObj.render(‘G#3 Frequency: 207.65Hz’, True, WHITE, BGCOLOR)
DISPLAYSURF.blit(freqlabelsurf, freqlabelrect)
current_played_square[‘b#’].play(-1)
else:
freqlabelsurf = fontObj.render(‘G3 Frequency: 196.00Hz’, True, WHITE, BGCOLOR)
DISPLAYSURF.blit(freqlabelsurf, freqlabelrect)
current_played_square[‘b’].play(-1)

elif event.key == K_n:
if mods:
freqlabelsurf = fontObj.render(‘A#3 Frequency: 233.08Hz’, True, WHITE, BGCOLOR)
DISPLAYSURF.blit(freqlabelsurf, freqlabelrect)
current_played_square[‘n#’].play(-1)
else:
freqlabelsurf = fontObj.render(‘A3 Frequency: 220.00Hz’, True, WHITE, BGCOLOR)
DISPLAYSURF.blit(freqlabelsurf, freqlabelrect)
current_played_square[‘n’].play(-1)

elif event.key == K_m:
if mods:
freqlabelsurf = fontObj.render(‘C4 Frequency: 261.63Hz’, True, WHITE, BGCOLOR)
DISPLAYSURF.blit(freqlabelsurf, freqlabelrect)
current_played_square[‘m#’].play(-1)
else:
freqlabelsurf = fontObj.render(‘B3 Frequency: 246.94Hz’, True, WHITE, BGCOLOR)
DISPLAYSURF.blit(freqlabelsurf, freqlabelrect)
current_played_square[‘m’].play(-1)

elif event.key == K_COMMA:
if mods:
freqlabelsurf = fontObj.render(‘C#4 Frequency: 277.18Hz’, True, WHITE, BGCOLOR)
DISPLAYSURF.blit(freqlabelsurf, freqlabelrect)
current_played_square[‘,#’].play(-1)
else:
freqlabelsurf = fontObj.render(‘C4 Frequency: 261.63Hz’, True, WHITE, BGCOLOR)
DISPLAYSURF.blit(freqlabelsurf, freqlabelrect)
current_played_square[‘,’].play(-1)

#Middle notes DOWN (middle row)
elif event.key == K_a:
if mods:
freqlabelsurf = fontObj.render(‘C#4 Frequency: 277.18Hz’, True, WHITE, BGCOLOR)
DISPLAYSURF.blit(freqlabelsurf, freqlabelrect)
current_played_square[‘a#’].play(-1)
else:
freqlabelsurf = fontObj.render(‘C4 Frequency: 261.63Hz’, True, WHITE, BGCOLOR)
DISPLAYSURF.blit(freqlabelsurf, freqlabelrect)
current_played_square[‘a’].play(-1)

elif event.key == K_s:
if mods:
freqlabelsurf = fontObj.render(‘D#4 Frequency: 311.33Hz’, True, WHITE, BGCOLOR)
DISPLAYSURF.blit(freqlabelsurf, freqlabelrect)
current_played_square[‘s#’].play(-1)
else:
freqlabelsurf = fontObj.render(‘D4 Frequency: 293.66Hz’, True, WHITE, BGCOLOR)
DISPLAYSURF.blit(freqlabelsurf, freqlabelrect)
current_played_square[‘s’].play(-1)

elif event.key == K_d:
if mods:
freqlabelsurf = fontObj.render(‘F4 Frequency: 349.23Hz’, True, WHITE, BGCOLOR)
DISPLAYSURF.blit(freqlabelsurf, freqlabelrect)
current_played_square[‘d#’].play(-1)
else:
freqlabelsurf = fontObj.render(‘E4 Frequency: 329.63Hz’, True, WHITE, BGCOLOR)
DISPLAYSURF.blit(freqlabelsurf, freqlabelrect)
current_played_square[‘d’].play(-1)

elif event.key == K_f:
if mods:
freqlabelsurf = fontObj.render(‘F#4 Frequency: 369.99Hz’, True, WHITE, BGCOLOR)
current_played_square[‘f#’].play(-1)
else:
freqlabelsurf = fontObj.render(‘F4 Frequency: 349.23Hz’, True, WHITE, BGCOLOR)
current_played_square[‘f’].play(-1)

elif event.key == K_g:
if mods:
freqlabelsurf = fontObj.render(‘G#4 Frequency: 415.30Hz’, True, WHITE, BGCOLOR)
DISPLAYSURF.blit(freqlabelsurf, freqlabelrect)
current_played_square[‘g#’].play(-1)
else:
freqlabelsurf = fontObj.render(‘G4 Frequency: 392.00Hz’, True, WHITE, BGCOLOR)
DISPLAYSURF.blit(freqlabelsurf, freqlabelrect)
current_played_square[‘g’].play(-1)

elif event.key == K_h:
if mods:
freqlabelsurf = fontObj.render(‘A#4 Frequency: 466.16Hz’, True, WHITE, BGCOLOR)
DISPLAYSURF.blit(freqlabelsurf, freqlabelrect)
current_played_square[‘h#’].play(-1)
else:
freqlabelsurf = fontObj.render(‘A4 Frequency: 440.00Hz’, True, WHITE, BGCOLOR)
DISPLAYSURF.blit(freqlabelsurf, freqlabelrect)
current_played_square[‘h’].play(-1)

elif event.key == K_j:
if mods:
freqlabelsurf = fontObj.render(‘C5 Frequency: 523.25Hz’, True, WHITE, BGCOLOR)
DISPLAYSURF.blit(freqlabelsurf, freqlabelrect)
current_played_square[‘j#’].play(-1)
else:
freqlabelsurf = fontObj.render(‘B4 Frequency: 493.88Hz’, True, WHITE, BGCOLOR)
DISPLAYSURF.blit(freqlabelsurf, freqlabelrect)
current_played_square[‘j’].play(-1)

elif event.key == K_k:
if mods:
freqlabelsurf = fontObj.render(‘C#5 Frequency: 554.37Hz’, True, WHITE, BGCOLOR)
DISPLAYSURF.blit(freqlabelsurf, freqlabelrect)
current_played_square[‘k#’].play(-1)
else:
freqlabelsurf = fontObj.render(‘C5 Frequency: 523.25Hz’, True, WHITE, BGCOLOR)
DISPLAYSURF.blit(freqlabelsurf, freqlabelrect)
current_played_square[‘k’].play(-1)

#Higher notes DOWN (top row)
elif event.key == K_q:
if mods:
freqlabelsurf = fontObj.render(‘C#5 Frequency: 554.37Hz’, True, WHITE, BGCOLOR)
DISPLAYSURF.blit(freqlabelsurf, freqlabelrect)
current_played_square[‘q#’].play(-1)
else:
freqlabelsurf = fontObj.render(‘C5 Frequency: 523.25Hz’, True, WHITE, BGCOLOR)
DISPLAYSURF.blit(freqlabelsurf, freqlabelrect)
current_played_square[‘q’].play(-1)

elif event.key == K_w:
if mods:
freqlabelsurf = fontObj.render(‘D#5 Frequency: 622.25Hz’, True, WHITE, BGCOLOR)
DISPLAYSURF.blit(freqlabelsurf, freqlabelrect)
current_played_square[‘w#’].play(-1)
else:
freqlabelsurf = fontObj.render(‘D5 Frequency: 587.33Hz’, True, WHITE, BGCOLOR)
DISPLAYSURF.blit(freqlabelsurf, freqlabelrect)
current_played_square[‘w’].play(-1)

elif event.key == K_e:
if mods:
freqlabelsurf = fontObj.render(‘F5 Frequency: 698.46Hz’, True, WHITE, BGCOLOR)
DISPLAYSURF.blit(freqlabelsurf, freqlabelrect)
current_played_square[‘e#’].play(-1)
else:
freqlabelsurf = fontObj.render(‘E5 Frequency: 659.25Hz’, True, WHITE, BGCOLOR)
DISPLAYSURF.blit(freqlabelsurf, freqlabelrect)
current_played_square[‘e’].play(-1)

elif event.key == K_r:
if mods:
freqlabelsurf = fontObj.render(‘F#5 Frequency: 739.99Hz’, True, WHITE, BGCOLOR)
DISPLAYSURF.blit(freqlabelsurf, freqlabelrect)
current_played_square[‘r#’].play(-1)
else:
freqlabelsurf = fontObj.render(‘F5 Frequency: 698.46Hz’, True, WHITE, BGCOLOR)
DISPLAYSURF.blit(freqlabelsurf, freqlabelrect)
current_played_square[‘r’].play(-1)

elif event.key == K_t:
if mods:
freqlabelsurf = fontObj.render(‘G#5 Frequency: 830.61Hz’, True, WHITE, BGCOLOR)
DISPLAYSURF.blit(freqlabelsurf, freqlabelrect)
current_played_square[‘t#’].play(-1)
else:
freqlabelsurf = fontObj.render(‘G5 Frequency: 783.99Hz’, True, WHITE, BGCOLOR)
DISPLAYSURF.blit(freqlabelsurf, freqlabelrect)
current_played_square[‘t’].play(-1)

elif event.key == K_y:
if mods:
freqlabelsurf = fontObj.render(‘A#5 Frequency: 932.33Hz’, True, WHITE, BGCOLOR)
DISPLAYSURF.blit(freqlabelsurf, freqlabelrect)
current_played_square[‘y#’].play(-1)
else:
freqlabelsurf = fontObj.render(‘A5 Frequency: 880.00Hz’, True, WHITE, BGCOLOR)
DISPLAYSURF.blit(freqlabelsurf, freqlabelrect)
current_played_square[‘y’].play(-1)

elif event.key == K_u:
if mods:
freqlabelsurf = fontObj.render(‘C6 Frequency: 1046.50Hz’, True, WHITE, BGCOLOR)
DISPLAYSURF.blit(freqlabelsurf, freqlabelrect)
current_played_square[‘u#’].play(-1)
else:
freqlabelsurf = fontObj.render(‘B5 Frequency: 987.77Hz’, True, WHITE, BGCOLOR)
DISPLAYSURF.blit(freqlabelsurf, freqlabelrect)
current_played_square[‘u’].play(-1)

elif event.key == K_i:
if mods:
freqlabelsurf = fontObj.render(‘C#6 Frequency: 1108.73Hz’, True, WHITE, BGCOLOR)
DISPLAYSURF.blit(freqlabelsurf, freqlabelrect)
current_played_square[‘i#’].play(-1)
else:
freqlabelsurf = fontObj.render(‘C6 Frequency: 1046.50Hz’, True, WHITE, BGCOLOR)
DISPLAYSURF.blit(freqlabelsurf, freqlabelrect)
current_played_square[‘i’].play(-1)

elif event.type == KEYUP:

#Fade out the note on key release (0.35 seconds)

#Lower notes UP (bottom row)
if event.key == K_z:
current_played_square[‘z’].fadeout(350) #350 milliseconds (0.35 seconds) fadeout
current_played_square[‘z#’].fadeout(350)

elif event.key == K_x:
current_played_square[‘x’].fadeout(350)
current_played_square[‘x#’].fadeout(350)

elif event.key == K_c:
current_played_square[‘c’].fadeout(350)
current_played_square[‘c#’].fadeout(350)

elif event.key == K_v:
current_played_square[‘v’].fadeout(350)
current_played_square[‘v#’].fadeout(350)

elif event.key == K_b:
current_played_square[‘b’].fadeout(350)
current_played_square[‘b#’].fadeout(350)

elif event.key == K_n:
current_played_square[‘n’].fadeout(350)
current_played_square[‘n#’].fadeout(350)

elif event.key == K_m:
current_played_square[‘m’].fadeout(350)
current_played_square[‘m#’].fadeout(350)

elif event.key == K_COMMA:
current_played_square[‘,’].fadeout(350)
current_played_square[‘,#’].fadeout(350)

#Middle notes UP (middle row)

elif event.key == K_a:
current_played_square[‘a’].fadeout(350)
current_played_square[‘a#’].fadeout(350)

elif event.key == K_s:
current_played_square[‘s’].fadeout(350)
current_played_square[‘s#’].fadeout(350)

elif event.key == K_d:
current_played_square[‘d’].fadeout(350)
current_played_square[‘d#’].fadeout(350)

elif event.key == K_f:
current_played_square[‘f’].fadeout(350)
current_played_square[‘f#’].fadeout(350)

elif event.key == K_g:
current_played_square[‘g’].fadeout(350)
current_played_square[‘g#’].fadeout(350)

elif event.key == K_h:
current_played_square[‘h’].fadeout(350)
current_played_square[‘h#’].fadeout(350)

elif event.key == K_j:
current_played_square[‘j’].fadeout(350)
current_played_square[‘j#’].fadeout(350)

elif event.key == K_k:
current_played_square[‘k’].fadeout(350)
current_played_square[‘k#’].fadeout(350)

#Higher notes UP (top row)

elif event.key == K_q:
current_played_square[‘q’].fadeout(350)
current_played_square[‘q#’].fadeout(350)

elif event.key == K_w:
current_played_square[‘w’].fadeout(350)
current_played_square[‘w#’].fadeout(350)

elif event.key == K_e:
current_played_square[‘e’].fadeout(350)
current_played_square[‘e#’].fadeout(350)

elif event.key == K_r:
current_played_square[‘r’].fadeout(350)
current_played_square[‘r#’].fadeout(350)

elif event.key == K_t:
current_played_square[‘t’].fadeout(350)
current_played_square[‘t#’].fadeout(350)

elif event.key == K_y:
current_played_square[‘y’].fadeout(350)
current_played_square[‘y#’].fadeout(350)

elif event.key == K_u:
current_played_square[‘u’].fadeout(350)
current_played_square[‘u#’].fadeout(350)

elif event.key == K_i:
current_played_square[‘i’].fadeout(350)
current_played_square[‘i#’].fadeout(350)

# fill the screen to draw from a blank state
DISPLAYSURF.fill(BGCOLOR)

# sine wave
yPos = -1 * math.sin(step) * AMPLITUDE – 200
posRecord[‘sin’].append((int(xPos), int(yPos) + WIN_CENTERY))
if current_type == SINE_WAVE_TYPE:
# draw the sine ball and label
pygame.draw.circle(DISPLAYSURF, BGCOLOR, (int(xPos), int(yPos) + WIN_CENTERY), 20)
#sinLabelRect.center = (int(xPos), int(yPos) + WIN_CENTERY + 20)
#DISPLAYSURF.blit(sinLabelSurf, sinLabelRect

# draw the waves from the previously recorded ball positions
if current_type == SINE_WAVE_TYPE:
for x, y in posRecord[‘sin’]:
pygame.draw.circle(DISPLAYSURF, DARKRED, (x, y), 8)

#drawing horizontal lines
# square
posRecord[‘square’].append((int(xPos), int(yPosSquare – 200) + WIN_CENTERY))
if current_type == SQUARE_WAVE_TYPE:
# draw the sine ball and label
pygame.draw.circle(DISPLAYSURF, BGCOLOR, (int(xPos), int(yPosSquare – 200) + WIN_CENTERY), 20)
#squareLabelRect.center = (int(xPos), int(yPosSquare) + WIN_CENTERY + 20)
#DISPLAYSURF.blit(squareLabelSurf, squareLabelRect)

# draw the waves from the previously recorded ball positions
if current_type == SQUARE_WAVE_TYPE:
for x, y in posRecord[‘square’]:
pygame.draw.circle(DISPLAYSURF, BLUE, (x, y), 8)

# draw the border
pygame.draw.rect(DISPLAYSURF, BGCOLOR, (0, 0, WINDOWWIDTH, WINDOWHEIGHT), 1)

screen.blit(image, (WINDOWWIDTH / 2 – (image.get_width() / 2), WINDOWHEIGHT – (75 + image.get_height())))

pygame.display.update()
FPSCLOCK.tick(FPS)

if not pause:
xPos += 5

if xPos >= WINDOWWIDTH:
#sine
xPos = 0
posRecord[‘sin’] = []
step = 0

#square
yPosSquare = AMPLITUDE
posRecord[‘square’] = []
else:
#sine
step += 0.08
step %= 2 * math.pi

# square
# jump top and bottom every 100 pixels
if xPos % 100 == 0:
yPosSquare *= -1
# add vertical line
for x in range(-AMPLITUDE, AMPLITUDE):
posRecord[‘square’].append((int(xPos), int(x) + WIN_CENTERY – 200))

pygame.quit()

#———————————————————————-

Instrument Program – Python (work in progress)

import pygame
from pygame.locals import *

import math
import numpy

pygame.init()
bits = 16

pygame.mixer.pre_init(44100, -bits, 2)

def generate_sound(freq):
volume = 0.5

for i in range(pygame.mixer.get_num_channels()):
c = pygame.mixer.Channel(i)
c.set_volume(volume)

#setup our numpy array to handle 16 bit ints, which is what we set our mixer to expect with "bits" up above
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*freq*t))) # left
buf[s][1] = int(round(max_sample*math.sin(2*math.pi*freq*t))) # right

return pygame.sndarray.make_sound(buf)

size = (1200, 720)
screen = pygame.display.set_mode(size, pygame.HWSURFACE | pygame.DOUBLEBUF)

duration = 2.0 # in seconds

sample_rate = 44100

n_samples = int(round(duration*sample_rate))

#lower
sound_130_81 = generate_sound(130.81) #C
sound_146_83 = generate_sound(146.83) #D
sound_164_81 = generate_sound(164.81) #E
sound_174_61 = generate_sound(174.61) #F
sound_196_00 = generate_sound(196.00) #G
sound_220_00 = generate_sound(220.00) #A
sound_246_94 = generate_sound(246.94) #B
#middle
sound_261_63 = generate_sound(261.63) #C
sound_293_66 = generate_sound(293.66) #D
sound_329_63 = generate_sound(329.63) #E
sound_349_23 = generate_sound(349.23) #F
sound_392_00 = generate_sound(392.00) #G
sound_440_00 = generate_sound(440.00) #A
sound_493_88 = generate_sound(493.88) #B
#higher
sound_523_25 = generate_sound(523.25) #C
sound_587_33 = generate_sound(587.33) #D
sound_659_25 = generate_sound(659.25) #E
sound_698_46 = generate_sound(698.46) #F
sound_783_99 = generate_sound(783.99) #G
sound_880_00 = generate_sound(880.00) #A
sound_987_77 = generate_sound(987.77) #B
sound_1046_50 = generate_sound(1046.50) #C

#This will keep the sound playing forever, the quit event handling allows the pygame window to close without crashing
_running = True
while _running:

for event in pygame.event.get():
if event.type == pygame.QUIT:
_running = False

if event.type == KEYDOWN:
#Volume control
if event.key == K_1:
for i in range(pygame.mixer.get_num_channels()):
c = pygame.mixer.Channel(i)
c.set_volume(0.0)

elif event.key == K_2:
for i in range(pygame.mixer.get_num_channels()):
c = pygame.mixer.Channel(i)
c.set_volume(0.1)

elif event.key == K_3:
for i in range(pygame.mixer.get_num_channels()):
c = pygame.mixer.Channel(i)
c.set_volume(0.2)

elif event.key == K_4:
for i in range(pygame.mixer.get_num_channels()):
c = pygame.mixer.Channel(i)
c.set_volume(0.3)

elif event.key == K_5:
for i in range(pygame.mixer.get_num_channels()):
c = pygame.mixer.Channel(i)
c.set_volume(0.4)

elif event.key == K_6:
for i in range(pygame.mixer.get_num_channels()):
c = pygame.mixer.Channel(i)
c.set_volume(0.5)

elif event.key == K_7:
for i in range(pygame.mixer.get_num_channels()):
c = pygame.mixer.Channel(i)
c.set_volume(0.6)

elif event.key == K_8:
for i in range(pygame.mixer.get_num_channels()):
c = pygame.mixer.Channel(i)
c.set_volume(0.7)

elif event.key == K_9:
for i in range(pygame.mixer.get_num_channels()):
c = pygame.mixer.Channel(i)
c.set_volume(0.8)

elif event.key == K_0:
for i in range(pygame.mixer.get_num_channels()):
c = pygame.mixer.Channel(i)
c.set_volume(0.9)

elif event.key == K_MINUS:
for i in range(pygame.mixer.get_num_channels()):
c = pygame.mixer.Channel(i)
c.set_volume(1.0)

volume = c.get_volume()

if event.key == K_ESCAPE:
_running = False

#lower notes DOWN

elif event.key == K_z:
sound_130_81.play(-1)

elif event.key == K_x:
sound_146_83.play(-1)

elif event.key == K_c:
sound_164_81.play(-1)

elif event.key == K_v:
sound_174_61.play(-1)

elif event.key == K_b:
sound_196_00.play(-1)

elif event.key == K_n:
sound_220_00.play(-1)

elif event.key == K_m:
sound_246_94.play(-1)

elif event.key == K_COMMA:
sound_261_63.play(-1)

#middle notes DOWN

elif event.key == K_a:
sound_261_63.play(-1)

elif event.key == K_s:
sound_293_66.play(-1)

elif event.key == K_d:
sound_329_63.play(-1)

elif event.key == K_f:
sound_349_23.play(-1)

elif event.key == K_g:
sound_392_00.play(-1)

elif event.key == K_h:
sound_440_00.play(-1)

elif event.key == K_j:
sound_493_88.play(-1)

elif event.key == K_k:
sound_523_25.play(-1)

#higher notes DOWN

elif event.key == K_q:
sound_523_25.play(-1)

elif event.key == K_w:
sound_587_33.play(-1)

elif event.key == K_e:
sound_659_25.play(-1)

elif event.key == K_r:
sound_698_46.play(-1)

elif event.key == K_t:
sound_783_99.play(-1)

elif event.key == K_y:
sound_880_00.play(-1)

elif event.key == K_u:
sound_987_77.play(-1)

elif event.key == K_i:
sound_1046_50.play(-1)

if event.type == KEYUP:

#lower notes UP

if event.key == K_z:
sound_130_81.fadeout(350) #fade sound by .35 seconds

elif event.key == K_x:
sound_146_83.fadeout(1000)

elif event.key == K_c:
sound_164_81.fadeout(350)

elif event.key == K_v:
sound_174_61.fadeout(350)

elif event.key == K_b:
sound_196_00.fadeout(350)

elif event.key == K_n:
sound_220_00.fadeout(350)

elif event.key == K_m:
sound_246_94.fadeout(350)

elif event.key == K_COMMA:
sound_261_63.fadeout(350)

#middle notes UP

elif event.key == K_a:
sound_261_63.fadeout(350)

elif event.key == K_s:
sound_293_66.fadeout(350)

elif event.key == K_d:
sound_329_63.fadeout(350)

elif event.key == K_f:
sound_349_23.fadeout(350)

elif event.key == K_g:
sound_392_00.fadeout(350)

elif event.key == K_h:
sound_440_00.fadeout(350)

elif event.key == K_j:
sound_493_88.fadeout(350)

elif event.key == K_k:
sound_523_25.fadeout(350)

#higher notes UP

elif event.key == K_q:
sound_523_25.fadeout(350)

elif event.key == K_w:
sound_587_33.fadeout(350)

elif event.key == K_e:
sound_659_25.fadeout(350)

elif event.key == K_r:
sound_698_46.fadeout(350)

elif event.key == K_t:
sound_783_99.fadeout(350)

elif event.key == K_y:
sound_880_00.fadeout(350)

elif event.key == K_u:
sound_987_77.fadeout(350)

elif event.key == K_i:
sound_1046_50.fadeout(350)

pygame.quit()

Things to work on – F453 June 2010

State two reasons why memory management is necessary

  • to allocate memory to allow separate processes to run at the same time
  • to deal with allocation when paging/segmentation
  • to reallocate memory when necessary
  • to protect processes/data from each other
  • to protect the operating system/provide security
  • to enable memory to be shared

Describe how virtual memory is used

  • use of backing store as if it were main memory/temporary storage
  • paging/fixed size units
  • swap pages between memory & backing store…
  • …to make space for pages needed

Describe the problem of disk threshing

  • occurs when using virtual memory/moving pages
    between memory & disk
  • disk is relatively slow
  • high rate of disk access
  • more time spent transferring pages than on processing

Describe one method of scheduling
Examples include:

  • round robin
  • each user allocated a short period of time/in a sequence
  • or

  • system of priorities
  • highest priority first
  • or

  • length of job
  • shortest job first
  • or

  • first come, first served
  • jobs processed in order of arrival

Explain why intermediate code may be more useful than executable code.

  • Can run on a variety of computers
  • Same intermediate code can be obtained from different high level languages
  • improves portability

Describe the Fetch-Execute cycle

  • PC holds address of next instruction
  • copy contents of PC to MAR
  • increment PC
  • load instruction pointed to by the MAR to MDR
  • copy instruction from MDR to CIR
  • decode instruction in CIR

Describe how a jump instruction is executed

  • By changing contents of PC (to address part of instruction)
  • Copy address part of instruction…
  • In CIR to PC

Give two reasons why binary numbers should be normalised

  • To ensure unique representation of a number
  • To provide maximum precision/accuracy
  • Multiplication is more accurate

Write an algorithm to merge two sorted files, stating any assumptions you make

  • open existing files
  • create new file
  • check existing files are not empty
  • use pointers/counters to identify records for comparison
  • repeat
  • compare records indicated by pointers
  • copy earlier value record to new file
  • move correct pointer
  • until end of one file
  • copy remaining records from other file
  • close files
  • Assume common key
  • Assume if two records are same…
  • …Only one is writtent to new file.

Explain the term backtracking

  • after finding a solution (to a goal)
  • go back and follow an alternative path…
  • …to attempt to find another solution

Explain how functions and procedures may be used to develop a program in a structured way

  • each module can be written as a functional
    procedure…
  • …which can be tested individually
  • library routines
  • code is reusable
  • main program consists of calls to
    functions/procedures…
  • …which may be nested

Explain the term mnemonics

  • A code that is easily remembered…
  • …Used to give the opcode/instruction
  • e.g. ADD

Describe relative addressing

  • allows a real address to be calculated…
  • …from a base address…
  • …by adding the relative address
  • relative address is an offset
  • can be used for arrays
  • can be used for branching

Describe indexed addressing

  • modifies the address given…
  • …by adding the number…
  • …from the index register
  • …to address in instruction

Explain the term flow-control (assembly language)

  • the order in which instructions are executed
  • the order may be changed by a jump instruction/conditional jump instruction

Fetch-Decode-Execute Cycle

  • The Pc holds the address of the next instruction.
  • The contents of the Pc are copied to the MAR
  • The instruction in the address shown in the MAR is copied to the MDR
  • Contents of the MDR are then copied into the CIR
  • The instruction in the CIR is sent to the control unit to be decoded. This means the control unit will look up the instruction in its instruction set to see what it needs to do.
  • The PC is then incremented to point to the next instruction in memory

More Progress with Project

After a good session with my client I have a new, fresh idea what I am doing with the program now. New code has been added to the program to enable the adding of images to the second form to be displayed to the customer. I still need to figure out a way of dealing with the calculations with the dimensions of the images to sort them into their respective groups. The groups have been discussed with my client and we have decided on 10 size ratios that will form the individual groups.

The problem is not particularly the calculations, but the actual categorising of the images. I need to find a way to add them to ‘groups’. This is something that I can’t quite get my head around.

I literally just thought of it as I wrote “add them to ‘groups'” I’m going to try adding them as separate collections.

User Notes on Program

Give each image a unique ID (14 digit number), so that they can be found later on

Customer ID as 4 digit number, Date as 6 digit number, followed by 4 digit image number to identify both the customer and the specific image

Database report to handle database (queries)

Example: image taken on 16/02/2014 will have id as follows:

01141602140001

take first 4 as one query parameter, then next 6, etc.
0114 (customerID)
160214 (Date)
0001 (image ID)

Then ability to send this one image to the print folder to be printed

Separate folders configured on first load of program to save images to

Images come from camera – need to be saved into a folder at the end

3 options on first startup:
Folder for database and related items
Folder for images
What backup software will use? (external hard drive, cd burner etc.)

Open file path of folder in Nero (or whatever)

Regular backup (“It’s been a week since you backed up! Do you want to back up this weeks work or whatever?”) etc.

Current shoot’s images only displayed, not all images from all shoots

Open images with percentages (2 images is half page each)

Normal shoot – 10 images
Model shoot – possibly a lot more

6 images per page

Wire frame – image fits both ways into frame, to keep dimensions in high quality (no matter what original dimensions were)

*NEED TO FIND WAY OF GROUPING IMAGES INTO GROUPS BASED ON DIMENSIONS!*
4×1 is biggest ratio
Wire frame should be 4×4 (fixed – image shouldn’t change it at all)

Groups:
A – 1.5 (3×2)
B – 1
C – 2
D – 3
E – 4
F – 1.4 (7×5)
G – 1.25 (8×10, 4×5, 16×20)
H – 4×3
I – 1.2
J – 5×3
K – 1.75
L – 1.6
M – 1.8
N – 7×6
O – 2.5
P – 8×7
Q – 9×7
R – 10×7
S – 11×7
T – 15×7
U – 1.125
V – 1.375
W – 1.875
X – 10×9
Y – 11×9
Z – 15×11
AA – 20×13

Final groups:
1 A – 7
2 B – SQUARES
3 D – PANORAMIC
5 F – 2
6 G – 4
7 H – 4
8 I – 2
9 J – 4
10 *DOES NOT FIT – ERROR! SPIT THAT SHIT OUT!!*

A 1 – A – 1.5
B 2 – B – 1
C 3 – D – 3
D 5 – F – 1.4
E 6 – G – 1.25
F 7 – H – 4×3
G 8 – I – 1.2
H 9 – J – 5×3
I 10 – *NOT FIT*

Commencement of Second Form

Having used the same fix for the duplicate customer names on the duplicate images I have found that work is progressing fairly quickly, as predicted. The second form is now underway and I have started working on counting the number of images in the database that share the same customer ID that we are working with.

So, as a rundown of the program’s current state of completion:

The program loads up and the user enters the current customer’s name; the images to be displayed by the program; and the date that the customer’s shoot had taken place. The option to select an image to be used as a background image for the second form will also be present in this first form.
Once the user has input this data, the data is input to the database to be used again in the second form and in future uses of the program.
Now the second form is opened. The images that reside in the database for the current customer are then counted to decide how many picture boxes will be needed to display them.

The next step towards finishing the program is to actually display the images and appropriate options and group them into their suitable groups based on their dimensions. These groups will enable the user to decide which canvases they can be printed on to be sold to the customer in their best quality.