Ask Your Question

alexbedolla's profile - activity

2021-09-08 23:31:06 +0200 received badge  Good Question (source)
2019-06-26 17:44:51 +0200 received badge  Famous Question (source)
2016-04-19 15:30:09 +0200 received badge  Popular Question (source)
2016-04-19 15:30:09 +0200 received badge  Notable Question (source)
2013-05-03 20:36:12 +0200 received badge  Nice Question (source)
2013-05-03 11:48:08 +0200 received badge  Student (source)
2013-05-03 04:05:04 +0200 asked a question Sound, how is it done?

Im working on an interactive piece using an alternate form of the doppler effect and I would appreciate some help in making it play sound. Thank you

import wave

class SoundFile:
   def  __init__(self, signal,lab=''):
       self.file = wave.open('./test' + lab + '.wav', 'wb')
       self.signal = signal
       self.sr = 44100

   def write(self):
       self.file.setparams((1, 2, self.sr, 44100*4, 'NONE', 'noncompressed'))
       self.file.writeframes(self.signal)
       self.file.close()

mypi = float(pi)
from math import sin
var('x')

@interact
def sinsound(f = slider(16,16000,1,5120)):
    v = 340.29
    html('$y = f*(((v)+ tan(x))/((v)+sin(x))))$')
    y = f*((v+tan(x))/(v+sin(x)))
    hz1 = y
    s2 = [sin(hz1*x*mypi*2) for x in srange(0,4,1/44100.0)]
    s2m = max(s2)
    s2f = [16384*x/s2m for x in s2]
    s2str = ''.join(wave.struct.pack('h',x) for x in s2f)
    lab="%1.2f"%float(f)
    f = SoundFile(s2str,lab=lab)
    f.write()
    pnum = 1500+int(500/f)
    show(list_plot(s2[0:pnum],plotjoined=True))
    html('<embed src="cell://test'+ lab +'.wav" width="200" height="100"></embed>')