java - NullPointerException java 声音文件

标签 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 = 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);

我相信我错误地设置了数组,并可能导致程序中出现内存泄漏。

最佳答案

你在这里声明你的数组

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

关于java - NullPointerException java 声音文件,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/20398509/

相关文章:

Java字符串字符分析

java - 使用 DrJava 通过管道(从文本文件重定向输入)到 Java 程序

java - 当 App Engine 中已经存在 3000 万个实体时,如何准确计算一组实体的数量?

java - 编写数组将csv导入java

audio - 应用程序的声音片段?

android - Android:浏览音频播放列表并从应用程序中打开M3U文件

java - 让 Eclipse 自动静态导入类

JavaFX tabpane 选项卡不切换

c# - 在 OSX 上使用 MONO 播放声音的最简单方法

java - 用JAVA写一个卖通票的方法,另一个卖票的方法