Ejemplo n.º 1
0
// MaxSampleRate returns the maximum samplerate possible for the device
func (handle *Handle) MaxSampleRate() (int, error) {

	var cHwParams *C.snd_pcm_hw_params_t

	err := C.snd_pcm_hw_params_malloc(&cHwParams)
	if err < 0 {
		return 0, errors.New(fmt.Sprintf("Cannot allocate hardware parameter structure. %s",
			strError(err)))
	}

	err = C.snd_pcm_hw_params_any(handle.cHandle, cHwParams)
	if err < 0 {
		return 0, errors.New(fmt.Sprintf("Cannot initialize hardware parameter structure. %s",
			strError(err)))
	}

	err = C.snd_pcm_hw_params_set_rate_resample(handle.cHandle, cHwParams, 0)
	if err < 0 {
		return 0, errors.New(fmt.Sprintf("Cannot restrict configuration space to contain only real hardware rates. %s",
			strError(err)))
	}

	var maxRate C.uint
	var dir C.int

	err = C.snd_pcm_hw_params_get_rate_max(cHwParams, &maxRate, &dir)
	if err < 0 {
		return 0, errors.New(fmt.Sprintf("Retrieving maximum samplerate failed. %s", err))
	}

	C.snd_pcm_hw_params_free(cHwParams)

	return int(maxRate), nil

}
Ejemplo n.º 2
0
// ApplyHwParams changes ALSA hardware parameters for the current stream.
func (handle *Handle) ApplyHwParams() error {
	var cHwParams *C.snd_pcm_hw_params_t

	err := C.snd_pcm_hw_params_malloc(&cHwParams)
	if err < 0 {
		return errors.New(fmt.Sprintf("Cannot allocate hardware parameter structure. %s",
			strError(err)))
	}

	err = C.snd_pcm_hw_params_any(handle.cHandle, cHwParams)
	if err < 0 {
		return errors.New(fmt.Sprintf("Cannot initialize hardware parameter structure. %s",
			strError(err)))
	}

	err = C.snd_pcm_hw_params_set_access(handle.cHandle, cHwParams, C.SND_PCM_ACCESS_RW_INTERLEAVED)
	if err < 0 {
		return errors.New(fmt.Sprintf("Cannot set access type. %s",
			strError(err)))
	}

	err = C.snd_pcm_hw_params_set_format(handle.cHandle, cHwParams, _Ctype_snd_pcm_format_t(handle.SampleFormat))
	if err < 0 {
		return errors.New(fmt.Sprintf("Cannot set sample format. %s",
			strError(err)))
	}

	var cSampleRate _Ctype_uint = _Ctype_uint(handle.SampleRate)
	err = C.snd_pcm_hw_params_set_rate_near(handle.cHandle, cHwParams, &cSampleRate, nil)
	if err < 0 {
		return errors.New(fmt.Sprintf("Cannot set sample rate. %s",
			strError(err)))
	}

	err = C.snd_pcm_hw_params_set_channels(handle.cHandle, cHwParams, _Ctype_uint(handle.Channels))
	if err < 0 {
		return errors.New(fmt.Sprintf("Cannot set number of channels. %s",
			strError(err)))
	}

	// Drain current data and make sure we aren't underrun.
	C.snd_pcm_drain(handle.cHandle)

	err = C.snd_pcm_hw_params(handle.cHandle, cHwParams)
	if err < 0 {
		return errors.New(fmt.Sprintf("Cannot set hardware parameters. %s",
			strError(err)))
	}

	C.snd_pcm_hw_params_free(cHwParams)

	return nil
}
Ejemplo n.º 3
0
// ApplyHwParams changes ALSA hardware parameters for the current stream.
func (handle *Handle) ApplyHwParams() error {
	var cHwParams *C.snd_pcm_hw_params_t

	err := C.snd_pcm_hw_params_malloc(&cHwParams)
	if err < 0 {
		return errors.New(fmt.Sprintf("Cannot allocate hardware parameter structure. %s",
			strError(err)))
	}

	err = C.snd_pcm_hw_params_any(handle.cHandle, cHwParams)
	if err < 0 {
		return errors.New(fmt.Sprintf("Cannot initialize hardware parameter structure. %s",
			strError(err)))
	}

	err = C.snd_pcm_hw_params_set_access(handle.cHandle, cHwParams, C.SND_PCM_ACCESS_RW_INTERLEAVED)
	if err < 0 {
		return errors.New(fmt.Sprintf("Cannot set access type. %s",
			strError(err)))
	}

	err = C.snd_pcm_hw_params_set_format(handle.cHandle, cHwParams, C.snd_pcm_format_t(handle.SampleFormat))
	if err < 0 {
		return errors.New(fmt.Sprintf("Cannot set sample format. %s",
			strError(err)))
	}

	var cSampleRate _Ctype_uint = _Ctype_uint(handle.SampleRate)
	err = C.snd_pcm_hw_params_set_rate_near(handle.cHandle, cHwParams, &cSampleRate, nil)
	if err < 0 {
		return errors.New(fmt.Sprintf("Cannot set sample rate. %s",
			strError(err)))
	}

	err = C.snd_pcm_hw_params_set_channels(handle.cHandle, cHwParams, _Ctype_uint(handle.Channels))
	if err < 0 {
		return errors.New(fmt.Sprintf("Cannot set number of channels. %s",
			strError(err)))
	}

	if handle.Periods > 0 {
		// Set number of periods. Periods used to be called fragments.
		/*err = C.snd_pcm_hw_params_set_periods(handle.cHandle, cHwParams, _Ctype_uint(handle.Periods), 0)
		if err < 0 {
			return os.NewError(fmt.Sprintf("Cannot set number of periods. %s",
				strError(err)))
		}*/

		var cPeriods _Ctype_uint = _Ctype_uint(handle.Periods)
		var cDir _Ctype_int = 0 // Exact value is <,=,> the returned one following dir (-1,0,1)
		err = C.snd_pcm_hw_params_set_periods_near(handle.cHandle, cHwParams, &cPeriods, &cDir)
		if err < 0 {
			return errors.New(fmt.Sprintf("Cannot set number of periods. %s",
				strError(err)))
		}
	}

	if handle.Buffersize > 0 {
		// Set buffer size (in frames). The resulting latency is given by
		// latency = periodsize * periods / (rate * bytes_per_frame)
		/*err = C.snd_pcm_hw_params_set_buffer_size(handle.cHandle, cHwParams, _Ctypedef_snd_pcm_uframes_t(handle.Buffersize))
		if err < 0 {
			return os.NewError(fmt.Sprintf("Cannot set buffersize. %s",
				strError(err)))
		}*/

		var cBuffersize C.snd_pcm_uframes_t = C.snd_pcm_uframes_t(handle.Buffersize)
		err = C.snd_pcm_hw_params_set_buffer_size_near(handle.cHandle, cHwParams, &cBuffersize)
		if err < 0 {
			return errors.New(fmt.Sprintf("Cannot set buffersize. %s",
				strError(err)))
		}

	}

	// Drain current data and make sure we aren't underrun.
	C.snd_pcm_drain(handle.cHandle)

	err = C.snd_pcm_hw_params(handle.cHandle, cHwParams)
	if err < 0 {
		return errors.New(fmt.Sprintf("Cannot set hardware parameters. %s",
			strError(err)))
	}

	C.snd_pcm_hw_params_free(cHwParams)

	return nil
}
Ejemplo n.º 4
0
func (d *device) createDevice(deviceName string, channels int, format Format, rate int, playback bool, bufferParams BufferParams) (err error) {
	deviceCString := C.CString(deviceName)
	defer C.free(unsafe.Pointer(deviceCString))
	var ret C.int
	if playback {
		ret = C.snd_pcm_open(&d.h, deviceCString, C.SND_PCM_STREAM_PLAYBACK, 0)
	} else {
		ret = C.snd_pcm_open(&d.h, deviceCString, C.SND_PCM_STREAM_CAPTURE, 0)
	}
	if ret < 0 {
		return fmt.Errorf("could not open ALSA device %s", deviceName)
	}
	runtime.SetFinalizer(d, (*device).Close)
	var hwParams *C.snd_pcm_hw_params_t
	ret = C.snd_pcm_hw_params_malloc(&hwParams)
	if ret < 0 {
		return createError("could not alloc hw params", ret)
	}
	defer C.snd_pcm_hw_params_free(hwParams)
	ret = C.snd_pcm_hw_params_any(d.h, hwParams)
	if ret < 0 {
		return createError("could not set default hw params", ret)
	}
	ret = C.snd_pcm_hw_params_set_access(d.h, hwParams, C.SND_PCM_ACCESS_RW_INTERLEAVED)
	if ret < 0 {
		return createError("could not set access params", ret)
	}
	ret = C.snd_pcm_hw_params_set_format(d.h, hwParams, C.snd_pcm_format_t(format))
	if ret < 0 {
		return createError("could not set format params", ret)
	}
	ret = C.snd_pcm_hw_params_set_channels(d.h, hwParams, C.uint(channels))
	if ret < 0 {
		return createError("could not set channels params", ret)
	}
	ret = C.snd_pcm_hw_params_set_rate(d.h, hwParams, C.uint(rate), 0)
	if ret < 0 {
		return createError("could not set rate params", ret)
	}
	var bufferSize = C.snd_pcm_uframes_t(bufferParams.BufferFrames)
	if bufferParams.BufferFrames == 0 {
		// Default buffer size: max buffer size
		ret = C.snd_pcm_hw_params_get_buffer_size_max(hwParams, &bufferSize)
		if ret < 0 {
			return createError("could not get buffer size", ret)
		}
	}
	ret = C.snd_pcm_hw_params_set_buffer_size_near(d.h, hwParams, &bufferSize)
	if ret < 0 {
		return createError("could not set buffer size", ret)
	}
	// Default period size: 1/8 of a second
	var periodFrames = C.snd_pcm_uframes_t(rate / 8)
	if bufferParams.PeriodFrames > 0 {
		periodFrames = C.snd_pcm_uframes_t(bufferParams.PeriodFrames)
	} else if bufferParams.Periods > 0 {
		periodFrames = C.snd_pcm_uframes_t(int(bufferSize) / bufferParams.Periods)
	}
	ret = C.snd_pcm_hw_params_set_period_size_near(d.h, hwParams, &periodFrames, nil)
	if ret < 0 {
		return createError("could not set period size", ret)
	}
	var periods = C.uint(0)
	ret = C.snd_pcm_hw_params_get_periods(hwParams, &periods, nil)
	if ret < 0 {
		return createError("could not get periods", ret)
	}
	ret = C.snd_pcm_hw_params(d.h, hwParams)
	if ret < 0 {
		return createError("could not set hw params", ret)
	}
	d.frames = int(periodFrames)
	d.Channels = channels
	d.Format = format
	d.Rate = rate
	d.BufferParams.BufferFrames = int(bufferSize)
	d.BufferParams.PeriodFrames = int(periodFrames)
	d.BufferParams.Periods = int(periods)
	return
}