File: madradio.py

package info (click to toggle)
pymad 0.11.3-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid, trixie
  • size: 244 kB
  • sloc: ansic: 518; python: 153; makefile: 6
file content (53 lines) | stat: -rw-r--r-- 1,294 bytes parent folder | download | duplicates (5)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
#! /usr/bin/env python

import glob
import os.path
import socket
import sys

try:
    from urllib.parse import urlparse
except ImportError:
    from urlparse import urlparse

import ao

for p in glob.glob('build/lib.*'):
    sys.path.insert(0, p)

import mad


def madradio(url):
    scheme, netloc, path, params, query, fragment = urlparse(url)
    try:
        host, port = netloc.split(':')
    except ValueError:
        host, port = netloc, 80
    if not path:
        path = '/'
    sock = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
    sock.connect((host, int(port)))
    sock.send('GET %s HTTP/1.0\r\n\r\n' % path)
    reply = sock.recv(1500)
    # print repr(reply)
    file = sock.makefile()
    mf = mad.MadFile(file)
    print(('bitrate %lu bps' % mf.bitrate()))
    print(('samplerate %d Hz' % mf.samplerate()))
    dev = ao.AudioDevice(0, rate=mf.samplerate())
    while True:
        buffy = mf.read()
        if buffy is None:
            break
        dev.play(buffy, len(buffy))

if __name__ == '__main__':
    import sys
    try:
        url = sys.argv[1]
    except IndexError:
        # url = 'http://62.67.195.6:8000' # lounge-radio.com
        # url = 'http://63.241.4.18:8069' # xtcradio.com
        url = 'http://mp2.somafm.com:2040'  # somafm
    madradio(url)