In [3]:
# Not the best version, just a first-off at this:
# By Keith O'Hara, Bard College

# Based on nifty guitar demo:
# http://introcs.cs.princeton.edu/java/assignments/guitar.html
# This assignment was developed by Andrew Appel, Jeff Bernstein, 
# Maia Ginsburg, Ken Steiglitz, Ge Wang, and Kevin Wayne. 
# Copyright © 2005

import wave
import math
import struct
import random
from collections import deque

def pluck(freq1=220, freq2=330): #A3, E4
    sampling_rate = 44100
    channels = 2
    bytes_per_sample = 2
    amplitude = 2**(8*bytes_per_sample) / 2

    buffer1_size = sampling_rate // freq1
    buffer2_size = sampling_rate // freq2

    w = wave.open("out.wav", 'w')
    w.setnchannels(channels)
    w.setsampwidth(bytes_per_sample)
    w.setframerate(sampling_rate)
    w.setnframes(sampling_rate * bytes_per_sample)

    buf1 = deque([random.uniform(-.5, .5) for x in range(buffer1_size)])
    buf2 = deque([random.uniform(-.5, .5) for x in range(buffer2_size)])
    gain = .994

    for i in range(int(amplitude * 1.5)):
         v1 = gain * .5*(buf1[0] + buf1[1])
         buf1.popleft()
         buf1.append(v1)
         v2 = gain * .5*(buf2[0] + buf2[1])
         buf2.popleft()
         buf2.append(v2)
         v = v1 + v2
         w.writeframes(struct.pack('h', int(v*amplitude)))
         w.writeframes(struct.pack('h', int(v*amplitude)))
    w.close()
In [2]:
pluck()
calico.display(calico.Audio("out.wav"))
In [1]:
freq1 = calico.IntSliderWidget(description="Freq1", min=100, max=2000)
In [2]:
freq2 = calico.IntSliderWidget(description="Freq2", min=100, max=2000)
In [4]:
[calico.display(f) for f in [freq1, freq2]]
Out[4]:
[None, None]
In [10]:
pluck(freq1.value, freq2.value)
In [11]:
calico.Audio("out.wav")
Out[11]:
In [ ]: