2
\$\begingroup\$

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;
}
200_success
145k22 gold badges190 silver badges478 bronze badges
asked Dec 26, 2016 at 21:05
\$\endgroup\$

3 Answers 3

1
\$\begingroup\$

There's probably good room for performance gain regarding the Math.sin call itself. You could easily create a lookup-table - there's no faster way than just taking the values straight from an array. (The link's benchmark measures a factor ~6 speed difference.)

It may be problematic because it may cause audible distortions, but that would depend on how much memory you're willing to invest in the lookup-table. Also, you could invest a few more cycles by obtaining both array entries addressed by (_tone*i) and then adding them up, both multiplied by 0.x and respectively (1-0.x), effectively fading over between values.

answered Feb 6, 2017 at 10:39
\$\endgroup\$
0
\$\begingroup\$

I don't know how much it will optimize it but this:

double dVal = Math.sin(_tone * i);

Shouldn't be needed.

Replace:

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

With:

final short val = (short) ((Math.sin(_tone * i) * 32767));

The compiler may be able to optimize it well enough that this doesn't matter, but it may help.

answered Dec 27, 2016 at 6:11
\$\endgroup\$
0
\$\begingroup\$

A few optimisation:

  • Using bitwise operators

    byte output[] = new byte[samples << 1];
    double _tone = (Math.PI << 1) / (sampleRate / frequency);
    final short val = (short) ((dVal<<15) - dVal);
    
  • Reducing variables

    short val = (short) ((Math.sin(_tone * i) * 32767));
    
  • Why final? Not sure if adds to the overhead.

answered Jan 27, 2017 at 6:57
\$\endgroup\$

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

By clicking "Post Your Answer", you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.