Syntax for AudioEffectFade

Status
Not open for further replies.

Teenfor3

Well-known member
I wonder could someone help with example for how to use AudioEffect Fade.....fadeIn and fadeOut
I set it up as per the GUI tools etc with connections etc
say.......sound1>>>>>>>>fade1>>>>>>mixer1in1>>>>>>mixer1out>>>>>>dac
.....sound2>>>>>>>>fade2>>>>>>mixer1in2>>>>>>mixer1out>>>>>>dac
all works and I get sound OK, but where in the sketch code do I tell it to fade or not fade..???

I seem to get fadeOut(fadetime) to work but cannot get fadeIn(fadetime) to do anything I can see or hear.
fadeOut(fadetime) fadesout to Zero.....then I dont hear that sound again....until restart...how do I switch it on again...???
I think I am using it in wrong place or wrong syntax but no compiler error..??
 
I can get fade to work with continuos synth sounds as playing a waveform
The code below would just give 2 second of sound followed by 1 second of silence repeated
and faded in and out for 100 ms

AudioWav1.begin(0.8, 1500, WAVEFORM_ARBITRARY);
AudioWav1.arbitraryWaveform(sndwav1, 1200);

fade1.fadeOut(100);

delay(1000);

fade1.fadeIn(100);

delay(1000);

fade1.fadeOut(100);

But I cannot see what it does or how to code ir for playing sound clip from memory array etc....???
or how to mix or merge several sounds cleanly.
 
Normally I would not look at this until you post a complete program, but as an exception I wrote a program just now to verify this fade effect is working.

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

// GUItool: begin automatically generated code
AudioSynthWaveformSine   sine1;          //xy=161,100
AudioEffectFade          fade1;          //xy=328,49
AudioOutputAnalogStereo  dacs1;          //xy=509,153
AudioOutputI2S           i2s1;           //xy=518,77
AudioConnection          patchCord1(sine1, fade1);
AudioConnection          patchCord2(sine1, 0, i2s1, 1);
AudioConnection          patchCord3(sine1, 0, dacs1, 1);
AudioConnection          patchCord4(fade1, 0, i2s1, 0);
AudioConnection          patchCord5(fade1, 0, dacs1, 0);
AudioControlSGTL5000     sgtl5000_1;     //xy=299,176
// GUItool: end automatically generated code


void setup() {
  AudioMemory(12);
  sgtl5000_1.enable();
  sgtl5000_1.volume(0.5);
  sine1.amplitude(1.0);
  sine1.frequency(440);
}

void loop() {
  fade1.fadeOut(100);
  delay(150);
  fade1.fadeIn(100);
  delay(150);
}

When I run this, here's what I see on the DAC0 pin.

file.png

file.png



But I cannot see what it does or how to code ir for playing sound clip from memory array etc....???
or how to mix or merge several sounds cleanly.

Generally, you would need to have each player go through a fade effect and combine them all with a mixer. Then you'd need to properly control them from your code, where you'd start the fade out near the end of the playing and start another playing and begin its fade in.

If that doesn't help enough, I know you've been on this forum long enough to know how we work here. The expectation is to show a complete program and post enough info for anyone to be able to reproduce the problem. At the very least that means sharing 2 sound files so your program can actually be run. When you don't follow the "Forum Rule", the result is little or no reply. We do really well at helping when you give a complete program, so if you want better help, you know what to do....

But at least this reply can prove the fade effect is indeed working, and I posted a complete program so you can run it and get the same result!
 
Thanks Paul for the reply
Sorry I didn't post any code but I was really only wanting some instructions how it worked, I didnt have any real application, just trying it in any sound sketch.
I have been playing with it over the last few days and doing as you have done in your sketch. At first it was easy enough to see what it was doing with one continuous sound, basically switching it on and off. The problem I didn't catch on to was when a sound clip is faded out you don't hear it again until it is faded in again some time before it is replayed. I was also trying to join the same sounds repeatedly to see if I could get continuous sounds but no matter how short the fade..even......fade1.fadeIn(1) there was still a change from one sound to another and I would hear a slight glitch. The join of different sounds is smooth enough, you hear the transition OK but it is smooth.
All this fading in and fading out and play each time means a lot of commands for fast changing sounds but it would be OK for longer slower sounds.
Below is code for a sketch I was playing with, I have added some comments to explain what I think it is doing...........it works OK....I don't think it is possible to cut playing a sound somewhere random in the middle and fade in the same sound with hearing the join.

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

// GUItool: begin automatically generated code

AudioPlaySerialflashRaw  playFlashRaw1;  //xy=269,424
AudioPlaySerialflashRaw  playFlashRaw2;  //xy=269,424
AudioOutputAnalog        dac1;           //xy=502,412
AudioEffectFade          fade1;          //xy=418,341
AudioEffectFade          fade2;          //xy=418,341
AudioMixer4              mixer1;         //xy=482,524
AudioConnection          patchCord1(playFlashRaw1, fade1);
AudioConnection          patchCord2(playFlashRaw2, fade2);
AudioConnection          patchCord3(fade2, 0, mixer1, 1);
AudioConnection          patchCord4(fade1, 0, mixer1, 0);
AudioConnection          patchCord5(mixer1, dac1);

// GUItool: end automatically generated code

#define FLASH_CHIP_SELECT  6

void setup() {
  Serial.begin(9600);
  AudioMemory(20); //4

    // using Teensy audio shield because SpiFlash chip is on it
  SPI.setSCK(14);  // Audio shield has SCK on pin 14
  SPI.setMOSI(7);  // Audio shield has MOSI on pin 7
  SPI.setMISO(12);  // Audio shield has MISO on pin 12
   
  delay(2000);

  // Start SerialFlash
  if (!SerialFlash.begin(FLASH_CHIP_SELECT)) {
    while (1) {
      Serial.println ("Cannot access SPI Flash chip");
      delay (1000);
    }
  }
 
}

void loop() {
 
   playFlashRaw1.play("SND1.RAW");   //start play loud normal no fade in
  //  while (playFlashRaw1.isPlaying());  // If I put this line in, file plays to normal end no fadeout
    delay(2000);  // only play 2 seconds, file is cut short and fades out fade1
 
   fade1.fadeOut(100);    // this fades out playFlashRaw1 SND1 after 2 secs
   fade2.fadeIn(100);    // this starts fade in for playFlashRaw2, should it be here or after the .play SND2
                         
  playFlashRaw2.play("SND2.RAW"); // fader is started so SND2 fades in
    delay(4000);  // play 4 seconds of this file and then fade out

   fade1.fadeIn(100);  // need to fade in fade1 for repeat around loop otherwise I wont hear it loud at start
   fade2.fadeOut(100);   // this fades out SND2 after the 4 seconds above

}
 
Status
Not open for further replies.
Back
Top