Question

This code is meant to open opensl_es audio record capture a stream in mono, copy the stream and process left channel and right channel separately, then mix both channels into an output stream which is later played using opensl_es as well. the reason of the assembly code is because i found a bottle neck in the mixing function i had previously written in c which was a simple for loop to join left and right buffer into output buffer

well the problem is quite weird, when i put the logs i get in the output stream just what i want, the mixing of left and right buffer working and i see it in the logs, when i try to play the stream the application crashes, the same happens whenever i comment the logs, for some reason the app just crashes, so i'm starting to think it has something to do with the registers i am using or something in assembly code, i am new to assembly so is there something i am missing about arm assembly?

any idea why this is happening or how should i fix this problem?

here is the code: the first function is the main function which i use to capture sound call process functions. Te second "mux" is the function with the inline assembly in it.

void start_playing()
{
    OPENSL_STREAM  *pStream;
    int samps, i, j;
    short  inbuffer[VECSAMPS_MONO], outbuffer[VECSAMPS_STEREO];
    pStream = android_OpenAudioDevice(SR,1,2,BUFFERFRAMES);
    if(pStream == NULL)
    {
        return;
    }

    on   = 1;
    iLog = 0;
    while (on)
    {
        samps = android_AudioInRaw(pStream,inbuffer,VECSAMPS_MONO); //audio recording
        //signal processing process called here for left channel then for right channel (equalizing, etc)
        mux(inbuffer, inbuffer, outbuffer,VECSAMPS_MONO); //Assembly mixing of left and right channel into output channel
        //android_AudioOutRaw(pStream,outbuffer,samps*2);//audio playing
    }

    android_CloseAudioDevice(pStream);
}


//assembly function here
void mux(short *pLeftBuf, short *pRightBuf, short *pOutBuf, int vecsamps_mono)
{
    int *pIter;
    *pIter = vecsamps_mono / 4;

    __android_log_print(ANDROID_LOG_INFO, "$$$$$$$$$$$$", "value : %d , %d , %d , %d",pLeftBuf[0],pLeftBuf[1], pRightBuf[0],pRightBuf[1]);

    asm volatile(
        "ldr r9, %[outbuf];"
        "ldr r0, %[leftbuf];"
        "ldr r1, %[rightbuf];"
        "ldr r2, %[iter];"
        "ldr r8, [r2];"

        "loop: "

        "ldr r2, [r0];"
        "ldr r3, [r1];"

        "ldr r7, =0xffff;"

        "and r4, r2, r7;"
        "and r5, r3, r7;"
        "lsl r5, r5, #16;"
        "orr r4, r4, r5;"

        "lsl r7, r7, #16;"

        "and r5, r2, r7;"
        "and r6, r3, r7;"
        "lsr r6, r6, #16;"
        "orr r5, r5, r6;"

        "str r4, [r9];"
        "str r5, [r9, #4];"

        "add r0, r0, #4;"
        "add r1, r1, #4;"
        "add r9, r9, #8;"

        "subs r8, r8, #1;"
        "bne loop"
                :[outbuf] "=m" (pOutBuf)
                :[leftbuf] "m" (pLeftBuf) ,[rightbuf] "m" (pRightBuf),[iter] "m" (pIter)
                :"r0","r1","r2","r3","r4","r5","r8","r9","memory","cc"
        );
    __android_log_print(ANDROID_LOG_INFO, "##################", "value : %d , %d , %d , %d" ,*pOutBuf,*(pOutBuf+1),*(pOutBuf+2) ,*(pOutBuf+3));
}

any suggestions? this is the error i get in logcat:

01-14 11:41:40.992: A/libc(16161): Fatal signal 11 (SIGSEGV) at 0x00000000 (code=1), thread 16178 (Thread-4783)

Was it helpful?

Solution

*pIter = vecsamps_mono / 4;
...
    "ldr r2, %[iter];"
    "ldr r8, [r2];"
...
     ...[iter] "m" (pIter)

Maybe, just maybe, vecsamps_mono isn't 4 times a valid memory address.

Not that you even get that far, dereferencing an uninitialised pointer in that first line.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top