Esempio n. 1
0
def GetFrameRate(url, maintype, subtype):
    fr = 20
    if string.find(string.lower(subtype), 'real') >= 0 or string.find(
            subtype, 'shockwave') >= 0:
        if maintype == 'audio':
            fr = 8000
        else:
            fr = 20
    elif maintype in ('image', 'text'):
        fr = 10
    elif maintype == 'video':
        if subtype.find('quicktime') >= 0 and winqt.HasQtSupport():
            player = winqt.QtPlayer()
            if player.open(url):
                return player.getFrameRate()
        fr = win32dxm.GetFrameRate(url)
    elif maintype == 'audio':
        try:
            import audio
            filename = MMurl.urlretrieve(url)[0]
            a = audio.reader(filename)
            fr = a.getframerate()
        except (audio.Error, IOError, EOFError), msg:
            print 'error in sound file', url, ':', msg
            fr = 8000
Esempio n. 2
0
 def getaltvalue(self, node):
     # Determine playability. Expensive, but this method is only
     # called when needed (i.e. the node is within a switch).
     fn = self.getfileurl(node)
     if not fn:
         return 0
     try:
         fn, hdr = MMurl.urlretrieve(fn)
     except (IOError, EOFError, audio.Error):
         return 0
     if string.find(hdr.type, 'real') >= 0:
         if self.__rc is None:
             try:
                 from RealChannel import RealChannel
                 self.__rc = RealChannel(self)
             except:
                 pass
         if self.__rc:
             return 1
         return 0
     try:
         fp = audio.reader(fn)
     except (IOError, EOFError, audio.Error):
         return 0
     return 1
Esempio n. 3
0
def getfullinfo(url):
    import audio
    from MMurl import urlretrieve
    try:
        filename = urlretrieve(url)[0]
        a = audio.reader(filename)
        nframes = a.getnframes()
        framerate = a.getframerate()
        markers = a.getmarkers()
    except (audio.Error, IOError, EOFError), msg:
        print 'error in sound file', url, ':', msg
        return 0, 8000, []
Esempio n. 4
0
def play(file):
    dev = audio.dev.writer()  # open output device
    # open the audio file and convert to an acceptable format
    rdr = audio.reader(file, dev.getformats(), dev.getframerates())
    # tell device about format and frame rate
    dev.setformat(rdr.getformat())
    dev.setframerate(rdr.getframerate())
    # read and play the file
    while 1:
        data, nf = rdr.readframes(
            10000)  # read and convert 10,000 audio frames
        if not data:  # if no data, we reached the end
            break
        dev.writeframes(data)  # send data to device
    dev.wait()  # wait for output to drain
Esempio n. 5
0
 def read_basic_audio(self, u, atype):
     try:
         rdr = audio.reader(u, [audio.format.linear_16_mono],
                            [8000, 11025, 16000, 22050, 32000, 44100],
                            filetype=atype)
         # to figure out the number of channels of the source file:
         ##             r = rdr
         ##             while hasattr(r, '_rdr'):
         ##                 r = r._rdr
         ##             ochans = r.getformat().getnchannels()
         fmt = rdr.getformat()
         bytesperframe = fmt.getblocksize() / fmt.getfpb()
         nchan = fmt.getnchannels()
         frate = rdr.getframerate()
         totframes = rdr.getnframes()
     except (audio.Error, IOError, EOFError), msg:
         u.close()
         raise error, msg
Esempio n. 6
0
def convertaudiofile(u, srcurl, dstdir, file, node, progress=None):
    import MMAttrdefs, audio, audio.format
    global engine, audiopin
    # ignore suggested extension and make our own
    file = os.path.splitext(file)[0] + '.ra'
    fullpath = os.path.join(dstdir, file)
    if identicalfiles(srcurl, fullpath):
        # src and dst files are the same, don't do anything
        u.close()
        if __debug__:
            print 'src and dst files are identical', fullpath
        return file
    if engine is None:
        engine = producer.CreateRMBuildEngine()
    if audiopin is None:
        for pin in engine.GetPins():
            if pin.GetOutputMimeType() == producer.MIME_REALAUDIO:
                audiopin = pin
    engine.SetDoOutputMimeType(producer.MIME_REALAUDIO, 1)
    engine.SetDoOutputMimeType(producer.MIME_REALVIDEO, 0)
    engine.SetDoOutputMimeType(producer.MIME_REALEVENT, 0)
    engine.SetDoOutputMimeType(producer.MIME_REALIMAGEMAP, 0)
    engine.SetDoOutputMimeType(producer.MIME_REALPIX, 0)
    engine.SetRealTimeEncoding(0)
    cp = engine.GetClipProperties()
    ts = engine.GetTargetSettings()
    ts.RemoveAllTargetAudiences()
    if node is not None:
        cp.SetTitle(MMAttrdefs.getattr(node, 'title'))
        cp.SetAuthor(MMAttrdefs.getattr(node, 'author'))
        cp.SetCopyright(MMAttrdefs.getattr(node, 'copyright'))
        cp.SetPerfectPlay(MMAttrdefs.getattr(node, 'project_perfect'))
        cp.SetMobilePlay(MMAttrdefs.getattr(node, 'project_mobile'))
        ts.SetAudioContent(MMAttrdefs.getattr(node, 'project_audiotype'))
        target = MMAttrdefs.getattr(node, 'project_targets')
        ntargets = 0
        for i in range(6):
            if (1 << i) & target:
                ts.AddTargetAudience(i)
                ntargets = ntargets + 1
        if ntargets == 0:
            ts.AddTargetAudience(producer.ENC_TARGET_28_MODEM)
            ntargets = ntargets + 1
    else:
        # we don't know nothin' about the node so use some defaults
        cp.SetTitle('')
        cp.SetAuthor('')
        cp.SetCopyright('')
        cp.SetPerfectPlay(1)
        cp.SetMobilePlay(0)
        ts.SetAudioContent(producer.ENC_AUDIO_CONTENT_VOICE)
        ts.AddTargetAudience(producer.ENC_TARGET_28_MODEM)
        ts.SetVideoQuality(producer.ENC_VIDEO_QUALITY_NORMAL)
        ntargets = 1
    engine.SetDoMultiRateEncoding(ntargets != 1)
    cp.SetSelectiveRecord(0)
    cp.SetDoOutputServer(0)
    cp.SetDoOutputFile(1)

    if u.headers.subtype == 'basic':
        atype = 'au'
    elif u.headers.subtype == 'x-aiff':
        atype = 'aiff'
    elif u.headers.subtype == 'x-wav':
        atype = 'wav'
    elif u.headers.subtype == 'mpeg':
        u.close()
        return None
    else:
        atype = 'au'  # XXX not correct
    try:
        rdr = audio.reader(
            u, [audio.format.linear_16_mono, audio.format.linear_16_stereo],
            [8000, 11025, 16000, 22050, 32000, 44100],
            filetype=atype)
        fmt = rdr.getformat()
        bytesperframe = fmt.getblocksize() / fmt.getfpb()
        nchan = fmt.getnchannels()
        frate = rdr.getframerate()
        totframes = rdr.getnframes()
    except (audio.Error, IOError, EOFError), msg:
        print 'error in sound file:', msg
        return
Esempio n. 7
0
class SoundChannel(ChannelAsync):
    node_attrs = ChannelAsync.node_attrs + [
        'clipbegin', 'clipend', 'project_audiotype', 'project_targets',
        'project_perfect', 'project_mobile'
    ]

    # shared between all instances
    __playing = 0  # # of active channels

    def __init__(self, name, attrdict, scheduler, ui):
        ChannelAsync.__init__(self, name, attrdict, scheduler, ui)
        if debug: print 'SoundChannel: init', name
        self.arm_fp = None
        self.play_fp = None
        self.__qid = None
        self.__evid = []
        self.__rc = None
        self.__playing = None

    def do_show(self, pchan):
        if not ChannelAsync.do_show(self, pchan):
            return 0
        # we can only be shown if we can play
        return player is not None or self.__rc is not None

    def getaltvalue(self, node):
        # Determine playability. Expensive, but this method is only
        # called when needed (i.e. the node is within a switch).
        fn = self.getfileurl(node)
        if not fn:
            return 0
        try:
            fn, hdr = MMurl.urlretrieve(fn)
        except (IOError, EOFError, audio.Error):
            return 0
        if string.find(hdr.type, 'real') >= 0:
            if self.__rc is None:
                try:
                    from RealChannel import RealChannel
                    self.__rc = RealChannel(self)
                except:
                    pass
            if self.__rc:
                return 1
            return 0
        try:
            fp = audio.reader(fn)
        except (IOError, EOFError, audio.Error):
            return 0
        return 1

    def do_arm(self, node, same=0):
        self.__ready = 0
        node.__type = ''
        if node.type != 'ext':
            self.errormsg(node, 'Node must be external.')
            return 1
        if debug: print 'SoundChannel: arm', node
        fn = self.getfileurl(node)
        if not fn:
            self.errormsg(node, 'No URL set on node.')
            return 1
        mtype = node.GetAttrDef('type', None)
        if not mtype:
            import urlcache
            mtype = urlcache.mimetype(fn)
        if mtype and string.find(mtype, 'real') >= 0:
            node.__type = 'real'
            if self.__rc is None:
                import RealChannel
                try:
                    self.__rc = RealChannel.RealChannel(self)
                except RealChannel.error, msg:
                    # can't do RealAudio
                    ##                     self.__rc = 0 # don't try again
                    self.errormsg(node, msg)
            if self.__rc:
                if self.__rc.prepare_player(node):
                    self.__ready = 1
            return 1
        if player is None:
            return 1
        try:
            fn = MMurl.urlretrieve(fn)[0]
            self.arm_fp = audio.reader(fn)
            rate = self.arm_fp.getframerate()
        except IOError:
            self.errormsg(node, 'Cannot open audio file: %s' % fn)
            self.arm_fp = None
            self.armed_duration = 0
            return 1
        except EOFError:
            self.errormsg(node, 'Unexpected EOF in audio file: %s' % fn)
            self.arm_fp = None
            self.armed_duration = 0
            return 1
        except audio.Error, msg:
            self.errormsg(node, 'Error in audio file: %s\n\n%s.' % (fn, msg))
            self.arm_fp = None
            self.armed_duration = 0
            return 1
Esempio n. 8
0
            if done > 0:
                decbuf = decbuf + decdata[:done]
        if status > 0:
            status = status - 1
        decfactor = 1.137 * len(decbuf) / float(decode_buf_size - status)
        filename = MMurl.urlretrieve(url)[0]
        fsize = winuser.GetFileSize(filename)
        dur = (decfactor * fsize) / float(wfx[2])
        dur = 0.001 * int(dur * 1000.0)
        return dur, 1, []

    import audio
    from MMurl import urlretrieve
    try:
        filename = urlretrieve(url)[0]
        a = audio.reader(filename)
        nframes = a.getnframes()
        framerate = a.getframerate()
        markers = a.getmarkers()
    except (audio.Error, IOError, EOFError), msg:
        print 'error in sound file', url, ':', msg
        return 0, 8000, []
    return nframes, framerate, markers


def get(url):
    nframes, framerate, markers = getfullinfo(url)
    if nframes == 0: nframes = framerate
    duration = float(nframes) / framerate
    return duration