Go Down

Topic: TMRpcm - multichannel, is it possible? (Read 3223 times) previous topic - next topic

MickeyGS

Hello everyone!
Quick question: I've learned how to make sounds with arduino, SD shield and a speaker, but can I play some sounds while the music is still playing? Is that what you call it "multichannel"? Is it possible with arduino? Is it possible with TMRpcm? Or do I have to buy multiple arduinos (well, ATMega328s for a circuit)  per each channel?
Thanks in advance

DVDdoug

I've never used the Arduino to generate audio...  But I do know a lot about audio & electronics and I can give you some general information.

Mixing is done by summation (addition).   An analog audio mixer is built around an analog summing amplifier.   A summing amplifier is easy to build with an op-amp if you wanted to mix the Arduino's "analog" output with another analog signal.

Digitally, you simply sum sample-by-sample. (i.e. 44,100 samples per second with "CD quality" sound.)  That means both audio files/streams must have the same sample rate & bit depth (or one needs to be upsampled/downsamped), and if you have a compressed format (such as MP3) it has to be decompressed before mixing.

You do have to be aware of potential clipping (distorted flat-topped "waves").   For example, if you have a "loud" that uses the full 0-5V range (or -2.5 to +2.5 after filtering) and you add another signal to it, you are going to get clipping.    The digital audio data can also get clipped.    So, you may have to reduce the digital level before mixing.     (You change the volume by multiplication...  i,e, Multiply each sample by 0.5 to cut it in half before summing.)

MickeyGS

Thank you, DVDdoug.

A lot of usefull info about sample rate and clipping, but I thought making two separate sounds sound from one arduino board at once would be as easy as
Code: [Select]
PlaySound("sound1.wav",channel=1)
PlaySound("sound2.wav",channel=2)

TMRh20

It is fairly straight-forward but for a few important details. I'll be including an example sketch for multi track sometime soon, but until then, here is some info:

Recommended: 16-20khz Sample Rate

Steps on Uno or similar boards:
1. Edit the pcmConfig.h file included with the library
    a: Uncomment  #define buffSize 128    (A bit more memory is required for multi track. Needs to be an even number)
    b: Uncomment  #define ENABLE_MULTI

2.  Connect one speaker or other device from pin 9 to ground, and another from pin 10 to ground. Optionally from pin to pin.

3. Modify the following sketch to include the desired wav file name(s):   wav.play("your.wav",1);

Code: [Select]

#include <SD.h>                      // need to include the SD library
#define SD_ChipSelectPin 4  //using digital pin 4 on arduino nano 328, can use other pins
#include <TMRpcm.h>           //  also need to include this library...
#include <SPI.h>

TMRpcm wav;   // create an object for use in this sketch

void setup(){

  wav.speakerPin = 9; //5,6,11 or 46 on Mega, 9 on Uno, Nano, etc
  //Enable the 2nd output:
  pinMode(10,OUTPUT); //Pin pairs: 9,10 Mega: 5-2,6-7,11-12,46-45
 
  Serial.begin(115200);

  if (!SD.begin(SD_ChipSelectPin)) {   
    return;
  }else{   Serial.println("SD ok"); }

}


void loop(){ 

  if(Serial.available()){   
    switch(Serial.read()){
    case 'a': wav.play("emb.wav",0); break;  //Play on pin 9
    case 's': wav.play("emb.wav",1); break;   //Play on pin 10
    case 'd': wav.play("one.wav",0); break;  //Play on pin 9
    case 'f': wav.play("two.wav",1); break;   //Play on pin 10
    case 'p': wav.pause(); break;
    case '?': if(wav.isPlaying()){ Serial.println("A wav file is being played");} break;
    case 'S': wav.stopPlayback(); break;
    case '=': wav.volume(1); break;
    case '-': wav.volume(0); break;
    case '0': wav.quality(0); break;
    case '1': wav.quality(1); break;
    default: break;
    }
  }

}


See https://github.com/TMRh20/TMRpcm/wiki/Advanced-Features for more detailed info


MickeyGS

Many thanks, TMRh20 this just may do the trick

Go Up