#NEWLINE# @ staticmethod #NEWLINE# def open( filename , sampleRate = 44100 ) : #NEWLINE# #INDENT# '\n        Open a file (WAV or MP3), return instance of this class with data loaded in\n        Note that this is a static method. This is the preferred method of constructing this object\n        ' #NEWLINE# ( _ , ext ) = os . path . splitext ( filename ) #NEWLINE# if ( ext . endswith ( 'mp3' ) or ext . endswith ( 'm4a' ) ) : #NEWLINE# #INDENT# ffmpeg = Popen ( [ 'ffmpeg' , '-i' , filename , '-vn' , '-acodec' , 'pcm_s16le' , '-ac' , '1' , '-ar' , str ( sampleRate ) , '-f' , 's16le' , '-' ] , stdin = PIPE , stdout = PIPE , stderr = open ( os . devnull , 'w' ) ) #NEWLINE# rawData = ffmpeg . stdout #NEWLINE# mp3Array = numpy . fromstring ( rawData . read ( ) , numpy . int16 ) #NEWLINE# mp3Array = ( mp3Array . astype ( 'float32' ) / 32767.0 ) #NEWLINE# audioFile = mp3Array . view ( AudioFile ) #NEWLINE# audioFile . sampleRate = sampleRate #NEWLINE# audioFile . channels = 1 #NEWLINE# audioFile . format = pyaudio . paFloat32 #NEWLINE# return audioFile #NEWLINE# #UNINDENT# else : #NEWLINE# #INDENT# if ext . endswith ( 'wav' ) : #NEWLINE# #INDENT# ( sampleRate , samples ) = scipy . io . wavfile . read ( filename ) #NEWLINE# samples = ( samples . astype ( 'float32' ) / 32767.0 ) #NEWLINE# if ( len ( samples . shape ) > 1 ) : #NEWLINE# #INDENT# samples = samples [ : , 0 ] #NEWLINE# #UNINDENT# audioFile = samples . view ( AudioFile ) #NEWLINE# audioFile . sampleRate = sampleRate #NEWLINE# audioFile . channels = 1 #NEWLINE# audioFile . format = pyaudio . paFloat32 #NEWLINE# return audioFile