def execute_flac_convert(): """ Cycles through test_data, converting all flac to wav Script includes a utility remove spaces and problem characters from file name """ files = [f for f in glob('*.flac')] for af in files: x = flacread(af)[0] log.debug("Found a flac file: '%s'" % af) n = switch_ext(strip_all(af), '.wav') print ("Converting '%s' to: '%s'" % (af, n)) wavwrite(x, n, 44100)
def open(filename, sampleRate=44100): """ Open a file (WAV or MP3), return instance of this class with data loaded in Note that this is a static method. This is the preferred method of constructing this object """ filename = filename.lower() if filename.endswith('mp3') or filename.endswith('m4a'): ffmpeg = Popen([ "ffmpeg", "-i", filename, "-vn", "-acodec", "pcm_s16le", # Little Endian 16 bit PCM "-ac", "1", "-ar", str(sampleRate), # -ac = audio channels (1) "-f", "s16le", "-"], # -f wav for WAV file stdin = PIPE, stdout = PIPE, stderr = open(os.devnull, "w")) rawData = ffmpeg.stdout mp3Array = numpy.fromstring(rawData.read(), numpy.int16) mp3Array = mp3Array.astype('float32') / 32767.0 audioFile = mp3Array.view(AudioFile) audioFile.sampleRate = sampleRate audioFile.channels = 1 audioFile.format = pyaudio.paFloat32 return audioFile elif filename.endswith('wav') or filename.endswith('flac'): if filename.endswith('wav'): sampleRate, samples = scipy.io.wavfile.read(filename) if filename.endswith('flac'): samples, sampleRate, encoding = flacread(filename) # Convert to float samples = samples.astype('float32') / 32767.0 # Get left channel if len(samples.shape) > 1: samples = samples[:, 0] audioFile = samples.view(AudioFile) audioFile.sampleRate = sampleRate audioFile.channels = 1 audioFile.format = pyaudio.paFloat32 return audioFile
# coding : utf-8 # create by ztypl on 2017/9/7 from mdp import fastica from scikits.audiolab import flacread, flacwrite from numpy import array sig1, fs1, enc1 = flacread('file1.flac') sig2, fs2, enc2 = flacread('file2.flac') sig1 = sig1[:, 0] sig2 = sig2[:, 0] minlen = min(len(sig1), len(sig2)) sig1 = sig1[:minlen] sig2 = sig2[:minlen] mixed1 = sig1 + 0.5 * sig2 mixed2 = sig1 + 0.8 * sig2 mixed = array([mixed1, mixed2]).T flacwrite(mixed, 'Mixed2.flac')
if len(sys.argv) == 2: inFile = sys.argv[1] else: if len(sys.argv) > 2: quit("only one optional argument: path to wav file that is to be analyzed") else: # inFile = 'audio-samples/b112.d54.t481481.flac' # inFile = 'audio-samples/b112.d92.t5639583333.flac' # inFile = 'audio-samples/b1136.d94.t282148.flac' # inFile = 'audio-samples/b1136.d94.t24981322.flac' # inFile = 'audio-samples/b1233.d48.t4839703.flac' # inFile = 'audio-samples/b1233.d92.t44511791.flac' # inFile = 'audio-samples/b1592.d54.t66047281.flac' inFile = 'audio-samples/b1592.d91.t65217093.flac' data, sample_freq, encoding = flacread(inFile) timeArray = np.arange(0.0, len(data), 1) timeArray = timeArray / sample_freq timeArray = timeArray * 1000 # scale to milliseconds NFFT = 1000 # the length of the windowing segments fft = fft(data) # FFT bp = fft[:] # copy for manipulating //currently not needed ibp = ifft(bp) # inverse FFT for spectrogram ax1 = plt.subplot(4, 1, 1) plt.plot(timeArray, data) plt.subplot(4, 1, 2) Pxx, freqs, bins, im = plt.specgram(data, Fs=sample_freq)
""" # Compute Fourier transform of windowed signal windowed = sig * blackmanharris(len(sig)) f = rfft(windowed) # Find the peak and interpolate to get a more accurate peak i = argmax(abs(f)) # Just use this for less-accurate, naive version true_i = parabolic(log(abs(f)), i)[0] # Convert to equivalent frequency return fs * true_i / len(windowed) filename = '440.wav' print('Reading file "%s"\n' % filename) try: signal, fs = sf.read(filename) except NameError: signal, fs, enc = flacread(filename) print('Calculating frequency from FFT:'), start_time = time() print('%f Hz' % freq_from_fft(signal, fs)) print('Time elapsed: %.3f s\n' % (time() - start_time)) print('Calculating frequency from zero crossings:'), start_time = time() print('%f Hz' % freq_from_crossings(signal, fs)) print('Time elapsed: %.3f s\n' % (time() - start_time))
for x in range(2,maxharms): a = copy(c[::x]) #Should average or maximum instead of decimating # max(c[::x],c[1::x],c[2::x],...) c = c[:len(a)] i = argmax(abs(c)) true_i = parabolic(abs(c), i)[0] print 'Pass %d: %f Hz' % (x, fs * true_i / len(windowed)) c *= a subplot(maxharms,1,x) plot(log(c)) show() filename ='/angry/1.wav' print 'Reading file "%s"\n' % filename signal, fs, enc = flacread(filename) print 'Calculating frequency from FFT:', start_time = time() print '%f Hz' % freq_from_fft(signal, fs) print 'Time elapsed: %.3f s\n' % (time() - start_time) print 'Calculating frequency from zero crossings:', start_time = time() print '%f Hz' % freq_from_crossings(signal, fs) print 'Time elapsed: %.3f s\n' % (time() - start_time) print 'Calculating frequency from autocorrelation:', start_time = time() print '%f Hz' % freq_from_autocorr(signal, fs) print 'Time elapsed: %.3f s\n' % (time() - start_time)
# coding : utf-8 # create by ztypl on 2017/9/7 from mdp import fastica from scikits.audiolab import flacread, flacwrite from numpy import abs, max # Load in the stereo file recording, fs, enc = flacread('Mixed3.flac') # Perform FastICA algorithm on the two channels sources = fastica(recording) # The output levels of this algorithm are arbitrary, so normalize them to 1.0. sources /= max(abs(sources), axis=0) source1 = sources[:, 0] source2 = sources[:, 1] # Write back to a file flacwrite(source1, 'source21.flac', fs, enc) flacwrite(source2, 'source22.flac', fs, enc)
from scikits.audiolab import flacread, play from scipy.signal import remez, lfilter #from pylab import * import matplotlib.pyplot as plt import numpy as np # convert mp3, read wav #mp3filename = 'XC124158.mp3' flacfile = '/home/tdh5188/workspace/comp596/Convnet-Music-Classification/03-Time.flac' #wname = mktemp('.wav') #print(wname) #check_call(['avconv', '-i', flacfile, wname]) #sig, fs, enc = flacread(wname) sig, fs, enc = flacread(flacfile) #os.unlink(wname) # bandpass filter bands = np.array([0, 3500, 4000, 5500, 6000, fs / 2.0]) / fs desired = [0, 1, 0] b = remez(513, bands, desired) sig_filt = lfilter(b, 1, sig) sig_filt /= 1.05 * max(abs(sig_filt)) # normalize subplot(211) specgram(sig, Fs=fs, NFFT=1024, noverlap=0) axis('tight') axis(ymax=8000) title('Original')
print fft w = np.fft.fft(data) ws = w # [:(len(w)/2)] freqs = np.fft.fftfreq(len(ws)) print 'koperek!' print(freqs.min(),freqs.max()) # (-0.5, 0.499975) # Find the peak in the coefficients idx=np.argmax(np.abs(ws)**2) freq=freqs[idx] freq_in_hertz=abs(freq*sample_frequency) print 'frequency new: %f' % freq_in_hertz # 439.8975 signal, fs, enc = flacread("l2.flac") print 'frequency autocorr: %f' % freq_from_autocorr(signal,fs) fft = scipy.fft(data) print len(fft) subplot(222) plot(t,data) subplot(223) plot(t,fft) subplot(224) Pxx, freqs, bins, im = specgram(data, Fs=sample_frequency, NFFT=1024, noverlap=900, cmap=cm.gist_heat) show()