Skip to main content
edited tags; edited title; edited tags
Link
200_success
  • 144.2k
  • 22
  • 188
  • 473

Optimizing Generating a sinusoid sound generationsignal in Java

Bumped by Community user
deleted 38 characters in body
Source Link
Jamal
  • 34.9k
  • 13
  • 133
  • 237

I have the following code snippet:Is there any legroom left for optimization (without switching to C)?

static byte[] generateSound(double frequency, int sampleRate, int samples) {
    byte output[] = new byte[2 * samples];
    
    int idx = 0;
    
    double _tone = 2 * Math.PI / (sampleRate / frequency);
    
    for (int i = 0; i < samples; ++i) {
        double dVal = Math.sin(_tone * i);

        final short val = (short) ((dVal * 32767));

        output[idx++] = (byte) (val & 0x00ff);
        output[idx++] = (byte) ((val & 0xff00) >>> 8);
    }

    return output;
}

Is there any legroom left for optimization (without switching to C)?

I have the following code snippet:

static byte[] generateSound(double frequency, int sampleRate, int samples) {
    byte output[] = new byte[2 * samples];
    
    int idx = 0;
    
    double _tone = 2 * Math.PI / (sampleRate / frequency);
    
    for (int i = 0; i < samples; ++i) {
        double dVal = Math.sin(_tone * i);

        final short val = (short) ((dVal * 32767));

        output[idx++] = (byte) (val & 0x00ff);
        output[idx++] = (byte) ((val & 0xff00) >>> 8);
    }

    return output;
}

Is there any legroom left for optimization (without switching to C)?

Is there any legroom left for optimization (without switching to C)?

static byte[] generateSound(double frequency, int sampleRate, int samples) {
    byte output[] = new byte[2 * samples];
    
    int idx = 0;
    
    double _tone = 2 * Math.PI / (sampleRate / frequency);
    
    for (int i = 0; i < samples; ++i) {
        double dVal = Math.sin(_tone * i);

        final short val = (short) ((dVal * 32767));

        output[idx++] = (byte) (val & 0x00ff);
        output[idx++] = (byte) ((val & 0xff00) >>> 8);
    }

    return output;
}
Source Link

Optimizing sound generation in Java

I have the following code snippet:

static byte[] generateSound(double frequency, int sampleRate, int samples) {
    byte output[] = new byte[2 * samples];
    
    int idx = 0;
    
    double _tone = 2 * Math.PI / (sampleRate / frequency);
    
    for (int i = 0; i < samples; ++i) {
        double dVal = Math.sin(_tone * i);

        final short val = (short) ((dVal * 32767));

        output[idx++] = (byte) (val & 0x00ff);
        output[idx++] = (byte) ((val & 0xff00) >>> 8);
    }

    return output;
}

Is there any legroom left for optimization (without switching to C)?