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;
}
}
我正在尝试组合用户将输入的音符,并能够获取其频率和幅度,并能够创建将输出声音的.wav文件。当我尝试编译当前代码时,我一直在行
处收到nullpointerexception错误 ssarrFS[(int)(index2+(samples*(noteIndex)))].setValue(value2);
我认为我错误地设置了我的数组并且可能导致我的程序中出现内存泄漏。
答案 0 :(得分:3)
你在这里声明你的数组
SoundSample[] ssarrFS = new SoundSample[(int)finalLen];
但是你从来没有填充它的价值观,你试着在这里使用它:
ssarrFS[(int)((index1+(samples*(noteIndex))))].setValue(value1);
当你尝试使用它时,你的数组看起来像这样:
ssarrFS = [0] = null
[1] = null
[2] = null
...
[finalLen] = null
您尝试在其中的元素上调用setValue
。由于您未填充ssarrFS
,因此相当于:
null.setValue(value1);
由于null
没有引用内存中的对象,因此会抛出NullPointerException
。