playSound() parameters: volume and pitch

Discussion in 'Resources' started by Pence128, Jun 5, 2013.

Thread Status:
Not open for further replies.
  1. Offline


    I couldn't find exact information on playSound() so I had a look:

    The volume of a sound source is determined by the volume parameter limited to the range 0.0 to 1.0. The volume of the sound as heard by the player is the volume of the sound multiplied by 1 minus the distance between the player and the source divided by the rolloff distance, multiplied by the player's sound volume setting. The rolloff distance is the greater of 16 and 16 times the volume. In code:

    sourceVolume = max(0.0, min(volume, 1.0));
    rolloffDistance = max(16, 16 * volume);
    distance = player.getLocation().distance(location);
    volumeOfSoundAtPlayer = sourceVolume * ( 1 - distance / rolloffDistance ) * PlayersSoundVolumeSetting;
    This means that 1.0 is the loudest a sound can possibly be. Setting it higher increases the distance from which the sound can be heard. For example, sounds with volumes of 1.0 and 10.0 are just as loud at their sources, but the one with a volume of 1.0 can barely be heard 15 blocks away, while the other can still be heard 150 blocks away.

    The pitch parameter is less complicated, it just controls how fast the sample is played. The range is 0.5 - 2.0. 1.0 is normal speed, 0.5 is half speed, 2.0 is double speed.

    To play a note exactly the way a note block would without the note block use

    world.playSound(location, sound, 3.0F, (float)pow(2.0, ((double)pitch - 12.0) / 12.0));
    where location is origin of the sound, sound is NOTE_PIANO, NOTE_BASS_DRUM, NOTE_SNARE_DRUM, NOTE_STICKS or NOTE_BASS_GUITAR and pitch is the number of times the imagined note block has been right clicked. If you have a real note block you can use it's getRawNote(). NOTE_BASS and NOTE_PLING are additional notes that can't be played from note blocks.
    MightyOne, Edgxxar, stirante and 4 others like this.
  2. Just wanted to say if your not using that formular
    world.playSound(location, sound, 3.0F, 0.5F);
    is the first note is 0.5F (right click) and every note is 0.033F(<< Still buggy , eventually goes off so probs not the right modifier) added onto it.

    So the second note is

    world.playSound(location, sound, 3.0F, 0.533F);
  3. Offline


    From looking at the formula, I'm gonna guess that you can't use simple addition as the formula involves both division and powers.
  4. Very interesting. I was looking for an explanation like this.
Thread Status:
Not open for further replies.

Share This Page