Lib AUDIO / dirty sound!?

Status
Not open for further replies.

Maurin

Member
i don't understand why my code is doing a dirty sound!?

Code:
#include <Audio.h>
#include <Wire.h>
#include <SPI.h>
#include <SD.h>

#define  BAUDRATE         115200     // vitesse du port serie
#define  SYNTH            4        // Synthesizers

AudioSynthWaveform Waveform0, Waveform1, Waveform2, Waveform3;
AudioEffectEnvelope Envelope0, Envelope1, Envelope2, Envelope3;

AudioMixer4              mixer1;
AudioOutputI2S           audioOut;

AudioConnection          patchCord1(Waveform0, Envelope0);
AudioConnection          patchCord2(Waveform1, Envelope1);
AudioConnection          patchCord3(Waveform2, Envelope2);
AudioConnection          patchCord4(Waveform3, Envelope3);
AudioConnection          patchCord5(Envelope0, 0, mixer1, 0);
AudioConnection          patchCord6(Envelope1, 0, mixer1, 1);
AudioConnection          patchCord7(Envelope2, 0, mixer1, 2);
AudioConnection          patchCord8(Envelope3, 0, mixer1, 3);

AudioConnection          patchCord9(mixer1, 0, audioOut, 0);
AudioConnection          patchCord10(mixer1, 0, audioOut, 1);

AudioControlSGTL5000     audioShield;

typedef struct synth {
  boolean state;        // true:free - false:buisy 
  byte sensorID;        // 1 to 64 (8_8 matrix)
  int metro;
  int timeON;
  unsigned long lastPlayTime;
  AudioSynthWaveform *Waveform;
  AudioEffectEnvelope *Envelope;
};

  synth allSynth[SYNTH] = {
 { false, -1, 2000, 100, 0, &Waveform0, &Envelope0 },
 { true, -1, 2000, 100, 0, &Waveform1, &Envelope1 },
 { true, -1, 2000, 100, 0, &Waveform2, &Envelope2 },
 { true, -1, 2000, 100, 0, &Waveform3, &Envelope3 },
};

int synthIndex = 0;

/////////////////////// INITIALISATION
void setup(){

  // Serial.begin(BAUDRATE);    // initialize serial
  //  analogReadRes(16);
  //  analogReadRes(12);
  // Setup Teensy audio shield
  AudioMemory(18);
  audioShield.enable();
  audioShield.volume(0.45);
  
  mixer1.gain(1, 0.8);
  mixer1.gain(2, 0.8);
  mixer1.gain(3, 0.8);
  mixer1.gain(4, 0.8);
  
  // Init all synthetizers
  for( int i=0; i<SYNTH; i++ ) {
    allSynth[i].Waveform -> pulseWidth( 0.5 );
    allSynth[i].Waveform -> begin( 0.5, 440, WAVEFORM_SINE );
    allSynth[i].Envelope -> attack( 5 );
    allSynth[i].Envelope -> decay( 100 );
    allSynth[i].Envelope -> release( 200 );
  }
  
}

/////////////////////// BOUCLE PRINCIPALE
void loop(){
  
    if( allSynth[synthIndex].state == false ){  // false == BUSY_SYNTH
    
      if( ( millis() - allSynth[synthIndex].lastPlayTime ) >= allSynth[synthIndex].metro ){
        allSynth[synthIndex].Envelope -> noteOn();
      }
      if( ( millis() - allSynth[synthIndex].lastPlayTime ) >= ( allSynth[synthIndex].metro + allSynth[synthIndex].timeON ) ){
        allSynth[synthIndex].Envelope -> noteOff();
        allSynth[synthIndex].lastPlayTime = millis();
      }
    }
  
  synthIndex++;
  synthIndex = synthIndex % SYNTH;
  
}
 
You're the same at this, right?

https://github.com/PaulStoffregen/Audio/issues/91

Before I start digging into this, could you try setting all the mixer channels to 0.25, and test again, for the sake of eliminating clipping in the mixer as a possible problem.

In other words, change this:

Code:
  mixer1.gain(1, 0.8);
  mixer1.gain(2, 0.8);
  mixer1.gain(3, 0.8);
  mixer1.gain(4, 0.8);

to this:

Code:
  mixer1.gain(1, 0.25);
  mixer1.gain(2, 0.25);
  mixer1.gain(3, 0.25);
  mixer1.gain(4, 0.25);
 
Yes i'm the one who post on the git sorry for that.
I changed the audio mixer as you said but I still have a dirty sound. :(
 
Code:
#include <Audio.h>
#include <Wire.h>
#include <SPI.h>
#include <SD.h>

#define  SYNTH            4        // Synthesizers

AudioSynthWaveform waveform0, waveform1, waveform2, waveform3;
AudioEffectEnvelope envelope0, envelope1, envelope2, envelope3;

AudioMixer4              mixer1;
AudioOutputI2S           audioOut;

AudioConnection          patchCord1(waveform0, envelope0);
AudioConnection          patchCord2(waveform1, envelope1);
AudioConnection          patchCord3(waveform2, envelope2);
AudioConnection          patchCord4(waveform3, envelope3);
AudioConnection          patchCord5(envelope0, 0, mixer1, 0);
AudioConnection          patchCord6(envelope1, 0, mixer1, 1);
AudioConnection          patchCord7(envelope2, 0, mixer1, 2);
AudioConnection          patchCord8(envelope3, 0, mixer1, 3);

AudioConnection          patchCord9(mixer1, 0, audioOut, 0);
AudioConnection          patchCord10(mixer1, 0, audioOut, 1);

AudioControlSGTL5000     audioShield;

typedef struct synth {
  boolean state;        // true:free - false:buisy 
  byte sensorID;        // 1 to 64 (8_8 matrix)
  int metro;
  int timeON;
  unsigned long lastPlayTime;
  AudioSynthWaveform *Waveform;
  AudioEffectEnvelope *Envelope;
};

synth allSynth[SYNTH] = {
 { true, -1, 2000, 300, 0, &waveform0, &envelope0 },
 { true, -1, 2000, 300, 0, &waveform1, &envelope1 },
 { true, -1, 2000, 300, 0, &waveform2, &envelope2 },
 { false, -1, 2000, 300, 0, &waveform3, &envelope3 },
};

int synthIndex = 0;

/////////////////////// INITIALISATION
void setup(){

  // Setup Teensy audio shield
  AudioMemory(8);
  audioShield.enable();
  audioShield.volume(0.7);
  
  mixer1.gain(0, 0.25);
  mixer1.gain(1, 0.25);
  mixer1.gain(2, 0.25);
  mixer1.gain(3, 0.25);
  
  // Init all synthetizers
  for( int i=0; i<SYNTH; i++ ) {
    allSynth[i].Waveform -> pulseWidth( 0.5 );
    allSynth[i].Waveform -> begin( 0.1, 440, WAVEFORM_SINE );
    allSynth[i].Envelope -> attack( 50 );
    allSynth[i].Envelope -> decay( 50 );
    allSynth[i].Envelope -> release( 250 );
  }
}

/////////////////////// BOUCLE PRINCIPALE
void loop(){
  
    if( allSynth[synthIndex].state == false ){  // false == BUSY_SYNTH
      if( ( millis() - allSynth[synthIndex].lastPlayTime ) >= allSynth[synthIndex].metro ){
        allSynth[synthIndex].Envelope -> noteOn();
      }
      if( ( millis() - allSynth[synthIndex].lastPlayTime ) >= ( allSynth[synthIndex].metro + allSynth[synthIndex].timeON ) ){
        allSynth[synthIndex].Envelope -> noteOff();
        allSynth[synthIndex].lastPlayTime = millis();
      }
    }
  synthIndex++;
  synthIndex = synthIndex % SYNTH;
}
 
Ok i found the bug In the logic level :mad:
I was sending to many noteON();
I added a boolean to solve the problem.
 
Try running this and view the output in the serial monitor.

Code:
#include <Audio.h>
#include <Wire.h>
#include <SPI.h>
#include <SD.h>

#define  SYNTH            4        // Synthesizers

AudioSynthWaveform waveform0, waveform1, waveform2, waveform3;
AudioEffectEnvelope envelope0, envelope1, envelope2, envelope3;

AudioMixer4              mixer1;
AudioOutputI2S           audioOut;

AudioConnection          patchCord1(waveform0, envelope0);
AudioConnection          patchCord2(waveform1, envelope1);
AudioConnection          patchCord3(waveform2, envelope2);
AudioConnection          patchCord4(waveform3, envelope3);
AudioConnection          patchCord5(envelope0, 0, mixer1, 0);
AudioConnection          patchCord6(envelope1, 0, mixer1, 1);
AudioConnection          patchCord7(envelope2, 0, mixer1, 2);
AudioConnection          patchCord8(envelope3, 0, mixer1, 3);

AudioConnection          patchCord9(mixer1, 0, audioOut, 0);
AudioConnection          patchCord10(mixer1, 0, audioOut, 1);

AudioControlSGTL5000     audioShield;

typedef struct synth {
  boolean state;        // true:free - false:buisy 
  byte sensorID;        // 1 to 64 (8_8 matrix)
  int metro;
  int timeON;
  unsigned long lastPlayTime;
  AudioSynthWaveform *Waveform;
  AudioEffectEnvelope *Envelope;
};

synth allSynth[SYNTH] = {
 { true, -1, 2000, 300, 0, &waveform0, &envelope0 },
 { true, -1, 2000, 300, 0, &waveform1, &envelope1 },
 { true, -1, 2000, 300, 0, &waveform2, &envelope2 },
 { false, -1, 2000, 300, 0, &waveform3, &envelope3 },
};

int synthIndex = 0;

/////////////////////// INITIALISATION
void setup(){

  // Setup Teensy audio shield
  AudioMemory(8);
  audioShield.enable();
  audioShield.volume(0.7);
  
  mixer1.gain(0, 0.25);
  mixer1.gain(1, 0.25);
  mixer1.gain(2, 0.25);
  mixer1.gain(3, 0.25);
  
  // Init all synthetizers
  for( int i=0; i<SYNTH; i++ ) {
    allSynth[i].Waveform -> pulseWidth( 0.5 );
    allSynth[i].Waveform -> begin( 0.1, 440, WAVEFORM_SINE );
    allSynth[i].Envelope -> attack( 50 );
    allSynth[i].Envelope -> decay( 50 );
    allSynth[i].Envelope -> release( 250 );
  }
}

/////////////////////// BOUCLE PRINCIPALE
void loop(){
  
    if ( allSynth[synthIndex].state == false ) {  // false == BUSY_SYNTH
      if( ( millis() - allSynth[synthIndex].lastPlayTime ) >= allSynth[synthIndex].metro ) {
        allSynth[synthIndex].Envelope -> noteOn();
        Serial.print(synthIndex);
        Serial.println(" on");
      }
      if( ( millis() - allSynth[synthIndex].lastPlayTime ) >= ( allSynth[synthIndex].metro + allSynth[synthIndex].timeON ) ){
        allSynth[synthIndex].Envelope -> noteOff();
        Serial.print(synthIndex);
        Serial.println(" off");
        allSynth[synthIndex].lastPlayTime = millis();
      }
    }
  synthIndex++;
  synthIndex = synthIndex % SYNTH;
}

Your code appears to be rapidly restarting the waveform. I don't understand what this is supposed to do, but I'm pretty sure it should not print hundreds of "on" for each "off", right?

I don't know what this code is supposed to do, and I'm not quite following your logic, but hopefully this helps you find a path to resolving the problem?
 
Status
Not open for further replies.
Back
Top