Quantcast
Channel: Raspberry Pi Forums
Viewing all articles
Browse latest Browse all 5226

MicroPython • Re: Building an Internet-Radio using a Pico W using just Python

$
0
0
I was finally able to get stable streaming and volume control. The required hardware is Raspberry Pi Pico 2 w and Pimoroni Pico Audio Pack.

Code:

import adafruit_connection_managerimport adafruit_requestsimport audiobusioimport audiomp3import boardimport wifiimport timeimport gcimport selectimport audiomixerdef show_mp3_props(mp3_decoder):    print("Sample Rate:", mp3_decoder.sample_rate, "Hz")    print("Bits per Sample:", mp3_decoder.bits_per_sample)    print("Channels:", mp3_decoder.channel_count)    print("Data Rate:", 1.0 * mp3_decoder.sample_rate * mp3_decoder.bits_per_sample * mp3_decoder.channel_count / 1000.0, "kbits/s")def show_mem():    gc.collect()    print("gc.mem_free():", gc.mem_free() )poll = Nonedef socket_readable(socket):    global poll    if poll == None:        poll = select.poll()    poll.register(socket, select.POLLIN)    list_of_tuples = poll.poll(0)    for t in list_of_tuples:        s, event = t        if s==socket and 0 != event & select.POLLIN:            return True    return Falsemp3_buffer = bytearray(2*16384)pool = adafruit_connection_manager.get_radio_socketpool(wifi.radio)ssl_context = adafruit_connection_manager.get_radio_ssl_context(wifi.radio)requests = adafruit_requests.Session(pool, ssl_context)stations = [    "http://n43a-eu.rcs.revma.com/an1ugyygzk8uv?rj-ttl=5&rj-tok=AAABlGvJH2gAetY13woP2xgp7A",    ]num_stations = len(stations)current_station = 0new_station = current_stationreconnects = 10while reconnects > 0:    reconnects -= 1    current_station = new_station    print("reconnects:", reconnects)    print("(Re-)Connecting to", stations[current_station])    with requests.get(stations[current_station], headers={"connection": "close"}, stream=True) \         as response, audiomp3.MP3Decoder(response.socket, mp3_buffer) \         as mp3_decoder, audiobusio.I2SOut(bit_clock=board.GP10, word_select=board.GP11, data=board.GP9) \         as i2s:        while socket_readable(response.socket) and new_station == current_station:            print("Starting to play.")            mixer = audiomixer.Mixer(voice_count=1, buffer_size=2*16384, sample_rate=44100, channel_count=2,                         bits_per_sample=16, samples_signed=True)            i2s.play(mixer) # attach mixer to audio playback            mixer.voice[0].level = 0.1            mixer.voice[0].play( mp3_decoder, loop=True )                            if (i2s.playing):                reconnects += 1                show_mp3_props(mp3_decoder)                show_mem()            else:                print("Failed to start or resume playing. Doing reconnect.")                break # socket readable but            seconds = 0            while i2s.playing and new_station == current_station:                time.sleep(1)                seconds += 1                print(seconds, end="\r")            print("\r\nStopped playing after", seconds, "seconds.")        # free resources        if poll != None:            poll.unregister(response.socket)        response.socket.close()print("out")

Statistics: Posted by kogucior — Wed Jan 15, 2025 10:19 pm



Viewing all articles
Browse latest Browse all 5226

Trending Articles