0

I am a high school student currently working on an arduidroid project using potentiometers, bluetooth, and a guitar app. My issue is that when I play my guitar app that I get an error that won't crashes.

AudioFlinger could not create track, status: -12
Error -12 initializing AudioTrack
Error code -20 when initializing AudioTrack.

How do I make it so that I can play as many sounds as I want with 3 different buttons using a tone generator from this question.

    void genTone(double freq){
    // fill out the array
    for (int i = 0; i < numSamples; ++i) {
        sample[i]= 2*(i%(sampleRate/freq))/(sampleRate/freq)-1;
    }

    // convert to 16 bit pcm sound array
    // assumes the sample buffer is normalised.
    int idx = 0;
    for (final double dVal : sample) {
        // scale to maximum amplitude
        final short val = (short) ((dVal * 32767));
        // in 16 bit wav PCM, first byte is the low order byte
        generatedSnd[idx++] = (byte) (val & 0x00ff);
        generatedSnd[idx++] = (byte) ((val & 0xff00) >>> 8);

    }
}

void playSound(){
    final AudioTrack audioTrack = new AudioTrack(AudioManager.STREAM_MUSIC,
            sampleRate, AudioFormat.CHANNEL_OUT_STEREO,
            AudioFormat.ENCODING_PCM_16BIT, generatedSnd.length,
            AudioTrack.MODE_STATIC);
    audioTrack.write(generatedSnd, 0, generatedSnd.length);
    audioTrack.play();

}

if(BString.isPressed()){
        /*genTone(BStringFreq);
        playSound();*/

        final Thread thread = new Thread(new Runnable() {
            public void run() {
                genTone(BStringFreq);
                handler.post(new Runnable() {

                    public void run() {
                        playSound();
                    }
                });
            }
        });
        thread.start();
    }

}

Here are some snippets of my code. Please help me out here as I am lost on this subject.

Community
  • 1
  • 1
Art
  • 11
  • 1
  • Lets say you want to play two different sounds - 440 and 880 Hz (A4, A5). You'd have to generate both tones and put it into the same buffer you put into `AudioTrack`. This means that the `genTone` function will have this pseudocode in its for loop: `sample[i] = generate440HzSample() + generate880HzSample()`. You might have to level out the amplitudes so it doesn't clip but that's the basic explanation of what you have to do. EDIT: as for generating 32 sounds/what your actual project needs, you'll probably need to post more / I don't know enough `AudioTrack` to help ya – yun Feb 29 '16 at 20:50
  • @yun.cloud ah thank you so much for that. But what if I'm receiving a frequency value from an arduino potentiometer. Let's say that potentiometer is my A string (analog value of 0 means 440Hz) and I want to move up a fret, how would I send the proper frequency for the corresponding fret? – Art Mar 01 '16 at 01:10
  • You have to map it in your java code - Lets say you have 127 values (0-127). 0 is 440 Hz and 127 is 880 Hz. You have to design it where you receive an analog input and then change that into a usable frequency. – yun Mar 01 '16 at 01:49
  • @yun.cloud Ah I see, so I have D C G as my notes and when I move up a step I add the samples from the next note to my base frequency sample? – Art Mar 04 '16 at 20:06
  • Hey Artiom, you want to play them together? Then yes, you just add each sample of a given frequency on top of each other. `outputSample = dSample + cSample + gSample;` – yun Mar 04 '16 at 20:12
  • Well what I meant was similar to a guitar. Using a potentiometer would give me an analog value of x. And the potentiometer that I am pressing on is the D "string" which gives me the frequency of D if the value of the potentiometer is 0. But let's say I move up the potentiometer and get a value of y, which would somehow give me a different frequency higher up the D string. How would I implement that, have a list of values and ranges for each note or something else? – Art Mar 07 '16 at 00:57
  • PS. I managed to get each string playing without getting error code -12 :) – Art Mar 07 '16 at 00:58
  • Are you saying like a pitch bend? And you got it by following my suggestions ? :) – yun Mar 07 '16 at 01:25

0 Answers0