我们从Python开源项目中,提取了以下27个代码示例,用于说明如何使用librosa.istft()。
def griffin_lim(mag, phase_angle, n_fft, hop, num_iters): """Iterative algorithm for phase retrival from a magnitude spectrogram. Args: mag: Magnitude spectrogram. phase_angle: Initial condition for phase. n_fft: Size of the FFT. hop: Stride of FFT. Defaults to n_fft/2. num_iters: Griffin-Lim iterations to perform. Returns: audio: 1-D array of float32 sound samples. """ fft_config = dict(n_fft=n_fft, win_length=n_fft, hop_length=hop, center=True) ifft_config = dict(win_length=n_fft, hop_length=hop, center=True) complex_specgram = inv_magphase(mag, phase_angle) for i in range(num_iters): audio = librosa.istft(complex_specgram, **ifft_config) if i != num_iters - 1: complex_specgram = librosa.stft(audio, **fft_config) _, phase = librosa.magphase(complex_specgram) phase_angle = np.angle(phase) complex_specgram = inv_magphase(mag, phase_angle) return audio
def test_stft_istft(self): try: import librosa ds = F.load_digit_wav() name = ds.keys()[0] path = ds[name] y, _ = speech.read(path, pcm=True) hop_length = int(0.01 * 8000) stft = signal.stft(y, n_fft=256, hop_length=hop_length, window='hann') stft_ = librosa.stft(y, n_fft=256, hop_length=hop_length, window='hann') self.assertTrue(np.allclose(stft, stft_.T)) y1 = signal.istft(stft, hop_length=hop_length, window='hann') y2 = librosa.istft(stft_, hop_length=hop_length, window='hann') self.assertTrue(np.allclose(y1, y2)) except ImportError: print("test_stft_istft require librosa.")
def NMF(stft, n_sources): """ Sound source separation using NMF :param stft: the short-time Fourier Transform of the signal :param n_sources: the number of sources :returns: - Ys: sources """ print "Computing approximations" W, H = librosa.decompose.decompose(np.abs(stft), n_components=n_sources, sort=True) print "Reconstructing signals" Ys = list(scipy.outer(W[:,i], H[i])*np.exp(1j*np.angle(stft)) for i in xrange(n_sources)) print "Saving sound arrays" ys = [librosa.istft(Y) for Y in Ys] del Ys return ys
def istft_mc(X,hop,dtype=np.float32,nsampl=None,flag_noDiv=0,window=None): #assumes X is of shape F x nfram x nch, where F=Nwin/2+1 #returns xr of shape nch x nsampl N=2*(X.shape[0]-1) nch=X.shape[2] for ich in range(0,nch): X0=X[:,:,ich] if flag_noDiv: x0r=istft_noDiv(X0,hop_length=hop,center=False,window=window,dtype=dtype) else: x0r=librosa.core.istft(X0,hop_length=hop,center=False,window=window,dtype=dtype) if ich==0: xr=np.zeros((nch,len(x0r))).astype(dtype) xr[0,:]=x0r else: xr[ich,:]=x0r #trim off extra zeros nfram=xr.shape[1] xr=xr[:,0:(nfram-N)] nfram=xr.shape[1] xr=xr[:,N:] if not nsampl is None: xr=xr[:,0:nsampl] return xr, N
def griffinlim(spectrogram, n_iter=50, window='hann', n_fft=2048, win_length=2048, hop_length=-1, verbose=False): if hop_length == -1: hop_length = n_fft // 4 angles = np.exp(2j * np.pi * np.random.rand(*spectrogram.shape)) t = tqdm(range(n_iter), ncols=100, mininterval=2.0, disable=not verbose) for i in t: full = np.abs(spectrogram).astype(np.complex) * angles inverse = librosa.istft(full, hop_length = hop_length, win_length = win_length, window = window) rebuilt = librosa.stft(inverse, n_fft = n_fft, hop_length = hop_length, win_length = win_length, window = window) angles = np.exp(1j * np.angle(rebuilt)) if verbose: diff = np.abs(spectrogram) - np.abs(rebuilt) t.set_postfix(loss=np.linalg.norm(diff, 'fro')) full = np.abs(spectrogram).astype(np.complex) * angles inverse = librosa.istft(full, hop_length = hop_length, win_length = win_length, window = window) return inverse
def reconstructed_audio_from_spectrogram(self) -> ndarray: return librosa.istft(self._complex_spectrogram(), win_length=self.fourier_window_length, hop_length=self.hop_length)
def _griffin_lim(S, n_fft, win_length, hop_length, num_iters): angles = np.exp(2j * np.pi * np.random.rand(*S.shape)) S_complex = np.abs(S).astype(np.complex) for i in range(num_iters): if i > 0: angles = np.exp(1j * np.angle(librosa.stft(y=y, n_fft=n_fft, hop_length=hop_length, win_length=win_length))) y = librosa.istft(S_complex * angles, hop_length=hop_length, win_length=win_length) return y
def create_audio_from_spectrogram(spec): spec_transposed = tf.transpose(spec).eval() return librosa.istft(spec_transposed, Config.hop_length)
def invert_spectrogram(spectrogram): ''' spectrogram: [f, t] ''' return librosa.istft(spectrogram, hp.hop_length, win_length=hp.win_length, window="hann")
def recons_spec_phase(Sxx_r, phase): Sxx_r = np.sqrt(10**Sxx_r) R = np.multiply(Sxx_r , phase) result = librosa.istft(R, hop_length=256, win_length=512, window=scipy.signal.hamming) return result
def expand(self, audio): ori_len = audio.shape[0] tmp = resample(audio, r=0.5, type='sinc_best') down_len = tmp.shape[0] tmp = resample(tmp, r=(ori_len+1) / float(down_len), type='sinc_best') tmp = librosa.stft(audio, 1024) phase = np.divide(tmp, np.abs(tmp)) spec_input = np.abs(librosa.stft(audio, 1024))[0:n_input, ::] spec_input = spec_input[::, 0:spec_input.shape[1]//n_len*n_len] spec_input = np.split(spec_input, spec_input.shape[1]//n_len, axis=1) spec_input = np.asarray(spec_input) spec_input = np.expand_dims(spec_input, axis=-1) feed_dict = {self.input_op: np.log1p(spec_input) / 12.0} debug = self.sess.run(self.debug_op, feed_dict=feed_dict) np.save('debug.npy', debug) S = self.sess.run(self.eva_op, feed_dict=feed_dict) S[S >= 5e3] = 5e3 S[S <= 0] = 0 print ('mean', np.mean(S)) print (np.sum(np.isinf(S))) S = np.squeeze(np.concatenate(np.split(S, S.shape[0]), axis=2), axis=(0, -1)) phase = phase[..., :S.shape[1]] print (phase.shape) print (S.shape) print (np.sum(np.isinf(np.multiply(S, phase)))) X = librosa.istft(np.multiply(S, phase)) return X
def mag2spec_and_audio_wiener(xhat, recons, MS, MSphase, arguments): #xhat = xhat.cpu().numpy() #recons = recons.cpu().numpy() try: # pytorch case MS = MS.cpu().numpy() MSphase = MSphase.cpu().numpy() Nmix = MSphase.shape[0] maghats = np.split(xhat, Nmix, axis=0) reconss = np.split(recons, Nmix, axis=0) mixmags = np.split(MS, Nmix, axis=0) phases = np.split(MSphase, Nmix, axis=0) except: maghats = [xhat] reconss = [recons] mixmags = [MS] phases = [MSphase] all_audio = [] eps = 1e-20 for maghat, recons, mixmag, phase in zip(maghats, reconss, mixmags, phases): mask = (maghat / (recons + eps)) all_audio.append(lr.istft((mask*mixmag*np.exp(1j*phase)).transpose(), win_length=arguments.win_length)) return all_audio, maghats
def mag2spec_and_audio(xhat, MSphase, arguments): MSphase = MSphase.cpu().numpy() Nmix = MSphase.shape[0] mags = np.split(xhat, Nmix, axis=0) phases = np.split(MSphase, Nmix, axis=0) all_audio = [] for mag, phase in zip(mags, phases): all_audio.append(lr.istft((mag*np.exp(1j*phase.squeeze())).transpose(), win_length=arguments.win_length)) return all_audio, mags
def relaxed_music_remix(self, neg_arous_dir, files, decisions): neg_arousal_h = self.sad_music_remix(neg_arous_dir, files, decisions, harmonic = True) relaxed_harmonic = librosa.istft(neg_arousal_h) onsets = hfc_onsets(np.float32(relaxed_harmonic)) interv = seconds_to_indices(onsets) steps = overlapped_intervals(interv) output = librosa.effects.remix(relaxed_harmonic, steps[::-1], align_zeros = True) output = librosa.effects.pitch_shift(output, sr = 44100, n_steps = 4) remix_filename = 'data/emotions/remixes/relaxed/'+str(time.strftime("%Y%m%d-%H:%M:%S"))+'multitag_remix.ogg' MonoWriter(filename=remix_filename, format = 'ogg', sampleRate = 44100)(output) subprocess.call(["ffplay", "-nodisp", "-autoexit", remix_filename])
def angry_music_remix(self, pos_arous_dir, files, decisions): pos_arousal_h = self.happy_music_remix(pos_arous_dir, files, decisions, harmonic = True) angry_harmonic = librosa.istft(pos_arousal_h) interv = RhythmExtractor2013()(angry_harmonic)[1] * 44100 steps = overlapped_intervals(interv) output = librosa.effects.remix(angry_harmonic, steps, align_zeros = True) output = librosa.effects.pitch_shift(output, sr = 44100, n_steps = 3) remix_filename = 'data/emotions/remixes/angry/'+str(time.strftime("%Y%m%d-%H:%M:%S"))+'multitag_remix.ogg' MonoWriter(filename=remix_filename, format = 'ogg', sampleRate = 44100)(np.float32(output)) subprocess.call(["ffplay", "-nodisp", "-autoexit", remix_filename])
def _griffin_lim_tensorflow(S): '''TensorFlow implementation of Griffin-Lim Based on https://github.com/Kyubyong/tensorflow-exercises/blob/master/Audio_Processing.ipynb ''' with tf.variable_scope('griffinlim'): # TensorFlow's stft and istft operate on a batch of spectrograms; create batch of size 1 S = tf.expand_dims(S, 0) S_complex = tf.identity(tf.cast(S, dtype=tf.complex64)) y = _istft_tensorflow(S_complex) for i in range(hparams.griffin_lim_iters): est = _stft_tensorflow(y) angles = est / tf.cast(tf.maximum(1e-8, tf.abs(est)), tf.complex64) y = _istft_tensorflow(S_complex * angles) return tf.squeeze(y, 0)
def _istft(y): _, hop_length, win_length = _stft_parameters() return librosa.istft(y, hop_length=hop_length, win_length=win_length)
def ispecgram(spec, n_fft=512, hop_length=None, mask=True, log_mag=True, re_im=False, dphase=True, mag_only=True, num_iters=1000): """Inverse Spectrogram using librosa. Args: spec: 3-D specgram array [freqs, time, (mag_db, dphase)]. n_fft: Size of the FFT. hop_length: Stride of FFT. Defaults to n_fft/2. mask: Reverse the mask of the phase derivative by the magnitude. log_mag: Use the logamplitude. re_im: Output Real and Imag. instead of logMag and dPhase. dphase: Use derivative of phase instead of phase. mag_only: Specgram contains no phase. num_iters: Number of griffin-lim iterations for mag_only. Returns: audio: 1-D array of sound samples. Peak normalized to 1. """ if not hop_length: hop_length = n_fft // 2 ifft_config = dict(win_length=n_fft, hop_length=hop_length, center=True) if mag_only: mag = spec[:, :, 0] phase_angle = np.pi * np.random.rand(*mag.shape) elif re_im: spec_real = spec[:, :, 0] + 1.j * spec[:, :, 1] else: mag, p = spec[:, :, 0], spec[:, :, 1] if mask and log_mag: p /= (mag + 1e-13 * np.random.randn(*mag.shape)) if dphase: # Roll up phase phase_angle = np.cumsum(p * np.pi, axis=1) else: phase_angle = p * np.pi # Magnitudes if log_mag: mag = (mag - 1.0) * 120.0 mag = 10**(mag / 20.0) phase = np.cos(phase_angle) + 1.j * np.sin(phase_angle) spec_real = mag * phase if mag_only: audio = griffin_lim( mag, phase_angle, n_fft, hop_length, num_iters=num_iters) else: audio = librosa.core.istft(spec_real, **ifft_config) return np.squeeze(audio / audio.max())
def bpm_cluster_files(files_dir, descriptor, euclidean_labels, files): """ locate bpm according to clusters in clusters directories :param files_dir: directory where sounds are located :param descriptor: descriptor used for similarity :param euclidean_labels: groups of clusters :param files: the .json files (use get_files) """ groups = [[] for i in xrange(len(np.unique(euclidean_labels)))] for i, x in enumerate(euclidean_labels): groups[x].append([files[0][i], x]) for c in xrange(len(groups)): if not os.path.exists(files_dir+'/tempo/'+str(c)): os.makedirs(files_dir+'/tempo/'+str(c)) os.makedirs(files_dir+'/tempo/'+str(c)+'/remix') for t in groups[c]: for s in list(os.walk(files_dir+'/tempo', topdown=False))[-1][-1]: if str(t[0]).split('.json')[0] == s.split('tempo.ogg')[0]: shutil.copy(files_dir+'/tempo/'+s, files_dir+'/tempo/'+str(c)+'/'+s) print str().join((str(t))) try: simil_audio = [MonoLoader(filename=files_dir+'/tempo/'+str(c)+f)() for f in list(os.walk(files_dir+'/tempo/'+str(c), topdown = False))[-1][-1]] audio0 = scratch_music(choice(simil_audio)) audio1 = scratch_music(choice(simil_audio)) del simil_audio audio_N = min([len(i) for i in [audio0, audio1]]) audio_samples = [i[:audio_N]/i.max() for i in [audio0, audio1]] simil_x = np.array(audio_samples).sum(axis=0) del audio_samples simil_x = 0.5*simil_x/simil_x.max() h, p = librosa.decompose.hpss(librosa.core.stft(simil_x)) del simil_x, h p = librosa.istft(p) MonoWriter(filename=files_dir+'/tempo/'+str(c)+'/remix/'+'similarity_mix_bpm.ogg', format = 'ogg', sampleRate = 44100)(p) del p except Exception, e: print logger.exception(e) continue # save mfcc clusters files in mfcc clusters directory
def mfcc_cluster_files(files_dir, descriptor, euclidean_labels, files): """ locate mfcc files according to clusters in clusters directories :param files_dir: directory where sounds are located :param descriptor: descriptor used for similarity :param euclidean_labels: groups of clusters :param files: the .json files (use get_files) """ groups = [[] for i in xrange(len(np.unique(euclidean_labels)))] for i, x in enumerate(euclidean_labels): groups[x].append([files[0][i], x]) for c in xrange(len(groups)): if not os.path.exists(files_dir+'/mfcc/'+str(c)): os.makedirs(files_dir+'/mfcc/'+str(c)) os.makedirs(files_dir+'/mfcc/'+str(c)+'/remix') for t in groups[c]: for s in list(os.walk(files_dir+'/mfcc', topdown=False))[-1][-1]: if str(t[0]).split('.')[0] == s.split('mfcc.ogg')[0]: shutil.copy(files_dir+'/mfcc/'+s, files_dir+'/mfcc/'+str(c)+'/'+s) print t try: simil_audio = [MonoLoader(filename=files_dir+'/mfcc/'+str(c)+f)() for f in list(os.walk(files_dir+'/mfcc/'+str(c), topdown = False))[-1][-1]] audio0 = scratch_music(choice(simil_audio)) audio1 = scratch_music(choice(simil_audio)) del simil_audio audio_N = min([len(i) for i in [audio0, audio1]]) audio_samples = [i[:audio_N]/i.max() for i in [audio0, audio1]] simil_x = np.array(audio_samples).sum(axis=0) del audio_samples simil_x = 0.5*simil_x/simil_x.max() h, p = librosa.decompose.hpss(librosa.core.stft(simil_x)) del simil_x, p h = librosa.istft(h) MonoWriter(filename=files_dir+'/mfcc/'+str(c)+'/remix/'+'similarity_mix_mfcc.ogg', format = 'ogg', sampleRate = 44100)(h) del h except Exception, e: print e continue # save contrast cluster files in contrast cluster directory
def contrast_cluster_files(files_dir, descriptor, euclidean_labels, files): """ locate contrast files according to clusters in clusters directories :param files_dir: directory where sounds are located :param descriptor: descriptor used for similarity :param euclidean_labels: groups of clusters :param files: the .json files (use get_files) """ groups = [[] for i in xrange(len(np.unique(euclidean_labels)))] for i, x in enumerate(euclidean_labels): groups[x].append([files[0][i], x]) for c in xrange(len(groups)): if not os.path.exists(files_dir+'/valleys/'+str(c)): os.makedirs(files_dir+'/valleys/'+str(c)) os.makedirs(files_dir+'/valleys/'+str(c)+'/remix') for t in groups[c]: for s in list(os.walk(files_dir+'/valleys', topdown=False))[-1][-1]: if str(t[0]).split('.')[0] == s.split('valleys.ogg')[0]: shutil.copy(files_dir+'/valleys/'+s, files_dir+'/valleys/'+str(c)+'/'+s) print t try: simil_audio = [MonoLoader(filename=files_dir+'/valleys/'+str(c)+f)() for f in list(os.walk(files_dir+'/valleys/'+str(c), topdown = False))[-1][-1]] audio0 = scratch_music(choice(simil_audio)) audio1 = scratch_music(choice(simil_audio)) del simil_audio audio_N = min([len(i) for i in [audio0, audio1]]) audio_samples = [i[:audio_N]/i.max() for i in [audio0, audio1]] simil_x = np.array(audio_samples).sum(axis=0) del audio_samples simil_x = 0.5*simil_x/simil_x.max() h, p = librosa.decompose.hpss(librosa.core.stft(simil_x)) del simil_x, p h = librosa.istft(h) MonoWriter(filename=files_dir+'/valleys/'+str(c)+'/remix/'+'similarity_mix_valleys.ogg', format = 'ogg', sampleRate = 44100)(h) del h except Exception, e: print e continue # save centroid cluster files in centroid cluster directory
def loudness_cluster_files(files_dir, descriptor, euclidean_labels, files): """ locate loudness files according to clusters in clusters directories :param files_dir: directory where sounds are located :param descriptor: descriptor used for similarity :param euclidean_labels: groups of clusters :param files: the .json files (use get_files) """ groups = [[] for i in xrange(len(np.unique(euclidean_labels)))] for i, x in enumerate(euclidean_labels): groups[x].append([files[0][i], x]) for c in xrange(len(groups)): if not os.path.exists(files_dir+'/loudness/'+str(c)): os.makedirs(files_dir+'/loudness/'+str(c)) os.makedirs(files_dir+'/loudness/'+str(c)+'/remix') for t in groups[c]: for s in list(os.walk(files_dir+'/loudness', topdown=False))[-1][-1]: if str(t[0]).split('.')[0] == s.split('loudness.ogg')[0]: shutil.copy(files_dir+'/loudness/'+s, files_dir+'/loudness/'+str(c)+'/'+s) print t try: simil_audio = [MonoLoader(filename=files_dir+'/loudness/'+str(c)+f)() for f in list(os.walk(files_dir+'/loudness/'+str(c), topdown = False))[-1][-1]] audio0 = scratch_music(choice(simil_audio)) audio1 = scratch_music(choice(simil_audio)) del simil_audio audio_N = min([len(i) for i in [audio0, audio1]]) audio_samples = [i[:audio_N]/i.max() for i in [audio0, audio1]] simil_x = np.array(audio_samples).sum(axis=0) del audio_samples simil_x = 0.5*simil_x/simil_x.max() h, p = librosa.decompose.hpss(librosa.core.stft(simil_x)) del simil_x, p h = librosa.istft(h) MonoWriter(filename=files_dir+'/loudness/'+str(c)+'/remix/'+'similarity_mix_centroid.ogg', format = 'ogg', sampleRate = 44100)(h) del h except Exception, e: print e continue # save hfc cluster files in hfc cluster directory
def hfc_cluster_files(files_dir, descriptor, euclidean_labels, files): """ locate hfc files according to clusters in clusters directories :param files_dir: directory where sounds are located :param descriptor: descriptor used for similarity :param euclidean_labels: groups of clusters :param files: the .json files (use get_files) """ groups = [[] for i in xrange(len(np.unique(euclidean_labels)))] for i, x in enumerate(euclidean_labels): groups[x].append([files[0][i], x]) for c in xrange(len(groups)): if not os.path.exists(files_dir+'/hfc/'+str(c)): os.makedirs(files_dir+'/hfc/'+str(c)) os.makedirs(files_dir+'/hfc/'+str(c)+'/remix') for t in groups[c]: for s in list(os.walk(files_dir+'/hfc', topdown=False))[-1][-1]: if str(t[0]).split('.')[0] == s.split('hfc.ogg')[0]: shutil.copy(files_dir+'/hfc/'+s, files_dir+'/hfc/'+str(c)+'/'+s) print t try: simil_audio = [MonoLoader(filename=files_dir+'/hfc/'+str(c)+f)() for f in list(os.walk(files_dir+'/hfc/'+str(c), topdown = False))[-1][-1]] audio0 = scratch_music(choice(simil_audio)) audio1 = scratch_music(choice(simil_audio)) del simil_audio audio_N = min([len(i) for i in [audio0, audio1]]) audio_samples = [i[:audio_N]/i.max() for i in [audio0, audio1]] simil_x = np.array(audio_samples).sum(axis=0) del audio_samples simil_x = 0.5*simil_x/simil_x.max() h, p = librosa.decompose.hpss(librosa.core.stft(simil_x)) del simil_x, h p = librosa.istft(p) MonoWriter(filename=files_dir+'/hfc/'+str(c)+'/remix/'+'similarity_mix_hfc.ogg', format = 'ogg', sampleRate = 44100)(p) del p except Exception, e: print e continue # save inharmonicity cluster files in inharmonicity cluster directory
def inharmonicity_cluster_files(files_dir, descriptor, euclidean_labels, files): """ locate inharmonicity files according to clusters in clusters directories :param files_dir: directory where sounds are located :param descriptor: descriptor used for similarity :param euclidean_labels: groups of clusters :param files: the .json files (use get_files) """ groups = [[] for i in xrange(len(np.unique(euclidean_labels)))] for i, x in enumerate(euclidean_labels): groups[x].append([files[0][i], x]) for c in xrange(len(groups)): if not os.path.exists(files_dir+'/inharmonicity/'+str(c)): os.makedirs(files_dir+'/inharmonicity/'+str(c)) os.makedirs(files_dir+'/inharmonicity/'+str(c)+'/remix') for t in groups[c]: for s in list(os.walk(files_dir+'/inharmonicity', topdown=False))[-1][-1]: if str(t[0]).split('.')[0] == s.split('inharmonicity.ogg')[0]: shutil.copy(files_dir+'/inharmonicity/'+s, files_dir+'/inharmonicity/'+str(c)+'/'+s) print t try: simil_audio = [MonoLoader(filename=files_dir+'/inharmonicity/'+str(c)+f)() for f in list(os.walk(files_dir+'/inharmonicity/'+str(c), topdown = False))[-1][-1]] audio0 = scratch_music(choice(simil_audio)) audio1 = scratch_music(choice(simil_audio)) del simil_audio audio_N = min([len(i) for i in [audio0, audio1]]) audio_samples = [i[:audio_N]/i.max() for i in [audio0, audio1]] simil_x = np.array(audio_samples).sum(axis=0) del audio_samples simil_x = 0.5*simil_x/simil_x.max() h, p = librosa.decompose.hpss(librosa.core.stft(simil_x)) del simil_x, p h = librosa.istft(h) MonoWriter(filename=files_dir+'/inharmonicity/'+str(c)+'/remix/'+'similarity_mix_inharmonicity.ogg', format = 'ogg', sampleRate = 44100)(h) del h except Exception, e: print e continue # save dissonance cluster files in dissonance cluster directory
def dissonance_cluster_files(files_dir, descriptor, euclidean_labels, files): """ locate dissonance files according to clusters in clusters directories :param files_dir: directory where sounds are located :param descriptor: descriptor used for similarity :param euclidean_labels: groups of clusters :param files: the .json files (use get_files) """ groups = [[] for i in xrange(len(np.unique(euclidean_labels)))] for i, x in enumerate(euclidean_labels): groups[x].append([files[0][i], x]) for c in xrange(len(groups)): if not os.path.exists(files_dir+'/dissonance/'+str(c)): os.makedirs(files_dir+'/dissonance/'+str(c)) os.makedirs(files_dir+'/dissonance/'+str(c)+'/remix') for t in groups[c]: for s in list(os.walk(files_dir+'/dissonance', topdown=False))[-1][-1]: if str(t[0]).split('.')[0] == s.split('dissonance.ogg')[0]: shutil.copy(files_dir+'/dissonance/'+s, files_dir+'/dissonance/'+str(c)+'/'+s) print t try: simil_audio = [MonoLoader(filename=files_dir+'/dissonance/'+str(c)+f)() for f in list(os.walk(files_dir+'/dissonance/'+str(c), topdown = False))[-1][-1]] audio0 = scratch_music(choice(simil_audio)) audio1 = scratch_music(choice(simil_audio)) del simil_audio audio_N = min([len(i) for i in [audio0, audio1]]) audio_samples = [i[:audio_N]/i.max() for i in [audio0, audio1]] simil_x = np.array(audio_samples).sum(axis=0) del audio_samples simil_x = 0.5*simil_x/simil_x.max() h, p = librosa.decompose.hpss(librosa.core.stft(simil_x)) del simil_x, p h = librosa.istft(h) MonoWriter(filename=files_dir+'/dissonance/'+str(c)+'/remix/'+'similarity_mix_dissonance.ogg', format = 'ogg', sampleRate = 44100)(h) del h except Exception, e: print e continue # save attack cluster files in attack cluster directory
def attack_cluster_files(files_dir, descriptor, euclidean_labels, files): """ locate attack files according to clusters in clusters directories :param files_dir: directory where sounds are located :param descriptor: descriptor used for similarity :param euclidean_labels: groups of clusters :param files: the .json files (use get_files) """ groups = [[] for i in xrange(len(np.unique(euclidean_labels)))] for i, x in enumerate(euclidean_labels): groups[x].append([files[0][i], x]) for c in xrange(len(groups)): if not os.path.exists(files_dir+'/attack/'+str(c)): os.makedirs(files_dir+'/attack/'+str(c)) os.makedirs(files_dir+'/attack/'+str(c)+'/remix') for t in groups[c]: for s in list(os.walk(files_dir+'/attack', topdown=False))[-1][-1]: if str(t[0]).split('.')[0] == s.split('attack.ogg')[0]: shutil.copy(files_dir+'/attack/'+s, files_dir+'/attack/'+str(c)+'/'+s) print t try: simil_audio = [MonoLoader(filename=files_dir+'/attack/'+str(c)+f)() for f in list(os.walk(files_dir+'/attack/'+str(c), topdown = False))[-1][-1]] audio0 = scratch_music(choice(simil_audio)) audio1 = scratch_music(choice(simil_audio)) del simil_audio audio_N = min([len(i) for i in [audio0, audio1]]) audio_samples = [i[:audio_N]/i.max() for i in [audio0, audio1]] simil_x = np.array(audio_samples).sum(axis=0) del audio_samples simil_x = 0.5*simil_x/simil_x.max() h, p = librosa.decompose.hpss(librosa.core.stft(simil_x)) del simil_x, h p = librosa.istft(p) MonoWriter(filename=files_dir+'/attack/'+str(c)+'/remix/'+'similarity_mix_attack.ogg', format = 'ogg', sampleRate = 44100)(p) del p except Exception, e: print e continue # save duration cluster files in dissonance cluster directory