Exemple #1
0
static void clear(struct device *dv)
{
    struct alsa *alsa = (struct alsa*)dv->local;

    pcm_close(&alsa->capture);
    pcm_close(&alsa->playback);
    free(dv->local);
}
Exemple #2
0
static size_t read_frames(void **frames)
{
    unsigned int card = 0;
    unsigned int device = 0;
    int flags = PCM_IN;

    const struct pcm_config config = {
        .channels = 2,
        .rate = 48000,
        .format = PCM_FORMAT_S32_LE,
        .period_size = 1024,
        .period_count = 2,
        .start_threshold = 1024,
        .silence_threshold = 1024 * 2,
        .stop_threshold = 1024 * 2
    };

    struct pcm *pcm = pcm_open(card, device, flags, &config);
    if (pcm == NULL) {
        fprintf(stderr, "failed to allocate memory for PCM\n");
        return EXIT_FAILURE;
    } else if (!pcm_is_ready(pcm)){
        pcm_close(pcm);
        fprintf(stderr, "failed to open PCM\n");
        return EXIT_FAILURE;
    }

    unsigned int frame_size = pcm_frames_to_bytes(pcm, 1);
    unsigned int frames_per_sec = pcm_get_rate(pcm);

    *frames = malloc(frame_size * frames_per_sec);
    if (*frames == NULL) {
        fprintf(stderr, "failed to allocate frames\n");
        pcm_close(pcm);
        return EXIT_FAILURE;
    }

    int read_count = pcm_readi(pcm, *frames, frames_per_sec);

    size_t byte_count = pcm_frames_to_bytes(pcm, read_count);

    pcm_close(pcm);

    return byte_count;
}

static int write_file(const void *frames, size_t size)
{
    FILE *output_file = fopen("audio.raw", "wb");
    if (output_file == NULL) {
        perror("failed to open 'audio.raw' for writing");
        return EXIT_FAILURE;
    }
    fwrite(frames, 1, size, output_file);
    fclose(output_file);
    return 0;
}
void play_sample(FILE *file, unsigned int device, unsigned int channels,
                 unsigned int rate, unsigned int bits)
{
    struct pcm_config config;
    struct pcm *pcm0;
    char *buffer;
    int size;
    int num_read;
/*
channels = 2,period_size = 8092;  buffer_size = 8092*4 = 32k
channels = 4, period_size = 4096, buffer_size = 4096*8 = 32k
channels = 4, period_size = 2048, buffer_size = 2048*8 = 16k
channels = 4, period_size = 1024, buffer_size = 1024*8 = 8k
*/
    config.channels = 4;
    config.rate = rate;
    config.period_size = 1024;//4096;//2048
    config.period_count = 1;
    if (bits == 32)
        config.format = PCM_FORMAT_S32_LE;
    else if (bits == 16)
        config.format = PCM_FORMAT_S16_LE;
    config.start_threshold = 0;
    config.stop_threshold = 0;
    config.silence_threshold = 0;

	/*0 is audiocodec, 1 is hdmiaudio, 2 is spdif*/
    pcm0 = pcm_open(1, device, PCM_OUT, &config);
    if (!pcm0 || !pcm_is_ready(pcm0)) {
		fprintf(stderr, "Unable to open PCM device %u (%s)\n", device, pcm_get_error(pcm0));
		return;
	}

    size = pcm_get_buffer_size(pcm0);
    buffer = malloc(size);
    if (!buffer) {
        fprintf(stderr, "Unable to allocate %d bytes\n", size);
        free(buffer);
        pcm_close(pcm0);
        return;
    }
    size =size;
    printf("hx-Playing sample:size:%d, %u ch, %u hz, %u bit\n", size, channels, rate, bits);

    do {
        num_read = fread(buffer, 1, size, file);
        if (num_read > 0) {
            if (pcm_write(pcm0, buffer, num_read)) {
                fprintf(stderr, "Error playing sample\n");
                break;
            }
        }
    } while (num_read > 0);

    free(buffer);
    pcm_close(pcm0);
}
Exemple #4
0
static int write_frames(const void * frames, size_t byte_count){

    unsigned int card = 0;
    unsigned int device = 0;
    int flags = PCM_OUT;

    const struct pcm_config config = {
        .channels = 2,
        .rate = 48000,
        .format = PCM_FORMAT_S32_LE,
        .period_size = 1024,
        .period_count = 2,
        .start_threshold = 1024,
        .silence_threshold = 1024 * 2,
        .stop_threshold = 1024 * 2
    };

    struct pcm * pcm = pcm_open(card, device, flags, &config);
    if (pcm == NULL) {
        fprintf(stderr, "failed to allocate memory for PCM\n");
        return -1;
    } else if (!pcm_is_ready(pcm)){
        pcm_close(pcm);
        fprintf(stderr, "failed to open PCM\n");
        return -1;
    }

    unsigned int frame_count = pcm_bytes_to_frames(pcm, byte_count);

    int err = pcm_writei(pcm, frames, frame_count);
    if (err != 0) {
      printf("error: %s\n", pcm_get_error(pcm));
    }

    pcm_close(pcm);

    return 0;
}

int main(void)
{
    void *frames;
    size_t size;

    size = read_file(&frames);
    if (size == 0) {
        return EXIT_FAILURE;
    }

    if (write_frames(frames, size) < 0) {
        return EXIT_FAILURE;
    }

    free(frames);
    return EXIT_SUCCESS;
}
Exemple #5
0
unsigned int capture_sample(FILE *file, unsigned int card, unsigned int device,
                            unsigned int channels, unsigned int rate,
                            enum pcm_format format, unsigned int period_size,
                            unsigned int period_count)
{
    struct pcm_config config;
    struct pcm *pcm;
    char *buffer;
    unsigned int size;
    unsigned int bytes_read = 0;

    memset(&config, 0, sizeof(config));
    config.channels = channels;
    config.rate = rate;
    config.period_size = period_size;
    config.period_count = period_count;
    config.format = format;
    config.start_threshold = 0;
    config.stop_threshold = 0;
    config.silence_threshold = 0;

    pcm = pcm_open(card, device, PCM_IN, &config);
    if (!pcm || !pcm_is_ready(pcm)) {
        fprintf(stderr, "Unable to open PCM device (%s)\n",
                pcm_get_error(pcm));
        return 0;
    }

    size = pcm_frames_to_bytes(pcm, pcm_get_buffer_size(pcm));
    buffer = malloc(size);
    if (!buffer) {
        fprintf(stderr, "Unable to allocate %d bytes\n", size);
        free(buffer);
        pcm_close(pcm);
        return 0;
    }

    printf("Capturing sample: %u ch, %u hz, %u bit\n", channels, rate,
           pcm_format_to_bits(format));

    while (capturing && !pcm_read(pcm, buffer, size)) {
        if (fwrite(buffer, 1, size, file) != size) {
            fprintf(stderr,"Error capturing sample\n");
            break;
        }
        bytes_read += size;
    }

    free(buffer);
    pcm_close(pcm);
    return pcm_bytes_to_frames(pcm, bytes_read);
}
Exemple #6
0
unsigned int capture_sample(FILE *file, unsigned int device,
                            unsigned int channels, unsigned int rate,
                            unsigned int bits)
{
    struct pcm_config config;
    struct pcm *pcm;
    char *buffer;
    unsigned int size;
    unsigned int bytes_read = 0;

    config.channels = channels;
    config.rate = rate;
    config.period_size = 1024;
    config.period_count = 4;
    if (bits == 32)
        config.format = PCM_FORMAT_S32_LE;
    else if (bits == 16)
        config.format = PCM_FORMAT_S16_LE;

    pcm = pcm_open(0, device, PCM_IN, &config);
    if (!pcm || !pcm_is_ready(pcm)) {
        fprintf(stderr, "Unable to open PCM device (%s)\n",
                pcm_get_error(pcm));
        return 0;
    }

    size = pcm_get_buffer_size(pcm);
    buffer = malloc(size);
    if (!buffer) {
        fprintf(stderr, "Unable to allocate %d bytes\n", size);
        free(buffer);
        pcm_close(pcm);
        return 0;
    }

    printf("Capturing sample: %u ch, %u hz, %u bit\n", channels, rate, bits);

    while (capturing && !pcm_read(pcm, buffer, size)) {
        if (fwrite(buffer, 1, size, file) != size) {
            fprintf(stderr,"Error capturing sample\n");
            break;
        }
        bytes_read += size;
    }

    free(buffer);
    pcm_close(pcm);
    return bytes_read / ((bits / 8) * channels);
}
static gboolean
gst_tinyalsa_sink_unprepare (GstAudioSink * asink)
{
  GstTinyalsaSink *sink = GST_TINYALSA_SINK (asink);

  if (pcm_stop (sink->pcm) < 0) {
    GST_ERROR_OBJECT (sink, "Could not stop device: %s",
        pcm_get_error (sink->pcm));
  }

  /* mutex with getcaps */
  GST_OBJECT_LOCK (sink);

  if (pcm_close (sink->pcm)) {
    GST_ERROR_OBJECT (sink, "Could not close device: %s",
        pcm_get_error (sink->pcm));
    return FALSE;
  }

  sink->pcm = NULL;

  gst_caps_replace (&sink->cached_caps, NULL);

  GST_OBJECT_UNLOCK (sink);

  GST_DEBUG_OBJECT (sink, "Device unprepared");

  return TRUE;
}
int muroard_driver_free(void) {
 if ( muroard_state_member(driver_vp) != NULL ) {
  pcm_close(muroard_state_member(driver_vp));
 }

 return 0;
}
Exemple #9
0
status_t AudioALSACaptureDataProviderBase::openPcmDriver(const unsigned int device)
{
    ALOGD("+%s(), pcm device = %d", __FUNCTION__, device);

    ASSERT(mPcm == NULL);
    mPcm = pcm_open(kAudioSoundCardIndex, device, PCM_IN, &mConfig);
    if (mPcm == NULL)
    {
        ALOGE("%s(), mPcm == NULL!!", __FUNCTION__);
    }
    else if (pcm_is_ready(mPcm) == false)
    {
        ALOGE("%s(), pcm_is_ready(%p) == false due to %s, close pcm.", __FUNCTION__, mPcm, pcm_get_error(mPcm));
        pcm_close(mPcm);
        mPcm = NULL;
    }
    else
    {
        pcm_start(mPcm);
    }

    ALOGD("-%s(), mPcm = %p", __FUNCTION__, mPcm);
    ASSERT(mPcm != NULL);
    return NO_ERROR;
}
Exemple #10
0
void pcm_set_16bits(int b)
{
        int open = wout ? 1 : 0;
        pcm_close();
        bits = b;
        if (open) pcm_init();
}
int proxy_open(alsa_device_proxy * proxy)
{
    alsa_device_profile* profile = proxy->profile;
    ALOGV("proxy_open(card:%d device:%d %s)", profile->card, profile->device,
          profile->direction == PCM_OUT ? "PCM_OUT" : "PCM_IN");

    if (profile->card < 0 || profile->device < 0) {
        return -EINVAL;
    }

    proxy->pcm = pcm_open(profile->card, profile->device,
            profile->direction | PCM_MONOTONIC, &proxy->alsa_config);
    if (proxy->pcm == NULL) {
        return -ENOMEM;
    }

    if (!pcm_is_ready(proxy->pcm)) {
        ALOGE("  proxy_open() pcm_open() failed: %s", pcm_get_error(proxy->pcm));
#if defined(LOG_PCM_PARAMS)
        log_pcm_config(&proxy->alsa_config, "config");
#endif
        pcm_close(proxy->pcm);
        proxy->pcm = NULL;
        return -ENOMEM;
    }

    return 0;
}
Exemple #12
0
void pcm_set_samplerate(int rate)
{
        int open = wout ? 1 : 0;
        pcm_close();
        samplerate = rate;
        if (open) pcm_init();
}
Exemple #13
0
void pcm_set_buffer_count(int count)
{
        int open = wout ? 1 : 0;
        pcm_close();
        buffers = count;
        if (open) pcm_init();
}
Exemple #14
0
void pcm_set_stereo(int stereomode)
{
        int open = wout ? 1 : 0;
        pcm_close();
        stereo = stereomode;
        if (open) pcm_init();
}
static int hdmi_out_open_pcm(hdmi_out_t *out)
{
    int card = hdmi_out_find_card();
    int dev = HDMI_PCM_DEV;
    int ret;

    TRACEM("out=%p", out);

    /* out->up must be 0 (down) */
    if (out->up) {
        ALOGE("Trying to open a PCM that's already up. "
             "This will probably deadlock... so aborting");
        return 0;
    }

    out->pcm = pcm_open(card, dev, PCM_OUT, &out->config);

    if(out->pcm && pcm_is_ready(out->pcm)) {
        out->up = 1;
        ret = 0;
    } else {
        ALOGE("cannot open HDMI pcm card %d dev %d error: %s",
              card, dev, pcm_get_error(out->pcm));
        pcm_close(out->pcm);
        out->pcm = 0;
        out->up = 0;
        ret = 1;
    }

    return ret;
}
Exemple #16
0
void emu_close()
{
        vid_close();
        pcm_close();
        loader_unload();

        emu_run = 0;
}
Exemple #17
0
EXTERN_TAG int  pcm_output_uninit()
{
	if(wfd_pcm)
		pcm_close(wfd_pcm);
	wfd_pcm = NULL;
	adec_print("pcm_output_uninit done \n");
	return 0;
}
Exemple #18
0
status_t TinyAudioStreamOut::set(
        TinyAudioHardware *hw,
        int *pFormat,
        uint32_t *pChannels,
        uint32_t *pRate)
{
    LOGI("TinyAudioStreamOut::set(): asked for rate %dHz, %d channels",
	 *pRate, *pChannels);

    struct pcm_config pcmConfig;

    memset(&pcmConfig, 0, sizeof(pcmConfig));
    pcmConfig.channels = 2;
    pcmConfig.period_size = 2048;
    pcmConfig.period_count = 12;
    pcmConfig.format = PCM_FORMAT_S16_LE;

    // Try 44.1kHz first...
    pcmConfig.rate = 44100;
    mPcm = pcm_open(0, 0, PCM_OUT, &pcmConfig);

    // ...but fall back to 48kHz
    if (!mPcm || !::pcm_is_ready(mPcm)) {
	pcm_close(mPcm);
	pcmConfig.rate = 48000;
	mPcm = pcm_open(0, 0, PCM_OUT, &pcmConfig);
    }

    if (!mPcm || !::pcm_is_ready(mPcm)) {
	LOGE("TinyAudioStreamOut::set() failed: %s\n",
	     ::pcm_get_error(mPcm));
	pcm_close(mPcm);
        return BAD_VALUE;
    }

    mSampleRate = pcmConfig.rate;
    *pRate = pcmConfig.rate;
    *pFormat = AudioSystem::PCM_16_BIT;
    *pChannels = AudioSystem::CHANNEL_OUT_STEREO;

    LOGI("TinyAudioStreamOut::set(): rate %dHz, %d channels",
	 *pRate, *pChannels);

    return NO_ERROR;
}
void AudioPlaybackLocal::releaseHw()
{
    if (mPcmHandle != NULL) {
        LOGV("releaseHw %x", (unsigned int)this);
        doStop();
        pcm_close(mPcmHandle);
        mPcmHandle = NULL;
    }
}
void proxy_close(alsa_device_proxy * proxy)
{
    ALOGV("proxy_close() [pcm:%p]", proxy->pcm);

    if (proxy->pcm != NULL) {
        pcm_close(proxy->pcm);
        proxy->pcm = NULL;
    }
}
void AudioRecordingLocal::releaseHw()
{
    if (mPcmHandle != NULL) {
        LOGV("releaseHw %x", (unsigned long)this);
        doStop();
        pcm_close(mPcmHandle);
        mPcmHandle = NULL;
    }
}
void TinyAlsaCtlPortConfig::doCloseStream(StreamDirection streamDirection)
{
    struct pcm *&streamHandle = _streamHandle[streamDirection];

    if (streamHandle) {

        pcm_close(streamHandle);
        streamHandle = NULL;
    }
}
status_t AudioALSALoopbackController::close()
{
    ALOGD("+%s()", __FUNCTION__);
    AudioAutoTimeoutLock _l(mLock);
    AudioAutoTimeoutLock _l2(*AudioALSADriverUtility::getInstance()->getStreamSramDramLock());

    mHardwareResourceManager->stopOutputDevice();

    pcm_stop(mPcmDL);
    pcm_stop(mPcmUL);
    pcm_close(mPcmDL);
    pcm_close(mPcmUL);

    mPcmDL = NULL;
    mPcmUL = NULL;

    mHardwareResourceManager->stopInputDevice(mHardwareResourceManager->getInputDevice());

    ALOGD("-%s()", __FUNCTION__);
    return NO_ERROR;
}
void AudioALSAPlaybackHandlerNormal::CloseHpImpeDancePcm(void)
{
    ALOGD("CloseHpImpeDancePcm");
    // Don't lock Sram/Dram lock here, it would be locked by caller function

    if (mHpImpeDancePcm != NULL)
    {
        pcm_stop(mHpImpeDancePcm);
        pcm_close(mHpImpeDancePcm);
        mHpImpeDancePcm = NULL;
    }
}
static int close_tinyalsa(out123_handle *ao)
{
        debug("close_tinyalsa()");

	mpg123_tinyalsa_t* ta = (mpg123_tinyalsa_t*)ao->userptr;

	if (ta->pcm)
	{
		pcm_close(ta->pcm);
	}

	return 0;
}
Exemple #26
0
status_t AudioALSACaptureDataProviderBase::closePcmDriver()
{
    ALOGD("+%s(), mPcm = %p", __FUNCTION__, mPcm);

    if (mPcm != NULL)
    {
        pcm_stop(mPcm);
        pcm_close(mPcm);
        mPcm = NULL;
    }

    ALOGD("-%s(), mPcm = %p", __FUNCTION__, mPcm);
    return NO_ERROR;
}
int hdmi_out_standby(struct audio_stream *stream)
{
    hdmi_out_t *out = (hdmi_out_t*)stream;

    TRACEM("stream=%p", stream);

    if (out->up && out->pcm) {
        out->up = 0;
        pcm_close(out->pcm);
        out->pcm = 0;
    }

    return 0;
}
Exemple #28
0
void pcm_init()
{
	int n;

	if(!oss_enabled) {
		pcm_close();
		return; //NOSOUND
	}

	/*if (!sound)
	{
		pcm.hz = 11025;
		pcm.len = 4096;
		pcm.buf = malloc(pcm.len);
		pcm.pos = 0;
		dsp = -1;
		return;
	}*/

	//if (!dsp_device) dsp_device = strdup(DSP_DEVICE);
	if(dsp<0) dsp = open(dsp_device, O_WRONLY);
	if(mixer<0) mixer=open(mixer_device, O_RDWR);

	//n = 0x80009;
	//ioctl(dsp, SNDCTL_DSP_SETFRAGMENT, &n);
	//n = AFMT_S8;

	n = AFMT_S16_LE;
	ioctl(dsp, SNDCTL_DSP_SETFMT, &n);
	n = stereo;
	//ioctl(dsp, SNDCTL_DSP_STEREO, &n);
	pcm.stereo = n;
	n++;
	ioctl(dsp, SNDCTL_DSP_CHANNELS, &n);

	n = samplerate;
	ioctl(dsp, SNDCTL_DSP_SPEED, &n);
	pcm.hz = (n * 100) / oss_speed; //FIXME TEST speed
	pcm.len = n / 60;
	pcm.buf = malloc(sizeof(short)*pcm.len);

	n = buf_len;
	ioctl(dsp, SNDCTL_DSP_SETFRAGMENT, &n);

	if (pcm.hz) snd.rate = (1<<21) / pcm.hz; //apply soundspeed now!

	n = (oss_volume << 8 )| oss_volume;
	ioctl(mixer, SOUND_MIXER_WRITE_PCM, &n);
}
int record_file(unsigned rate, unsigned channels, int fd, unsigned count)
{
    struct pcm *pcm;
    unsigned avail, xfer, bufsize;
    char *data, *next;
    int r;

    pcm = pcm_open(PCM_IN|PCM_MONO);
    if (!pcm_ready(pcm)) {
        pcm_close(pcm);
        goto fail;
    }

    bufsize = pcm_buffer_size(pcm);
    
    data = malloc(bufsize);
    if (!data) {
        fprintf(stderr,"could not allocate %d bytes\n", count);
        return -1;
    }

    while (!pcm_read(pcm, data, bufsize)) {
        if (write(fd, data, bufsize) != bufsize) {
            fprintf(stderr,"could not write %d bytes\n", bufsize);
            return -1;
        }
    }

    close(fd);
    pcm_close(pcm);
    return 0;
    
fail:
    fprintf(stderr,"pcm error: %s\n", pcm_error(pcm));
    return -1;
}
static int close_device(struct audio_stream *stream)
{
    struct stream_out *out = (struct stream_out *)stream;
    struct audio_device *adev = out ? out->dev : NULL;

    if(adev == NULL) return -ENOSYS;

    /* close the device
     * 1. if HD stream close is invoked
     * 2. if no HD stream is active.
     */
    if((adev->active_pcm) && (adev->hdaudio_active) && (out->flags & AUDIO_OUTPUT_FLAG_DIRECT)) {
        ALOGV("%s HD PCM device closed",__func__);
        pcm_close(adev->active_pcm);
        adev->active_pcm = NULL;
        adev->hdaudio_active = false;
    } else if((out->pcm) && (!adev->hdaudio_active)) {
        ALOGV("%s Default PCM device closed",__func__);
        pcm_close(out->pcm);
        out->pcm = NULL;
    }

    return 0;
}