/**
 * \brief start output
 * \param audec pointer to audec
 * \return 0 on success otherwise negative error code
 *
 * Call android_start(), then the callback will start being called.
 */
extern "C" int android_start(struct aml_audio_dec* audec)
{
    adec_print("android out start");

    status_t status;
    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;
    }

    status = track->initCheck();
    if (status != NO_ERROR) {
        delete track;
        out_ops->private_data = NULL;
        return -1;
    }

    track->start();
    adec_print("AudioTrack initCheck OK and started.");

    return 0;
}
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 #3
0
int libmedia_start(msm_ctx *ctx, int channels, int samplerate) {

   if(!ctx) return LIBLOSSLESS_ERR_NOCTX;
  __android_log_print(ANDROID_LOG_INFO,"liblossless","libmedia_start chans=%d rate=%d afd=%d atrack=%p",
                channels, samplerate,ctx->afd,ctx->track);

   if(ctx->track && ctx->samplerate == samplerate && ctx->channels == channels) {
	((AudioTrack *) ctx->track)->stop();
	((AudioTrack *) ctx->track)->flush();
	((AudioTrack *)ctx->track)->start();
	return 0; 
   }	

   if(ctx->track) {
	((AudioTrack *) ctx->track)->stop();
	((AudioTrack *) ctx->track)->flush();
	delete (AudioTrack *) ctx->track;
   }	
   ctx->track = 0;	
   AudioTrack* atrack = new AudioTrack();


  __android_log_print(ANDROID_LOG_INFO,"liblossless","AudioTrack created at %p. Now trying to setup", atrack);

   if(!atrack) return LIBLOSSLESS_ERR_INIT;
   ctx->track = atrack; 	
   status_t status;	
   	
#ifndef BUILD_JB
   status = atrack->set(_MUSIC, samplerate, FMTBPS, channels, DEFAULT_CONF_BUFSZ/(2*channels));
   if(status != NO_ERROR) { 
  __android_log_print(ANDROID_LOG_INFO,"liblossless","AudioTrack->set failed, error code=%d!", status);
#endif
  __android_log_print(ANDROID_LOG_INFO,"liblossless","Well... trying new Android AudioSystem interface");
	int chans = (channels == 2) ? 12 : 4;
	status = atrack->set(_MUSIC, samplerate, FMTBPS, chans, DEFAULT_CONF_BUFSZ/(2*channels));
	   if(status != NO_ERROR) {
  __android_log_print(ANDROID_LOG_INFO,"liblossless","Does not work, error code=%d. Bailing out.", status);
		delete atrack; ctx->track = 0;
		return LIBLOSSLESS_ERR_INIT;  
	   }		
#ifndef BUILD_JB
  }
#endif
  __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!");
   return 0; 
}
/**
 * \brief resume output
 * \param audec pointer to audec
 * \return 0 on success otherwise negative error code
 */
extern "C" int android_resume(struct aml_audio_dec* audec)
{
    adec_print("android out resume");

    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->start();

    return 0;
}
Example #5
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;
}
Example #6
0
qboolean SNDDMA_Init(void)
{
  if ( ! enableSound() ) {
    return false;
  }

  gDMAByteIndex = 0;

  // Initialize the AudioTrack.

  status_t result = gAudioTrack.set(
    AudioSystem::DEFAULT, // stream type
    SAMPLE_RATE,   // sample rate
    BITS_PER_SAMPLE == 16 ? AudioSystem::PCM_16_BIT : AudioSystem::PCM_8_BIT,      // format (8 or 16)
    (CHANNEL_COUNT > 1) ? AudioSystem::CHANNEL_OUT_STEREO : AudioSystem::CHANNEL_OUT_MONO,       // channel mask
    0,       // default buffer size
    0, // flags
    AndroidQuakeSoundCallback, // callback
    0,  // user
    0); // default notification size

  LOGI("AudioTrack status  = %d (%s)\n", result, result == NO_ERROR ? "success" : "error");

  if ( result == NO_ERROR ) {
    LOGI("AudioTrack latency = %u ms\n", gAudioTrack.latency());
    LOGI("AudioTrack format = %u bits\n", gAudioTrack.format() == AudioSystem::PCM_16_BIT ? 16 : 8);
    LOGI("AudioTrack sample rate = %u Hz\n", gAudioTrack.getSampleRate());
    LOGI("AudioTrack frame count = %d\n", int(gAudioTrack.frameCount()));
    LOGI("AudioTrack channel count = %d\n", gAudioTrack.channelCount());

    // Initialize Quake's idea of a DMA buffer.

    shm = &sn;
    memset((void*)&sn, 0, sizeof(sn));

    shm->splitbuffer = false;	// Not used.
    shm->samplebits = gAudioTrack.format() == AudioSystem::PCM_16_BIT ? 16 : 8;
    shm->speed = gAudioTrack.getSampleRate();
    shm->channels = gAudioTrack.channelCount();
    shm->samples = TOTAL_BUFFER_SIZE / BYTES_PER_SAMPLE;
    shm->samplepos = 0; // Not used.
    shm->buffer = (unsigned char*) Hunk_AllocName(TOTAL_BUFFER_SIZE, (char*) "shmbuf");
    shm->submission_chunk = 1; // Not used.

    shm->soundalive = true;

    if ( (shm->samples & 0x1ff) != 0 ) {
      LOGE("SNDDDMA_Init: samples must be power of two.");
      return false;
    }

    if ( shm->buffer == 0 ) {
      LOGE("SNDDDMA_Init: Could not allocate sound buffer.");
      return false;
    }

    gAudioTrack.setVolume(1.0f, 1.0f);
    gAudioTrack.start();
  }

  return result == NO_ERROR;
}
Example #7
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; 
}