Perhaps not. But, if you did as requested, others could recreate your issue and test potential solutions. Is there some reason you're unwilling to show code?
Hey thanks for also jumping in to help! It's not that I am "unwilling to show code." My question was just more general and hypothetical rather than relating to any specific code I have, but I probably didn't communicate it clear enough.
My question is probably better concisely asked as
"Is it possible to modify the envelope/fade libraries or (write something new) to achieve 'exponential ADSR/fades' vs the 'linear ADSR/fades' that are prepackaged in the envelope/fade functions included in the Teensy/Audio Shield library?"
The reason I haven't shared any code is because I really didn't have any code to share since it was all in different bits and pieces and experimenting with the example sketches.
But I would love help finding a solution, so you're probably right that more information and clarification is needed!
HERE is a link that compiles all the information I am about to reference...
The core of the issue that I am trying to solve: when using slow fade outs of sustained notes via
release(7000); or
fadeOut(7000);, the tail end of the fade is perceived to end rather abruptly, while the beginning of the fade is nice and smooth. I'm about 90% sure that this is because both
release(); and
fadeOut(); follow linear functions (which is understandable and kind of the standard).
When trying to achieve a longer and "smoother" fade out, it helps to have the function be exponential or logarithmic vs linear. It's a subtle difference, but it's there.
HERE is a recording of a fade in and fader out recorded from Teensy 4.0 w/ Audio Shield REV D. If you listen VERY closely to the fade out at the end, you'll hear what I'm talking about. The sustained sawtooth note starts to fade out slowly, but kind of cuts out at the tail end. It's slightly tough to hear at first in this recording because theirs some background ambience. It would help to listen with headphones.
HERE is a recording of that same sound exponentially faded out in a DAW
(warning: this file is a bit louder than the first file – so you might want to adjust your speakers/headphones!) You'll notice in this recording that the tail end of the fade and transition to silence is smoother than the linear fade out recorded from the Teensy.
The reason why I'm 90% sure that this issue isn't from my code is because I get the same issue when testing linear vs exponential fades in a DAW (Logic) with the exact same file.
Here's a screen shot of that:
If you want to try it out on a Teensy...
HERE is the sawtooth sustained note that I am using (titled "1.wav").
And here is a code to try it out:
Code:
// entering "play" into the serial monitor causes "1.wav" to play with a 7000ms fade in.
// entering "stop" into the serial monitor causes "1.wav" to play with a 7000ms fade out.
#include <Audio.h>
#include <Wire.h>
#include <SPI.h>
#include <SD.h>
#include <SerialFlash.h>
// GUItool: begin automatically generated code
AudioPlaySdWav playSdWav1; //xy=119,334
AudioEffectEnvelope envelope1; //xy=337,365
AudioOutputI2S i2s1; //xy=566,359
AudioConnection patchCord1(playSdWav1, 0, envelope1, 0);
AudioConnection patchCord2(envelope1, 0, i2s1, 0);
AudioConnection patchCord3(envelope1, 0, i2s1, 1);
AudioControlSGTL5000 sgtl5000_1; //xy=118,427
// GUItool: end automatically generated code
// Use these with the Teensy Audio Shield
#define SDCARD_CS_PIN 10
#define SDCARD_MOSI_PIN 7
#define SDCARD_SCK_PIN 14
// Use these with the Teensy 3.5 & 3.6 SD card
//#define SDCARD_CS_PIN BUILTIN_SDCARD
//#define SDCARD_MOSI_PIN 11 // not actually used
//#define SDCARD_SCK_PIN 13 // not actually used
// Use these
// for the SD+Wiz820 or other adaptors
//#define SDCARD_CS_PIN 4
//#define SDCARD_MOSI_PIN 11
//#define SDCARD_SCK_PIN 13
String serial_command; // A string for handing serial commands
void setup() {
Serial.begin(9600);
AudioMemory(10);
sgtl5000_1.enable();
sgtl5000_1.volume(0.5);
SPI.setMOSI(SDCARD_MOSI_PIN);
SPI.setSCK(SDCARD_SCK_PIN);
if (!(SD.begin(SDCARD_CS_PIN))) {
while (1) {
Serial.println("Unable to access the SD card");
delay(500);
}
}
// Envelope Settings
envelope1.delay(0); // no attack delay necessary
envelope1.attack(7000); // 7000ms fade in
envelope1.release(7000); // 7000ms fade out
envelope1.sustain(1.0); // sustain indefinitely for duration of file
delay(200);
serial_command = "waiting for command"; // A neutral 'idle' state for the string
}
void loop() {
if(Serial.available()){
serial_command= Serial.readStringUntil('\n'); // receive commands and store in a string
Serial.print("Command Received: ");
Serial.println(serial_command.c_str());
}
// If serial monitor receives "play" then play a wav file with a 7000ms fade in via the 'envelope1.noteOn()'
if(serial_command== "play"){
Serial.println("[ PLAYING FILE ]"); // Communicate which action is occurring
playSdWav1.play("1.wav"); // Play wav file (which is pretty much a 60 sec sawtooth sustained note for this example)
envelope1.noteOn(); // Essentially begins the 'fade in'
serial_command = "waiting for command"; // Reset the string to a neutral 'idle' state for the string
}
// If serial monitor receives "stop" then fade out for 7000ms via the 'envelope1.noteOff()'
if(serial_command== "stop"){
Serial.println("[ STOPPING FILE ]"); // Communicate which action is occurring
envelope1.noteOff(); // Essentially begins the 'fade out'
serial_command = "waiting for command"; // Reset the string to a neutral 'idle' state for the string
}
}
- The code plays "1.wav" from an SD card when you type "play" into the serial monitor and fades the file in via envelope1.noteOn(); with a 7000ms attack.
- The file is then faded out by typing "stop" into the serial monitor via envelope1.noteOff(); with a 7000ms release.
- If you listen carefully, you should hear what I am describing my issue to be: the note starts to slowly fade, but then at the tail end of the fade seems to just cut out (again – I believe due to the linear behavior).
Here's a diagram that visually compares 'exponential ADSR' vs "linear ADSR.':
One isn't right or wrong. In fact, linear is more standard, but exponential accomplishes a smoother fade to silence when using a longer fade time.
Does anyone have any ideas for how I can either modify the envelope or fade to be more exponential? Or accomplish this smoother (log/exponential) fade effect another way?
PS. thanks gfvalvo for requesting more info!