Example #1
0
    def cache_from_local(self):
        """
        从本地载入音频数据
        发送消息必须与 cache_from_url 一致,以便外部程序获取消息进行后续操作
        其中专辑封面图片文件如果没有找到,则使用默认的封面图片 LOGO
        同时需要补充音频的时间长度数据
        """
        self.emit(SIGNAL('before_cache()'))
        if self.song_info is None:
            self.stop()
            return

        self.image_data = self.check_image_cache()
        if not self.image_data:
            self.image_data = QM_DEFAULT_ICON_DATA
        self.audio_segment = AudioSegment.from_file(self.song_info.song_path)

        # 下面是补充两项属性,音频的秒数和转换为时间格式的时长
        self.song_info.interval = int(
            round(self.audio_segment.duration_seconds))
        self.song_info.length = seconds2time(
            self.audio_segment.duration_seconds)

        self.emit(SIGNAL('caching()'))
        self.is_stop = True
        self.emit(SIGNAL('after_cache()'))
def 获取单首歌曲特征(file):  #fetch_index_label
    '''转换音乐文件格式并且提取其特征'''
    '''./data/music\\50 Cent - Ready For War.mp3'''
    items = file.split('.')
    file_format = items[-1].lower()  #获取歌曲格式
    file_name = file[:-(len(file_format) + 1)]  #获取歌曲名称
    if file_format != 'wav':
        '''把mp3格式转换为wav,保存至原文件夹中'''
        print('file_format:' + file_format)
        song = AudioSegment.from_file(file, format='mp3')
        file = file_name + '.wav'
        song.export(file, format='wav')
    try:
        '''提取wav格式歌曲特征'''
        rate, data = wavfile.read(file)
        mfcc_feas = mfcc(data, rate, numcep=13, nfft=2048)
        mm = np.transpose(mfcc_feas)
        mf = np.mean(mm, axis=1)  # mf变成104维的向量
        mc = np.cov(mm)
        result = mf
        for i in range(mm.shape[0]):
            result = np.append(result, np.diag(mc, i))


#         os.remove(file)
        return result  #返回1个104维的向量
    except Exception as msg:
        print(msg)
Example #3
0
def unscramble(bp, stories, suffix):
    if type(bp) == str: bp = pd.read_csv(bp)
    intact = bp.sort(['old_name', 'order'])
    stories = {story : AudioSegment.from_file(story) for story in stories}
    for ii, g in intact.groupby(['old_name']):
        aud = aud_from_log(g, **stories)
        try: ftype = suffix.split('.')[-1]
        except IndexError: ftype = "wav"
        aud.export(ii + suffix, ftype)
Example #4
0
    def check_audio_cache(self):
        """
        检查缓存文件是否存在,且是否缓存完毕
        :return: False 或 AudioSegment 对象
        """
        cache_song = QM_DEFAULT_CACHE_PATH + str(self.song_info.filename)
        if not os.path.isfile(cache_song):
            return False
        if os.path.getsize(cache_song) < 1024:
            return False

        audio_seg = AudioSegment.from_file(cache_song)

        interval = int(self.song_info.interval) * 1000
        duration = audio_seg.duration_seconds * 1000
        if duration > interval:
            return audio_seg
        else:
            return False
Example #5
0
def convert_audio(from_path, to_dir, from_format=None, to_format='wav'):
    """Convert audios to a specified audio format."""
    assert os.path.isdir(to_dir)

    from_files = []
    if os.path.isfile(from_path):
        from_files.append(from_path)
    elif os.path.isdir(from_path):
        file_paths = [
            os.path.join(from_path, name) for name in os.listdir(from_path)
        ]
        from_files = [file for file in file_paths if os.path.isfile(file)]

    for from_file in tqdm(from_files):
        to_name = os.path.basename(from_file). \
            replace(os.path.splitext(from_file)[1], '.' + to_format)
        to_path = os.path.join(to_dir, to_name)
        audio = AudioSegment.from_file(from_file, from_format)
        audio.export(to_path, to_format)
Example #6
0
def convert_wav(file, counter):
    filename, ext = os.path.splitext(file)
    wav_dir = os.path.dirname(file) + "/wav"
    if not os.path.exists(wav_dir):
        os.mkdir(wav_dir)
    ext = ext[1:]
    song = AudioSegment.from_file(file, ext)
    msecs = song.duration_seconds * 1000
    halfpoint = msecs/2
    thir_sec = song[halfpoint:(halfpoint+15000)]
    sample_rate = thir_sec.frame_rate
    #standard decibel range
    normal_db = [-32.0, -18.0]
    #normalize sample before stripping a channel to avoid clipping
    thir_sec = normalize(thir_sec, sample_rate, normal_db)
    #convert to mono
    thir_sec = thir_sec.set_channels(1)
    new_filename = wav_dir + "/" + str(counter) + ".wav"
    thir_sec.export(new_filename, format="wav")
    return new_filename
Example #7
0
def make_audio(bp, stories, audout="default.wav", bpout="default.csv"):
    """Generate wav with concatenated segments from blueprint."""
    print "----Loading Data----"
    df = pd.read_csv(bp)
    print df

    print "----Loading Audio----"
    story_dict = {}
    for key, fname in stories.iteritems():
        story_dict[key] = AudioSegment.from_file(fname)

    print "----Exporting Audio----"
    audio = aud_from_log(df, **story_dict)
    audio.export(audout, format=audout.split('.')[-1])

    print "----Saving Blueprint For Export----"
    df['old_name'] = df['name']
    df['name'] = audout
    update_splits(df)
    df.to_csv(bpout)
Example #8
0
def 获取单首歌曲特征(file):  #fetch_index_label
    items = file.split('.')
    file_format = items[-1].lower()  #获取歌曲格式
    file_name = file[:-(len(file_format) + 1)]  #获取歌曲名称
    if file_format != 'wav':
        song = AudioSegment.from_file(file, format='mp3')
        file = file_name + '.wav'
        song.export(file, format='wav')
    try:
        rate, data = wavfile.read(file)
        mfcc_feas = mfcc(data, rate, numcep=13, nfft=2048)
        mm = np.transpose(mfcc_feas)  #先LDA再降唯,如果不进行转置?
        mf = np.mean(mm, axis=1)  # mf变成104维的向量
        mc = np.cov(mm)
        result = mf
        for i in range(mm.shape[0]):
            result = np.append(result, np.diag(mc, i))
#         os.remove(file)
        return result
    except Exception as msg:
        print(msg)
Example #9
0
    def cache_from_url(self):
        """
        从一个 URL 地址获取音乐数据,并缓存在临时目录中
        实际装载的过程是首先检查缓存目录中是否存在有效的音乐副本和封面图片副本
        如果有,就直接从缓存播放,否则从网络下载,并缓存
        :return: 返回缓存的临时文件对象
        """
        self.emit(SIGNAL('before_cache()'))
        self.is_stop = False
        self.exception_list = []
        try:
            if self.song_info.song_url is None:
                tencent = TencentProtocol()
                tencent.get_play_key(self.song_info)
                tencent.get_song_address(self.song_info)
                tencent.get_image_address(self.song_info)
                if tencent.has_exception:
                    self.exception_list += tencent.exception_list
                    raise tencent.exception_list[0]

            # 首先尝试从本地缓存中载入封面图片,未找到则下载并缓存
            self.image_data = self.check_image_cache()
            if not self.image_data:
                """
                从网络读取专辑封面,并写入本地缓存文件
                """
                self.image_data = requests.get(
                    self.song_info.image_url).content
                cache_image_path = QM_DEFAULT_CACHE_PATH + str(
                    self.song_info.mid) + '.jpg'
                if os.path.isfile(cache_image_path):
                    os.remove(cache_image_path)
                with open(cache_image_path, 'wb') as cover_file:
                    cover_file.write(self.image_data)

            # 首先尝试从本地缓存中载入音频文件,未找到则下载并缓存
            cache_audio = self.check_audio_cache()
            if isinstance(cache_audio, AudioSegment):
                """
                从缓存载入音频
                """
                self.audio_segment = cache_audio
                self.emit(SIGNAL('caching()'))
            else:
                """
                从网络缓存音频,并写入本地缓存
                """
                request = Request(self.song_info.song_url)
                pipe = urlopen(url=request, timeout=QM_TIMEOUT)

                cache_file = QM_DEFAULT_CACHE_PATH + str(
                    self.song_info.filename)
                if os.path.isfile(cache_file):
                    os.remove(cache_file)
                with open(cache_file, 'wb') as audio_file:
                    while True:
                        data = pipe.read(QM_BUFFER_SIZE)

                        if self.is_stop or data is None or len(data) == 0:
                            audio_file.close()
                            break

                        # print 'wirte >>> ' + str(self.song_info.name) + ' >>> ' + filename
                        audio_file.write(data)
                        sleep(0.01)
                        self.audio_segment = AudioSegment.from_file(
                            audio_file.name)
                        self.emit(SIGNAL('caching()'))
                    audio_file.close()
        except RuntimeError as e:
            e.message += u"运行时错误。"
            self.exception_list.append(e)
        except BaseException as e:
            e.message += u"获取音乐数据错误。"
            self.exception_list.append(e)

        self.is_stop = True
        self.emit(SIGNAL('after_cache()'))
Example #10
0
"""speech data process.
"""

from pydub.audio_segment import AudioSegment

if __name__ == '__main__':
    audio = AudioSegment.from_file('102.wav')
    print(audio.frame_rate)
Example #11
0
#-*- conding:utf-8 -*-
import pandas as pd
import numpy as np
from pydub.audio_segment import AudioSegment#pydub是处理音乐文件的一个库
from scipy.io import wavfile
from python_speech_features import mfcc
import os
import sys
song =AudioSegment.from_file('./我们的纪念.mp3',format="mp3")#先读一下
#切分歌曲
# song_split=song[-30*100:]
song.export("./我们的纪念.wav",format='wav')#转化为wav格式
rate,data=wavfile.read("./我们的纪念.wav")#每秒读取速度以及数据
print(rate)
print(data.shape)
mf_feat = mfcc(data,rate,numcep=13,nfft=2048)#数据转化为13维,频率改为2048
print(mf_feat.shape)
mm =np.mean(mf_feat,axis=0)#隐含了时序上的相关性
print(mm.shape)
mf = np.transpose(mf_feat)
mc = np.cov(mf)#协方差矩阵,里面的值也就是各个特征之间的协方差
print(mc.shape)
result = mm
#np.diag(mc,k)方针里面的值,k=0为对角线的元素,
for k in range(len(mm)):
    result=np.append(result,np.diag(mc,k))
print(result.shape)


Example #12
0
#coding:utf-8

from pydub.audio_segment import AudioSegment#pydub是python中用户处理音频文件的一个库
from scipy.io import wavfile
from python_speech_features.base import mfcc #傅里叶变换+梅尔倒谱
import pandas as pd
import numpy as np
import sys


#mfcc 包含了两个步骤,一个是傅里叶变换,一个是梅尔倒谱系数
song = AudioSegment.from_file('./data/灰姑娘.mp3', format = 'mp3')#读入歌曲
# song_split = song[-30*1000:]#切分歌曲
song.export('./data/灰姑娘.wav', format= 'wav')#MP3到wav的转换
rate, data = wavfile.read('./data/灰姑娘.wav')#每秒播放速度及数据
mf_feat = mfcc(data, rate, numcep = 13, nfft = 2048)#傅里叶变换速度每秒多少帧
#  numcep = 13 越大越慢
# 108键, 小于1/4 欢快,大于1/4悲伤

print(mf_feat)
print(mf_feat.shape)
sys.exit(0)
# df = pd.DataFrame(mf_feat)
# df.to_csv('./mfFeat.csv')
# print(mf_feat)
# print(mf_feat.shape)
mm = np.mean(mf_feat, axis = 0)#隐含了时域上的相关性
mf = np.transpose(mf_feat)
mc = np.cov(mf) #原mf_feat矩阵列的协方差矩阵
# print(mc)
result = mm