Sound, Perlin Noise, and Processing. Looking for a demo

Hi

Simple question: has anybody developed In Processing a couple of simple examples as those at What Does Perlin Noise Sound Like? ?

If you did, I appreciate if you may share your code. Thanks a lot.

Hello @mario60 ,

Try generating some Perlin noise data and saving it to a sound file and see what it sounds like.

Saving AudioSample as a sound file of any format

I will certainly be exploring this in the future but not anytime soon…

I had some success with this!

Replace the sine wave data with Perlin noise and write that to the array.

I cleaned up the code in one of the examples to demonstrate beat frequencies (click mouse on sketch window to generate and play):

// Original here:
// https://forum.processing.org/two/discussion/4339/how-to-save-a-wav-file-using-audiosystem-and-audioinputstream-of-javasound

// Updates by glv to generate stereo with beat frequencies

// References
// https://stackoverflow.com/questions/44415863/what-is-the-byte-format-of-an-8-bit-monaural-wav-file
// https://forum.processing.org/two/discussion/4339/how-to-save-a-wav-file-using-audiosystem-and-audioinputstream-of-javasound
// https://www.szynalski.com/tone-generator/

import javax.sound.sampled.*;
import java.io.*;

import processing.sound.*;
SoundFile file;

void setup() {}

void draw() {}

void mousePressed()
  {
  byte[] pcm_data = new byte[44100*2];
  double L1 = 44100.0/240.0;
  double L2 = 44100.0/250.0;
  for (int i=0; i<pcm_data.length; i+=2) // glv modified
    {
    pcm_data[i] =   (byte)(55*Math.sin((i/L1)*Math.PI*2));
    pcm_data[i+1] = (byte)(55*Math.sin((i/L2)*Math.PI*2)); // glv modified
    }

  AudioFormat frmt = new AudioFormat(44100, 8, 2, true, true);
  AudioInputStream ais = new AudioInputStream(
    new ByteArrayInputStream(pcm_data), frmt, 
    pcm_data.length / frmt.getFrameSize()
  );

  try 
    {
    AudioSystem.write(ais, AudioFileFormat.Type.WAVE, new 
      File(sketchPath() + "/test.wav")   // glv added brackets for sketchPath
    );
    println("Done!");  
    } 
  catch(Exception e) 
    {
    e.printStackTrace();
    }
  // glv added:
  file = new SoundFile(this, "test.wav");
  file.play();
  }

Do a bit of research and fill the array by encoding Perlin noise values into WAV file data. This is achievable.

:)

1 Like

Hi @mario60!
You inspired me to try this, and since Processing comes with a Perlin Noise generator it was quite simple. I’m using the Pd4P3 sound library mainly because it allows you write directly to the audio loop in realtime. It sounds different than I imagined, but then we rarely use Perlin noise in audio. But it was an interesting idea, and I may use this more in the future.
The example code is below and also lives here:

import com.pdplusplus.*;

/*
A simple implementation of Perlin Noise
The x-axis will set the noise scale which will
change the spectrum of the noise.  
*/

 Pd pd;
 MyMusic music;
 int counter = 0;
 float[] output;
 
 void setup() {
   size(640, 360);
   background(255);
   
   music = new MyMusic();
   pd = Pd.getInstance(music);
   
   //start the Pd engine thread
   pd.start();
   
 }
 
 void draw() {
  background(0);
  stroke(255);
  strokeWeight(2);
  noFill();
  //set our scale from .005 = .09, change if you like
  float s = map(mouseX, 0, width, .005, .09);
  music.setScale(s);
  output = music.getOutput();
 
 //Draw the shape based on the output block, once per frame
  beginShape();
  for(int i = 0; i < output.length; i++){
    vertex(
      map(i, 0, output.length, width, 0),
      map(output[i], -1, 1, 0, height)
    );
  }
  endShape(); 
 
 }
 
 public void dispose() {
   //stop Pd engine
   pd.stop();
   println("Pd4P3 audio engine stopped.");
   super.dispose();
}
 
 /*
   This is our audio class that writes noise() directly to the audio loop
 */
 class MyMusic extends PdAlgorithm {
   
   float x = 0;
   int counter = 0;
   int block = 1024; //change this to bigger or small to get better graphing
   float[] writeOutput = new float[block];
   float scale = .01;
   
   //Our audio loop.  All DSP code goes here
   void runAlgorithm(double in1, double in2) {
     x = x + getScale();
     float n = noise(x);
     outputL = outputR = n - .5; //drop down 50% to avoid DC
     
     //our ring buffer
     writeOutput[counter++] = (float)outputL;
     //writes a block to our graphics loops
     if(counter == block)
     {
       setOutput(writeOutput);
       counter = 0;
     }
     
   }
   
   synchronized void setOutput(float[] o) {
      writeOutput = o; 
   }
   
   synchronized float[] getOutput() {
     
    return writeOutput; 
   }
   
   synchronized void setScale(float s) {
      scale = s; 
   }
   
   synchronized float getScale() {
      return scale; 
   }
   
   //Free all objects created from Pd4P3 lib
   void free() {
     
     //nothing to free
   }
   
 }
1 Like

Thanks
When I try to run your DummyTemplate example, I get the following error:

UnsatisfiedLinkError: no pdplusplus in java.library.path: :/home/.../processing-4.3.1-linux-x64/processing-4.3.1/core/library/linux-amd64:/home/.../sketchbook/libraries/Pd4P3/library:/usr/java/packages/lib:/usr/lib64:/lib64:/lib:/usr/lib
A library used by this sketch relies on native code that is not available.

I am not so familiar with Java to be able to fix it. Is there a way?

>> java --version
openjdk 21.0.6 2025-01-21
OpenJDK Runtime Environment (Red_Hat-21.0.6.0.7-1) (build 21.0.6+7)
OpenJDK 64-Bit Server VM (Red_Hat-21.0.6.0.7-1) (build 21.0.6+7, mixed mode, sharing)