PlaySamples with Legato API

Hi,
I’m using legato API to play a sine, that is a test to my real app. But I get a problems and need support.

If put the loop with 20ms delay, I dont listen audio from speaker, but if I put delay 5ms I listen.

Why?

My Code:


#include "legato.h"
#include "interfaces.h"

typedef struct le_audio_Connector* le_audio_ConnectorRef_t;
typedef struct le_audio_Stream* le_audio_StreamRef_t;

static le_thread_Ref_t RecPbThreadRef = NULL;
static le_audio_StreamRef_t PlayerOutRef = NULL;
static le_audio_StreamRef_t SpeakerOutRef = NULL;

static le_audio_ConnectorRef_t AudioOutputConnectorRef = NULL;

int my_pipe[2];

#define SIN_425_LEN 627
const char SIN_425[] = {127,143,158,169,176,177,172,163,150,134,117,102,89,80,77,79,87,98,113,//19
                       127,143,158,169,176,177,172,163,150,134,117,102,89,80,77,79,87,98,113,//38
					   127,143,158,169,176,177,172,163,150,134,117,102,89,80,77,79,87,98,113,//57
					   127,143,158,169,176,177,172,163,150,134,117,102,89,80,77,79,87,98,113,//76
					   127,143,158,169,176,177,172,163,150,134,117,102,89,80,77,79,87,98,113,//95
					   127,143,158,169,176,177,172,163,150,134,117,102,89,80,77,79,87,98,113,//114
					   127,143,158,169,176,177,172,163,150,134,117,102,89,80,77,79,87,98,113,//133
					   127,143,158,169,176,177,172,163,150,134,117,102,89,80,77,79,87,98,113,//152
					   127,143,158,169,176,177,172,163,150,134,117,102,89,80,77,79,87,98,113,//171
					   127,143,158,169,176,177,172,163,150,134,117,102,89,80,77,79,87,98,113,//190
					   127,143,158,169,176,177,172,163,150,134,117,102,89,80,77,79,87,98,113,//209
					   127,143,158,169,176,177,172,163,150,134,117,102,89,80,77,79,87,98,113,//228
					   127,143,158,169,176,177,172,163,150,134,117,102,89,80,77,79,87,98,113,//247
					   127,143,158,169,176,177,172,163,150,134,117,102,89,80,77,79,87,98,113,//266
					   127,143,158,169,176,177,172,163,150,134,117,102,89,80,77,79,87,98,113,//285
					   127,143,158,169,176,177,172,163,150,134,117,102,89,80,77,79,87,98,113,//304
					   127,143,158,169,176,177,172,163,150,134,117,102,89,80,77,79,87,98,113,//323
					   127,143,158,169,176,177,172,163,150,134,117,102,89,80,77,79,87,98,113,//342
					   127,143,158,169,176,177,172,163,150,134,117,102,89,80,77,79,87,98,113,//361
					   127,143,158,169,176,177,172,163,150,134,117,102,89,80,77,79,87,98,113,//380
					   127,143,158,169,176,177,172,163,150,134,117,102,89,80,77,79,87,98,113,//399
					   127,143,158,169,176,177,172,163,150,134,117,102,89,80,77,79,87,98,113,//418
					   127,143,158,169,176,177,172,163,150,134,117,102,89,80,77,79,87,98,113,//437
					   127,143,158,169,176,177,172,163,150,134,117,102,89,80,77,79,87,98,113,//456
					   127,143,158,169,176,177,172,163,150,134,117,102,89,80,77,79,87,98,113,//475
					   127,143,158,169,176,177,172,163,150,134,117,102,89,80,77,79,87,98,113,//494
					   127,143,158,169,176,177,172,163,150,134,117,102,89,80,77,79,87,98,113,//513
					   127,143,158,169,176,177,172,163,150,134,117,102,89,80,77,79,87,98,113,//532
					   127,143,158,169,176,177,172,163,150,134,117,102,89,80,77,79,87,98,113,//551
					   127,143,158,169,176,177,172,163,150,134,117,102,89,80,77,79,87,98,113,//570
					   127,143,158,169,176,177,172,163,150,134,117,102,89,80,77,79,87,98,113,//589
					   127,143,158,169,176,177,172,163,150,134,117,102,89,80,77,79,87,98,113,//608
					   127,143,158,169,176,177,172,163,150,134,117,102,89,80,77,79,87,98,113};//627


void player_main(void);


COMPONENT_INIT{
   player_main();
}

void DisconnectAllAudio(void){
      LE_INFO("%s",__FUNCTION__);
      if(AudioOutputConnectorRef){
	    if(PlayerOutRef){
		   LE_INFO("Disconnect %p from connector.%p", PlayerOutRef, AudioOutputConnectorRef);
		   le_audio_Disconnect(AudioOutputConnectorRef, PlayerOutRef);
	   }
    }

   if(AudioOutputConnectorRef){
	    le_audio_DeleteConnector(AudioOutputConnectorRef);
	    AudioOutputConnectorRef = NULL;
   }
   if(PlayerOutRef){
	   le_audio_Close(PlayerOutRef);
	   PlayerOutRef = NULL;
   }
   exit(0);
}

void OpenSpeaker(void){
    le_result_t res;
    SpeakerOutRef = le_audio_OpenSpeaker();
    AudioOutputConnectorRef = le_audio_CreateConnector();
   if(AudioOutputConnectorRef==NULL){
	    LE_ERROR("AudioOutputConnectorRef is NULL!");
	    exit(EXIT_FAILURE);
    }else{
	    res = le_audio_Connect(AudioOutputConnectorRef, SpeakerOutRef);
	    LE_ERROR_IF((res!=LE_OK), "Failed to connect ALLYSON TX on Output connector!");
	    LE_INFO("Speaker oppened successful\n");
    }
}

 static void OpenPlayer(void){
    le_audio_SetPcmCompanding(LE_AUDIO_COMPANDING_ALAW);
    PlayerOutRef = le_audio_OpenPlayer();
    if(AudioOutputConnectorRef==NULL){
	    LE_ERROR("Failed to insert player into connector\n");
    }else{
	    le_audio_Connect(AudioOutputConnectorRef, PlayerOutRef);
	    my_pipe[0]=-1;
	    my_pipe[1]=-1;
	    if (pipe2(my_pipe,O_NONBLOCK|O_DIRECT|O_CLOEXEC) == -1){
		    LE_ERROR("Failed to create the pipe");
	    }
	    if (le_audio_PlaySamples(PlayerOutRef, my_pipe[0]) != LE_OK){
		    LE_ERROR("Failed to start player\n");
	   }
    }
}

static void SigHandler(int sigNum){
    LE_INFO("Disconnect All Audio and end test");
    if (RecPbThreadRef){
	   le_thread_Cancel(RecPbThreadRef);
	   RecPbThreadRef = NULL;
   }
   DisconnectAllAudio();
   exit(EXIT_SUCCESS);
}

void aud_usr_init(void){
    signal(SIGINT, SigHandler);
    OpenSpeaker();
    OpenPlayer();
}


void player_main(void){
    aud_usr_init();
    while(1){
		write( my_pipe[1], SIN_425, 2*160*sizeof(char));
		usleep(20000);
		printf("delay\n");
	}
}

Hello guys,
I need your help, please.

Can someone help me?

Hi @Allyson,

I don’t know much about audio, but I would check the return of the write as a first step when it fails, you know, like

if ( write(…) ==-1 ) { …handle error…}

That might give you some clues

Hi,

I think you might refer to the sample code “audioPlaybackRec” on how to play audio.


app runProc audioPlaybackRec audioPlaybackRecTest --exe=audioPlaybackRecTest – PB_SAMPLES MIC /usr/share/sounds/0-to-9.wav 1 8000 16


I don’t see it adding delay.

Hi @jyijyi,

My problem is reverse situation. I imagine that audioPlaybackRec is to read audio samples and write into file.
But my issue is when I try read file and write to PCM.

Can you help-me?

Hi,

audioPlaybackRec is to read the /usr/share/sounds/0-to-9.wav and play it on the speaker.
What is the difference compared to your application?

Have you been able to solve your problem @Allyson?