Пример #1
0
func newMixer(mem *memory) (mix *mixer, err interface{}) {
	format := ao.SampleFormat{
		Bits:       16,
		Rate:       mem.config.AudioFreq,
		Channels:   2,
		ByteFormat: ao.FormatNative,
		Matrix:     "L,R",
	}

	ao.Initialize()

	id := ao.DriverID(mem.config.AudioDriver)
	if id < 0 {
		id = ao.DefaultDriverID()
	}

	var device *ao.Device
	device, err = ao.OpenLive(id, &format)
	if err != nil {
		ao.Shutdown()
		return
	}

	if mem.config.AudioBuffers < 3 {
		mem.config.AudioBuffers = 3
	}

	if mem.config.Verbose {
		info, _ := ao.DriverInfo(id)
		fmt.Println("Opened audio:")
		fmt.Printf("  driver:      [%s] %s\n", info.ShortName, info.Name)
		fmt.Printf("  rate:        %dHz\n", format.Rate)
		fmt.Printf("  channels:    %d\n", format.Channels)
		fmt.Printf("  buffer size: %d samples\n", 1024)
		fmt.Printf("  buffers:     %d\n", mem.config.AudioBuffers)
	}

	mix = &mixer{SampleFormat: format, dev: device, memory: mem}

	mix.buf = make([][]int16, mem.config.AudioBuffers)
	for i := 0; i < len(mix.buf); i++ {
		mix.buf[i] = make([]int16, 1024*2)
	}

	mix.ch4.initialize()

	mix.send = make(chan []int16, len(mix.buf)-2)
	mix.status = make(chan bool)
	mix.quit = make(chan int)

	go mix.runAudio()

	return mix, nil
}
Пример #2
0
func (mix *mixer) runAudio() {
	pause := false
	for {
		select {
		case buf := <-mix.send:
			if !pause {
				mix.dev.Play16(buf)
			}
		case pause = <-mix.status:
			// okay, pause updated
		case <-mix.quit:
			mix.dev.Close()
			ao.Shutdown()
			mix.quit <- 1
			return
		}
	}
}