2016-05-16 43 views
0

私はpythonのmp3から振幅データを取得する方法を見つけることができたと思っていました。大胆さに似ていますが、私はビジュアルを望んでいません。値の単純な配列ができます。私はコードがより大きくなる特定のポイントでサウンドに反応するようにしたい。私はpygameを使ってオーディオを再生し、それをsndarrayに変換しようとしていましたが、最初の0.00018秒しか与えませんでした。とにかく私は全体のmp3を得ることができますか?とにかく前もって反応できるようにするためにはリアルタイムである必要はなく、パイゲームを使って自分の位置を把握しています。Python - mp3の波形/振幅を取得

私はthis cloudをビルドしていますが、代わりにラズベリーパイを使用しています。私は既にライティング作業をしており、稲妻に反応する必要があるので、lightshowpiは悲しい選択肢ではありません。どんな助けにも大いに感謝します

編集: これは私がこれまでにコーダードンに感謝しているものです。それは動作しますが、私はwhileループでハングします。何故かはわからない。私が使用しているmp3はかなり長いです、それは問題でしょうか?

import os, sys 
from gi.repository import Gst, GObject 
Gst.init() 
GObject.threads_init() 

def get_peaks(filename): 
global do_run 

pipeline_txt = (
    'filesrc location="%s" ! decodebin ! audioconvert ! ' 
    'audio/x-raw,channels=1,rate=22050,endianness=1234,' 
    'width=32,depth=32,signed=(bool)TRUE !' 
    'level name=level interval=1000000000 !' 
    'fakesink' % filename) 
pipeline = Gst.parse_launch(pipeline_txt) 

level = pipeline.get_by_name('level') 
bus = pipeline.get_bus() 
bus.add_signal_watch() 

peaks = [] 
do_run = True 

def show_peak(bus, message): 
    global do_run 
    if message.type == Gst.MESSAGE_EOS: 
     pipeline.set_state(Gst.State.NULL) 
     do_run = False 
     return 
    # filter only on level messages 
    if message.src is not level or \ 
     not message.structure.has_key('peak'): 
     return 
    peaks.append(message.structure['peak'][0]) 

# connect the callback 
bus.connect('message', show_peak) 

# run the pipeline until we got eos 
pipeline.set_state(Gst.State.PLAYING) 
ctx = GObject.MainContext() 
while ctx and do_run: 
    ctx.iteration() 

return peaks 

def normalize(peaks): 
    _min = min(peaks) 
    print(_min) 
    _max = max(peaks) 
    print(_max) 
    d = _max - _min 
    return [(x - _min)/d for x in peaks] 

if __name__ == '__main__': 
    filename = os.path.realpath(sys.argv[1]) 
    peaks = get_peaks(filename) 

    print('Sample is %d seconds' % len(peaks)) 
    print('Minimum is', min(peaks)) 
    print('Maximum is', max(peaks)) 

    peaks = normalize(peaks) 
    print(peaks) 
+0

私は、これはすでに求められていると思います - http://stackoverflow.com/questions/9344888/getting-max-amplitude-for-an-audio-file-per-second?lq=1 のhttp: //stackoverflow.com/questions/10304495/python-getting-the- amplitudes-of-a-sound-file –

答えて

0

pydub使用して、あなたは非常に簡単にmp3ファイルのloudnesshighest amplitudeを得ることができます。これらのパラメータの1つを使用して、コード/ライトをそれに反応させることができます。 pydubウェブサイトから

AudioSegment(...)は

AudioSegmentにおける任意のサンプルの最大振幅を.MAX。正規化(pydub.effects.normalizeで提供される)のようなものに役立ちます。

from pydub import AudioSegment 
sound = AudioSegment.from_file("/path/to/sound.mp3", format="mp3")  
peak_amplitude = sound.max 

AudioSegment(...).dBFS

dBFSでAudioSegmentのラウドネスを返し(最大可能ラウドネスに対するdB)。最大振幅の方形波は約0 dBFS(最大ラウドネス)ですが、最大振幅の正弦波はおおよそ-3 dBFSになります。

from pydub import AudioSegment 
sound = AudioSegment.from_file("/path/to/sound.mp3", format="mp3") 
loudness = sound.dBFS 
関連する問題