Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
What kind of values should we normally expect here?
Have you tested with an oscilloscope to check the output?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
At worst the buffer length could be 1 byte short of a full buffer but as we generally sample at comparitively high rates, this should result in only a few milliseconds at most.
I can instrument this up and record it, but I'm not concerned as the buffers are usually quite small (<1k).
I haven't thrown this on a scope yet, as running this with a MakeCode program requires a custom stack, which I do not have access to right now.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The main reason I ask is because I'm not familiar with what kind of numbers we are dealing with.
What is the smallest, average and larger buffer size and sample rate? Based on that, what would be the min, average and max values we would expect here?
Does the buffer size usually match the sample rate? double it, halve it?
Is the sampleRate value clamped somewhere? Are we protected from a division by zero?
What happens if the calculation results in a
fiber_sleep(0)
?Might be worth seeing how easy it is to replicate the original report with a CODAL-only programme and then see how this PR affects it.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Thats an easy one - if you attempt to
fiber_sleep()
with a0
or negative number, you'll effectively just get yourself descheduled for 1 scheduler loop and then you'll be back on the ready queue.There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
But your point about sample rate is valid - it is certainly possible to hand-craft a stream with zero-samples-per-sec set, although quite why that would be useful I don't know right now.
We do later also do this:
codal-microbit-v2/source/SoundEmojiSynthesizer.cpp
Line 233 in 9075651
The only place we set the sample rate is here:
codal-microbit-v2/source/SoundEmojiSynthesizer.cpp
Line 328 in 9075651
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Looking at this, I have two questions.
Carlos and I have just tracked through the numbers and it looks from visual inspection like we're sleeping for 512/44100 milliseconds, which I don't think fiber_sleep can handle well. Do we have a unit mismatch between sample rate and fiber_sleep?
If (as we guess) this should be (512/(44100/1000)) then we're still going to be calling fiber_sleep(11.6099773243) which will end up as 12ms sleep requested. With the jitter involved from the scheduler, isn't there a risk here that we end up sleeping both longer and shorter than that, which will stop us smoothly chaining sounds together?
It feels like it would be better to wait here until we get an event from the audio subsystem that the transfer has completed? @finneyj I know you were specifically optimising this so that buffers could be smoothly chained, so perhaps I've not understood the special casing here? WDYT?