PDA

View Full Version : Audio Library Tutorial ?



heliboy
02-17-2015, 02:09 PM
I have been attempting to use the Audio Library. It looks very powerful but I have been unable to use it. The link off the website opens a design window and I can pick and place objects but there is no way to connect them that I can figure out ? The offline tool is apparently in the audio.zip distribution but I cannot find it. Are there any instructions available of how to use the audio library ? Also, does the tone() function work on the teensy 3.1 on the DAC output ?

Any help appreciated. I have the audio shield but I really would prefer to use the teensy 3.1 ADC and DAC alone.

Nantonos
02-17-2015, 06:31 PM
You connect the objects by clicking on the output of one and dragging to the input of another. Also, whenever you select an object, documentation for it appears over on the right hand side. Take the time to read it and scroll down to read more. There are also tooltips on the objects.

The offline tool is in gui/index.html but some strange security restrictions with file:/// urls mean that it does not work offline in chrome or internet explorer. It works offline in firefox, and works online in all browsers.

There isn't a step by step tutorial. Your best bet is to study the examples. Unfortunately its not possible to import examples into the gui and modify them, which would probably be a useful feature.

tone() is a separate thing, you can't integrate it into the audio library. But nor is it needed, because the waveform object does the same thing.

You can use the gui to make sketches that only use the onboard adc and dac objects.

PaulStoffregen
02-17-2015, 06:39 PM
I really need to make a good video demo / tutorial. Maybe after Teensy-LC is released....

heliboy
02-17-2015, 07:53 PM
I cannot connect anything to anything in IE online. All I get is a cross hair and I can move the objects all around to no avail.

Nantonos
02-17-2015, 09:18 PM
I just tried this (Win7, IE11) and making connections works. I can't reproduce your problem.

kpc
02-17-2015, 09:23 PM
Make sure the window of the editing field is not completely scrolled to the top.
I have the same, when the scrollbar is all the way to the top the contents disappears. Just one pixel below that (or more) everything is fine.
I am on OpenSuse 13.2 Firefox 34.0.5
Edit: The problem is not present in Chrome 28.0.1500.45
Edit 2: I had a look at the gui source code. At first glance the scrollbar/scaling code seems ok, so maybe a bug in firefox?

heliboy
02-18-2015, 01:37 PM
For whatever reason it works on another computer. Thanks for help. Last issue - how do you delete a connection or an element ? How do you clear the whole thing ?

kpc
02-18-2015, 04:47 PM
So I tested it at my work computer:
Opensuse 13.1, Firefox 35.0 Works, When downgrading to 34.0.5, it fails. So definitely a firefox bug.

Nantonos
02-18-2015, 04:48 PM
Delete a connection or an object by selecting it, then using the delete key. Drag-selecting a bunch of items hilights them, but delete does not delete them, oddly.

heliboy
02-27-2015, 09:28 PM
No> Does not work for me. Cannot seem to get this to work for me. Really frustrated with the audio library and 3.1 in general. I can't even get the example SamplePlayer to work anymore. I get a message Error Communicating with Teensy loader - SamplePlayer.cpp not found. What gives ? I can upload Blink fine so the 3.1 is working and the connection is working. Redownloaded the library - No difference.

I have bought over 100 teensy 2.0 and I use them in a commercial application. Love them. But for this poject I heeded better audio and FFT but just cannot get the 3.1 to work reliably. I think I will have to give up on this.

PaulStoffregen
02-27-2015, 10:08 PM
Is this a Windows machine? Sounds like things are really messed up. Maybe try on a different computer?

Teensy Loader has a Help > Verbose Info window, which can show you much more detail on what it's doing... as least from its point of view. But if the Windows machine has some sort of malware or other junk interfering with your browser and other stuff, there may not be much Teensy Loader or anything else can do.

heliboy
02-27-2015, 11:41 PM
Thanks. I will check - but it uploads other porgrams on the audio library distribution fine. For example - ToneSweep works OK - I get sound out of the headphone jack. Same with pulseWidth. Sound out of headphone jack and lineout. But no joy with uploading SamplePlayer. Same error as before - Error Communicating with Teensy loader - SamplePlayer.cpp not found- . Weird. Same PC - same 3.1, all three programs compiled from the audio library and uploaded within 2 minutes.

tenkai
02-28-2015, 12:16 AM
SamplePlayer.cpp needs buttons connected to the right pins in order to work... if you dont have buttons,you could re-write it to trigger the sounds in a loop by replacing the void loop with





void loop() {

sound0.play(AudioSampleSnare);
delay(1000);
sound1.play(AudioSampleTomtom);
delay(1000);
sound2.play(AudioSampleHihat);
delay(1000);
sound3.play(AudioSampleKick);
delay(1000);
sound4.play(AudioSampleGong);
delay(1000);
sound5.play(AudioSampleCashregister);

}



this is a pretty cool loop actually :D kind of pink floyd's money with a GONGGG

defragster
02-28-2015, 03:08 AM
I may be one of the few other folks on Windows - and I've had various issues - but uploading to my Teensy 3.1's has not been one of them.

I've spent days trying to compile AUDIO code [Arduino barfing on my Sketchbook placement (https://forum.pjrc.com/threads/27871-Audio-quot-examples-Analysis-FFT-FFT-ino-quot-Compile-failure-ide-1-6-Beta6?p=66179&viewfull=1#post66179)] so this is a great thing. I just opened and uploaded the SamplePlayer sketch with no issues to a bare Teensy. Very boring though with no blink and no USB output with no Audio hardware or buttons.

I just made the FFT sample more compelling and informative (to me). I vary the sinewave frequency so you can see the shift through the FFT buckets, and the sample transition changes as well. To see more I threw away the float ("0.") and went to integers showing the hundredths value measured. 3715


FFT: 6 7 8 15 23 23 15 6 3 2 2 1 1 1 1 - - - - - - - - - - - - - - - - - - - - -
FFT: - - - 5 37 45 12 1 - - - - - - - - - - - - - - - - - - - - - - - - - - - -
FFT: - - - 5 37 45 12 1 - - - - - - - - - - - - - - - - - - - - - - - - - - - -
FFT: - - - 5 37 45 12 1 - - - - - - - - - - - - - - - - - - - - - - - - - - - -
FFT: - - - 5 37 45 12 1 - - - - - - - - - - - - - - - - - - - - - - - - - - - -
FFT: - - - 5 37 45 12 1 - - - - - - - - - - - - - - - - - - - - - - - - - - - -
FFT: 2 3 4 11 21 25 21 15 21 25 20 10 3 2 1 1 1 - - - - - - - - - - - - - - - - - - -
FFT: - - - - - - - 1 14 46 34 3 - - - - - - - - - - - - - - - - - - - - - - - -
FFT: - - - - - - - 1 14 46 34 3 - - - - - - - - - - - - - - - - - - - - - - - -
FFT: - - - - - - - 1 14 46 34 3 - - - - - - - - - - - - - - - - - - - - - - - -
FFT: - - - - - - - 1 14 46 34 3 - - - - - - - - - - - - - - - - - - - - - - - -
FFT: - - - - - - - 1 14 46 34 3 - - - - - - - - - - - - - - - - - - - - - - - -
FFT: - - - - 1 1 2 5 14 23 25 18 17 24 24 16 7 2 2 1 1 - - - - - - - - - - - - - - -
FFT: - - - - - - - - - - - - - 27 48 21 - - - - - - - - - - - - - - - - - - - -
FFT: - - - - - - - - - - - - - 27 48 21 - - - - - - - - - - - - - - - - - - - -
FFT: - - - - - - - - - - - - - 27 48 21 - - - - - - - - - - - - - - - - - - - -
FFT: - - - - - - - - - - - - - 27 48 21 - - - - - - - - - - - - - - - - - - - -
FFT: - - - - - - - - - - - - - 27 48 21 - - - - - - - - - - - - - - - - - - - -
FFT: - - - - - - - 1 1 1 2 3 8 18 24 23 16 19 25 22 13 5 2 1 1 1 - - - - - - - - - -
FFT: - - - - - - - - - - - - - - - - 1 6 39 43 10 1 - - - - - - - - - - - - - -
FFT: - - - - - - - - - - - - - - - - 1 6 39 43 10 1 - - - - - - - - - - - - - -
FFT: - - - - - - - - - - - - - - - - 1 6 39 43 10 1 - - - - - - - - - - - - - -
FFT: - - - - - - - - - - - - - - - - 1 6 39 43 10 1 - - - - - - - - - - - - - -
FFT: - - - - - - - - - - - - - - - - 1 6 39 43 10 1 - - - - - - - - - - - - - -
FFT: - - - - - - - - - - - - - 1 1 2 4 11 21 25 20 15 22 25 19 9 3 2 1 1 - - - - - -
FFT: - - - - - - - - - - - - - - - - - - - - - 1 17 47 32 2 - - - - - - - - - -
FFT: - - - - - - - - - - - - - - - - - - - - - 1 16 47 32 2 - - - - - - - - - -
FFT: - - - - - - - - - - - - - - - - - - - - - 1 17 47 32 2 - - - - - - - - - -
FFT: - - - - - - - - - - - - - - - - - - - - - 1 17 47 32 2 - - - - - - - - - -
FFT: - - - - - - - - - - - - - - - - - - - - - 1 16 47 32 2 - - - - - - - - - -
FFT: - - - - - - - - - - - - - - - - - 1 1 2 2 6 15 23 24 17 17 24 24 16 6 2 2 1 1 -

heliboy
02-28-2015, 01:33 PM
Thanks to all the help on this. From everyones input I had to believe it was some dumb Windows problem. So I got a good nights sleep - took the PC down - removed power, batteries, scrubbed with spybot and voila - sampleplayer runs again. Now onto figuring out the audio library tool. I need to process an analog signal to band pass between 1 and 10 Hz. Then FFT to get the two dominant frequencies. One should be about 0.25Hz, the other around 1.25 Hz. I guess I could forego the FFT and just figure out the periods by timing the peaks of these slow waves. I am using the DAC to an oscilloscope to see the filtered signal, but is there a PC program that can be used to capture data from the serial output for visualization ?

heliboy
02-28-2015, 08:07 PM
Hey, is it possible to get a signal as an input to the audio library that does not come from the audio shield chip line_in or I2S ? Can I use a variable in my program as the input feed into the audio library ?

PaulStoffregen
02-28-2015, 08:35 PM
Hey, is it possible to get a signal as an input to the audio library that does not come from the audio shield chip line_in or I2S ?

Sure. The audio library currently has 2 working input objects. The other one uses the on-chip ADC.



Can I use a variable in my program as the input feed into the audio library ?

There must be at least 1 hardware input or output. The library doesn't run unless at least 1 of those hardware objects is present.

You could use the DAC output or PWM output, and simply not connect anything to those pins. That will let the library run.

There are several synth objects that create signals mathematically. Maybe the DC object will be close to "use a variable in my program as the input feed"?

http://www.pjrc.com/teensy/gui/?info=AudioSynthWaveformDc

defragster
02-28-2015, 08:49 PM
Yes, how for sure I can't say. Web Doc page is here: Teensy Audio Library (http://www.pjrc.com/teensy/td_libs_Audio.html) it links to here: Audio System Design Tool (http://www.pjrc.com/teensy/gui/?info=AudioInputAnalog)

Using T_FFT_SINEWAVE.ino sample above (the included analyze/fft.ino) you'll see the SINEWAVE as input to a patch cord.

From the Design tool you drag and drop lay out to what you want (items on left and notes on right) and it generates the init code like this from Export in the browser:


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

// GUItool: begin automatically generated code
AudioInputAnalog adc1; //xy=143,73.99998474121094
AudioAnalyzeFFT1024 fft1024_1; //xy=311.79998779296875,73.19999694824219
AudioConnection patchCord1(adc1, fft1024_1);
// GUItool: end automatically generated code


Given this I assume you just adjust the patch cord setting in the FFT sample to specify a PIN ( "adc1" ) and that will sample from that point. This is my next step with my analog mic.

I'm still using the FFT_SINEWAVE.ino as my basis and have done some edits to see the time taken and merged in the MemoryAndCpuUsage.ino so I can see CPU and RAM usage in this sample and then recompiling for 512 and 1024 FFT's.

QUESTION:
> Is this the common/best/intended process?
> After Setup can I changed the processing type in loop()? i.e. Stop 1024 and Start 512?
> I looked for an .end() function and didn't see it.
> Will it quit if I just stop doing .read()? What about the resources?
> Similarly can I cycle through the FFT Methods while in loop() or do I need to restart?
> I'm not even sure what I don't know yet, but from the sample I found CPU higher on 1024, but 512 takes twice as long at 25% of CPU usage?
> Can I record/capture the Sample 'analog mic' data and create a way to feed it in statically like the Sine wave

My status info on my 120 sample loop:
// CPU%, Mem Use, cum loop() pass count sample Ready and Not, Samples per loop, Per sample time, and time for Samples
// fft1024=52,52 all=53.31,53.92 Memory: 4,8 Ready:240 Not:967795 # Samples:120 Time per sample:11 Sample Time:1393
// fft256=12,12 all=13.84,14.34 Memory: 1,2 Ready:240 Not:2160366 # Samples:120 Time per sample:23 Sample Time:2785

defragster
02-28-2015, 09:18 PM
From Paul's note - re my last question:
It seems I could do a sketch to get an interrupt set of samples for data I like on my source and store that data. Then for testing I could push that stored data out the DAC wired to an Analog input with another interrupt driven write while the FFT is running and it should see some similar version of what I recorded coming back in that pin?

I don't have an audio board - but it seems like it might be useful for my development in cases like this for 'component' testing?

My initial hope was maybe there was a way to have the FFT sample ask me [if I could trigger an interrupt to it] for the next value rather than actually read it from an input. If it could do that and I had the actual sample set from the FFT code for a good sample I could bypass my sampling and the DAC output simulation - and feed in a direct set of prior sample data without sample/conversion rate issues altering the 'testing'.

defragster
02-28-2015, 09:49 PM
There must be at least 1 hardware input or output. The library doesn't run unless at least 1 of those hardware objects is present
http://www.pjrc.com/teensy/gui/?info=AudioSynthWaveformDc


... because it is the hardware's interrupt that activates the Library's sampling code.

Would that be a valid completion of that sentence?

PaulStoffregen
02-28-2015, 11:05 PM
Actually, the hardware interrupt triggers a second software-only interrupt, which then runs the update functions on all audio objects.

This structure is used so the hardware interrupt can run at normal interrupt priority, but the lengthy updates can run at a much lower priority, allowing the nested priority interrupt controller to service other hardware interrupts while the audio library updates do all the computational work.

defragster
03-01-2015, 02:11 AM
Cool - knowing that hardware interrupts are involved makes it clear. Still wondering about the other questions above (https://forum.pjrc.com/threads/27807-Audio-Library-Tutorial?p=66246&viewfull=1#post66246) - and now if the software interrupt mechanism could be manually provoked to prompt the data read of a present analog output port in some fashion?

PaulStoffregen
03-01-2015, 02:42 AM
Still wondering about the other questions above

Ok, but quickly....




I'm still using the FFT_SINEWAVE.ino as my basis and have done some edits to see the time taken and merged in the MemoryAndCpuUsage.ino so I can see CPU and RAM usage in this sample and then recompiling for 512 and 1024 FFT's.

QUESTION:
> Is this the common/best/intended process?


yes/probably/yes



> After Setup can I changed the processing type in loop()? i.e. Stop 1024 and Start 512?
> I looked for an .end() function and didn't see it.


The audio library is designed to continuously stream and process audio. There's not .end() or stop feature.




> Will it quit if I just stop doing .read()? What about the resources?
> Similarly can I cycle through the FFT Methods while in loop() or do I need to restart?


The buffer only holds the most recent FFT analysis.

If you stop checking FFT.available(), the FFT analysis will go on and on, overwriting the old data with each new update.




> Can I record/capture the Sample 'analog mic' data and create a way to feed it in statically like the Sine wave


Sure. The two "queue" objects let you get access to the 128 sample blocks from your sketch. You can grab the blocks and store them to a file on a SD card, for example. In fact, the Recorder example does exactly that.

You can also inject blocks into the library using the play queue object. See the web design tool for documentation.

Those objects are called "queue" because they will queue up data, of course within the limit of how much memory you gave to the audio library. That lets you put more than 1 block at a time into the play queue, which is handy if you read a 512 byte sector from the SD card or receive data some other way. Likewise, the record queue will buffer up data arriving from the library, which really helps if the SD card or something else occasionally takes longer. As long as you maintain the overall data rate, those queue help relieve your responsibility to always have very low latency in your code.

defragster
03-01-2015, 03:05 AM
that should get me closer to understanding enough to start, thanks.

defragster
03-27-2015, 10:42 AM
Paul: regarding PlayQueue Feeding to fft256, int16_t, I found the Freescale_Demo_3.ino that plays from a queue.

int16_t *p = audio.getBuffer();
// ...
audio.playBuffer();

Do I understand this assigns me a buffer I fill and then never see again, that fft will return to the pool?

I plan to set up an ADC Lib read of my own data and fill the buffer to forward. Or according to post #23 above I could play back saved buffers.

The design tool gave me this:

AudioPlayQueue queue1; //xy=230,152
AudioAnalyzeFFT256 fft256_1; //xy=401,152
AudioConnection patchCord1(queue1, fft256_1);

Is this interaction missing a hardware link - or will queue1.playBuffer(); excite the needed software?

Giving me a fft256_1.available() when it gets buffer data for the 256 data points?

Note: Need quick FFT and response to sounds (ideally <.01 sec) and min CPU burn here for main tasks
Note: Using ADC to sample near 8kHz captures needed audio range, and narrows freq/bin in 4kHz
Note: Perhaps preloading last 128 samples as start of next set of 256 so FFT's finish faster
Note: Perhaps monitor peak (100db+) sounds as I get ADC data and not even bother with FFT

kpc
03-27-2015, 07:46 PM
You could always call AudioStream::update_all() yourself. This way you can also stop/start the processing of data, as you sample the input yourself.
If you just need the fft from the Audio library and nothing else, just call the CMSIS fft.

defragster
03-27-2015, 10:06 PM
kpc: Thanks for the hint if it doesn't go by itself- I don't see any sketches doing that and not published on PJRC paraphrase - Hardware involvement needed to trigger Audio function, and Paul noted once that Stopping wasn't provided for once hooked up. 'update_all' Forum search hit two threads - your post (google finds already) and one I found one from Paul : https://forum.pjrc.com/threads/27194-Allocation-of-software-triggered-interrupts?p=59082&viewfull=1#post59082

If I asked well and Paul reads my post he should have good feedback as he pointed me that way.

defragster
03-27-2015, 11:12 PM
kpc: given your new fft256 code will be getting the data.

> If I send in a 'block' full of pre-read data will the playBuffer(); replace the series of Analog reads currently used to store the data so fft256 code knows it contains 128 values? Or do I need to manually call the update with each new value?

> I'll have to send in two blocks to get the 256 samples, will your optimized code start processing those first 128 samples as they came in 'normally'?

> If I do send a block of 128 data samples the logical ordering would be [0]-[127] on int16_t boundaries.

kpc
03-27-2015, 11:39 PM
Manually call with each new block of samples, not per sample.
It will start processing as the samples come in. But due to pipelining, the fft is not immediately available when all inut data is input. You should feed in more data before the fft data becomes available.
And yes, just a normal linear array if in16_t samples.

defragster
03-28-2015, 12:59 AM
Thanks again for your attention kpc - I'll see if I can run with the info have.

john-mike
10-21-2015, 10:30 PM
I've found the format for importing nodes but there are some issues.



[{"id":"i2s2","type":"AudioOutputI2S","x":2105,"y":176,"z":0,"wires":[]},{"id":"i2s1","type":"AudioInputI2S","x":133,"y":176,"z":0,"wires":[["mixer1:0"],[]]},{"id":"i2s2","type":"AudioOutputI2S","x":489,"y":158,"z":0,"wires":[]},{"id":"mixer1","type":"AudioMixer4","x":311,"y":181,"z":0,"wires":[["i2s2:0"]]}]


If you drag this string to the tool or hit CTRL+SHIFT+i the nodes will come up but there seems to be a problem with naming, as mentioned by Paul here (https://github.com/PaulStoffregen/Audio/issues/56). They are given random ones and cannot be renamed.

Changing the end of that string to
:[["i2s2:0"]]},0] doesn't turn off random naming as the import nodes function (https://github.com/PaulStoffregen/Audio/blob/b3b3f857a89693d11860ac3c9d3782fd00bf010b/gui/red/nodes.js#L309) should but if you change


if (createNewIds) {
//node.z = RED.view.getWorkspace();
//node.id = getID();
node.id = n.id;
}
the imported names are correct.

It shouldn't be too hard to add this string to the output of the save function after the arduino stuff but the naming thing needs to be fixed too.

npashine
03-29-2016, 11:23 AM
Hi all,
is there any way to play audio data from within teensy without using const.

I mean can we play non-const dynamically created audio data.

or can we play dynamically created audio data without using external serial flash.

PaulStoffregen
03-29-2016, 01:49 PM
is there any way to play audio data from within teensy without using const.

I mean can we play non-const dynamically created audio data.


Maybe you're asking about the memory player object? The one used in the Part 2-3 "Playing Samples" part of the tutorial.

Sure, you can pass it an array which isn't const. That code expects the first 4 bytes to tell the format and length of the data, so make sure you look into that important detail and get those first 4 bytes correct. Then it'll just play whatever is in your buffer. The only issue is you'll probably be limited to about 50K for the buffer, since Teensy has only 64K RAM.



or can we play dynamically created audio data without using external serial flash.

Well, maybe. You can use the queue object to put your own data into the audio library. Or you can just edit the library to add your own code.

The library already has some synthesis and effect objects, so maybe those can help. Hard to say without any idea of what you're trying to accomplish.

npashine
03-30-2016, 06:14 AM
Hi paul,

Actually, what I'm trying to achieve is I'm generating a dynamic audio data from SDK which is about >65537B and after passing that raw data to wav2sketch it becomes 13355B buffer. This buffer I have to keep as const to play and actual raw data memory is out of RAM that's why I'm using external flash but to reduce the cost I don't want to use an external flash and looking for any provision.

Also, paul can you redesign the teensy 3.2 as per our requirement which includes little add-on. After that, I can order in bulk.

PaulStoffregen
03-30-2016, 10:38 AM
I don't understand your question.

Part of it seems like you want to put a sound inside Teensy's program memory, which won't fit, but these small sizes you mention can easily fit (well, unless you're written a *lot* of code to fill up the internal flash memory).

Part of your question seems like you wish to use RAM instead of flash. You certainly can. Just create a similar array without "const", and fill it with data. Remember the first int (4 bytes) must be the format+size.

I do understand the second part. No, we don't do customized Teensy boards.