Getting correct resolution for AudioThumbnail using addBlock

(both of these screenshots use an AudioThumbnail initialised with sourceSamplesPerThumbnailSample = 512)

I am having a few problems in getting my audio plugin to display the waveform correctly. The upper of the two strips displays what happens when I set the AudioThumbnail up using:

thumbnail.setSource(new FileInputSource(someFileHere));

The lower strip is what happens when I add a sample using:

thumbnail.reset(numThumbnailChannels, sampleRate, numSamples); thumbnail.addBlock(0, testBuffer, 0, numSamples);

Basically when using FileInputSource method, the sourceSamplesPerThumbnailSample variable seems to adjust based on the sample length (shorter samples are drawn with higher resolution). When using the addBlock method, it seems fixed by the value given in the constructor. This means that while sourceSamplesPerThumbnailSample = 512 may display longer samples fine, shorter ones (such as that in the screenshot) remain blocky.

So my question is: is there a way to have a sensible resolution provided by the addBlock method? And if not, is there any way to change sourceSamplesPerThumbnailSample on the fly? Or am I missing something really obvious?

Any help greatly appreciated!


Well, that’s totally as you’d expect - if you’re supplying the data with addBlock, then if the thumbnail is zoomed-in and needs greater detail, then it has no possible way to get higher-resolution data for the blocks that you’ve already given it.

You can figure out the best block size yourself - you know the the number of samples in the file and the number of pixels in your GUI, so just divide the numbers.

OK sorry I’m still a little confused. I’m adding the full sample (from an AudioSampleBuffer) with addBlock, i.e. it has information to draw at full resolution. I’m not zooming in at any stage.

My issue is that I am loading samples of widely varying lengths into the thumbnail. So while I can choose a sensible value of sourceSamplesPerThumbnailSample for a 20 second sample, when I load a 1 second sample there is too much averaging, and I can’t see a way to change sourceSamplesPerThumbnailSample after the thumbnail object has been initialised.

How do I go about explicitly setting the block size? Do I have to do the averaging myself and pass a “reduced resolution” buffer to the addBlock method (rather than the full buffer)? I guess I’m slightly unclear on the exact meaning / mechanism of the addBlock method.

Thanks, Hemmer

You give it all the data, but obviously it’s not going to store every single bit of that data - it’s a thumbnail, not a copy of the whole damn file! It stores a low-resolution map of the peaks in the data. If you want it to store a higher resolution, then that’s what the sourceSamplesPerThumbnailSample value is for!

Sure I understand that, but how do I request that it is stored at a higher resolution!? The parameter sourceSamplesPerThumbnailSample seems fixed at initialisation of the AudioThumbnail object. Surely the appropriate value of sourceSamplesPerThumbnailSample depends on the length of the sample and I can’t see how to change this after initialisation (as I want to pass different lengths of sample to the Thumbnail during the run of the program). Apologies if I’m being really thick here, you might need to spell it out to me!

No, you can’t change it after initialisation! How could you change the resolution when the thumbnail may already contain some data at the original resolution?? If you need to regenerate it at a new resolution, just make a new thumbnail!

im just wondering one thing,

there must be strong reason why [sourceSamplesPerThumbnailSample] is not settable outside the constructor. Can someone elaborate why it would be dangerous to have it available within class interface ?

For the sake of my own experiment i've changed the [AudioThumbnail] class to allow for that, and it works so far. But i wont keep for the production unless i know more about it.

the class itself is pretty interesting to study, but havent spoted any crucial parts when it would cause some disasters. i would love to hear/read about any concerns if possible.