tag:blogger.com,1999:blog-3704048544762396176.post7791148048303107017..comments2023-06-01T16:38:21.578+02:00Comments on Coding Mess: How to make a .wav file with Python, revisitedR.L.http://www.blogger.com/profile/14598815714075739940noreply@blogger.comBlogger8125tag:blogger.com,1999:blog-3704048544762396176.post-6473585063955115192016-12-19T17:20:32.698+01:002016-12-19T17:20:32.698+01:00your indents are gone in your comment.
anyway, on...your indents are gone in your comment.<br /><br />anyway, on all these python 2.7 scripts, i'm getting this error on python 3.5:<br /><br />struct.error: required argument is not an integerAnonymousnoreply@blogger.comtag:blogger.com,1999:blog-3704048544762396176.post-7645373041925701882012-01-27T05:15:17.342+01:002012-01-27T05:15:17.342+01:00Thanks for the starting point help!
In the spir...Thanks for the starting point help! <br /><br />In the spirit of community, here's the program I wrote to let me do what I needed to do, using the OP as a starting point:<br /><br />import numpy as N<br />import wave<br />import struct<br /><br />def MakeSineWaveBuffer(frequency = [440], amplitude = [.5], duration = 5, Fs = 44100):<br /> samplecount = duration * Fs<br /><br /> # create the signals<br /> signals = [] <br /> for f, a in zip(frequency, amplitude):<br /> period = Fs / float(f) # in sample points<br /> omega = N.pi * 2 / period<br /> <br /> x = N.arange(samplecount, dtype = N.float) * omega<br /> signal = (a) * N.sin(x)<br /> signals.append(signal)<br /><br /> # add them up<br /> signal = signals[0]<br /> for s in signals[1:]: <br /> signal += s<br /><br /> # see if we are too loud<br /> m = N.max(signal)<br /> if m > 1.0:<br /> signal /= m<br /> <br /> # convert to 16-bit audio numerical space<br /> signal *= 32767 <br /> <br /> fmtstr = 'h' * samplecount<br /> signalbuffer = struct.pack(fmtstr,*tuple(signal))<br /> <br /> return signalbuffer<br /> <br />class SoundFile(object):<br /> def __init__(self, signal, Fs = 44100):<br /> self.signal = signal<br /> self.Fs = Fs<br /><br /> def write(self, fname):<br /> numsamples = len(self.signal) / 2<br /> fp = wave.open(fname, "wb")<br /> fp.setparams((1, 2, self.Fs, numsamples, 'NONE', 'noncompressed'))<br /> fp.writeframes(self.signal)<br /> fp.close()<br /> <br /> <br />def ExtractSignalFromBuffer(buffer):<br /> samplecount = len(buffer) / 2<br /> fmtstr = 'h' * samplecount<br /> return struct.unpack(fmtstr, buffer)<br /><br />if __name__ == "__main__":<br /> sig = MakeSineWaveBuffer(frequency = [100, 200,300,400], amplitude = [.3, .5, .7, .9], duration = 2, Fs = 44100)<br /> sf = SoundFile(sig)<br /> sf.write("test.wav")<br /> reclaimedsig = ExtractSignalFromBuffer(sig)<br /><br /> import pylab<br /> <br /> pylab.subplot(211)<br /> pylab.plot(reclaimedsig)<br /> pylab.grid() <br /> pylab.subplot(212)<br /> fftmag = (N.abs(N.fft.rfft(reclaimedsig)) ** 2)<br /> m = N.max(fftmag)<br /> fftmag /= m<br /> fftfreq = N.fft.fftfreq(len(reclaimedsig)/2+1) <br /> print len(fftmag), len(fftfreq)<br /> pylab.plot(fftfreq, fftmag)<br /> pylab.grid() <br /> pylab.show()Keithhttps://www.blogger.com/profile/12388833419194438104noreply@blogger.comtag:blogger.com,1999:blog-3704048544762396176.post-48953919793309017032012-01-26T17:38:42.409+01:002012-01-26T17:38:42.409+01:00Anonymous: just use the struct module itself, not ...Anonymous: just use the struct module itself, not the wave.struct module (it's the same thing).<br /><br />RL, to do the pack, I think I get better performance if I do this instead of the string join approach:<br /><br /> x = N.arange(samplecount, dtype = N.float) * omega<br /> signal = (32767 * amplitude) * N.sin(x)<br /><br /> fmtstr = 'h' * samplecount<br /> signalbuffer = struct.pack(fmtstr,*tuple(signal))Keithhttps://www.blogger.com/profile/12388833419194438104noreply@blogger.comtag:blogger.com,1999:blog-3704048544762396176.post-45940341839754599342011-06-01T18:06:06.700+02:002011-06-01T18:06:06.700+02:00So I'm working on a linux machine with and 32b...So I'm working on a linux machine with and 32bit version of python and I keep getting the error "'module' object has no attribute 'struct'". <br />I have also run the code on a machine with the 64bit version and it works fine. Any thoughts on what is going on and/or how to fix it?Anonymousnoreply@blogger.comtag:blogger.com,1999:blog-3704048544762396176.post-37304619992513804782011-05-14T02:26:11.856+02:002011-05-14T02:26:11.856+02:00FYI, I had a range problem with the sample values ...FYI, I had a range problem with the sample values after changing the sampling frequency to 96k. The sample values can include 32768.0, which a signed short cannot handle.<br /><br />This is a band aid fix:<br /><br /> # The output range includes 32768 which cannot fit int16_t.<br /> # Clip it to 32767.<br /> samples = map( lambda item: 32767 if item >= 32768 else item, samples )Anonymousnoreply@blogger.comtag:blogger.com,1999:blog-3704048544762396176.post-83870465422928164132011-05-03T05:29:35.617+02:002011-05-03T05:29:35.617+02:00Thanks very much for posting an update, doing so t...Thanks very much for posting an update, doing so two years later is a true act of generosity of yours. I want to do some tests with siren sounds, and your script will be very helpful, so as the tip from Jason F.Helton Moraeshttps://www.blogger.com/profile/07379130483212866818noreply@blogger.comtag:blogger.com,1999:blog-3704048544762396176.post-21088413213602967652010-09-02T18:29:47.721+02:002010-09-02T18:29:47.721+02:00could you please write a code segment for reading ...could you please write a code segment for reading a .wav file and to perform FFT on it using scipylabRVJnoreply@blogger.comtag:blogger.com,1999:blog-3704048544762396176.post-35773748303212180962010-02-28T15:11:00.778+01:002010-02-28T15:11:00.778+01:00Thanks for the post, this got me started on writin...Thanks for the post, this got me started on writing wav files of my <a href="http://chateaufortineux.blogspot.com/2010/02/lt-spice-model-of-wierd-sound-generator.html" rel="nofollow">simulated analog synthesizer output</a>. I found that you can write integer NumPy arrays directly to wav files using scipy.io.wavefile.write:<br /><br /><a href="http://www.scipy.org/doc/api_docs/SciPy.io.wavfile.html" rel="nofollow">http://www.scipy.org/doc/api_docs/SciPy.io.wavfile.html</a>Jason Fhttps://www.blogger.com/profile/05770695479191864573noreply@blogger.com