def plotstft(audiopath, binsize=2**10, plotpath=None, colormap="jet"): audio = AudioFileClip(audiopath) samplerate, samples = audio.fps, audio.to_soundarray() s = stft(samples, binsize) sshow, freq = logscale_spec(s, factor=1.0, sr=samplerate) ims = 20.*np.log10(np.abs(sshow)/10e-6) # amplitude to decibel timebins, freqbins = np.shape(ims) plt.figure(figsize=(15, 7.5)) plt.imshow(np.transpose(ims), origin="lower", aspect="auto", cmap=colormap, interpolation="none") plt.colorbar() plt.xlabel("time (s)") plt.ylabel("frequency (hz)") plt.xlim([0, timebins-1]) plt.ylim([0, freqbins]) xlocs = np.float32(np.linspace(0, timebins-1, 5)) plt.xticks(xlocs, ["%.02f" % l for l in ((xlocs*len(samples)/timebins)+(0.5*binsize))/samplerate]) ylocs = np.int16(np.round(np.linspace(0, freqbins-1, 10))) plt.yticks(ylocs, ["%.02f" % freq[i] for i in ylocs]) if plotpath: plt.savefig(plotpath, bbox_inches="tight") else: plt.show() plt.clf()
def getAudioData(path): """ Get audio data (sound, sr, fps, duration, andio) :param path: string - path of audio file source """ from moviepy.editor import AudioFileClip audio = AudioFileClip(path) fps = 24 sound = audio.to_soundarray() sr = int(sound.shape[0] / audio.duration) return sound, sr, fps, audio.duration, audio
def _spectrogram(filename: str, path: str): ''' Generates a Spectrogram from an mp3 and saves it''' try: audio = AudioFileClip(f'{path}/{filename}.mp3') audio_data = audio.to_soundarray() audio_data = audio_data[:, 0] vmin = 20 * np.log10(np.max(audio_data)) - 100 fig = plt.figure() plt.specgram(audio_data, Fs=SPECTROGRAM_PARAMS['FRAME_RATE'], NFFT=SPECTROGRAM_PARAMS['NFFT'], window=np.hamming(512), cmap='inferno', vmin=vmin) fig.savefig(f'{path}/{filename}.jpg') plt.close(fig) LOG.info(f'Successfully saved Spectrogram {filename}.jpg to {path}') except Exception as ex: LOG.error( f'Failed to convert {filename} to Spectrogram. Reason="{ex}"')
def spectrogram( filename: str, species: str, nfft: int = 512, window=np.hamming(512), format=AudioFormat.MP3, frame_rate: int = 22050, ): ''' Plot a spectrogram for WAV file and save ''' # TODO: Overlap? # TODO: Ignoring frequency range try: if format == AudioFormat.WAV: audio_data = read_wav(filename, species) elif format == AudioFormat.MP3: audio = AudioFileClip( f'{SPECIES_RAW_AUDIO_PATH}{species}/mp3/{filename}.mp3') audio_data = audio.to_soundarray() audio_data = audio_data[:, 0] else: return NotImplementedError() fig = plt.figure() vmin = 20 * np.log10(np.max(audio_data)) - 100 plt.specgram( audio_data, Fs=frame_rate, NFFT=nfft, window=window, cmap='inferno', vmin=vmin, ) fig.savefig(f'{SPECTROGRAM_PATH}{species}/{filename}.jpg') plt.close(fig) except Exception as ex: print( f'ERROR: Failed to convert {filename} to Spectrogram. Reason="{ex}"' )
def extract_wav(file): audioclip = AudioFileClip(file) audioclip_arr = audioclip.to_soundarray(fps=48000, nbytes=2) return audioclip_arr
else: all_square[i].image = all_square[i].image_empty all_square[i].is_triggered = False all_square[i].frame_tick = 0 #print(all_square[0].frame_tick) #initialisation score = 0 song = "myaudio.mp3" # Get the sound (we be an array with values between 0 and 1) audio = AudioFileClip(song) # could be a wav, ogg... 13553253 vlcplayer = vlc.MediaPlayer(song) sound_array = audio.to_soundarray() PlaybackThread = threading.Thread(target=vlcplayer.play) PlaybackThread.daemon = True GAME_WIDTH = 720 GAME_HEIGHT = 720 all_square = [] game_map = [] #initialisation de la sequence de pop à partir du son charger #sound_array a pour valeur -1 à +1 donc on on décale vers 0 à 2 for i in range (0,len(sound_array)): sound_array[i] += 1 #print(sound_array[i])
all_square[i].frame_tick = 0 else: all_square[i].image = all_square[i].image_empty all_square[i].is_triggered = False all_square[i].frame_tick = 0 #print(all_square[0].frame_tick) #initialisation score = 0 song = "myaudio.mp3" # Get the sound (we be an array with values between 0 and 1) audio = AudioFileClip(song) # could be a wav, ogg... 13553253 vlcplayer = vlc.MediaPlayer(song) sound_array = audio.to_soundarray() PlaybackThread = threading.Thread(target=vlcplayer.play) PlaybackThread.daemon = True GAME_WIDTH = 720 GAME_HEIGHT = 720 all_square = [] game_map = [] #initialisation de la sequence de pop à partir du son charger #sound_array a pour valeur -1 à +1 donc on on décale vers 0 à 2 for i in range(0, len(sound_array)): sound_array[i] += 1 #print(sound_array[i])