status_t LibmediaPlayback::Play(int duration_secs) {
  audio_channel_mask_t audio_mask =
      audio_channel_out_mask_from_count(num_channels_);
  const audio_stream_type_t kStreamType = AUDIO_STREAM_MUSIC;
  size_t frame_count = 0;  // Use default value for frame count.
  audio_output_flags_t audio_output_flags = AUDIO_OUTPUT_FLAG_NONE;

  AudioTrack* track = new AudioTrack(
      kStreamType, sample_rate_, audio_format_, audio_mask, frame_count,
      audio_output_flags, LibmediaPlayback::AudioCallback,
      reinterpret_cast<void*>(this));
  status_t status = track->initCheck();
  if (status != OK) {
    LOG(ERROR) << "Audio track initialization failed.";
    return status;
  }

  float volume = 0.9;
  track->setVolume(volume);
  status = track->start();
  if (status != OK) {
    LOG(ERROR) << "Audio track failed to start.";
    return status;
  }

  sleep(duration_secs);
  track->stop();

  if (in_file_)
    sf_close(in_file_);
  else
    sine_data_buffer_->release();

  return status;
}
Example #2
0
int APlaybackDevice::handlePlayback()
{
    int bufferSizeInSamples;

    bufferSizeInSamples = mBuffer->size() / aluFrameSizeFromFormat(mDevice->Format);

    mAudioTrack.start();

    while(mPlaybackEnabled) {
        aluMixData(mDevice, mBuffer->data(), bufferSizeInSamples);
        if(!write(mBuffer)) {
            LOGE("Can't write audio buffer into audio track");
            mPlaybackEnabled = false;
        }
    }

    mAudioTrack.stop();
    mAudioTrack.flush();
    return 0;
}
/**
 * \brief stop output
 * \param audec pointer to audec
 * \return 0 on success otherwise negative error code
 */
extern "C" int android_stop(struct aml_audio_dec* audec)
{
    adec_print("android out stop");

    audio_out_operations_t *out_ops = &audec->aout_ops;
    AudioTrack *track = (AudioTrack *)out_ops->private_data;

    Mutex::Autolock _l(mLock);

    if (!track) {
        adec_print("No track instance!\n");
        return -1;
    }

    track->stop();

    /* release AudioTrack */
    delete track;
    out_ops->private_data = NULL;

    return 0;
}
Example #4
0
/*
==============
SNDDMA_Shutdown

Reset the sound device for exiting
===============
*/
void SNDDMA_Shutdown(void)
{
  gAudioTrack.stop();
}
Example #5
0
int libmediacb_start(msm_ctx *ctx, int channels, int samplerate) {
	 __android_log_print(ANDROID_LOG_INFO,"liblossless","libmedia_ START REEEEACHED REACHEDDDDDD1");
   status_t status;
   int chans; 

   if(!ctx) return LIBLOSSLESS_ERR_NOCTX;

  __android_log_print(ANDROID_LOG_INFO,"liblossless","libmediacb_start ctx=%p chans=%d rate=%d afd=%d atrack=%p",
                ctx, channels, samplerate,ctx->afd,ctx->track);

   AudioTrack* atrack = (AudioTrack *) ctx->track;

   if(atrack && ctx->samplerate == samplerate && ctx->channels == channels) {
  __android_log_print(ANDROID_LOG_INFO,"liblossless","same audio track parameters, restarting");
	atrack->stop();
	atrack->flush();
	ctx->cbstart = 0; ctx->cbend = 0;
	atrack->start();
	return 0; 
   }	

   if(!ctx->cbbuf) {
	ctx->cbbuf = (unsigned char *) malloc(DEFAULT_CB_BUFSZ);
	if(!ctx->cbbuf) return LIBLOSSLESS_ERR_NOMEM;
	ctx->cbbuf_size = DEFAULT_CB_BUFSZ;
   }		

   ctx->cbstart = 0; ctx->cbend = 0;	

   if(!atrack) {
   	atrack = new AudioTrack();
	if(!atrack) {
		__android_log_print(ANDROID_LOG_ERROR,"liblossless","could not create AudioTrack!");
		return LIBLOSSLESS_ERR_INIT;
	}
	 __android_log_print(ANDROID_LOG_INFO,"liblossless","AudioTrack created at %p. Now trying to setup (buffsz %d)", 
		atrack, DEFAULT_ATRACK_CONF_BUFSZ);
	if(!sdk_version) {	
		char c[PROP_VALUE_MAX];
		if(__system_property_get("ro.build.version.sdk",c) > 0) sscanf(c,"%d",&sdk_version);
		else sdk_version = 8;
		__android_log_print(ANDROID_LOG_INFO,"liblossless","got sdk_version %d", sdk_version);		
	} 	

	if(sdk_version > 13) chans = (channels == 2) ? 3 : 1;
	else if(sdk_version > 6) chans = (channels == 2) ? 12 : 4;
	else chans = channels;

#ifdef BUILD_JB
	status = atrack->set(_MUSIC, samplerate, FMTBPS, chans, DEFAULT_ATRACK_CONF_BUFSZ/(2*channels),AUDIO_OUTPUT_FLAG_NONE,cbf,ctx);
#else
	status = atrack->set(_MUSIC, samplerate, FMTBPS, chans, DEFAULT_ATRACK_CONF_BUFSZ/(2*channels),0,cbf,ctx);
#endif
	
   	if(status != NO_ERROR) {
		__android_log_print(ANDROID_LOG_INFO,"liblossless","AudioTrack setup failed");
		delete atrack;
		return LIBLOSSLESS_ERR_INIT;  
	}
	ctx->track = atrack;
   } else {
        atrack->stop();
	atrack->flush();
        ctx->cbstart = 0; ctx->cbend = 0;
	__android_log_print(ANDROID_LOG_INFO,"liblossless","trying to reconfigure old AudioTrack");
	status = atrack->setSampleRate(samplerate);
        if(status != NO_ERROR) {
                __android_log_print(ANDROID_LOG_INFO,"liblossless","could not set AudioTrack sample rate");
                return LIBLOSSLESS_ERR_INIT;
        }
   }		

  __android_log_print(ANDROID_LOG_INFO,"liblossless","AudioTrack setup OK, starting audio!");
   ctx->conf_size = DEFAULT_CONF_BUFSZ; 	
   atrack->start();	
  __android_log_print(ANDROID_LOG_INFO,"liblossless","playback started!");

   atrack->setPositionUpdatePeriod(0);
   atrack->setMarkerPosition(0); 	

   static int s(0); if(!s) { print_priority(__FUNCTION__); s = 1; } 		

   return 0; 
}