func (w *audioWriter) streamWriter(stream *portAudioStream) {
	defer w.wg.Done()
	defer stream.Close()
	buffer := make([]int16, audioOutputBufferSize)
	output := buffer[:]
	for {
		var input audio
		select {
		case input = <-w.input:
		case <-w.quit:
			return
		}
		err := stream.Stream(&output, input.format.Channels, input.format.SampleRate)
		if err != nil {
			panic(err)
		}
		i := 0
		for i < len(input.frames) {
			j := 0
			for j < len(buffer) && i < len(input.frames) {
				buffer[j] = int16(input.frames[i]) | int16(input.frames[i+1])<<8
				j += 1
				i += 2
			}
			output = buffer[:j]
			stream.Write()
		}
	}
}
streamWriter reads data from the input buffer and writes it to the output
portaudio buffer.
package main
import (
	"flag"
	"fmt"
	"io/ioutil"
	"log"
	"os"
	"os/signal"
	"path"
	"strings"
	"sync"
	"syscall"
	"time"
	"code.google.com/p/portaudio-go/portaudio"
	"github.com/op/go-libspotify/spotify"
	"github.com/visionmedia/go-spin"
)
var (
	appKeyPath = flag.String("key", "spotify_appkey.key", "path to app.key")
	username   = flag.String("username", "o.p", "spotify username")
	password   = flag.String("password", "", "spotify password")
	debug      = flag.Bool("debug", false, "debug output")
)
var (
	audioInputBufferSize = 8
	audioOutputBufferSize = 8192
)
type audio struct {
	format spotify.AudioFormat
	frames []byte
}
type audioWriter struct {
	input chan audio
	quit  chan bool
	wg    sync.WaitGroup
}
func newAudioWriter() (*audioWriter, error) {
	w := &audioWriter{
		input: make(chan audio, audioInputBufferSize),
		quit:  make(chan bool, 1),
	}
	stream, err := newPortAudioStream()
	if err != nil {
		return w, err
	}
	w.wg.Add(1)
	go w.streamWriter(stream)
	return w, nil
}
func (w *audioWriter) Close() error {
	select {
	case w.quit <- true:
	default:
	}
	w.wg.Wait()
	return nil
}
func (w *audioWriter) WriteAudio(format spotify.AudioFormat, frames []byte) int {
	select {
	case w.input <- audio{format, frames}:
		return len(frames)
	default:
		return 0
	}
}