[Numpy-discussion] arrayrange
Miguel Oliveira, Jr.
miguel.oliveira.jr@gmail....
Wed Mar 14 11:03:04 CDT 2007
Hello,
I've got a few codes that use "arrayrange" within numpy. It happens
that the new version of numpy apparently doesn't recognize
"arrayrange"... I've tried to change it to "arange", but it doesn't
work... So, for example, the code below used to create a sine wave
file, but it's not working anymore... Does anyone have any clue of
what may be a solution for this?
Thanks!!!
Miguel
# Basic sound generation
# Generate a sine wave and stick it in an AIFC file
# Assumes mono, 16-bit sound
# SRate, duration, and frequency are variables
from numpy import arange, sin, pi
from array import array
import aifc
print "----------------"
SRate = 44100.0 # sampling rate of the output file
durSecs = 1.0 #its duration in seconds
durSamps = int(SRate * durSecs) # resulting number of samples
freq = 330 #frequency of the sine wave in Hz (cycles per second)
maxShort = 2** 15 # the largest possible signed 16-bit integer
# Make a floating point sinewave array using two handy functions from
NumPy:
t = arange(0, durSecs, 1.0/SRate)
sineWave = sin(2*pi*freq*t)
# print "max value of sine is: " + str( max(sineWave))
# Now write it into a 16-bit integer array, adjusting the values to
"fill" that format
sineWaveArray = array('h', [0]*len(sineWave))
for i in range(0, len(sineWave)):
sineWaveArray[i] = int(sineWave[i] * maxShort)
# Finally, write it to an AIFF file
filename = str(freq) + ".aiff"
out = aifc.open(filename, "wb")
out.aiff()
#out.setparams((1, 2, 44100, 100, 'NONE', 'not compressed'))
out.setparams((1, 2, int(SRate), int(durSamps), 'NONE', 'not
compressed'))
out.writeframes(sineWaveArray.tostring()) # this is the crucial
step: the .tostring() method forces this into the string format that
AIFC requires
out.close()
print "Successfully wrote a sine wave of frequency " + str(freq) + "
to file: " + filename
More information about the Numpy-discussion
mailing list