0

This is the first time I am on this website. I have been trying this project but trying various methods and mixing different methods for this have not helped and some or the other error just keeps crashing the app.

My code requirements are specific, I need to pass two arrays of 0's and 1's to create a square signal on the headphone jack left and right channels at the same frequency and I want the array to be played just once and for it to play on headphone jack (seperate array for left and right) even if the device(android phone) doesn't detect headphones connected. The array uploaded and the frequency selection are to be changable through a function.

I know I am asking for a lot here but I have tried for over a month now and I am just blocked. I would do the work even if I got specific guidance for each of the above functions.

Thankyou.

1 Answers1

0

I would recommend you to use AudioTrack for playing audio in PCM 16bit and in stereo (check also the WAV specification), the signal is ranging from -1 to +1. You will also need a thread which will loop and play your arrays.

And for the frequency f, you have the trivial formula of f = 1/T where T is the period, taking in account the samplerate.

void setFreq(float f)
{
  x = 1.0f/(samplerate/f)
}

Below code will be continuously run inside a loop to generate a square signal at a specific frequency which will be eaten by AudioTrack through an array buffer (byte[] or short[]):

output = phase  < 0.5f ? 1.0f : -1.0f;
phase = phase + x;
if (phase > 1.0f)
   phase = 0.0f;
Aladin Q
  • 550
  • 5
  • 12