func (p *PlaybackStream) SampleRate() int {
	return int(p.createReply.Rate)
}
SampleRate returns the stream's sample rate (samples per second).
package pulse
import "github.com/jfreymuth/pulse/proto"
type PlaybackStream struct {
	c *Client
	index     uint32
	state     streamState
	underflow bool
	err       error
	front, back []byte
	requested   int
	request     chan int
	started     chan bool
	r Reader
	createRequest  proto.CreatePlaybackStream
	createReply    proto.CreatePlaybackStreamReply
	bytesPerSample int
}
const EndOfData endOfData = false
func (c *Client) NewPlayback(r Reader, opts ...PlaybackOption) (*PlaybackStream, error) {
	p := &PlaybackStream{
		c: c,
		createRequest: proto.CreatePlaybackStream{
			SinkIndex:             proto.Undefined,
			ChannelMap:            proto.ChannelMap{proto.ChannelMono},
			SampleSpec:            proto.SampleSpec{Format: r.Format(), Channels: 1, Rate: 44100},
			BufferMaxLength:       proto.Undefined,
			Corked:                true,
			BufferTargetLength:    proto.Undefined,
			BufferPrebufferLength: proto.Undefined,
			BufferMinimumRequest:  proto.Undefined,
			Properties:            proto.PropList{},
		},
		bytesPerSample: bytes(r.Format()),
		r:              r,
	}
	for _, opt := range opts {
		opt(p)
	}
	if p.createRequest.ChannelVolumes == nil {
		cvol := make(proto.ChannelVolumes, len(p.createRequest.ChannelMap))
		for i := range cvol {
			cvol[i] = 0x100
		}
		p.createRequest.ChannelVolumes = cvol
	}
	err := c.c.Request(&p.createRequest, &p.createReply)
	if err != nil {
		return nil, err
	}
	p.index = p.createReply.StreamIndex
	p.front = make([]byte, p.createReply.BufferMaxLength)
	p.back = make([]byte, p.createReply.BufferMaxLength)
	p.request = make(chan int)
	p.started = make(chan bool)
	c.mu.Lock()
	c.playback[p.index] = p
	c.mu.Unlock()
	go p.run()
	return p, nil
}
func (p *PlaybackStream) run() {
	for n := range p.request {
		if p.state != running {
			continue
		}
		p.requested += n
		for p.requested > 0 {
			n, err := p.r.Read(p.front[:p.requested])
			if n > 0 {
				p.c.c.Send(p.index, p.front[:n])
				p.requested -= n
				p.front, p.back = p.back, p.front
			}
			if err != nil {
				if err != EndOfData {
					p.err = err
				}
				p.state = idle
				break
			}
			select {
			case n = <-p.request:
				p.requested += n
			default:
			}
		}
	}
}
func (p *PlaybackStream) Start() {
	if p.state == idle {
		p.c.c.Request(&proto.FlushPlaybackStream{StreamIndex: p.index}, nil)
		p.state = running
		p.err = nil
		p.request <- int(p.createReply.BufferTargetLength)
		p.underflow = false
		p.c.c.Request(&proto.CorkPlaybackStream{StreamIndex: p.index, Corked: false}, nil)
		<-p.started
	}
}
func (p *PlaybackStream) Stop() {
	if p.state == running || p.state == paused {
		p.state = idle
	}
}
func (p *PlaybackStream) Pause() {
	if p.state == running {
		p.c.c.Request(&proto.CorkPlaybackStream{StreamIndex: p.index, Corked: true}, nil)
		p.state = paused
	}
}
func (p *PlaybackStream) Resume() {
	if p.state == paused {
		p.c.c.Request(&proto.CorkPlaybackStream{StreamIndex: p.index, Corked: false}, nil)
		p.state = running
		p.underflow = false
	}
}
func (p *PlaybackStream) Drain() {
	if p.state == running {
		p.c.c.Request(&proto.DrainPlaybackStream{StreamIndex: p.index}, nil)
	}
}
func (p *PlaybackStream) Close() {
	if !p.Closed() {
		p.c.c.Request(&proto.DeletePlaybackStream{StreamIndex: p.index}, nil)
		p.state = closed
		close(p.request)
		p.c.mu.Lock()
		delete(p.c.playback, p.index)
		p.c.mu.Unlock()
	}
}
func (p *PlaybackStream) Closed() bool { return p.state == closed || p.state == serverLost }
func (p *PlaybackStream) Running() bool { return p.state == running }
func (p *PlaybackStream) Underflow() bool { return p.underflow }
func (p *PlaybackStream) Error() error { return p.err }