import java.util.*;
import java.awt.*;
import java.io.*;
public class Project4
{
public static void main (String[] args)
{
// MAIN CODE
String noteRepresentation = "78!9@a#bc$d%ef^g&A*BC(D)EF_G+H";
String noteString = SimpleInput.getString("String of notes");
// init strLen etc
int strLen = noteString.length();
int samples = (int)((.125) * (22050)); // 1/8 Times the sample per sec
int finalLen = strLen * samples;
// Create a silence array
double silenceArray[] = new double[(int)samples];
// = new double[(double)samples]; // Be able to create a new array for the silence (periods).
// Create a main sound sample
SoundSample[] ssarrFS = new SoundSample[(int)finalLen];
String noteLetter;
for ( int noteIndex = 0; noteIndex < noteString.length(); noteIndex++)
{
double freq = -1;
double amplitude = -1;
noteLetter = noteString.substring(noteIndex, noteIndex + 1);
getFreq(noteLetter);
getAmp(freq, noteLetter);
SoundSample[] tempArray = createSineWave(freq, amplitude);
if (noteRepresentation.indexOf(noteLetter) == -1)
{
//Silence
for(int index1 = 0;index1<samples;index1++)
{
int value1 = tempArray[index1].getValue();
ssarrFS[(int)((index1+(samples*(noteIndex))))].setValue(value1);
}
}
else
{
freq = -1;
amplitude = -1;
for(int index2 = 0;index2<samples;index2++)
{
getFreq(noteLetter);
getAmp(freq, noteLetter);
int value2 = tempArray[index2].getValue();
ssarrFS[(int)(index2+(samples*(noteIndex)))].setValue(value2);
}
}
}
Sound sFinal = modifySound(finalLen, ssarrFS);
sFinal.explore();
}
public static Sound modifySound(int finalLen, SoundSample[] ssarrFS)
{
Sound sFinal = new Sound( finalLen+1);
SoundSample[] ssarr3 = sFinal.getSamples();
int ind;
for (ind = 0 ; ind < finalLen ; ++ind )
{
int valueFinal = ssarrFS[ind].getValue();
// check for clipping
if ( valueFinal > 32767 )
{
valueFinal = 32767;
}
if ( valueFinal < -32768 )
{
valueFinal = -32768;
}
ssarr3[ind].setValue ( valueFinal );
}
return sFinal;
}
public static double getFreq (String noteLetter)
{
String noteRepresentation = "78!9@a#bc$d%ef^g&A*BC(D)EF_G+H";
double x = noteRepresentation.indexOf(noteLetter); // x becomes 15
double y = noteRepresentation.indexOf('H'); // y becomes 29
double exponent = (double)((x - y)/12.0);
double freq = (double)(440.0 * Math.pow (2.0, exponent)); // fr
System.out.println(freq);
return freq;
}
public static SoundSample[] createSineWave (double freq, double amplitude)
{
Sound s = new Sound ((int)((.125) * (22050)));
int samplingRate = (int)(s.getSamplingRate());
int rawValue = 0;
int value = 0;
int interval = (int)(1.0 / freq);
int samplesPerCycle = (int)(interval * samplingRate);
int maxValue =(int)( 2 * Math.PI);
SoundSample[] tempArray = s.getSamples();
int index;
for (int i = 0 ; i < s.getLength(); ++i )
{
rawValue = (int)(Math.sin ((i / samplesPerCycle) * maxValue));
value = (int) (amplitude * rawValue);
tempArray[i].setValue(value);
}
//s = null;
//System.gc();
return tempArray;
}
public static double getAmp (double freq, String noteLetter)
{
double samplesPerCycle = 22050 / freq;
double sampleIndex = 22050 / 8;
double wavePoint = sampleIndex / samplesPerCycle;
double rawSample = Math.sin (wavePoint * 2.0 * Math.PI);
double amplitude = rawSample * 20000;
System.out.println(amplitude);
return amplitude;
}
}
I am trying to combine notes that a user will input and be able to take their frequency and amplitudes and be able to create a .wav file that will output the sound. When I try to compile the current code, I keep receiving a nullpointerexception error at line
ssarrFS[(int)(index2+(samples*(noteIndex)))].setValue(value2);
I believe that I have incorrectly set up my arrays and may have caused memory leaks in my program.
You declare your array here
SoundSample[] ssarrFS = new SoundSample[(int)finalLen];
But then you never populate it's values, and you try to use it here:
ssarrFS[(int)((index1+(samples*(noteIndex))))].setValue(value1);
When you try to use it though, your array looks like this:
ssarrFS = [0] = null
[1] = null
[2] = null
...
[finalLen] = null
You attempt to call setValue
on an element inside of it. Because you've not populated ssarrFS
, this amounts to:
null.setValue(value1);
And because null
does not refer to an object in memory, this will throw your NullPointerException
.