Changeset 1f8e522


Ignore:
Timestamp:
Oct 21, 2009, 3:16:02 PM (15 years ago)
Author:
Paul Brossier <piem@piem.org>
Branches:
feature/autosink, feature/cnn, feature/cnn_org, feature/constantq, feature/crepe, feature/crepe_org, feature/pitchshift, feature/pydocstrings, feature/timestretch, fix/ffmpeg5, master, pitchshift, sampler, timestretch, yinfft+
Children:
09b1777
Parents:
40acfbc
Message:

aubioinput.py: use uridecodebin, add run method, return 1 on error

File:
1 edited

Legend:

Unmodified
Added
Removed
  • interfaces/python/aubioinput.py

    r40acfbc r1f8e522  
    5858gobject.type_register(AubioSink)
    5959
    60 class aubioinput:
    61     def __init__(self, location, process = None, hopsize = 512,
     60class aubioinput(gst.Bin):
     61
     62    ret = 0
     63
     64    def __init__(self, uri, process = None, hopsize = 512,
    6265            caps = None):
    63         from os.path import isfile
    64         if not isfile(location):
    65             raise ValueError, "location should be a file"
    66         src = gst.element_factory_make('filesrc')
    67         src.set_property('location', location)
    68         dec = gst.element_factory_make('decodebin')
    69         dec.connect('new-decoded-pad', self.on_new_decoded_pad)
     66        if uri.startswith('/'):
     67            from urllib import quote
     68            uri = 'file://'+quote(uri)
     69        src = gst.element_factory_make('uridecodebin')
     70        src.set_property('uri', uri)
     71        src.connect('pad-added', self.source_pad_added_cb)
    7072        conv = gst.element_factory_make('audioconvert')
     73        self.conv = conv
    7174        rsmpl = gst.element_factory_make('audioresample')
    7275        capsfilter = gst.element_factory_make('capsfilter')
     
    8083        self.bus = self.pipeline.get_bus()
    8184        self.bus.add_signal_watch()
    82         self.bus.connect('message::eos', self.on_eos)
     85        self.bus.connect('message', self.on_eos)
    8386
    8487        self.apad = conv.get_pad('sink')
    8588
    86         self.pipeline.add(src, dec, conv, rsmpl, capsfilter, sink)
     89        self.pipeline.add(src, conv, rsmpl, capsfilter, sink)
    8790
    88         src.link(dec)
    8991        gst.element_link_many(conv, rsmpl, capsfilter, sink)
    9092
    9193        self.mainloop = gobject.MainLoop()
    9294        self.pipeline.set_state(gst.STATE_PLAYING)
     95
     96    def run(self):
    9397        self.mainloop.run()
     98        return self.ret
    9499
    95     def on_new_decoded_pad(self, element, pad, last):
    96         caps = pad.get_caps()
    97         name = caps[0].get_name()
    98         #print 'on_new_decoded_pad:', name
     100    def source_pad_added_cb(self, src, pad):
     101        name = pad.get_caps()[0].get_name()
    99102        if name == 'audio/x-raw-float' or name == 'audio/x-raw-int':
    100             if not self.apad.is_linked(): # Only link once
    101                 pad.link(self.apad)
     103            pad.link(self.conv.get_pad("sink"))
     104
     105    def source_pad_removed_cb(self, src, pad):
     106        pad.unlink(self.conv.get_pad("sink"))
    102107
    103108    def on_eos(self, bus, msg):
    104         self.bus.remove_signal_watch()
    105         self.pipeline.set_state(gst.STATE_PAUSED)
    106         self.mainloop.quit()
     109        if msg.type == gst.MESSAGE_EOS:
     110            self.bus.remove_signal_watch()
     111            self.pipeline.set_state(gst.STATE_PAUSED)
     112            self.mainloop.quit()
     113        elif msg.type == gst.MESSAGE_ERROR:
     114            print "ERROR", msg.parse_error()
     115            self.bus.remove_signal_watch()
     116            self.pipeline.set_state(gst.STATE_PAUSED)
     117            self.mainloop.quit()
     118            self.ret = 1 # set return value to 1 in case of error
    107119
    108120if __name__ == '__main__':
     
    112124        sys.exit(1)
    113125    for filename in sys.argv[1:]:
    114         peak = [0., 0.]
     126        peak = [0.] # use a mutable
    115127        def process(buf, hop):
    116128            peak[0] = max( peak[0], abs(buf.max()) )
    117             peak[1] = min( peak[1], abs(buf.min()) )
    118         aubioinput(filename, process = process, hopsize = 512)
    119         print "Finished reading %s, peak value is %f" % (filename, min(peak))
     129        a = aubioinput(filename, process = process, hopsize = 512)
     130        if a.run() == 0: # only display the results if no
     131            print "Finished reading %s, peak value is %f" % (filename, max(peak))
Note: See TracChangeset for help on using the changeset viewer.