Tips on what to include in your question:
I'm trying to get subsamples of "source" samples for a samples/sequencer type application. The getSubsample()
method denotes that the arguments are in frames, not bytes, but there is no information (that I could find in either the docs or here) on producing frame offsets.
I've looked at a few sources and mostly found that you can find the frame size of ADPCM when it's uncompressed, but not when it's compressed (riff - How to determine samples count in ADPCM wav file? - Stack Overflow) and that you need the byte layout to be able to calculate anything (riff - How to determine samples count in ADPCM wav file? - Stack Overflow).
Even scouring the SDK I can only find the stubs for these methods, no implementation, so no clues as to how it all works under the hood. There was an earlier thread about creating API for converting between seconds/frames/bytes, but it seems to have gone nowhere: An easy way to unify units of measurement for sample/file players
What's the "official" conversion? I'm using the bare default 44100Hz compressed audio samples you get from playdate.sound.sample.new().
Thank you!
I managed to bruteforce that the "frames" referenced in the docs are just the sample rate of the audio (44100 in this case) using this snippet:
local length = 0
local rate = 0
while length ~= 1.0 do
sub = sample:getSubsample(0, rate)
if edited == nil then
print("nil result")
else
length = edited:getLength()
print("length:", length)
end
rate = rate + 1
end
print(rate - 1)
This is the frames per second, so to use it you also need to multiply it with the length of the sample:
local frameCount = sample:getLength() * sample:getSampleRate()
local sub = sample:getSubsample(0.5 * frameCount, 0.7 * frameCount)
print(sub:getLength()) -- prints "0.2 0.0"
I think it would be nice to clarify in the docs, since the behaviour of :getSubsample()
is not super intuitive, particularly because it can also return a nil value, which is not mentioned by the docs. Just mentioning that and replacing "frames" with "samples" and referencing the audio's sample rate would already do plenty. 
Follow up question, the subsample's length now seems correct but the length of the "allocated buffer" (second return value) is always 0. Is this because the subsample is a window into the original sample?
Cheers!