Beispiel #1
0
func (so SndFileOutput) Write(sampleRate float64, channels []chan float64) (err error) {
	info := sndfile.Info{
		Samplerate: int32(sampleRate),
		Channels:   int32(len(channels)),
		Format:     so.Format,
	}

	f, err := sndfile.Open(so.Filename, sndfile.Write, &info)
	if err != nil {
		return err
	}
	defer f.Close()

	for buffer := range soundio.Interlace(channels, so.BufferSize) {
		_, err = f.WriteItems(buffer)
		if err != nil {
			return err
		}
	}

	return nil
}
Beispiel #2
0
func (so Output) Write(sampleRate float64, channels []chan float64) (err error) {
	handle := alsa.New()
	err = handle.Open(so.Device, alsa.StreamTypePlayback, alsa.ModeBlock)
	if err != nil {
		return err
	}
	defer handle.Close()

	handle.SampleFormat = alsa.SampleFormatS16LE
	handle.SampleRate = int(sampleRate)
	handle.Channels = len(channels)
	err = handle.ApplyHwParams()
	if err != nil {
		return err
	}

	byteBuffer := make([]uint8, len(channels)*so.BufferSize*2)

	for buffer := range soundio.Interlace(channels, so.BufferSize) {
		for i, x := range buffer {
			y := int16(x * 32767)
			byteBuffer[i*2] = uint8(y)
			byteBuffer[i*2+1] = uint8(y >> 8)
		}

		_, err = handle.Write(byteBuffer)
		if err != nil {
			return err
		}
	}

	err = handle.Drain()
	if err != nil {
		return err
	}

	return nil
}