Ejemplo n.º 1
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; 
}
Ejemplo n.º 2
0
status_t APlaybackDevice::open()
{
    status_t err;
    int sampleRateInHz;
    int channelConfig;
    int audioFormat;
    int bufferSizeInBytes;

    LOG_FUNC_START

    sampleRateInHz = mDevice->Frequency;
    channelConfig = aluChannelsFromFormat(mDevice->Format) == 1 ?
        AUDIO_CHANNEL_OUT_MONO : AUDIO_CHANNEL_OUT_STEREO;

    audioFormat = aluBytesFromFormat(mDevice->Format) == 1 ?
        AUDIO_FORMAT_PCM_8_BIT : AUDIO_FORMAT_PCM_16_BIT;

    err = AudioTrack::getMinFrameCount(&bufferSizeInBytes, audioFormat, sampleRateInHz);
    RETURN_IF(err);

    LOGV("rate(%i), channel(%i), format(%i), buffSize(%i), numUpdates(%i)",
        sampleRateInHz, channelConfig, audioFormat, bufferSizeInBytes, mDevice->NumUpdates);

    err = mAudioTrack.set(AUDIO_STREAM_MUSIC,
                          sampleRateInHz,
                          audioFormat,
                          channelConfig,
                          bufferSizeInBytes,     // frameCount
                          0,                     // flags
                          0, 0);                 // callback, callback data (user)
    RETURN_IF(err);

    err = mAudioTrack.initCheck();
    RETURN_IF(err);

    if(mBuffer) {
        delete mBuffer;
    }
    mBuffer = new AAudioBuffer(bufferSizeInBytes);

    LOG_FUNC_END

    return NO_ERROR;
}
Ejemplo n.º 3
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;
}
Ejemplo n.º 4
0
/**
 * \brief output initialization
 * \param audec pointer to audec
 * \return 0 on success otherwise negative error code
 */
extern "C" int android_init(struct aml_audio_dec* audec)
{
    adec_print("android out init");

    status_t status;
    AudioTrack *track;
    audio_out_operations_t *out_ops = &audec->aout_ops;

    Mutex::Autolock _l(mLock);

    track = new AudioTrack();
    if (track == NULL) {
        adec_print("AudioTrack Create Failed!");
        return -1;
    }

	int SessionID = audec->SessionID;
	adec_print("SessionID = %d",SessionID);
	
#if defined(_VERSION_JB)
    status = track->set(AUDIO_STREAM_MUSIC,
                        audec->samplerate,
                        AUDIO_FORMAT_PCM_16_BIT,
                        (audec->channels == 1) ? AUDIO_CHANNEL_OUT_MONO : AUDIO_CHANNEL_OUT_STEREO,
                        0,       // frameCount
                        AUDIO_OUTPUT_FLAG_NONE, // flags
                        audioCallback,
                        audec,    // user when callback
                        0,       // notificationFrames
                        0,       // shared buffer
                        false,   // threadCanCallJava
                        SessionID);      // sessionId
                        
#elif defined(_VERSION_ICS)
    status = track->set(AUDIO_STREAM_MUSIC,
                        audec->samplerate,
                        AUDIO_FORMAT_PCM_16_BIT,
                        (audec->channels == 1) ? AUDIO_CHANNEL_OUT_MONO : AUDIO_CHANNEL_OUT_STEREO,
                        0,       // frameCount
                        0,       // flags
                        audioCallback,
                        audec,    // user when callback
                        0,       // notificationFrames
                        0,       // shared buffer
                        false,	 // threadCanCallJava
                        SessionID);      // sessionId
#else   // GB or lower:
    status = track->set(AudioSystem::MUSIC,
                        audec->samplerate,
                        AudioSystem::PCM_16_BIT,
                        (audec->channels == 1) ? AudioSystem::CHANNEL_OUT_MONO : AudioSystem::CHANNEL_OUT_STEREO,
                        0,       // frameCount
                        0,       // flags
                        audioCallback,
                        audec,    // user when callback
                        0,       // notificationFrames
                        0,       // shared buffer
                        SessionID);
#endif

    if (status != NO_ERROR) {
        adec_print("track->set returns %d", status);
        adec_print("audio out samplet %d", audec->samplerate);
        adec_print("audio out channels %d", audec->channels);
        delete track;
        track = NULL;
        return -1;

    }
    af_resample_linear_init();
    out_ops->private_data = (void *)track;
    return 0;
}
Ejemplo n.º 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; 
}