Core Electronics Forum

Streaming audio through Teensy 3.5 and nRF24

Hi!

I have 2 Teensy 3.5’s, each connected to a Teensy audio adaptor and a nRF24. I have a mic attached to one that is set to transmit, and a speaker attached to the one set to receive. Currently, they are communicating, but the audio quality is so bad that its barely recognisable. I know the audio adaptors are capable of 16bit 44kHz audio and have tested them using example libraries. I’m fairly certain the problem is in my code, as I’m a complete newbie to programming. Can anyone help?

Here is my transmitter code:

// Transmitter

#include <Audio.h>
#include <Wire.h>
#include <SPI.h>
#include <SerialFlash.h>
#include <nRF24L01.h>
#include <RF24.h>
RF24 radio(28, 39); // CE, CSN
const byte address[6] = “00001”;

// GUItool: begin automatically generated code

AudioInputI2S i2s1; //xy=206,128
AudioRecordQueue queue1; //xy=437,134
AudioConnection patchCord1(i2s1, 0, queue1, 0);
AudioControlSGTL5000 sgtl5000_1; //xy=394,305
// GUItool: end automatically generated code

uint8_t bufr[256];

void setup() {
Serial.begin(9600);
AudioMemory(64);
// Enable the audio shield, select input, and enable output
SPI.setMOSI(7);
SPI.setSCK(14);
sgtl5000_1.enable();
sgtl5000_1.inputSelect(AUDIO_INPUT_MIC);
sgtl5000_1.volume(0.6);
sgtl5000_1.micGain(1);
radio.begin();
radio.openWritingPipe(address);
radio.setPALevel(RF24_PA_MAX);
radio.setDataRate(RF24_2MBPS);
radio.stopListening();
queue1.begin();
}

void loop() {
if (queue1.available() >= 2) {
memcpy(bufr, queue1.readBuffer(), 32);
queue1.freeBuffer();
radio.write(&bufr[0], 32);

And here is my receiver code:

//Receiver

#include <Audio.h>
#include <Wire.h>
#include <SerialFlash.h>
#include <nRF24L01.h>
#include <RF24.h>
RF24 radio(28, 39); // CE, CSN
const byte address[6] = “00001”;

// GUItool: begin automatically generated code
AudioPlayQueue queue1; //xy=299,187
AudioOutputI2S i2s1; //xy=587,179
AudioConnection patchCord1(queue1, 0, i2s1, 0);
AudioConnection patchCord2(queue1, 0, i2s1, 1);
AudioControlSGTL5000 sgtl5000_1; //xy=393,311
// GUItool: end automatically generated code

void setup() {
AudioMemory(64);
SPI.setMOSI(7);
SPI.setSCK(14);
sgtl5000_1.enable();
sgtl5000_1.volume(0.6);
radio.begin();
Serial.begin(9600);
radio.openReadingPipe(0, address);
radio.setPALevel(RF24_PA_MAX);
radio.setDataRate(RF24_2MBPS);
radio.startListening();

}

void loop() {
if (radio.available()) {
uint8_t text[32];
radio.read(&text, sizeof(text));
Serial.println(text[0]);
Serial.println(text[15]);
Serial.println(text[31]);
int16_t *p=queue1.getBuffer();
memcpy(p, text, 32);
for (int i=32; i < 256; i++) {
*((char *)p + i) =0; // fill rest of buffer with zero
}
queue1.playBuffer();
}
}

Any assistance would be super appreciated!!!

I would try bumping up the buffer size from 32 to something bigger if you can and see how that affects the noise. Are you able to provide me with links to the source of the examples you are working from, I’m not familiar with these libraries so might help me track down the problem.

Thanks for replying Clinton!
The payload length for the nRF24 in 32 bytes. I’ve tried setting it higher, and then it doesn’t work at all.
The Audio lib is here:
https://github.com/PaulStoffregen/Audio

And there RF24 lib is here:

Hello @Michael47822. Did you get this working? I want to build a similar project.
Could you share the code with me please? Thank you very much.

Hello.

I did get it working! It was a while ago now so let me know if this doesn’t work. I’m pretty sure its correct.

The transmit code:

#include <Audio.h>
#include <Wire.h>
#include <SPI.h>
#include <SerialFlash.h>
#include <nRF24L01.h>
#include <RF24.h>
#include "printf.h"
#define RF24_PAYLOAD 32

RF24 radio(28, 39); // CE, CSN

const byte address[6] = "00001";


// GUItool: begin automatically generated code

AudioInputI2S            i2s1;           //xy=206,128
AudioRecordQueue         queue1;         //xy=437,134
AudioConnection          patchCord1(i2s1, 0, queue1, 0);
AudioControlSGTL5000     sgtl5000_1;     //xy=394,305
// GUItool: end automatically generated code



uint8_t bufr[256];

uint8_t   audioArray[20480];
uint16_t  arrayIndex = 0;
uint8_t   byteIndex = 0;
bool      programFinished = false;
int i;

void setup() {
 

  
 
  AudioMemory(512);
  // Enable the audio shield, select input, and enable output
  SPI.setMOSI(7);
  SPI.setSCK(14);
  SPI.begin();
  sgtl5000_1.enable();
 
  sgtl5000_1.inputSelect(AUDIO_INPUT_MIC);
  sgtl5000_1.volume(0.8);
  sgtl5000_1.micGain(20);
 
  radio.begin();
  radio.setAutoAck(0);
  radio.setRetries(0,15);
  radio.setCRCLength(RF24_CRC_8);
  radio.openWritingPipe(address);
  radio.setChannel(20);
  radio.setPALevel(RF24_PA_MAX);
  radio.setDataRate(RF24_2MBPS);
  radio.stopListening();
  queue1.begin();

 
 
  


}





//String buffer = "";

void loop() { 
 
 

      if (queue1.available() >= 2)
      {
        // get audio data and put into bufr
        memcpy(bufr, queue1.readBuffer(), 256);
        queue1.freeBuffer();

         i = 0;

         // output audio data via radio 8 times, 32 bytes at a time
         while (i < 8) {
         radio.write(&bufr[i * RF24_PAYLOAD], RF24_PAYLOAD);

        i++;
        }
      }


 
}

And the receive code:

#include <Audio.h>
#include <Wire.h>
#include <SerialFlash.h>
#include <nRF24L01.h>
#include <RF24.h>
#include <SPI.h>


RF24 radio(28, 39); // CE, CSN

const byte address[6] = "00001";

// GUItool: begin automatically generated code
AudioPlayQueue           queue1;         //xy=346,168
AudioFilterStateVariable filter1;        //xy=528,210
AudioFilterStateVariable filter2;        //xy=531,285
AudioMixer4              mixer1;         //xy=696,254
AudioOutputI2S           i2s1;           //xy=869,222
AudioConnection          patchCord1(queue1, 0, filter1, 0);
AudioConnection          patchCord2(queue1, 0, filter2, 0);
AudioConnection          patchCord3(filter1, 0, mixer1, 0);
AudioConnection          patchCord4(filter2, 2, mixer1, 1);
AudioConnection          patchCord5(mixer1, 0, i2s1, 0);
AudioConnection          patchCord6(mixer1, 0, i2s1, 1);
AudioControlSGTL5000     sgtl5000_1;     //xy=214,439
// GUItool: end automatically generated code


const unsigned short cAudio_mem_size = 256;
uint8_t   text[cAudio_mem_size]; // audio buffer
uint16_t  textOffset = 0; // postition in text[] - where to write the next (32 Byte) block of radio data



void setup()
{

  AudioMemory(600);
  SPI.setMOSI(7);
  SPI.setSCK(14);
  SPI.begin();
  sgtl5000_1.enable();
  
  sgtl5000_1.volume(0.7);
  radio.begin();
  radio.setAutoAck(0);
  radio.setRetries(0,15);
  radio.setCRCLength(RF24_CRC_16);
  //radio.disableCRC();
  Serial.begin(9600);
  radio.openReadingPipe(0, address);
  radio.setChannel(20);
  radio.setPALevel(RF24_PA_MAX);
  radio.setDataRate(RF24_2MBPS);
  radio.startListening(); 
   
}


void loop()
{ 


 if (radio.available())
  {
    
    
    radio.read(&text[textOffset], 32);
    textOffset += 32;
    
    if (textOffset >= cAudio_mem_size)
    {
      textOffset = 0;
      
      int16_t *p=queue1.getBuffer();
      memcpy(p, text, cAudio_mem_size);
      queue1.playBuffer();

      
      
    } // end-if
  } // end-if


} // end-loop
2 Likes

@Michael47822 Thank you very much for the code.

Do you happen to have a schematic as to how you wired all the hardware up?

I am waiting for the Teensy 4.0 to be available and then i will give this a try.