NullPointerException java声音文件 import java.util.*; 导入java.awt.*; 导入java.io.*; 公共类项目4 { 公共静态void main(字符串[]args) { //主代码 String noteRepresentation=“78!9@a#bc$d%ef^g&A*bc(d)ef_g+H”; String noteString=SimpleInput.getString(“注释字符串”); //init strLen等 int strLen=noteString.length(); int samples=(int)(.125)*(22050));//每秒采样数的1/8倍 int finalLen=strLen*样本; //创建静默数组 双消声耳环[]=新的双[(int)样本]; //=new double[(double)samples];//能够为静默(句点)创建新数组。 //创建一个主声音样本 SoundSample[]ssarrFS=新的SoundSample[(int)finalLen]; 字串字母; 对于(int noteIndex=0;noteIndex
,在这里声明数组NullPointerException java声音文件 import java.util.*; 导入java.awt.*; 导入java.io.*; 公共类项目4 { 公共静态void main(字符串[]args) { //主代码 String noteRepresentation=“78!9@a#bc$d%ef^g&A*bc(d)ef_g+H”; String noteString=SimpleInput.getString(“注释字符串”); //init strLen等 int strLen=noteString.length(); int samples=(int)(.125)*(22050));//每秒采样数的1/8倍 int finalLen=strLen*样本; //创建静默数组 双消声耳环[]=新的双[(int)样本]; //=new double[(double)samples];//能够为静默(句点)创建新数组。 //创建一个主声音样本 SoundSample[]ssarrFS=新的SoundSample[(int)finalLen]; 字串字母; 对于(int noteIndex=0;noteIndex,java,audio,drjava,Java,Audio,Drjava,,在这里声明数组 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 = Simp
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;
}
}
但是,您从不填充它的值,您尝试在这里使用它:
SoundSample[] ssarrFS = new SoundSample[(int)finalLen];
当您尝试使用它时,您的数组如下所示:
ssarrFS[(int)((index1+(samples*(noteIndex))))].setValue(value1);
您试图对其中的一个元素调用setValue
。因为您没有填充ssarrFS
,这相当于:
ssarrFS = [0] = null
[1] = null
[2] = null
...
[finalLen] = null
因为
null
没有引用内存中的对象,这将抛出NullPointerException
您在这里声明数组
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;
}
}
但是,您从不填充它的值,您尝试在这里使用它:
SoundSample[] ssarrFS = new SoundSample[(int)finalLen];
当您尝试使用它时,您的数组如下所示:
ssarrFS[(int)((index1+(samples*(noteIndex))))].setValue(value1);
您试图对其中的一个元素调用setValue
。因为您没有填充ssarrFS
,这相当于:
ssarrFS = [0] = null
[1] = null
[2] = null
...
[finalLen] = null
因为
null
没有引用内存中的对象,这将抛出NullPointerException
您在这里声明数组
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;
}
}
但是,您从不填充它的值,您尝试在这里使用它:
SoundSample[] ssarrFS = new SoundSample[(int)finalLen];
当您尝试使用它时,您的数组如下所示:
ssarrFS[(int)((index1+(samples*(noteIndex))))].setValue(value1);
您试图对其中的一个元素调用setValue
。因为您没有填充ssarrFS
,这相当于:
ssarrFS = [0] = null
[1] = null
[2] = null
...
[finalLen] = null
因为
null
没有引用内存中的对象,这将抛出NullPointerException
您在这里声明数组
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;
}
}
但是,您从不填充它的值,您尝试在这里使用它:
SoundSample[] ssarrFS = new SoundSample[(int)finalLen];
当您尝试使用它时,您的数组如下所示:
ssarrFS[(int)((index1+(samples*(noteIndex))))].setValue(value1);
您试图对其中的一个元素调用setValue
。因为您没有填充ssarrFS
,这相当于:
ssarrFS = [0] = null
[1] = null
[2] = null
...
[finalLen] = null
由于
null
不引用内存中的对象,这将引发NullPointerException
请向我们显示堆栈跟踪。编译代码时会出现异常?还是说运行代码时会出现异常?在哪一行会出现错误?在调试器中逐步执行此操作时,哪个值是null
?您试图在哪里设置该值?在调试模式下运行程序,这样您可以在我运行代码时看到哪个值为null,我收到错误。它编译得很好。请向我们显示堆栈跟踪。您在编译代码时出现异常?还是说在运行代码时?在哪一行出现错误?何时u在调试器中逐步执行此操作,哪个值是null
?您尝试在哪里设置该值?在调试模式下运行您的程序,这样您可以在我运行代码时看到哪个值为null,我收到错误。它编译得很好。请向我们显示堆栈跟踪。编译代码时您会遇到异常?或者您的意思是当您n代码?在哪一行得到错误?当你在调试器中单步执行此操作时,哪个值是null
?你在哪里尝试设置该值?在调试模式下运行你的程序,这样你可以在我运行代码时看到哪个值是null,我收到错误。它编译良好。请向我们显示堆栈跟踪。你得到异常n编译代码时?还是指运行代码时?在哪一行出现错误?当您在调试器中单步执行此操作时,哪个值是null
?您尝试在何处设置该值?在调试模式下运行程序,这样您可以在运行收到错误的代码时看到哪个值是null。它编译很好。当有数千个声音样本值时,我该如何填充声音样本值呢?我真的不知道;我对您的程序一无所知。但这就是您错误的根源,也是您问题的答案!如果您对代码有其他疑问,请提出另一个问题:)如果这回答了您的问题,您能将其标记为正确吗?当将有数千个声音样本值时,我将如何填充声音样本值?我真的不知道;我对您的程序一无所知。但这是您错误的根源,也是您问题的答案!如果您对代码有其他疑问,请提交另一个问题on:)如果这回答了您的问题,您能将其标记为正确吗?当将有数千个声音样本值时,我将如何填充声音样本值?我真的不知道;我对您的程序一无所知。但这是您错误的根源,也是您问题的答案!如果您对代码有其他疑问,请提交另一个q问题:)如果这回答了您的问题,您能将其标记为正确吗?当将有数千个声音样本值时,我将如何填充声音样本值?我真的不知道;我对您的程序一无所知。但这是您错误的根源,也是您问题的答案!如果您对