Search a dict

realshafer's icon

Hi, I have probably a pretty amateurish question about [dict] and JS.

I'm testing using [dict] to store information about samples in buffers. The structure of the [dict] as I imagine it looks something like:


{
    "sound1" :     {
        "descriptors" :         {
            "pitchclass" : [ 0, 1, 3 ],
            "noisiness" : 0.3,
            "loudness" : 0.8
        }
,
        "playback" :         {
            "buffer name" : "vla_trem2s",
            "sample start" : 66551.929688000003807,
            "sample end" : 71836.484375,
            "main out level" : 0.01,
            "aux send level" : 1.2714,
            "pan setting" : "all"
        }
    }
,
    "sound2" :     {
        "descriptors" :         {
            "pitchclass" : [ 0, 4, 8 ],
            "noisiness" : 0.9,
            "loudness" : 0.1
        }
,
        "playback" :         {
            "buffer name" : "vla_temp",
            "sample start" : 1000,
            "sample end" : 9999,
            "main out level" : 0.5,
            "aux send level" : 0.1,
            "pan setting" : "left"
        }
    }
}

(sorry if the formatting is wacky, I can repost as a maxpat if that's a problem)

Now, let's say I want to find all sounds that have pitch class 0. What I want returned is "sound1" and "sound2." Or, just sounds with pitch class 4, which returns only "sound2."

Follow up feature: Rank all sounds by loudness, low to high, which I want returned as "sound2" followed by "sound1."

I've sort of built a primitive version of this using [coll] but I'm reaching the limits of organization and usability. In my [coll] version, I'm iterating through my data at each request, keeping track of the data index. I had a hunch that [dict] in combination with some JS might be a more descriptive, expandable, and readable route to try. However, my JS and [dict] skills are coming up short.

Any help is greatly appreciated!

broc's icon

Using 'getkeys' you can iterate through the dict (keeping track of the key), and at each step use 'get' to check the data.

Here is a patch in plain Max (which I generally prefer over JS).

Max Patch
Copy patch and select New From Clipboard in Max.


realshafer's icon

BROC! Amazing. Thanks for making a working patch to do that. Following your lead, I was able to complete my follow up sorting feature. I've posted in case this helps someone else.

Dict Search and Sort.maxpat
Max Patch