Tibbar
Posts: 1
Joined: Fri Feb 28, 2020 5:09 am

RF24 audio stream

Fri Feb 28, 2020 5:46 am

Hi,

I'm trying to stream audio from one RPi 3B+ to another using nRF24 transceivers. Each RPi is connected to an RF24 to transmit and receive, and a Respeaker Mic Array V2.0 to input and output audio. I'm using Raspian and Python 3.7. I can send and receive data with the RF24's using a basic "Hello World" script, so I know they are connected correctly. And I can stream audio from the mic straight out a speaker using on the same RPi using a basic audio stream script, so I know my mics and speakers work also. When I try to transmit the audio data from one RPi to the other, however, the audio output is just a screeching beeping mess. Can anyone help? I had this working on Teensy 3.5's programmed with the Arduino IDE, so I'm fairly certain its possible o Raspberry Pi 3B+ also.

## Receive script ##

Code: Select all

import RPi.GPIO as GPIO
from lib_nrf24 import NRF24
import time
import spidev
import pyaudio
import queue
import numpy as np

p = pyaudio.PyAudio()
q = queue.Queue(8192)

#input stream setup
stream = p.open(
            rate=48000,
            format=p.get_format_from_width(2),
            channels=1,
            input=True,
            input_device_index=2,)

#output stream setup
player=p.open(format = pyaudio.paInt16,rate=48000,channels=1, output=True, output_device_index=2, frames_per_buffer=4096)

 
GPIO.setmode(GPIO.BCM)
 
pipes = [[0xe7, 0xe7, 0xe7, 0xe7, 0xe7], [0xc2, 0xc2, 0xc2, 0xc2, 0xc2]]
 
radio = NRF24(GPIO, spidev.SpiDev())
radio.begin(0, 17)
radio.setPayloadSize(32)
radio.setChannel(0x60)
 
radio.setDataRate(NRF24.BR_2MBPS)
radio.setPALevel(NRF24.PA_MIN)
radio.setAutoAck(False)
radio.enableDynamicPayloads()
radio.enableAckPayload()
 
radio.openReadingPipe(0, pipes[1])
radio.printDetails()
 
radio.startListening()

def startAudio():
    while True:
        if radio.available():
            print ("THERE IS RADIO")
        ##while not radio.available():
            #time.sleep(1 / 100)
            #print ("no radio.available")
        receivedMessage = []
        radio.read(receivedMessage, radio.getDynamicPayloadSize())
        q.put(receivedMessage)
        chunk = 0
    
        if q.full():
            print("q full")
            q.get(chunk)
            data=np.frombuffer(stream.read(chunk,exception_on_overflow = False),dtype=np.int16)
            player.write(data, 4096)
            #stream.write(q.get())
            
startAudio()

        

## Transmit script ##

Code: Select all

import pyaudio
import time
import RPi.GPIO as GPIO
from lib_nrf24 import NRF24
import time
import spidev

p = pyaudio.PyAudio()
stream = p.open(format = pyaudio.paInt16, channels = 1, rate = 44100, frames_per_buffer = 4096, output = False, input = True, input_device_index = 2)#, stream_callback = callback)
stream.start_stream()

GPIO.setmode(GPIO.BCM)

pipes = [[0xe7, 0xe7, 0xe7, 0xe7, 0xe7], [0xc2, 0xc2, 0xc2, 0xc2, 0xc2]]

radio = NRF24(GPIO, spidev.SpiDev())
radio.begin(0, 17)
radio.setPayloadSize(32)
radio.setChannel(0x60)

radio.setDataRate(NRF24.BR_2MBPS)
radio.setPALevel(NRF24.PA_MAX)
radio.setAutoAck(False)
radio.enableDynamicPayloads()
radio.enableAckPayload()

# radio.openReadingPipe(1, pipes[1])
radio.openWritingPipe(pipes[1])
radio.printDetails()


def startAudio():
    while True:
        data2 = stream.read(4096,exception_on_overflow=False)
        i=0
        print ("sending")
        while i<128:
            radio.write(data2[i*32])
            i += 1

startAudio()



Return to “Automation, sensing and robotics”