0

playtone() "blocking"
Moderators: adafruit_support_bill, adafruit

Please be positive and constructive with your questions and comments.

playtone() "blocking"

by nickine9 on Sun Apr 23, 2017 1:42 pm

looking at github (https://github.com/adafruit/Adafruit_Ci ... ground.cpp) it looks as though there is a wait parameter for the playtone() command.

If i do not set this will the rest of the code continue to execute while playing a tone?

if yes to the above are there any drawbacks/restrictions that apply whilst playing a tone?

thanks in advance

nick

nickine9
 
Posts: 39
Joined: Fri Jan 06, 2017 4:14 am

Re: playtone() "blocking"

by adafruit_support_mike on Mon Apr 24, 2017 2:26 am

Yes, tone() is a nonblocking function.

It uses one of the microcontroller's timers and a couple of interrupts associated with it, so you have to be careful about using it with other code that does interrupts or disables interrupts to maintain strict timing (like the NeoPixels).

adafruit_support_mike
 
Posts: 43219
Joined: Thu Feb 11, 2010 2:51 pm

Re: playtone() "blocking"

by nickine9 on Mon Apr 24, 2017 2:36 am

many thanks for that.

nickine9
 
Posts: 39
Joined: Fri Jan 06, 2017 4:14 am

Re: playtone() "blocking"

by tkinsman on Fri May 19, 2017 10:03 pm

Does anyone know the format for the sound input for the method:
void Adafruit_CPlay_Speaker::playSound( const uint8_t *data, uint16_t bytesToGo, uint16_t sampleRate)

I can't read the inline assembly accurately, but it seems that some of the info in the file is used to wait for a while, while the speaker moves.

I don't think it is just a straight byte code of speaker positions.
Can anyone describe how to make a pure signwave using playSound( )??

tkinsman
 
Posts: 40
Joined: Sun Sep 25, 2016 2:04 pm

Re: playtone() "blocking"

by adafruit_support_mike on Sat May 20, 2017 1:43 am

It's a PWM function, so there's no way to get a pure sine wave at all. The best you can get is an approximation based on the PWM values.

The assembly code sets timer/counter 4 to generate a given PWM duty cycle, then waits for a period defined by the variable 'interval'. The value of 'interval' is calculated from the CPU clock speed and the parameter sampleRate.

The values in the audio sample are PWM duty cycles, and correspond roughly to speaker position. 127 (a 50% duty cycle) leaves the speaker coil more or less centered. 0 moves the speaker coil to one side as strongly as it can. 255 moves the coil to the other side as strongly as it can.

adafruit_support_mike
 
Posts: 43219
Joined: Thu Feb 11, 2010 2:51 pm

Re: playtone() "blocking"

by adafruit_support_carter on Sat May 20, 2017 4:07 pm

If i do not set this will the rest of the code continue to execute while playing a tone?


No. The code will block.

The default behavior of CircuitPlayground.playTone() was changed by this pull request:
https://github.com/adafruit/Adafruit_Ci ... nd/pull/18
as discussed in this issue:
https://github.com/adafruit/Adafruit_Ci ... /issues/17

If you want a non-blocking behavior, you will have to pass in the third parameter as 'false'.
Code: Select all | TOGGLE FULL SIZE
playTone(800, 1000);        // blocking call
playTone(800, 1000, true);  // same as above
playTone(800, 1000, false); // non-blocking call

adafruit_support_carter
 
Posts: 2872
Joined: Tue Nov 29, 2016 2:45 pm

Please be positive and constructive with your questions and comments.