# Randomisation of which sample is loaded in the synthesiser

**URL:** <https://forum.juce.com/t/randomisation-of-which-sample-is-loaded-in-the-synthesiser/23997>\
**Category:** General JUCE discussion\
**Created:** [August 31, 2017, 7:55pm UTC](https://forum.juce.com/t/randomisation-of-which-sample-is-loaded-in-the-synthesiser/23997 "2017-08-31T19:55:56Z")\
**Posts on this page:** 9\
**Page:** 1

<div class="post-metadata">

**Author:** ![RichieT88](https://avatars.discourse-cdn.com/v4/letter/r/34f0e0/32.png) [@RichieT88](https://forum.juce.com/u/RichieT88)\
**Post date:** [August 31, 2017, 7:55pm UTC](https://forum.juce.com/t/randomisation-of-which-sample-is-loaded-in-the-synthesiser/23997/1 "2017-08-31T19:55:56Z")

</div>

Is there any way using JUCE classes/methods that I can assign multiple samples to a key and randomise which one plays when the note is pressed? I wouldn’t mind round robins but if proper randomisation is possible I would prefer to use that.

---

<div class="post-metadata">

**Author:** ![xenakios](https://sea2.discourse-cdn.com/flex026/user_avatar/forum.juce.com/xenakios/32/4047_2.png) [@xenakios](https://forum.juce.com/u/xenakios)\
**Post date:** [August 31, 2017, 8:02pm UTC](https://forum.juce.com/t/randomisation-of-which-sample-is-loaded-in-the-synthesiser/23997/2 "2017-08-31T20:02:56Z")

</div>

You need to do your own subclass or subclasses inheriting from SamplerSound and/or SamplerVoice. (I am probably actually going to be implementing something like this soon myself…)

---

<div class="post-metadata">

**Author:** ![RichieT88](https://avatars.discourse-cdn.com/v4/letter/r/34f0e0/32.png) [@RichieT88](https://forum.juce.com/u/RichieT88)\
**Post date:** [August 31, 2017, 8:15pm UTC](https://forum.juce.com/t/randomisation-of-which-sample-is-loaded-in-the-synthesiser/23997/3 "2017-08-31T20:15:50Z")

</div>

That’s good to know, I’ll look into these things and keep in touch with you if I hit any road blocks, thanks

---

<div class="post-metadata">

**Author:** ![railjonrogut](https://sea2.discourse-cdn.com/flex026/user_avatar/forum.juce.com/railjonrogut/32/505_2.png) [@railjonrogut](https://forum.juce.com/u/railjonrogut)\
**Post date:** [August 31, 2017, 8:17pm UTC](https://forum.juce.com/t/randomisation-of-which-sample-is-loaded-in-the-synthesiser/23997/4 "2017-08-31T20:17:13Z")

</div>

> [@Xenakios](#):
>
> You need to do your own subclass or subclasses inheriting from SamplerSound and/or SamplerVoice. (I am probably actually going to be implementing something like this soon myself…)

Actually you need to derive your own class from Synthesizer and handle the selection in selectSound()… but you do also need to subclass SamplerSound and samplerVoice and have a mechanism to assign a range to each sound.

Rail

---

<div class="post-metadata">

**Author:** ![RichieT88](https://avatars.discourse-cdn.com/v4/letter/r/34f0e0/32.png) [@RichieT88](https://forum.juce.com/u/RichieT88)\
**Post date:** [August 31, 2017, 8:19pm UTC](https://forum.juce.com/t/randomisation-of-which-sample-is-loaded-in-the-synthesiser/23997/5 "2017-08-31T20:19:21Z")

</div>

Is that so? I am adding my sounds using addSound() in my own class which is derived from Synthesizer in the setup() method, I’ve not come across selectSound() yet, how or why to use it so I guess I’ll read into that. Thank you!

---

<div class="post-metadata">

**Author:** ![xenakios](https://sea2.discourse-cdn.com/flex026/user_avatar/forum.juce.com/xenakios/32/4047_2.png) [@xenakios](https://forum.juce.com/u/xenakios)\
**Post date:** [August 31, 2017, 8:25pm UTC](https://forum.juce.com/t/randomisation-of-which-sample-is-loaded-in-the-synthesiser/23997/6 "2017-08-31T20:25:53Z")

</div>

> [@Rail\_Jon\_Rogut](#):
>
> derive your own class from Synthesizer and handle the selection in selectSound()…

Hmm I am not seeing a selectSound() method in that, virtual or non-virtual…?

---

<div class="post-metadata">

**Author:** ![railjonrogut](https://sea2.discourse-cdn.com/flex026/user_avatar/forum.juce.com/railjonrogut/32/505_2.png) [@railjonrogut](https://forum.juce.com/u/railjonrogut)\
**Post date:** [August 31, 2017, 8:30pm UTC](https://forum.juce.com/t/randomisation-of-which-sample-is-loaded-in-the-synthesiser/23997/7 "2017-08-31T20:30:13Z")

</div>

Damn, it’s been years since I wrote that code… I thought that selectSound(0 was part of Synthesizer… it isn’t… basically you override Synthesizer::noteOn() and in there you call selectSound() to find the sound(s) that are in the correct range and you do your randomization, etc…

Cheers,

Rail

---

<div class="post-metadata">

**Author:** ![RichieT88](https://avatars.discourse-cdn.com/v4/letter/r/34f0e0/32.png) [@RichieT88](https://forum.juce.com/u/RichieT88)\
**Post date:** [August 31, 2017, 8:34pm UTC](https://forum.juce.com/t/randomisation-of-which-sample-is-loaded-in-the-synthesiser/23997/8 "2017-08-31T20:34:42Z")

</div>

So do I add all the sounds using addSound to the same note and then sift out which one to play in the overriden noteOn method?

Could I also use this for ascertaining velocity? Thus answering [my earlier question](https://forum.juce.com/t/velocity-when-setting-samplersound/23992)?

---

<div class="post-metadata">

**Author:** ![railjonrogut](https://sea2.discourse-cdn.com/flex026/user_avatar/forum.juce.com/railjonrogut/32/505_2.png) [@railjonrogut](https://forum.juce.com/u/railjonrogut)\
**Post date:** [August 31, 2017, 8:36pm UTC](https://forum.juce.com/t/randomisation-of-which-sample-is-loaded-in-the-synthesiser/23997/9 "2017-08-31T20:36:36Z")

</div>

in noteOn() you can get the current note, channel, velocity, etc… you look through the OwnedArray of sounds to find the sounds in the range of notes and velocity and select one of the sounds amongst those to play in selectSound(). Each sound should have a Range of notes and velocities assigned to it.

Rail
