Pitching a note

Hi guys,

For a school project i want to make a laser instrument were as you cut the laser beam, it will play a certain note. Now I know this is possible with some libraries but my question is: Can you change the pitch of the note in processing when playing a certain note.
I want to for example play a C and if you move your hand up over a distance sensor then the pitch will rise to a maximum of a C#. same for other notes. B will be a C, D - D# etc.
Do you guys know if this is possible?

Cheers,

Max

Maybe you should look at http://www.abstract-codex.net/tactu5/ which is a music library for Processing.

Pitching the notes : http://www.abstract-codex.net/tactu5/class_tactu5notes.html

2 Likes

Thanks for the quick reply, I’m going to look into it!

Assuming you have the code for the distance sensor already, then what you need to do is to map the range of your detected distances to a range of defined frequencies. For instance, if your sensor detects from 40cm to 200cm, you will map these distances from a minimum frequency to a top frequency. This can be easily done using the map() function. For the sound, very limited experience but I suggest you explore the minim library. You could start by looking at the provided examples (In the PDE, go to Files>>Examples and then go to Contributed Libraries>>Minim folder. You should be able to find an example that plays tones based on defined frequencies.

as a simple exercise, and for code development and debugging, you can drop the distance part and map your frequency based on mouse position in the sketch. If the mouse is on the left side of the sketch it would play a low freq tone. As you move the mouse to the right, you get a higher note. This is just a suggestion.

Kf

2 Likes

Take a look at :

/* frequencyExample<br/>
   is an example of using the Frequency class to easily turn keyboard input 
   into the frequency of an Oscil. Simply type on the home row to change 
   the pitch of the tone. 
   <p>
   For more information about Minim and additional features, 
   visit http://code.compartmental.net/minim/
*/

// import everything necessary to make sound.
import ddf.minim.*;
import ddf.minim.ugens.*;

// create all of the variables that will need to be accessed in
// more than one methods (setup(), draw(), stop()).
Minim minim;
AudioOutput out;

Oscil wave;
// keep track of the current Frequency so we can display it
Frequency  currentFreq;

// setup is run once at the beginning
void setup()
{
  // initialize the drawing window
  size(512, 200);
  
  // initialize the minim and out objects
  minim = new Minim(this);
  out   = minim.getLineOut();

  currentFreq = Frequency.ofPitch( "A4" );
  wave = new Oscil( currentFreq, 0.6f, Waves.TRIANGLE );
  
  wave.patch( out );
}

// draw is run many times
void draw()
{
  // erase the window to brown
  background( 64, 32, 0 );
  // draw using a beige stroke
  stroke( 255, 238, 192 );
  
  text( "Current Frequency in Hertz: " + currentFreq.asHz(), 5, 15 );
  text( "Current Frequency as MIDI note: " + currentFreq.asMidiNote(), 5, 30 );
  
  // draw the waveforms
  for( int i = 0; i < out.bufferSize() - 1; i++ )
  {
    // find the x position of each buffer value
    float x1  =  map( i, 0, out.bufferSize(), 0, width );
    float x2  =  map( i+1, 0, out.bufferSize(), 0, width );
    // draw a line from one buffer position to the next for both channels
    line( x1, 50 + out.left.get(i)*50, x2, 50 + out.left.get(i+1)*50);
    line( x1, 150 + out.right.get(i)*50, x2, 150 + out.right.get(i+1)*50);
  }  
}

// change the midi note when pressing keys on the keyboard
// we set midiNoteIn directly with the setMidiNoteIn method
// but you could also use a Line to lerp to the next note
// by patching it to midiNoteIn.
void keyPressed()
{
  if ( key == 'a' ) currentFreq = Frequency.ofPitch( "A4" );
  if ( key == 's' ) currentFreq = Frequency.ofPitch( "B4" );
  if ( key == 'd' ) currentFreq = Frequency.ofPitch( "C#5" );
  if ( key == 'f' ) currentFreq = Frequency.ofPitch( "D5" );
  if ( key == 'g' ) currentFreq = Frequency.ofPitch( "E5" );
  if ( key == 'h' ) currentFreq = Frequency.ofPitch( "F#5" );
  if ( key == 'j' ) currentFreq = Frequency.ofPitch( "G#5" );
  if ( key == 'k' ) currentFreq = Frequency.ofPitch( "A5" );
  if ( key == 'l' ) currentFreq = Frequency.ofPitch( "B5" );
  if ( key == ';' ) currentFreq = Frequency.ofPitch( "C#6" );
  if ( key == '\'') currentFreq = Frequency.ofPitch( "E6" );
  
  // note that there are two other static methods for constructing Frequency objects
  // currentFreq = Frequency.ofHertz( 440 );
  // currentFreq = Frequency.ofMidiNote( 69 ); 
  
  wave.setFrequency( currentFreq );
}
2 Likes

Thanks for the tips guys, I think these solutions can help me out! If I have more questions then I will post them but you guys really helped me out with it!

Max

1 Like

Pls keep us informed on how your project goes! Building a laser harp has been on my to-do list for way too long now…

I will, my deadline is at the begin of July so I will post how it went :slight_smile:

1 Like