Exemplo n.º 1
0
/* API: get capability */
static pj_status_t strm_get_cap(pjmedia_aud_stream *s,
	 		        pjmedia_aud_dev_cap cap,
			        void *pval)
{
    struct pa_aud_stream *strm = (struct pa_aud_stream*)s;

    PJ_ASSERT_RETURN(strm && pval, PJ_EINVAL);

    if (cap==PJMEDIA_AUD_DEV_CAP_INPUT_LATENCY && strm->rec_strm) {
	const PaStreamInfo *si = Pa_GetStreamInfo(strm->rec_strm);
	if (!si)
	    return PJMEDIA_EAUD_SYSERR;

	*(unsigned*)pval = (unsigned)(si->inputLatency * 1000);
	return PJ_SUCCESS;
    } else if (cap==PJMEDIA_AUD_DEV_CAP_OUTPUT_LATENCY && strm->play_strm) {
	const PaStreamInfo *si = Pa_GetStreamInfo(strm->play_strm);
	if (!si)
	    return PJMEDIA_EAUD_SYSERR;

	*(unsigned*)pval = (unsigned)(si->outputLatency * 1000);
	return PJ_SUCCESS;
    } else {
	return PJMEDIA_EAUD_INVCAP;
    }
}
Exemplo n.º 2
0
/*
 * Get stream info.
 */
PJ_DEF(pj_status_t) pjmedia_snd_stream_get_info(pjmedia_snd_stream *strm,
						pjmedia_snd_stream_info *pi)
{
    const PaStreamInfo *paPlaySI = NULL, *paRecSI = NULL;

    PJ_ASSERT_RETURN(strm && pi, PJ_EINVAL);
    PJ_ASSERT_RETURN(strm->play_strm || strm->rec_strm, PJ_EINVALIDOP);

    if (strm->play_strm) {
	paPlaySI = Pa_GetStreamInfo(strm->play_strm);
    }
    if (strm->rec_strm) {
	paRecSI = Pa_GetStreamInfo(strm->rec_strm);
    }

    pj_bzero(pi, sizeof(*pi));
    pi->dir = strm->dir;
    pi->play_id = strm->play_id;
    pi->rec_id = strm->rec_id;
    pi->clock_rate = (unsigned)(paPlaySI ? paPlaySI->sampleRate : 
				paRecSI->sampleRate);
    pi->channel_count = strm->channel_count;
    pi->samples_per_frame = strm->samples_per_frame;
    pi->bits_per_sample = strm->bytes_per_sample * 8;
    pi->rec_latency = (unsigned)(paRecSI ? paRecSI->inputLatency * 
					   paRecSI->sampleRate : 0);
    pi->play_latency = (unsigned)(paPlaySI ? paPlaySI->outputLatency * 
					     paPlaySI->sampleRate : 0);

    return PJ_SUCCESS;
}
Exemplo n.º 3
0
/** 
 * Initializes PortAudio with a callback that fills output buffers mixed by the Kowalski Engine.
 * @param engine
 * @param sampleRate
 * @param numChannels
 * @param bufferSize
 * @return A Kowalski error code.
 */
kwlError kwlEngine_hostSpecificInitialize(kwlEngine* engine, int sampleRate, int numOutChannels, int numInChannels, int bufferSize)
{
    PaError err = Pa_Initialize();
    KWL_ASSERT(err == paNoError && "error initializing portaudio");
    
    /* Open an audio I/O stream. */
    err = Pa_OpenDefaultStream(&stream,
                               numInChannels,
                               numOutChannels,
                               paFloat32,   /* 32 bit floating point output. */
                               sampleRate,
                               bufferSize, /* frames per buffer, i.e. the number
                                            of sample frames that PortAudio will
                                            request from the callback. Many apps
                                            may want to use
                                            paFramesPerBufferUnspecified, which
                                            tells PortAudio to pick the best,
                                            possibly changing, buffer size.*/
                               (PaStreamCallback*)&paCallback,
                               engine->mixer);
    //printf("PortAudio error: %s\n", Pa_GetErrorText(err));
    KWL_ASSERT(err == paNoError);
    
    err = Pa_StartStream(stream);
    //printf("PortAudio error: %s\n", Pa_GetErrorText(err));
    KWL_ASSERT(err == paNoError);

	const PaStreamInfo* si = Pa_GetStreamInfo(stream);

    return KWL_NO_ERROR;
}
Exemplo n.º 4
0
void JackPortAudioDriver::UpdateLatencies()
{
    jack_latency_range_t input_range;
    jack_latency_range_t output_range;
    jack_latency_range_t monitor_range;

    const PaStreamInfo* info = Pa_GetStreamInfo(fStream);
    assert(info);

    for (int i = 0; i < fCaptureChannels; i++) {
        input_range.max = input_range.min = fEngineControl->fBufferSize + (info->inputLatency * fEngineControl->fSampleRate) + fCaptureLatency;
        fGraphManager->GetPort(fCapturePortList[i])->SetLatencyRange(JackCaptureLatency, &input_range);
    }

    for (int i = 0; i < fPlaybackChannels; i++) {
        output_range.max = output_range.min = (info->outputLatency * fEngineControl->fSampleRate) + fPlaybackLatency;
        if (fEngineControl->fSyncMode) {
            output_range.max = output_range.min += fEngineControl->fBufferSize;
        } else {
            output_range.max = output_range.min += fEngineControl->fBufferSize * 2;
        }
        fGraphManager->GetPort(fPlaybackPortList[i])->SetLatencyRange(JackPlaybackLatency, &output_range);
        if (fWithMonitorPorts) {
            monitor_range.min = monitor_range.max = fEngineControl->fBufferSize;
            fGraphManager->GetPort(fMonitorPortList[i])->SetLatencyRange(JackCaptureLatency, &monitor_range);
        }
    }
}
Exemplo n.º 5
0
void DevicePortAudio::deviceInputOpen( const PaDeviceInfo *DevInf )
{
	deviceInputClose();

	PaStreamParameters	 StrPrm;

	memset( &StrPrm, 0, sizeof( StrPrm ) );

	StrPrm.device           = mDeviceIndex;
	StrPrm.channelCount     = DevInf->maxInputChannels;
	StrPrm.sampleFormat     = paNonInterleaved | paFloat32;
	StrPrm.suggestedLatency = DevInf->defaultLowInputLatency;

	if( Pa_OpenStream( &mStreamInput, &StrPrm, nullptr, outputSampleRate(), paFramesPerBufferUnspecified, paNoFlag, &DevicePortAudio::streamCallbackStatic, this ) != paNoError )
	{
		return;
	}

	mInputChannelCount = DevInf->maxInputChannels;

	const PaStreamInfo	*StreamInfo = Pa_GetStreamInfo( mStreamInput );

	qDebug() << DevInf->name << StreamInfo->sampleRate << StreamInfo->inputLatency << mInputChannelCount;

	mInputSampleRate   = StreamInfo->sampleRate;
	mInputTimeLatency  = StreamInfo->inputLatency;
	mInputAudioOffset  = 0; //QDateTime::currentMSecsSinceEpoch() * qint64( mSampleRate / 1000.0 );
	mInputSampleFormat = fugio::AudioSampleFormat::Format32FS;

	if( Pa_StartStream( mStreamInput ) != paNoError )
	{
		return;
	}
}
Exemplo n.º 6
0
static ALCboolean pa_reset_playback(ALCdevice *device)
{
    pa_data *data = (pa_data*)device->ExtraData;
    const PaStreamInfo *streamInfo;
    PaError err;

    streamInfo = Pa_GetStreamInfo(data->stream);
    if(device->Frequency != streamInfo->sampleRate)
    {
        if((device->Flags&DEVICE_FREQUENCY_REQUEST))
            ERR("PortAudio does not support changing sample rates (wanted %dhz, got %.1fhz)\n", device->Frequency, streamInfo->sampleRate);
        device->Flags &= ~DEVICE_FREQUENCY_REQUEST;
        device->Frequency = streamInfo->sampleRate;
    }
    device->UpdateSize = data->update_size;

    err = Pa_StartStream(data->stream);
    if(err != paNoError)
    {
        ERR("Pa_StartStream() returned an error: %s\n", Pa_GetErrorText(err));
        return ALC_FALSE;
    }

    return ALC_TRUE;
}
Exemplo n.º 7
0
PaError paqaCheckLatency( PaStreamParameters *outputParamsPtr, 
                         paTestData *dataPtr, double sampleRate, unsigned long framesPerBuffer )
{
    PaError err;
    PaStream *stream;
    const PaStreamInfo* streamInfo;

    dataPtr->minFramesPerBuffer = 9999999;
    dataPtr->maxFramesPerBuffer = 0;
    dataPtr->minDeltaDacTime = 9999999.0;
    dataPtr->maxDeltaDacTime = 0.0;
    dataPtr->callbackCount = 0;
    
    printf("Stream parameter: suggestedOutputLatency = %g\n", outputParamsPtr->suggestedLatency );
    if( framesPerBuffer == paFramesPerBufferUnspecified ){
        printf("Stream parameter: user framesPerBuffer = paFramesPerBufferUnspecified\n" );
    }else{
        printf("Stream parameter: user framesPerBuffer = %lu\n", framesPerBuffer );
    }
    err = Pa_OpenStream(
                        &stream,
                        NULL, /* no input */
                        outputParamsPtr,
                        sampleRate,
                        framesPerBuffer,
                        paClipOff,      /* we won't output out of range samples so don't bother clipping them */
                        patestCallback,
                        dataPtr );
    if( err != paNoError ) goto error1;
    
    streamInfo = Pa_GetStreamInfo( stream );
    printf("Stream info: inputLatency  = %g\n", streamInfo->inputLatency );
    printf("Stream info: outputLatency = %g\n", streamInfo->outputLatency );

    err = Pa_StartStream( stream );
    if( err != paNoError ) goto error2;

    printf("Play for %d seconds.\n", NUM_SECONDS );
    Pa_Sleep( NUM_SECONDS * 1000 );
    
    printf("  minFramesPerBuffer = %4d\n", dataPtr->minFramesPerBuffer );
    printf("  maxFramesPerBuffer = %4d\n", dataPtr->maxFramesPerBuffer );
    printf("  minDeltaDacTime = %f\n", dataPtr->minDeltaDacTime );
    printf("  maxDeltaDacTime = %f\n", dataPtr->maxDeltaDacTime );

    err = Pa_StopStream( stream );
    if( err != paNoError ) goto error2;

    err = Pa_CloseStream( stream );
    Pa_Sleep( 1 * 1000 );

    
    printf("-------------------------------------\n");
    return err;
error2:
    Pa_CloseStream( stream );
error1:
    printf("-------------------------------------\n");
    return err;
}
Exemplo n.º 8
0
int main(int argc, char *argv[])
{
//    system("./soxclient '80/C#5 40/C6'");

    if (argc < 3) {
        displayOption();
        exit(0);
    }

    loadMidiHzTxt();

    int InDeviceId = atoi(argv[1]);
    int OutDeviceId = atoi(argv[2]);

    const int SamplingRate = 44100;
    const int BufLength = SamplingRate / 50;

    // FFT用に2のべき乗を計算する
    for (double i = 1.0; i < 1000; i = i + 1.0) {
        fft_num = pow(2.0, i);
        if (fft_num > BufLength * 2) {
            break;
        }
    }

    Pa_Initialize();

    PaStreamParameters in_param;
    in_param.channelCount = 2;
    in_param.device = InDeviceId;
    in_param.hostApiSpecificStreamInfo = NULL;
    in_param.sampleFormat = paInt16;    
    in_param.suggestedLatency = Pa_GetDeviceInfo(in_param.device)->defaultLowInputLatency;

    PaStreamParameters out_param;
    out_param.channelCount = 1;
    out_param.device = OutDeviceId;
    out_param.hostApiSpecificStreamInfo = NULL;
    out_param.sampleFormat = paInt16;
    out_param.suggestedLatency = Pa_GetDeviceInfo(out_param.device)->defaultLowInputLatency;

    PaStream *Stream;
    Pa_OpenStream(&Stream, &in_param, &out_param, SamplingRate, BufLength, NULL, CallBack, NULL);
    const PaStreamInfo *info = Pa_GetStreamInfo(Stream);

    fprintf(stderr, "----------Start----------\n");
    Pa_StartStream(Stream);

    getchar();
    Pa_CloseStream(Stream);
    Pa_Terminate();

    fftw_destroy_plan(p);
    fftw_free(fft_in); 
    fftw_free(fft_out);

    (void) pclose(file_p);

}
Exemplo n.º 9
0
Arquivo: audio.c Projeto: vacaboja/tg
int start_portaudio(int *nominal_sample_rate, double *real_sample_rate)
{
	PaStream *stream;

	if(pthread_mutex_init(&audio_mutex,NULL)) {
		error("Failed to setup audio mutex");
		return 1;
	}

	PaError err = Pa_Initialize();
	if(err!=paNoError)
		goto error;

#ifdef DEBUG
	if(testing) {
		*nominal_sample_rate = PA_SAMPLE_RATE;
		*real_sample_rate = PA_SAMPLE_RATE;
		goto end;
	}
#endif

	PaDeviceIndex default_input = Pa_GetDefaultInputDevice();
	if(default_input == paNoDevice) {
		error("No default audio input device found");
		return 1;
	}
	long channels = Pa_GetDeviceInfo(default_input)->maxInputChannels;
	if(channels == 0) {
		error("Default audio device has no input channels");
		return 1;
	}
	if(channels > 2) channels = 2;
	err = Pa_OpenDefaultStream(&stream,channels,0,paFloat32,PA_SAMPLE_RATE,paFramesPerBufferUnspecified,paudio_callback,(void*)channels);
	if(err!=paNoError)
		goto error;

	err = Pa_StartStream(stream);
	if(err!=paNoError)
		goto error;

	const PaStreamInfo *info = Pa_GetStreamInfo(stream);
#ifdef LIGHT
	*nominal_sample_rate = PA_SAMPLE_RATE / 2;
	*real_sample_rate = info->sampleRate / 2;
#else
	*nominal_sample_rate = PA_SAMPLE_RATE;
	*real_sample_rate = info->sampleRate;
#endif
#ifdef DEBUG
end:
#endif
	debug("sample rate: nominal = %d real = %f\n",*nominal_sample_rate,*real_sample_rate);

	return 0;

error:
	error("Error opening audio input: %s", Pa_GetErrorText(err));
	return 1;
}
Exemplo n.º 10
0
bool Portaudio::init()
      {
      PaError err = Pa_Initialize();
      if (err != paNoError) {
            qDebug("Portaudio initialize failed: %s", Pa_GetErrorText(err));
            return false;
            }
      initialized = true;
      if (MScore::debugMode)
            qDebug("using PortAudio Version: %s", Pa_GetVersionText());

      PaDeviceIndex idx = preferences.portaudioDevice;
      if (idx < 0)
            idx = Pa_GetDefaultOutputDevice();

      const PaDeviceInfo* di = Pa_GetDeviceInfo(idx);
      _sampleRate = int(di->defaultSampleRate);

      /* Open an audio I/O stream. */
      struct PaStreamParameters out;
      memset(&out, 0, sizeof(out));

      out.device           = idx;
      out.channelCount     = 2;
      out.sampleFormat     = paFloat32;
      out.suggestedLatency = 0.020;
      out.hostApiSpecificStreamInfo = 0;

      err = Pa_OpenStream(&stream, 0, &out, double(_sampleRate), 0, 0, paCallback, (void*)this);
      if (err != paNoError) {
            // fall back to default device:
            out.device = Pa_GetDefaultOutputDevice();
            err = Pa_OpenStream(&stream, 0, &out, double(_sampleRate), 0, 0, paCallback, (void*)this);
            if (err != paNoError) {
                  qDebug("Portaudio open stream %d failed: %s", idx, Pa_GetErrorText(err));
                  return false;
                  }
            }
      const PaStreamInfo* si = Pa_GetStreamInfo(stream);
      if (si)
            _sampleRate = int(si->sampleRate);
#ifdef USE_ALSA
      midiDriver = new AlsaMidiDriver(seq);
#endif
#ifdef USE_PORTMIDI
      midiDriver = new PortMidiDriver(seq);
#endif
      if (midiDriver && !midiDriver->init()) {
            qDebug("Init midi driver failed");
            delete midiDriver;
            midiDriver = 0;
#ifdef USE_PORTMIDI
            return true;                  // return OK for audio driver; midi is only input
#else
            return false;
#endif
            }
      return true;
      }
Exemplo n.º 11
0
 void report_latency()
 {
     const PaStreamInfo *psi = Pa_GetStreamInfo(stream);
     if (psi){
         fprintf(stdout,"  Sample rate: %.3f\n", psi->sampleRate);
         fprintf(stdout,"  Latency (in/out): %.3f / %.3f sec\n", psi->inputLatency, psi->outputLatency); 
     }
 }
Exemplo n.º 12
0
/* Returns true on success, false on failure */
bool PortAudioStreamer::Start(const PaStreamParameters *inputParams,
                              const PaStreamParameters *outputParams,
                              double sampleRate)
{
  PaError error;

  qDebug("Trying Pa_OpenStream() with sampleRate %g inputLatency %g outputLatency %g innch %d outnch %d",
         sampleRate,
         inputParams->suggestedLatency,
         outputParams->suggestedLatency,
         inputParams->channelCount,
         outputParams->channelCount);
  const PaDeviceInfo *deviceInfo = Pa_GetDeviceInfo(inputParams->device);
  if (deviceInfo) {
    const PaHostApiInfo *hostApiInfo = Pa_GetHostApiInfo(deviceInfo->hostApi);
    qDebug("Input device: %s (%s)", deviceInfo->name,
           hostApiInfo ? hostApiInfo->name : "<invalid host api>");
  }
  deviceInfo = Pa_GetDeviceInfo(outputParams->device);
  if (deviceInfo) {
    const PaHostApiInfo *hostApiInfo = Pa_GetHostApiInfo(deviceInfo->hostApi);
    qDebug("Output device: %s (%s)", deviceInfo->name,
           hostApiInfo ? hostApiInfo->name : "<invalid host api>");
  }

  error = Pa_OpenStream(&stream, inputParams, outputParams,
                        sampleRate, paFramesPerBufferUnspecified,
                        paPrimeOutputBuffersUsingStreamCallback,
                        streamCallbackTrampoline, this);
  if (error != paNoError) {
    logPortAudioError("Pa_OpenStream() failed", error);
    stream = NULL;
    return false;
  }

  m_srate = sampleRate;
  m_innch = inputParams->channelCount;
  m_outnch = outputParams->channelCount;
  m_bps = 32;

  error = Pa_StartStream(stream);
  if (error != paNoError) {
    logPortAudioError("Pa_StartStream() failed", error);
    Pa_CloseStream(stream);
    stream = NULL;
    return false;
  }

  const PaStreamInfo *streamInfo = Pa_GetStreamInfo(stream);
  if (streamInfo) {
    qDebug("Stream started with sampleRate %g inputLatency %g outputLatency %g",
           streamInfo->sampleRate,
           streamInfo->inputLatency,
           streamInfo->outputLatency);
  }

  return true;
}
Exemplo n.º 13
0
int PortAudioStreamer::streamCallback(const void *input, void *output,
    unsigned long frameCount, const PaStreamCallbackTimeInfo *timeInfo,
    PaStreamCallbackFlags statusFlags)
{
  float **inbuf = (float**)input; // const-cast due to SPLPROC prototype
  float **outbuf = static_cast<float**>(output);
  const PaStreamInfo *info = Pa_GetStreamInfo(stream);

  splproc(inbuf, m_innch, outbuf, m_outnch, frameCount, info->sampleRate);
  return paContinue;
}
Exemplo n.º 14
0
portaudio_decoder *quiet_portaudio_decoder_create(const decoder_options *opt, PaDeviceIndex device, PaTime latency, double sample_rate, size_t sample_buffer_size) {
    PaStream *stream;
    PaStreamParameters param = {
        .device = device,
        .channelCount = 2,
        .sampleFormat = paFloat32,
        .suggestedLatency = latency,
        .hostApiSpecificStreamInfo = NULL,
    };
    PaError err = Pa_OpenStream(&stream, &param, NULL, sample_rate,
                        sample_buffer_size, paNoFlag, NULL, NULL);
    if (err != paNoError) {
        printf("failed to open port audio stream, %s\n", Pa_GetErrorText(err));
        return NULL;
    }

    err = Pa_StartStream(stream);
    if (err != paNoError) {
        printf("failed to start port audio stream, %s\n", Pa_GetErrorText(err));
        return NULL;
    }

    const PaStreamInfo *info = Pa_GetStreamInfo(stream);
    decoder *d = quiet_decoder_create(opt, info->sampleRate);

    quiet_sample_t *sample_buffer = malloc(2 * sample_buffer_size * sizeof(quiet_sample_t));
    quiet_sample_t *mono_buffer = malloc(sample_buffer_size * sizeof(quiet_sample_t));
    portaudio_decoder *decoder = malloc(1 * sizeof(portaudio_decoder));
    decoder->dec = d;
    decoder->stream = stream;
    decoder->sample_buffer = sample_buffer;
    decoder->mono_buffer = mono_buffer;
    decoder->sample_buffer_size = sample_buffer_size;

    return decoder;
}

ssize_t quiet_portaudio_decoder_recv(quiet_portaudio_decoder *d, uint8_t *data, size_t len) {
    return quiet_decoder_recv(d->dec, data, len);
}

void quiet_portaudio_decoder_consume(quiet_portaudio_decoder *d) {
    PaError err = Pa_ReadStream(d->stream, d->sample_buffer, d->sample_buffer_size);
    if (err != paNoError) {
        printf("failed to read port audio stream, %s\n", Pa_GetErrorText(err));
        return;
    }
    for (size_t i = 0; i < d->sample_buffer_size; i++) {
        d->mono_buffer[i] = d->sample_buffer[i * 2] + d->sample_buffer[i * 2 + 1];
    }
    quiet_decoder_consume(d->dec, d->mono_buffer, d->sample_buffer_size);
}
Exemplo n.º 15
0
/* API: Get stream parameters */
static pj_status_t strm_get_param(pjmedia_aud_stream *s,
				  pjmedia_aud_param *pi)
{
    struct pa_aud_stream *strm = (struct pa_aud_stream*)s;
    const PaStreamInfo *paPlaySI = NULL, *paRecSI = NULL;

    PJ_ASSERT_RETURN(strm && pi, PJ_EINVAL);
    PJ_ASSERT_RETURN(strm->play_strm || strm->rec_strm, PJ_EINVALIDOP);

    if (strm->play_strm) {
	paPlaySI = Pa_GetStreamInfo(strm->play_strm);
    }
    if (strm->rec_strm) {
	paRecSI = Pa_GetStreamInfo(strm->rec_strm);
    }

    pj_bzero(pi, sizeof(*pi));
    pi->dir = strm->dir;
    pi->play_id = strm->play_id;
    pi->rec_id = strm->rec_id;
    pi->clock_rate = (unsigned)(paPlaySI ? paPlaySI->sampleRate : 
				paRecSI->sampleRate);
    pi->channel_count = strm->channel_count;
    pi->samples_per_frame = strm->samples_per_frame;
    pi->bits_per_sample = strm->bytes_per_sample * 8;
    if (paRecSI) {
	pi->flags |= PJMEDIA_AUD_DEV_CAP_INPUT_LATENCY;
	pi->input_latency_ms = (unsigned)(paRecSI ? paRecSI->inputLatency * 
						    1000 : 0);
    }
    if (paPlaySI) {
	pi->flags |= PJMEDIA_AUD_DEV_CAP_OUTPUT_LATENCY;
	pi->output_latency_ms = (unsigned)(paPlaySI? paPlaySI->outputLatency * 
						     1000 : 0);
    }

    return PJ_SUCCESS;
}
Exemplo n.º 16
0
bool CCPortAudio::open()
{
    DPTR_D(CCPortAudio);
    //
    d.outputParameters->channelCount = d.channels;
    PaError err = Pa_OpenStream(&d.stream, NULL, d.outputParameters, d.sample_rate, 0, paNoFlag, NULL, NULL);
    if (err == paNoError) {
        d.outputLatency = Pa_GetStreamInfo(d.stream)->outputLatency;
        d.available = true;
    } else {
        qWarning("Open portaudio stream error: %s", Pa_GetErrorText(err));
        d.available = false;
    }
    return err == paNoError;
}
Exemplo n.º 17
0
	void Initialize(bool StartThread)
	{
		RingbufData = new char[BUFF_SIZE*sizeof(float)];

		WaitForRingbufferSpace = false;

		PaUtil_InitializeRingBuffer(&RingBuf, sizeof(float), BUFF_SIZE, RingbufData);

		Threaded = StartThread;
		Stream = nullptr;

		if (StartThread)
		{
			thread(&PaMixer::Run, this).detach();
		}
#ifdef WIN32
		if (UseWasapi)
		{
			OpenStream(&Stream, GetWasapiDevice(), 44100, (void*) this, Latency, Mix);

			if (!Stream)
			{
				// This was a Wasapi problem. Retry without it.
				Log::Logf("Problem initializing WASAPI. Falling back to default API.");
				UseWasapi = false;
				OpenStream(&Stream, Pa_GetDefaultOutputDevice(), 44100, (void*) this, Latency, Mix);
			}

		}
		else
		{
			OpenStream(&Stream, DefaultDSDevice, 44100, (void*) this, Latency, Mix);
		}
#else
		OpenStream( &Stream, Pa_GetDefaultOutputDevice(), 44100, (void*) this, Latency, Mix );

#endif

		if (Stream)
		{
			Pa_StartStream(Stream);
			std::this_thread::sleep_for(std::chrono::milliseconds(16));
			Latency = Pa_GetStreamInfo(Stream)->outputLatency;
			Log::Logf("AUDIO: Latency after opening stream = %f \n", Latency);
		}

		ConstFactor = 1.0;
	}
Exemplo n.º 18
0
void SoundOutThread::run()
{
  PaError paerr;
  PaStreamParameters outParam;
  PaStream *outStream;
  paUserData udata;
  quitExecution = false;

  outParam.device=m_nDevOut;                 //Output device number
  outParam.channelCount=2;                   //Number of analog channels
  outParam.sampleFormat=paInt16;             //Send short ints to PortAudio
  outParam.suggestedLatency=0.05;
  outParam.hostApiSpecificStreamInfo=NULL;

  udata.nTRperiod=m_TRperiod;
  paerr=Pa_IsFormatSupported(NULL,&outParam,11025.0);
  if(paerr<0) {
    qDebug() << "PortAudio says requested output format not supported.";
    qDebug() << paerr;
    return;
  }
  paerr=Pa_OpenStream(&outStream,           //Output stream
        NULL,                               //No input parameters
        &outParam,                          //Output parameters
        11025.0,                            //Sample rate
        FRAMES_PER_BUFFER,                  //Frames per buffer
        paClipOff,                          //No clipping
        d2aCallback,                        //output callbeck routine
        &udata);                            //userdata

  paerr=Pa_StartStream(outStream);
  if(paerr<0) {
    qDebug() << "Failed to start audio output stream.";
    return;
  }
  const PaStreamInfo* p=Pa_GetStreamInfo(outStream);
  outputLatency = p->outputLatency;
  bool qe = quitExecution;

//---------------------------------------------- Soundcard output loop
  while (!qe) {
    qe = quitExecution;
    if (qe) break;
    msleep(100);
  }
  Pa_StopStream(outStream);
  Pa_CloseStream(outStream);
}
Exemplo n.º 19
0
//TODO: call open after audio format changed?
bool AudioOutputPortAudio::open()
{
    DPTR_D(AudioOutputPortAudio);
    QMutexLocker lock(&d.mutex);
    Q_UNUSED(lock);
    d.outputParameters->sampleFormat = toPaSampleFormat(audioFormat().sampleFormat());
    d.outputParameters->channelCount = audioFormat().channels();
    PaError err = Pa_OpenStream(&d.stream, NULL, d.outputParameters, audioFormat().sampleRate(), 0, paNoFlag, NULL, NULL);
    if (err == paNoError) {
        d.outputLatency = Pa_GetStreamInfo(d.stream)->outputLatency;
        d.available = true;
    } else {
        qWarning("Open portaudio stream error: %s", Pa_GetErrorText(err));
        d.available = false;
    }
    return err == paNoError;
}
Exemplo n.º 20
0
/*-----------------------------------------------------------------------------------------*/
static int TestBadActions( void )
{
    PaStream*           stream = NULL;
    PaError             result;
    PaQaData            myData;
    PaStreamParameters  opp;
    const PaDeviceInfo* info = NULL;

    /* Setup data for synthesis thread. */
    myData.framesLeft = (unsigned long)(SAMPLE_RATE * 100); /* 100 seconds */
    myData.numChannels = 1;
    myData.mode = MODE_OUTPUT;

    opp.device                    = Pa_GetDefaultOutputDevice(); /* Default output. */
    opp.channelCount              = 2;                           /* Stereo output.  */
    opp.hostApiSpecificStreamInfo = NULL;
    opp.sampleFormat              = paFloat32;
    info = Pa_GetDeviceInfo(opp.device);
    opp.suggestedLatency          = info ? info->defaultLowOutputLatency : 0.100;

    if (opp.device != paNoDevice) {
        HOPEFOR(((result = Pa_OpenStream(&stream, NULL, /* Take NULL as input parame-     */
                                         &opp,          /* ters, meaning try only output. */
                                         SAMPLE_RATE, FRAMES_PER_BUFFER,
                                         paClipOff, QaCallback, &myData )) == paNoError));
    }

    HOPEFOR(((result = Pa_StartStream(NULL))    == paBadStreamPtr));
    HOPEFOR(((result = Pa_StopStream(NULL))     == paBadStreamPtr));
    HOPEFOR(((result = Pa_IsStreamStopped(NULL)) == paBadStreamPtr));
    HOPEFOR(((result = Pa_IsStreamActive(NULL)) == paBadStreamPtr));
    HOPEFOR(((result = Pa_CloseStream(NULL))    == paBadStreamPtr));
    HOPEFOR(((result = Pa_SetStreamFinishedCallback(NULL, NULL)) == paBadStreamPtr));
    HOPEFOR(((result = !Pa_GetStreamInfo(NULL))));
    HOPEFOR(((result = Pa_GetStreamTime(NULL))  == 0.0));
    HOPEFOR(((result = Pa_GetStreamCpuLoad(NULL))  == 0.0));
    HOPEFOR(((result = Pa_ReadStream(NULL, NULL, 0))  == paBadStreamPtr));
    HOPEFOR(((result = Pa_WriteStream(NULL, NULL, 0))  == paBadStreamPtr));

    /** @todo test Pa_GetStreamReadAvailable and Pa_GetStreamWriteAvailable */

    if (stream != NULL) Pa_CloseStream(stream);
    return result;
}
Exemplo n.º 21
0
void AudioIO::print(){
	if(mInDevice.id() == mOutDevice.id()){
		printf("I/O Device:  "); mInDevice.print();
	}
	else{
		printf("Device In:   "); mInDevice.print();
		printf("Device Out:  "); mOutDevice.print();
	}

		printf("Chans In:    %d (%dD + %dV)\n", channelsIn(), channelsInDevice(), channelsIn() - channelsInDevice());
		printf("Chans Out:   %d (%dD + %dV)\n", channelsOut(), channelsOutDevice(), channelsOut() - channelsOutDevice());

	const PaStreamInfo * sInfo = Pa_GetStreamInfo(mImpl->mStream);
	if(sInfo){
		printf("In Latency:  %.0f ms\nOut Latency: %0.f ms\nSample Rate: %0.f Hz\n",
			sInfo->inputLatency * 1000., sInfo->outputLatency * 1000., sInfo->sampleRate);
	}
	printf("Frames/Buf:  %d\n", mFramesPerBuffer);
}
Exemplo n.º 22
0
/* buffer_size is in samples - frames would be better */
_BOOLEAN
CPaCommon::Init(int iSampleRate, int iNewBufferSize, _BOOLEAN bNewBlocking)
{
    if (device_changed == false && double(iSampleRate) == samplerate)
        return FALSE;

    unsigned long channels=2;

    samplerate = double(iSampleRate);

    if (is_capture)
        framesPerBuffer = iNewBufferSize / channels;
    else
        framesPerBuffer = 256;

    blocking = bNewBlocking; /* TODO honour this */
    iBufferSize = iNewBufferSize;

    ReInit();

    if (stream)
    {
        const PaStreamInfo* info = Pa_GetStreamInfo( stream );
        if (is_capture)
            cout << "init capture ";
        else
            cout << "init play ";
        cout << iNewBufferSize;
        if (info)
            cout << " latency " << info->outputLatency;
        else
            cout << " can't read latency";
        cout << endl;
    }
    else
    {
        cerr << "portaudio can't open stream" << endl;
        //throw "portaudio open error";
    }

    return TRUE;
}
Exemplo n.º 23
0
void pa_init(){
    printf("Opening Portaudio stream\n");
	//
	// Initialize PortAudio
	////////////////////////
	err = Pa_Initialize();
	if (err != paNoError){
		print_error(err);
	}
    
	//
	// Open a stream
	////////////////
	err = Pa_OpenDefaultStream(	&stream,
                               1,
                               1,
                               paFloat32,
                               SAMPLE_RATE,
                               64,//256,
                               callback,
                               NULL);
	if (err != paNoError){
		print_error(err);
	}
    
	//
	// Start the stream
	///////////////////
	err = Pa_StartStream(stream);
	if (err != paNoError){
		print_error(err);
	}
    
	const PaStreamInfo* info = Pa_GetStreamInfo(stream);
	printf("\nStream Information:\n");
	printf("StructVersion: %d\n",info->structVersion);
	printf("inputLatency: %f\n",info->inputLatency);
	printf("outputLatency: %f\n",info->outputLatency);
	printf("Sample Rate: %f\n\n",info->sampleRate);
}
Exemplo n.º 24
0
bool PortAudio::write(float *buffer, unsigned int samples)
{
    size_t elemWritten = ringbuf.writeElements(buffer, samples*mChannels);

    // Try starting the stream
    if(!isStarted && ringbuf.getWriteAvailable() <= (RINGBUFFERSIZE/2)) {
        LOG4CXX_TRACE(narratorPaLog, "Starting stream");
        mError = Pa_StartStream(pStream);
        if(mError != paNoError) {
            LOG4CXX_ERROR(narratorPaLog, "Failed to start stream: " << Pa_GetErrorText(mError));
        }
        mLatency = (long) (Pa_GetStreamInfo(pStream)->outputLatency * 1000.0);
        isStarted = true;
    }
    else if(!isStarted )
        LOG4CXX_TRACE(narratorPaLog, "Buffering: " << ((RINGBUFFERSIZE - ringbuf.getWriteAvailable()) * 100) / (RINGBUFFERSIZE) << "%");

    if( elemWritten < samples)
        return false;

    return false;
}
Exemplo n.º 25
0
static ALCboolean pa_reset_playback(ALCdevice *device)
{
    pa_data *data = (pa_data*)device->ExtraData;
    const PaStreamInfo *streamInfo;

    streamInfo = Pa_GetStreamInfo(data->stream);
    device->Frequency = streamInfo->sampleRate;
    device->UpdateSize = data->update_size;

    if(data->params.sampleFormat == paInt8)
        device->FmtType = DevFmtByte;
    else if(data->params.sampleFormat == paUInt8)
        device->FmtType = DevFmtUByte;
    else if(data->params.sampleFormat == paInt16)
        device->FmtType = DevFmtShort;
    else if(data->params.sampleFormat == paInt32)
        device->FmtType = DevFmtInt;
    else if(data->params.sampleFormat == paFloat32)
        device->FmtType = DevFmtFloat;
    else
    {
        ERR("Unexpected sample format: 0x%lx\n", data->params.sampleFormat);
        return ALC_FALSE;
    }

    if(data->params.channelCount == 2)
        device->FmtChans = DevFmtStereo;
    else if(data->params.channelCount == 1)
        device->FmtChans = DevFmtMono;
    else
    {
        ERR("Unexpected channel count: %u\n", data->params.channelCount);
        return ALC_FALSE;
    }
    SetDefaultChannelOrder(device);

    return ALC_TRUE;
}
bool AlgorhythmicAudioIO::Start()
{
    if(!m_inited && !Init())
        return false;
    
    PaError err;
    /* Open an audio I/O stream. */
    PaDeviceIndex outDev = Pa_GetDefaultOutputDevice();
    PaStreamParameters playbackParameters;
    playbackParameters.device = outDev;
    playbackParameters.sampleFormat = paFloat32;
    playbackParameters.hostApiSpecificStreamInfo = NULL;
    playbackParameters.channelCount = 2;
    playbackParameters.suggestedLatency = kAudioLatency/1000.0;
    
    err = Pa_OpenStream( &m_stream,
                        NULL, &playbackParameters,
                        SAMPLE_RATE, /*paFramesPerBufferUnspecified, */256,
                        paNoFlag,
                        SortingPortAudioCallback, &data  );
    
    if( err != paNoError ) return false;
    
    
    err = Pa_StartStream( m_stream );
    if( err != paNoError ) return false;
    
    const PaStreamInfo *info;
    info = Pa_GetStreamInfo(&m_stream);
    
    if(info && info->outputLatency > 0)
        m_actualLatency = info->outputLatency;
    else {
        m_actualLatency = (256.0/44100.0)/1000.0;
    }
    
    return true;
}
Exemplo n.º 27
0
int PortAudioStreamer::streamCallback(const void *input, void *output,
    unsigned long frameCount, const PaStreamCallbackTimeInfo *timeInfo,
    PaStreamCallbackFlags statusFlags)
{
  Q_UNUSED(timeInfo);
  Q_UNUSED(statusFlags);

  float **inbuf = (float**)input; // const-cast due to SPLPROC prototype
  float **outbuf = static_cast<float**>(output);
  const PaStreamInfo *info = Pa_GetStreamInfo(stream);

  /* Mix down to mono */
  if (m_innch == 2) {
    if (inputMonoBufFrames < frameCount) {
      /* Allocation should happen rarely so don't worry about real-time */
      delete [] inputMonoBuf;
      inputMonoBuf = new float[frameCount];
      inputMonoBufFrames = frameCount;
    }
    for (unsigned long i = 0; i < frameCount; i++) {
      inputMonoBuf[i] = inbuf[0][i] * 0.5 + inbuf[1][i] * 0.5;
    }
    inbuf = &inputMonoBuf;
  }

  splproc(inbuf, 1, outbuf, 1, frameCount, info->sampleRate);

  /* Mix up to multi-channel audio */
  if (m_outnch > 1) {
    for (int channel = 1; channel < m_outnch; channel++) {
      for (unsigned long i = 0; i < frameCount; i++) {
        outbuf[channel][i] = outbuf[0][i];
      }
    }
  }

  return paContinue;
}
Exemplo n.º 28
0
void DevicePortAudio::deviceOutputOpen( const PaDeviceInfo *DevInf )
{
	deviceOutputClose();

	PaStreamParameters	 StrPrm;

	memset( &StrPrm, 0, sizeof( StrPrm ) );

	StrPrm.device           = mDeviceIndex;
	StrPrm.channelCount     = DevInf->maxOutputChannels;
	StrPrm.sampleFormat     = paNonInterleaved | paFloat32;
	StrPrm.suggestedLatency = DevInf->defaultLowOutputLatency;

	if( Pa_OpenStream( &mStreamOutput, 0, &StrPrm, outputSampleRate(), paFramesPerBufferUnspecified, paNoFlag, &DevicePortAudio::streamCallbackStatic, this ) != paNoError )
	{
		return;
	}

	mOutputChannelCount = DevInf->maxOutputChannels;

	const PaStreamInfo	*StreamInfo = Pa_GetStreamInfo( mStreamOutput );

	qDebug() << DevInf->name << StreamInfo->sampleRate << StreamInfo->outputLatency << mOutputChannelCount;

	mOutputSampleRate   = StreamInfo->sampleRate;
	mOutputTimeLatency  = StreamInfo->outputLatency;
	mOutputAudioOffset  = ( PortAudioPlugin::instance()->fugio()->timestamp() * mOutputSampleRate ) / 1000;

	mOutputAudioOffset -= ( mOutputTimeLatency * mOutputSampleRate ) / 1000;

	if( Pa_StartStream( mStreamOutput ) != paNoError )
	{
		return;
	}

	//emit latencyUpdated( mTimeLatency );
}
Exemplo n.º 29
0
Audio::Audio(Oscilloscope* scope, int16_t initialJitterBufferSamples) :
    _stream(NULL),
    _ringBuffer(true),
    _scope(scope),
    _averagedLatency(0.0),
    _measuredJitter(0),
    _jitterBufferSamples(initialJitterBufferSamples),
    _wasStarved(0),
    _numStarves(0),
    _lastInputLoudness(0),
    _lastVelocity(0),
    _lastAcceleration(0),
    _totalPacketsReceived(0),
    _firstPacketReceivedTime(),
    _packetsReceivedThisPlayback(0),
    _echoSamplesLeft(NULL),
    _isSendingEchoPing(false),
    _pingAnalysisPending(false),
    _pingFramesToRecord(0),
    _samplesLeftForFlange(0),
    _lastYawMeasuredMaximum(0),
    _flangeIntensity(0.0f),
    _flangeRate(0.0f),
    _flangeWeight(0.0f),
    _collisionSoundMagnitude(0.0f),
    _collisionSoundFrequency(0.0f),
    _collisionSoundNoise(0.0f),
    _collisionSoundDuration(0.0f),
    _proceduralEffectSample(0),
    _heartbeatMagnitude(0.0f),
    _listenMode(AudioRingBuffer::NORMAL),
    _listenRadius(0.0f)
{
    outputPortAudioError(Pa_Initialize());
    
    //  NOTE:  Portaudio documentation is unclear as to whether it is safe to specify the
    //         number of frames per buffer explicitly versus setting this value to zero.
    //         Possible source of latency that we need to investigate further.
    // 
    unsigned long FRAMES_PER_BUFFER = BUFFER_LENGTH_SAMPLES_PER_CHANNEL;
    
    //  Manually initialize the portaudio stream to ask for minimum latency
    PaStreamParameters inputParameters, outputParameters;
    
    inputParameters.device = Pa_GetDefaultInputDevice();
    outputParameters.device = Pa_GetDefaultOutputDevice();

    if (inputParameters.device == -1 || outputParameters.device == -1) {
        qDebug("Audio: Missing device.\n");
        outputPortAudioError(Pa_Terminate());
        return;
    }

    inputParameters.channelCount = 1;                    //  Stereo input
    inputParameters.sampleFormat = (paInt16 | paNonInterleaved);
    inputParameters.suggestedLatency = Pa_GetDeviceInfo(inputParameters.device)->defaultLowInputLatency;
    inputParameters.hostApiSpecificStreamInfo = NULL;

    outputParameters.channelCount = 2;                    //  Stereo output
    outputParameters.sampleFormat = (paInt16 | paNonInterleaved);
    outputParameters.suggestedLatency = Pa_GetDeviceInfo(outputParameters.device)->defaultLowOutputLatency;
    outputParameters.hostApiSpecificStreamInfo = NULL;
    
    outputPortAudioError(Pa_OpenStream(&_stream, 
                                       &inputParameters,
                                       &outputParameters,
                                       SAMPLE_RATE,
                                       FRAMES_PER_BUFFER,
                                       paNoFlag,
                                       audioCallback,
                                       (void*) this));
        
    if (! _stream) {
        return;
    }
 
    _echoSamplesLeft = new int16_t[AEC_BUFFERED_SAMPLES + AEC_TMP_BUFFER_SIZE];
    memset(_echoSamplesLeft, 0, AEC_BUFFERED_SAMPLES * sizeof(int16_t));
    
    // start the stream now that sources are good to go
    outputPortAudioError(Pa_StartStream(_stream));
    
    // Uncomment these lines to see the system-reported latency
    //qDebug("Default low input, output latency (secs): %0.4f, %0.4f\n",
    //         Pa_GetDeviceInfo(Pa_GetDefaultInputDevice())->defaultLowInputLatency,
    //         Pa_GetDeviceInfo(Pa_GetDefaultOutputDevice())->defaultLowOutputLatency);
    
    const PaStreamInfo* streamInfo = Pa_GetStreamInfo(_stream);
    qDebug("Started audio with reported latency msecs In/Out: %.0f, %.0f\n", streamInfo->inputLatency * 1000.f,
             streamInfo->outputLatency * 1000.f);

    gettimeofday(&_lastReceiveTime, NULL);
}
Exemplo n.º 30
0
void _pa_open(void) {
	PaStreamParameters outputParameters;
	PaError err = paNoError;
	int device_id;

	if (pa.stream) {
		if ((err = Pa_CloseStream(pa.stream)) != paNoError) {
			LOG_WARN("error closing stream: %s", Pa_GetErrorText(err));
		}
	}

	if (output.state == OUTPUT_OFF) {
		// we get called when transitioning to OUTPUT_OFF to create the probe thread
		// set err to avoid opening device and logging messages
		err = 1;

	} else if ((device_id = pa_device_id(output.device)) == -1) {
		LOG_INFO("device %s not found", output.device);
		err = 1;

	} else {

		outputParameters.device = device_id;
		outputParameters.channelCount = 2;
		outputParameters.sampleFormat = paInt32;
		outputParameters.suggestedLatency =
			output.latency ? (double)output.latency/(double)1000 : Pa_GetDeviceInfo(outputParameters.device)->defaultHighOutputLatency;
		outputParameters.hostApiSpecificStreamInfo = NULL;
		
#if OSX
		// enable pro mode which aims to avoid resampling if possible
		// see http://code.google.com/p/squeezelite/issues/detail?id=11 & http://code.google.com/p/squeezelite/issues/detail?id=37
		// command line controls osx_playnice which is -1 if not specified, 0 or 1 - choose playnice if -1 or 1
		PaMacCoreStreamInfo macInfo;
		unsigned long streamInfoFlags;
	 	if (output.osx_playnice) {
			LOG_INFO("opening device in PlayNice mode");
			streamInfoFlags = paMacCorePlayNice;
		} else {
			LOG_INFO("opening device in Pro mode");
			streamInfoFlags = paMacCorePro;
		}
		PaMacCore_SetupStreamInfo(&macInfo, streamInfoFlags);
		outputParameters.hostApiSpecificStreamInfo = &macInfo;
#endif
	}

	if (!err &&
		(err = Pa_OpenStream(&pa.stream, NULL, &outputParameters, (double)output.current_sample_rate, paFramesPerBufferUnspecified,
							 paPrimeOutputBuffersUsingStreamCallback | paDitherOff, pa_callback, NULL)) != paNoError) {
		LOG_WARN("error opening device %i - %s : %s", outputParameters.device, Pa_GetDeviceInfo(outputParameters.device)->name, 
				 Pa_GetErrorText(err));
	}

	if (!err) {
		LOG_INFO("opened device %i - %s at %u latency %u ms", outputParameters.device, Pa_GetDeviceInfo(outputParameters.device)->name,
				 (unsigned int)Pa_GetStreamInfo(pa.stream)->sampleRate, (unsigned int)(Pa_GetStreamInfo(pa.stream)->outputLatency * 1000));

		pa.rate = output.current_sample_rate;

		if ((err = Pa_SetStreamFinishedCallback(pa.stream, pa_stream_finished)) != paNoError) {
			LOG_WARN("error setting finish callback: %s", Pa_GetErrorText(err));
		}
	
		if ((err = Pa_StartStream(pa.stream)) != paNoError) {
			LOG_WARN("error starting stream: %s", Pa_GetErrorText(err));
		}
	}

	if (err && !monitor_thread_running) {
		vis_stop();

		// create a thread to check for output state change or device return
#if LINUX || OSX
		pthread_create(&monitor_thread, NULL, pa_monitor, NULL);
#endif
#if WIN
		monitor_thread = CreateThread(NULL, OUTPUT_THREAD_STACK_SIZE, (LPTHREAD_START_ROUTINE)&pa_monitor, NULL, 0, NULL);
#endif
	}

	output.error_opening = !err;
}