# SuperCollider Audio Query Class

**URL:** <https://discourse.flucoma.org/t/supercollider-audio-query-class/1444>\
**Category:** Code Sharing\
**Created:** [May 13, 2022, 1:45pm UTC](https://discourse.flucoma.org/t/supercollider-audio-query-class/1444 "2022-05-13T13:45:25Z")\
**Posts on this page:** 7\
**Page:** 1

<div class="post-metadata">

**Author:** ![heretogo](https://discourse.flucoma.org/letter_avatar_proxy/v4/letter/h/278dde/32.png) [@heretogo](https://discourse.flucoma.org/u/heretogo)\
**Post date:** [May 13, 2022, 1:45pm UTC](https://discourse.flucoma.org/t/supercollider-audio-query-class/1444/1 "2022-05-13T13:45:25Z")

</div>

This relates to @tedmoore’s # [Audio Query in SuperCollider Demo](https://discourse.flucoma.org/t/audio-query-in-supercollider-demo/1149) example patch.

I have started to move this functionality inside a class and I’ve made the repo public [here](https://github.com/heretogo/audioquery/pull/1).

There are still some action items. I’ve been a little busy lately to be able to finish but I thought I’d share in case this was useful to anyone else.

---

<div class="post-metadata">

**Author:** ![heretogo](https://discourse.flucoma.org/letter_avatar_proxy/v4/letter/h/278dde/32.png) [@heretogo](https://discourse.flucoma.org/u/heretogo)\
**Post date:** [May 23, 2022, 7:19pm UTC](https://discourse.flucoma.org/t/supercollider-audio-query-class/1444/2 "2022-05-23T19:19:59Z")

</div>

Can someone show me an example of using the `channelFunc` method of the `FluidLoadFolder` UGen?

The following code generates errors:

```auto
FluidLoadFolder(source_files_folder, channelFunc: {
  arg ...args; args.postln;
});

```

Error log:

```auto
[1, 1, 1]
[1, 1, 2]
[1, 1, 3]
[1, 1, 4]
[1, 1, 5]
[1, 1, 6]
[1, 1, 7]
[1, 1, 8]
[1, 1, 9]
[1, 1, 10]
[1, 1, 11]
[1, 1, 12]
[1, 1, 13]
[1, 1, 14]
[1, 1, 15]
[1, 1, 16]
[1, 1, 17]
[1, 1, 18]
Channel index out of range.
Channel index out of range.
...

```

What is the expected return value of `channelFunc`? Something seems not normal here (perhaps a bug?).

---

<div class="post-metadata">

**Author:** ![weefuzzy](https://discourse.flucoma.org/user_avatar/discourse.flucoma.org/weefuzzy/32/362_2.png) [@weefuzzy](https://discourse.flucoma.org/u/weefuzzy)\
**Post date:** [May 24, 2022, 9:25am UTC](https://discourse.flucoma.org/t/supercollider-audio-query-class/1444/3 "2022-05-24T09:25:42Z")

</div>

It should return an array of channel numbers to read from the file, such as could be passed to `Buffer.readChannel`. Its purpose is to give fine control over cases where the channel count in your source folder is variable (e.g. a mixture of mono and stereo). The `Buffer`s populated by `FluidLoadFolder` will always have as many channels as whatever the ‘widest’ file in the folder is, and the default behaviour with ‘narrower’ files is just to wrap and repeat source channels until the destination is full.

The input arguments to the function are: the channel count of the current file; the maximum channel count across the folder; the index of the current file.

For the above, it looks like all your files are mono anyway, so you could just leave `channelFunc` as `nil`, or do `^[0]`.

It could absolutely be documented better, and perhaps designed better too 😬

---

<div class="post-metadata">

**Author:** ![heretogo](https://discourse.flucoma.org/letter_avatar_proxy/v4/letter/h/278dde/32.png) [@heretogo](https://discourse.flucoma.org/u/heretogo)\
**Post date:** [May 29, 2022, 10:09pm UTC](https://discourse.flucoma.org/t/supercollider-audio-query-class/1444/4 "2022-05-29T22:09:19Z")

</div>

Made [some progress](https://github.com/heretogo/audioquery/pull/2) with @tedmoore.

Here’s what it sounds like [in action](https://www.youtube.com/watch?v=6ffZmTAV3O0).

Still having some weird behavior with corpus directories which have a mix of mono and stereo files. My channelFunc looks like this:

```auto
{
  arg currentFile, channelCount, currentIndex;
  if(channelCount == 1, {^[0,0]});
  if(channelCount == 2, {^[0,1]});
});

```

I expect my `channelFunc` to work but I get an error like this:

```auto
AudioQuery: Initializing Class
loadCorpusFilesSync
bufnum: 2
numFrames: 546274
numChannels: 2
sampleRate: 48000.0

processCorpusSlices
ERROR: FluidBufOnsetSlice: Invalid source buffer

```

---

<div class="post-metadata">

**Author:** ![weefuzzy](https://discourse.flucoma.org/user_avatar/discourse.flucoma.org/weefuzzy/32/362_2.png) [@weefuzzy](https://discourse.flucoma.org/u/weefuzzy)\
**Post date:** [May 30, 2022, 7:48am UTC](https://discourse.flucoma.org/t/supercollider-audio-query-class/1444/5 "2022-05-30T07:48:47Z")

</div>

That looks more like there’s a problem with the buffer that got loaded (that server error comes from within the `FluidBufOnsetSlice` c++ code). You should be able to leave your `channelFunc` as `nil` if this is the behaviour you want, as that’s the default anyway.

Looking at your code, I’m wondering if it’s an asynchrony problem with

```nohighlight
  this.loadCorpusFilesSync(path); s.sync;
  this.processCorpusSlices; s.sync;
// etc

```

i.e `loadCorpusFiles` might not have finished before you call `processCorpusSlices`? Do your various `postln` calls definitely happen in the right order? Looks to me from above that there should have been

```auto
"all files loaded"
"num channels: 2" 

```

posted as well?

---

<div class="post-metadata">

**Author:** ![heretogo](https://discourse.flucoma.org/letter_avatar_proxy/v4/letter/h/278dde/32.png) [@heretogo](https://discourse.flucoma.org/u/heretogo)\
**Post date:** [June 1, 2022, 11:36pm UTC](https://discourse.flucoma.org/t/supercollider-audio-query-class/1444/6 "2022-06-01T23:36:24Z")

</div>

Ok, so the first thing is that you are right about the async problem. The code runs without any error if I fork like this:

```auto
FluidLoadFolder(corpus_files_folder, channelFunc: {
  arg currentFile, channelCount, currentIndex;
  fork {
    channelCount.postln;
    if(channelCount == 1, {[0,0].yield}); s.sync;
    if(channelCount == 2, {[0,1].yield}); s.sync;
  }
}); s.sync;

```

The behavior I want is to always assume a stereo sample for both playback and analysis. If there’s a stereo sample available then I want it played back. If not I want to hear here two copies of the signal. Playbuf should never complain like this:  
`Buffer UGen channel mismatch: expected 2, yet buffer has 1 channels`

I expected that `if(channelCount == 1, {[0,0].yield});` would work a bit like how you might do `sig!2` expect in this case it would read channel `0` twice into the load folder buffer.

In the recording you hear two test examples.

First, I load a mono kit and then playback the corpus in its entirely. Here I hear only the left channel where I expect to hear both channels. They I perform some real-time queries against a real-time input. Works as expected minus the mono thing.

Then I load a mixed (mono and stereo) kit and then playback the corpus in its entirely. Here I hear both channels for all sounds. They I perform some real-time queries against a real-time input. Something is wrong here. I’m not sure if it’s a problem with the analysis or the indexing.

I feel like I’m missing something obvious. Otherwise everything works and I have made tweaks to the synth to synchronize the query with an onset detector.

[SC\_220601\_192250.aiff.zip](https://discourse.flucoma.org/uploads/short-url/mxDEkvesiLUJzjWEEi6xti7QsgW.zip) (5.3 MB)

---

<div class="post-metadata">

**Author:** ![heretogo](https://discourse.flucoma.org/letter_avatar_proxy/v4/letter/h/278dde/32.png) [@heretogo](https://discourse.flucoma.org/u/heretogo)\
**Post date:** [June 1, 2022, 11:38pm UTC](https://discourse.flucoma.org/t/supercollider-audio-query-class/1444/7 "2022-06-01T23:38:13Z")

</div>

By the way, if I leave `channelFunc` as `nil` I still get the weird playback issue you hear in the recording with the bongos.

Short video example of improvements using an [onset detector](https://www.youtube.com/watch?v=0YglZyz8o8Q).
