librosa & soundfile & ffmpeg === Google Colab: https://colab.research.google.com/drive/1yL9xmNR0iurtKI86UMIkMllhpjrUkxrg?usp=sharing Install library (ex. .mp3 support): https://github.com/librosa/librosa#hints-for-the-installation Soundfile: --- https://github.com/librosa/librosa#hints-for-the-installation - installation: - pip install soundfile - or (sudo apt-get install libsndfile1) - import: - import soundfile as sf - Load audio: - data, samplerate = sf.read('name.wav') - Write audio: - sf.write('new.flac', data, samplerate) - Information: - sf.info('name.wav') - Read short blocks: > rms = [np.sqrt(np.mean(block**2)) for block in sf.blocks('name.wav', blocksize=1024, overlap=512)] - load mp3? - ffmpeg below: ffmpeg: --- - sudo apt-get install ffmpeg - mp3 -> wav > ffmpeg -i Heros.mp3 -vn -acodec pcm_s16le -ac 1 -ar 44100 -f wav Heros.wav - wav -> mp3 > mpg123 -w foo.wav foo.mp3 librosa: --- Installation: https://github.com/librosa/librosa#hints-for-the-installation **Document**: https://librosa.org/doc/latest/core.html - import: - impoer librosa - import librosa.display - Load audio: - y, sr = librosa.load(filename) - y: waveform - sr: sampling rate - Output audio: with soundfile: - sf.write('test01.wav', y, samplerate=sr) - Beat Tracker: - tempo, beat_frames = librosa.beat.beat_track(y=y, sr=sr) - tempo: beat per minute - beat_frames: type 'beat_frames' - Frame to time - librosa.frames_to_time(beat_frames, sr=sr) - Separate harmonics and percussives ``` # Separate harmonics and percussives into two waveforms y_harmonic, y_percussive = librosa.effects.hpss(y) ``` - Compute Feature ``` # Compute MFCC features from the raw signal mfcc = librosa.feature.mfcc(y=y, sr=sr, hop_length=512, n_mfcc=13) ``` ``` # And the first-order differences (delta features) mfcc_delta = librosa.feature.delta(mfcc) ``` - Stack and synchronize between beat events ``` # Stack and synchronize between beat events ## This time, we'll use the mean value (default) instead of median beat_mfcc_delta = librosa.util.sync(np.vstack([mfcc, mfcc_delta]), beat_frames) ``` - Compute chroma features from the harmonic signal ``` # Compute chroma features from the harmonic signal chromagram = librosa.feature.chroma_cqt(y=y_harmonic, sr=sr) ``` - Aggregate chroma features between beat events ``` # Aggregate chroma features between beat events # We'll use the median value of each feature between beat frames beat_chroma = librosa.util.sync(chromagram, beat_frames, aggregate=np.median) ``` - Stack ``` # Finally, stack all beat-synchronous features together beat_features = np.vstack([beat_chroma, beat_mfcc_delta]) ``` - ---- - librosa.core - librosa - beat 偵測節奏 - decompose 消除背景(HPSS)(Scikit Learn) - display 畫圖( matplotlib ) - effects 編輯效果啊 - feature 低階特徵偵測 > chromagrams, Mel, MFCC, and other spectral and rhythmic features. - filters - onset 偵測onset(開始?) - segment 切割(segmentation) - sequence // Funcitons for sequential modeling - util 工具:normalization, padding, centering, etc. - ---- - Examples ``` # Beat tracking example import librosa # 1. Get the file path to an included audio example filename = librosa.example('nutcracker') # 2. Load the audio as a waveform `y` # Store the sampling rate as `sr` y, sr = librosa.load(filename) # 3. Run the default beat tracker tempo, beat_frames = librosa.beat.beat_track(y=y, sr=sr) print('Estimated tempo: {:.2f} beats per minute'.format(tempo)) # 4. Convert the frame indices of beat events into timestamps beat_times = librosa.frames_to_time(beat_frames, sr=sr) ```