python measure volum from audio file

Solutions on MaxInterview for python measure volum from audio file by the best coders in the world

showing results for - "python measure volum from audio file"
Ryan
09 Feb 2019
1import os, sys, pygst
2pygst.require('0.10')
3import gst, gobject
4gobject.threads_init()
5
6def get_peaks(filename):
7    global do_run
8
9    pipeline_txt = (
10        'filesrc location="%s" ! decodebin ! audioconvert ! '
11        'audio/x-raw-int,channels=1,rate=44100,endianness=1234,'
12        'width=32,depth=32,signed=(bool)True !'
13        'level name=level interval=1000000000 !'
14        'fakesink' % filename)
15    pipeline = gst.parse_launch(pipeline_txt)
16
17    level = pipeline.get_by_name('level')
18    bus = pipeline.get_bus()
19    bus.add_signal_watch()
20
21    peaks = []
22    do_run = True
23
24    def show_peak(bus, message):
25        global do_run
26        if message.type == gst.MESSAGE_EOS:
27            pipeline.set_state(gst.STATE_NULL)
28            do_run = False
29            return
30        # filter only on level messages
31        if message.src is not level or \
32           not message.structure.has_key('peak'):
33            return
34        peaks.append(message.structure['peak'][0])
35
36    # connect the callback
37    bus.connect('message', show_peak)
38
39    # run the pipeline until we got eos
40    pipeline.set_state(gst.STATE_PLAYING)
41    ctx = gobject.gobject.main_context_default()
42    while ctx and do_run:
43        ctx.iteration()
44
45    return peaks
46
47def normalize(peaks):
48    _min = min(peaks)
49    _max = max(peaks)
50    d = _max - _min
51    return [(x - _min) / d for x in peaks]
52
53if __name__ == '__main__':
54    filename = os.path.realpath(sys.argv[1])
55    peaks = get_peaks(filename)
56
57    print 'Sample is %d seconds' % len(peaks)
58    print 'Minimum is', min(peaks)
59    print 'Maximum is', max(peaks)
60
61    peaks = normalize(peaks)
62    print peaks
63
similar questions
queries leading to this page
python measure volum from audio file