Audio Library

Status
Not open for further replies.
Hi Paul

Your audio library is very interesting for me many thanks great work !

Is possible make simple noise oscillator with speed (high speed = noise and low speed like random lfo)
and freq/note quantiser module (my idea rnd lfo -> quantiser -> oscillator = simple melodic generator)

Future idea is many lfos like fractals, chaotic attractors and cellular automaton - for algoritmic music :)

Some algo. examples on my old site http://jlswbs.blogspot.cz/

Kamil
 
Puredata was initially much of my inspiration for the connection-oriented design of this library.
I'm really interested to try Syso2342's GUI program. Long-term, I think it could really help people to understand and more easily build the object instances to copy into their Arduino sketches. I'd also like to use screenshots of the objects in the library documentation. It's really a piece of this system I never imagined I'd be able to create, so just seeing that screenshot has me really excited.

My experiment was actually a hacked together version of NodeRed (http://nodered.org/), because I liked the way of visualization (and because I am a big modular synthesizer fan). I didn't come so fat yet with the Audio/Teensyduino library implementation so it actually does something (like code generation). It was more of a visual demo how I think it should look.

Initially I wanted to implement a visual audio code generator based on the "WireIt" JavaScript library, so people could just Copy&Paste the code from the browser to have a starting point for their own Teensyduino code. I still hope that Paul will implement "Control"-Inputs for Modules in the Audio library so we could build a small Modular Synthesizer Development Studio ;)

Let me know if the general interest in my idea is bigger - then I might consider to invest some more time in that.

Right now I am quite busy building a hybrid (analog / digital) polysynth using the teensy.
 
Any chance of a short example using play_queue?

Perhaps something like this? But that will compile. Or am I off in left field as to what this is trying to do?

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

const int myInput = AUDIO_INPUT_LINEIN;
//const int myInput = AUDIO_INPUT_MIC;

// Create the Audio components.  These should be created in the
// order data flows, inputs/sources -> processing -> outputs
//
//AudioInputAnalog analogPinInput(16); // analog A2 (pin 16)
AudioInputI2S    audioInput;         // audio shield: mic or line-in
AudioOutputI2S   audioOutput;        // audio shield: headphones & line-out

AudioPlayQueue   myQueue;
// Create Audio connections between the components
//
AudioConnection c1(myQueue, 0, audioOutput, 0);


// Create an object to control the audio shield.
// 
AudioControlSGTL5000 audioShield;
static  int16_t sintable[] = {
	     0,    784,   1569,   2352,   3134,   3914,   4692,   5467,
	  6239,   7007,   7770,   8529,   9283,  10031,  10774,  11509,
	 12238,  12960,  13673,  14379,  15075,  15763,  16441,  17109,
	 17767,  18414,  19051,  19675,  20288,  20889,  21477,  22052,
	 22613,  23162,  23696,  24216,  24721,  25212,  25687,  26147,
	 26591,  27019,  27431,  27826,  28204,  28566,  28910,  29237,
	 29546,  29838,  30111,  30366,  30603,  30822,  31022,  31203,
	 31366,  31510,  31634,  31740,  31827,  31894,  31942,  31971,
	 31981,  31971,  31942,  31894,  31827,  31740,  31634,  31510,
	 31366,  31203,  31022,  30822,  30603,  30366,  30111,  29838,
	 29546,  29237,  28910,  28566,  28204,  27826,  27431,  27019,
	 26591,  26147,  25687,  25212,  24721,  24216,  23696,  23162,
	 22613,  22052,  21477,  20889,  20288,  19675,  19051,  18414,
	 17767,  17109,  16441,  15763,  15075,  14379,  13673,  12960,
	 12238,  11509,  10774,  10031,   9283,   8529,   7770,   7007,
	  6239,   5467,   4692,   3914,   3134,   2352,   1569,    784,
	     0,   -784,  -1569,  -2352,  -3134,  -3914,  -4692,  -5467,
	 -6239,  -7007,  -7770,  -8529,  -9283, -10031, -10774, -11509,
	-12238, -12960, -13673, -14379, -15075, -15763, -16441, -17109,
	-17767, -18414, -19051, -19675, -20288, -20889, -21477, -22052,
	-22613, -23162, -23696, -24216, -24721, -25212, -25687, -26147,
	-26591, -27019, -27431, -27826, -28204, -28566, -28910, -29237,
	-29546, -29838, -30111, -30366, -30603, -30822, -31022, -31203,
	-31366, -31510, -31634, -31740, -31827, -31894, -31942, -31971,
	-31981, -31971, -31942, -31894, -31827, -31740, -31634, -31510,
	-31366, -31203, -31022, -30822, -30603, -30366, -30111, -29838,
	-29546, -29237, -28910, -28566, -28204, -27826, -27431, -27019,
	-26591, -26147, -25687, -25212, -24721, -24216, -23696, -23162,
	-22613, -22052, -21477, -20889, -20288, -19675, -19051, -18414,
	-17767, -17109, -16441, -15763, -15075, -14379, -13673, -12960,
	-12238, -11509, -10774, -10031,  -9283,  -8529,  -7770,  -7007,
	 -6239,  -5467,  -4692,  -3914,  -3134,  -2352,  -1569,   -784 };

void setup() {
  // Audio connections require memory to work.  For more
  // detailed information, see the MemoryAndCpuUsage example
  AudioMemory(12);

  // Enable the audio shield and set the output volume.
  audioShield.enable();
  audioShield.inputSelect(myInput);
  audioShield.volume(0.6);
}


void loop() {
 myQueue.play(&sintable[0],256);
  

}
 
Hi,

Is this Audio library compatible with the Adafruit Electret mic amp (MAX4466)? https://www.adafruit.com/products/1063

I've been trying to use the MonoPeakMeterAnalog with this mic amp board connected on A0, but i'm not getting any input at all. I know the mic does work as it works with AnalogRead() and other applications, but I'd really like to use the Audio library if possible.

I'm on a Teensy3.1.
 
Hi mattcamp; we could help you a lot more if you would post the code you've been trying this with, as in your attempt to make it work with the library and not so much any other experiements like reading it with AnalogRead();

It would probably be best actually if you make a new thread using something like 'having trouble with AudioInputAnalog using electret with preamp from adafruit' repeat the brief you gave above and post your code in [code]<code goes here please>[/code]
 
What I did on my summer vacation or some Audio Library add on's ( hat tip robsoles )

analyze_freq.h

Code:
/* Audio Library for Teensy 3.X
 * Copyright (c) 2014, Paul Stoffregen, paul@pjrc.com
 *
 * Development of this audio library was funded by PJRC.COM, LLC by sales of
 * Teensy and Audio Adaptor boards.  Please support PJRC's efforts to develop
 * open source software by purchasing Teensy or other PJRC products.
 *
 * Permission is hereby granted, free of charge, to any person obtaining a copy
 * of this software and associated documentation files (the "Software"), to deal
 * in the Software without restriction, including without limitation the rights
 * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
 * copies of the Software, and to permit persons to whom the Software is
 * furnished to do so, subject to the following conditions:
 *
 * The above copyright notice, development funding notice, and this permission
 * notice shall be included in all copies or substantial portions of the Software.
 *
 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
 * THE SOFTWARE.
 */

#ifndef analyze_freq_h_
#define analyze_freq_h_

#include "AudioStream.h"

class AudioFreq : public AudioStream
{
public:
	AudioFreq(void) : AudioStream(1, inputQueueArray) { }

	virtual void update(void);

	void begin(bool noReset);
	void begin(void) { begin(false); }
	void stop(void) { m_enabled=false; }
	double period(void);
	double rms(void);
	double dbm(void);

private:
	audio_block_t *inputQueueArray[1];
	bool m_enabled;
	double delta;
	double numSamples;
	double rmsSamples;
	double rmsSamplesSave;
	double valueSquared;
	double valueSquaredSave;
	double Samples;
	int16_t oldSample, cycleKnt ;
	int16_t currentSample;
};

#endif


analyze_freq.cpp

Code:
/* Audio Library for Teensy 3.X
 * Copyright (c) 2014, Paul Stoffregen, paul@pjrc.com
 *
 * Development of this audio library was funded by PJRC.COM, LLC by sales of
 * Teensy and Audio Adaptor boards.  Please support PJRC's efforts to develop
 * open source software by purchasing Teensy or other PJRC products.
 *
 * Permission is hereby granted, free of charge, to any person obtaining a copy
 * of this software and associated documentation files (the "Software"), to deal
 * in the Software without restriction, including without limitation the rights
 * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
 * copies of the Software, and to permit persons to whom the Software is
 * furnished to do so, subject to the following conditions:
 *
 * The above copyright notice, development funding notice, and this permission
 * notice shall be included in all copies or substantial portions of the Software.
 *
 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
 * THE SOFTWARE.
 */

#include "analyze_freq.h"

void AudioFreq::update(void)
{
	audio_block_t *block;

	const int16_t *p, *end;
	block = receiveReadOnly();
	if (!block) {
		return;
	}
	if (!m_enabled) {
		release(block);
		return;
	}
	p = block->data;
	end = p + AUDIO_BLOCK_SAMPLES;

	do {
		int16_t d=*p++;
		numSamples=numSamples+1.0;
		if((oldSample<0)& (d>=0)) {
         if(cycleKnt>15){
          delta=float(oldSample)/(float(d)-float(oldSample));
          Samples=numSamples-delta;
          numSamples=delta;

          cycleKnt=0;
          rmsSamplesSave = rmsSamples;
          rmsSamples = 0.0;
          valueSquaredSave = valueSquared;
          valueSquared=0;
		 }
		cycleKnt++;

		}
		oldSample=d;
        valueSquared+= d*d;
        rmsSamples++;
	} while (p < end);
	release(block);
}

void AudioFreq::begin(bool noReset)
{

	m_enabled=true;
}
double AudioFreq::period(void)
{

   return Samples;

}

double AudioFreq::dbm(void)
{
    return 10.0*log10(valueSquaredSave/(rmsSamplesSave/2));
}

double AudioFreq::rms(void)
{
    return sqrt(valueSquaredSave/rmsSamplesSave);
}

Example sketch

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


const int myInput = AUDIO_INPUT_LINEIN;
// const int myInput = AUDIO_INPUT_MIC;
 
AudioInputI2S        audioInput;
AudioFreq            Freq_L;
AudioOutputI2S       audioOutput;        // audio shield: headphones & line-out

AudioConnection c1(audioInput,0,Freq_L,0);
AudioConnection c4(audioInput,1,audioOutput,1);
AudioControlSGTL5000 audioShield;

void setup() {
  
  AudioMemory(6);
  audioShield.enable();
  audioShield.inputSelect(myInput);
  audioShield.volume(0.75);
  audioShield.unmuteLineout();
  Serial.begin(Serial.baud());
  Freq_L.begin();
 
}

int i;
double avgfreq;
void loop() {
    Serial.println();
    avgfreq=0.0;
    for(i=0;i<10;i++){
       avgfreq+= AUDIO_SAMPLE_RATE_EXACT/Freq_L.period();
       delay(3);
    }
    Serial.print(round((avgfreq/10.0)));
    
  }
 
I'm doing some synth work with the Audio library. Is there any way to do frequency or amplitude modulation of an AudioSynthWaveform? If not, is it worth taking a swing at it myself, or is someone else already working on this?
 
My experiment was actually a hacked together version of NodeRed (http://nodered.org/), because I liked the way of visualization (and because I am a big modular synthesizer fan). I didn't come so fat yet with the Audio/Teensyduino library implementation so it actually does something (like code generation). It was more of a visual demo how I think it should look.

I recently started fiddling with Node-RED. There's now a hacked copy in the Audio library on GitHub.

I modified the GUI to allow multi-input nodes, and to prevent more than one connection to any input. It also reads the node list from static html, so you can simply load "index.html" in your browser without any node.js back end server.

Much work remains to make it actually useful, but here's a screenshot...

noderedhacking.png
 
Looks great but not working me.

When run index.html open windows and frames but do not includes buttons/nodes. Tested WinXP SP3 32bit and Win7 64bit with updated java.

How to work ?

Many thanks help

Kamil
 
Until yesterday, it still depended on having node running red.js locally. Maybe you had that early code.

I'm happy to say it's now able to run stand-alone AND export to C++ code. There are still a lot of rough edges, and currently there's no way to get code back into the canvas.

Here's a copy you can try without downloading anything.

http://www.pjrc.com/teensy/beta/gui/

Ignore "Error: Lost connection to server". It's just a leftover from a bunch of Node-Red functionality that has nothing to do with the audio library.

You should get the same if you just load index.html from a copy on your hard drive. But do get the latest from github. I just committed the change to actually export C++ code only a few minutes ago!
 
Audio library broken in 1.20 rc2

This report would have more information with
"Show verbose output during compilation"
enabled in File > Preferences.
Arduino: 1.0.5 (Windows 7), Board: "Teensy 3.1"
C:\Program Files\Arduino\libraries\SD\utility\Sd2Card.cpp:21:17: fatal error: SPI.h: No such file or directory
compilation terminated.

When trying to compile the audio library examples I get the above error after loading 1.20 rc2 which I need for the ILI9431 library.
 
Add #include <SPI.h> in the Arduino sketch.

Probably most of the examples need to be updated. I'm going to go through them all soon....
 
I've uploaded the latest GUI. Same link:

http://www.pjrc.com/teensy/beta/gui/

This new version keeps your design in browser persistent storage (a html5 feature), so your work isn't lost if you leave and return to the page.

There's a massive increase in documentation. Still many objects are not documented, or have incomplete documentation, but it's definitely improving. :)
 
More updates to the GUI, filling in missing documentation.

If you're trying the GUI, please use "refresh" in your browser to get the latest version. You should see a "special thanks" appear in the info tab when you load the newest code.
 
More updates to the GUI, filling in missing documentation.

If you're trying the GUI, please use "refresh" in your browser to get the latest version. You should see a "special thanks" appear in the info tab when you load the newest code.

Paul, I am happy to see that you liked my idea and made it into something, what I also wanted to achieve!
 
Last edited:
Yes, thanks. Before you mentioned it, I had never even heard of Node-RED.

Hacking it to accept multiple inputs on a node and to prohibit more than one connection to each input took some work, but I'm really happy with the way it looks.

I still really need to go through all the examples. There's a lot of mess and cruft to clean up, and a few really great contributions sitting in messages on this forum.
 
I've been fiddling with making a frequency-modulable version of the waveform synth. It works, but the code is starting to get a little ugly. Would I be better off implementing this as an effect, with input 1 being the carrier signal and input 2 being the modulation signal? The only downside I can think of is that I don't think that architecture would permit amplitude and frequency modulation with the same object - I'd need an effect_amplitudeModulation and an effect_frequencyModulation.
 
I added an envelope object today.

This made me squeal. really. you just dropped my sample ram requirements by at least 25%, I can use this for bass at the very least....possibly piano, need to look at your code and see what it will be able to do. Adlib used to be able to do a decent job of piano notes......

This just gets better and better, and thats before i stumbled across the gui this evening.

Epic Work Mr Stoffregen.

Thank you :)
 
Status
Not open for further replies.
Back
Top