diff options
| author | Grissess <grissess@nexusg.org> | 2016-06-14 23:52:53 -0400 | 
|---|---|---|
| committer | Grissess <grissess@nexusg.org> | 2016-06-14 23:52:53 -0400 | 
| commit | 5a43b7b90716296b164bc3c6a617a033710b4c3a (patch) | |
| tree | 3969bdb7ee72ca7bdeac4d0d284bfdb9d0f233e5 | |
| parent | 40b1e56164c3b4e95d7d35f5ed800c5626944b01 (diff) | |
Minor PCM fixes
| -rw-r--r-- | broadcast.py | 42 | 
1 files changed, 30 insertions, 12 deletions
| diff --git a/broadcast.py b/broadcast.py index b0d5efa..450874a 100644 --- a/broadcast.py +++ b/broadcast.py @@ -317,30 +317,48 @@ if options.repeat:  for fname in args:      if options.pcm and not fname.endswith('.iv'): -        try: -            import audiotools -            pcr = audiotools.open(fname).to_pcm() -            assert pcr.channels == 1 and pcr.bits_per_sample == 16 and pcr.sample_rate == 44100 -        except ImportError: +        if fname == '-':              import wave -            pcr = wave.open(fname, 'r') -            assert pcr.getnchannels() == 1 and pcr.getsampwidth() == 2 and pcr.getframerate() == 44100 +            pcr = wave.open(sys.stdin) +            samprate = pcr.getframerate() +            pcr.read = pcr.readframes +        else: +            try: +                import audiotools +                pcr = audiotools.open(fname).to_pcm() +                assert pcr.channels == 1 and pcr.bits_per_sample == 16 and pcr.sample_rate == 44100 +                samprate = pcr.sample_rate +            except ImportError: +                import wave +                pcr = wave.open(fname, 'r') +                assert pcr.getnchannels() == 1 and pcr.getsampwidth() == 2 and pcr.getframerate() == 44100 +                samprate = pcr.getframerate() +                pcr.read = pcr.readframes + +        def read_all(fn, n): +            buf = '' +            while len(buf) < n: +                nbuf = fn.read(n - len(buf)) +                if not isinstance(nbuf, str): +                    nbuf = nbuf.to_bytes(False, True) +                buf += nbuf +            return buf          BASETIME = time.time() - options.pcmlead          sampcnt = 0 -        buf = pcr.read(16).to_bytes(False, True) -        while buf: +        buf = read_all(pcr, 16) +        while len(buf) >= 32:              frag = buf[:32]              buf = buf[32:]              for cl in clients:                  s.sendto(struct.pack('>L', CMD.PCM) + frag, cl)              sampcnt += len(frag) / 2 -            delay = max(0, BASETIME + (sampcnt / float(pcr.sample_rate)) - time.time()) +            delay = max(0, BASETIME + (sampcnt / float(samprate)) - time.time())              #print sampcnt, delay              if delay > 0:                  time.sleep(delay) -            if not buf: -                buf = pcr.read(16).to_bytes(False, True) +            if len(buf) < 32: +                buf += read_all(pcr, 16)      try:          iv = ET.parse(fname).getroot()      except IOError: | 
