从mp3到功能:音乐聚类

2024-04-20 11:29:01 发布

您现在位置:Python中文网/ 问答频道 /正文

是否有人有兴趣提供一组MP3音乐? 我使用librosa、ffmpeg和我编写的一个Python脚本,以便将单个MP3文件映射到包含114个数字特征(如tempo、mfcc等)的特征序列。 如果重复该过程,比如说,100.000 MP3,则可能有一组音乐。 问题是我如何让人们将他们的MP3转换为功能? 另一个有趣的问题是,这些功能是否可以恢复为MP3? 以下是脚本:

#!/bin/bash

ffmpeg -y -i "$1" -acodec pcm_u8 -ar 22050 "$1".wav

python prova.py "$1".wav

Python Wav to的功能代码为prova.py:

# Beat tracking example
from __future__ import print_function
import librosa
import numpy as np
import sys


# ffmpeg -i song.mp3 -acodec pcm_u8 -ar 22050 song.wav

# 1. Get the file path to the included audio example
filename = sys.argv[1]
print(filename)
name, extension1, extension2 = filename.split(".")

print("Song name = " + name)

# 2. Load the audio as a waveform `y`
#    Store the sampling rate as `sr`
y, sr = librosa.load(filename)

onset_env = librosa.onset.onset_strength(y, sr=sr)
tempo = librosa.beat.tempo(onset_envelope=onset_env, sr=sr)

# Set the hop length; at 22050 Hz, 512 samples ~= 23ms
hop_length = 512

# Separate harmonics and percussives into two waveforms
y_harmonic, y_percussive = librosa.effects.hpss(y)


#print("y_armonic= " + str(len(y_harmonic)))
#print("y_percussive= " + str(len(y_percussive)))

# Beat track on the percussive signal
tempo, beat_frames = librosa.beat.beat_track(y=y_percussive,
                                             sr=sr)

# Compute MFCC features from the raw signal
mfcc = librosa.feature.mfcc(y=y, sr=sr, hop_length=hop_length, n_mfcc=13)

# And the first-order differences (delta features)
mfcc_delta = librosa.feature.delta(mfcc)

# 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
chromagram = librosa.feature.chroma_cqt(y=y_harmonic,
                                        sr=sr)

# 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)

# Finally, stack all beat-synchronous features together
beat_features = np.vstack([beat_chroma, beat_mfcc_delta])

print(str(mfcc.shape) + " " + str(mfcc_delta.shape) + " " + str(beat_mfcc_delta.shape) + " " + str(chromagram.shape) + " " + str(beat_chroma.shape) + str(beat_features.shape))

f = open(name + ".txt","w")

f.write(str(tempo)+ ',')

for i in range(0, len(mfcc)):
    f.write(str(np.mean(mfcc[i]))+ ',')

for i in range(0, len(mfcc_delta)):
    f.write(str(np.mean(mfcc_delta[i]))+ ',')

for i in range(0, len(beat_mfcc_delta)):
    f.write(str(np.mean(beat_mfcc_delta[i]))+ ',')

for i in range(0, len(chromagram)):
    f.write(str(np.mean(chromagram[i]))+ ',')

for i in range(0, len(beat_chroma)):
    f.write(str(np.mean(beat_chroma[i]))+ ',')

for i in range(0, len(beat_features)):
    f.write(str(np.mean(beat_features[i]))+ ',')

f.close()

通过这种方式,如果一个功能集与它的流派相结合,给定一首新歌,它就可以自动判断它属于哪个流派


Tags: thelennpmeanwritedeltafeaturesprint
1条回答
网友
1楼 · 发布于 2024-04-20 11:29:01

您可以提供人们可以在其MP3上运行的代码(如命令行工具或桌面应用程序),也可以提供人们可以在其MP3上运行代码的代码(如web应用程序或HTTP API)

相关问题 更多 >