示例#1
0
func main() {
	var data []int16
	switch len(os.Args) {
	case 1:
		log.Println("Loading white noise.")
		data = genWhiteNoise()
		break

	case 2:
		filename := os.Args[1]
		log.Printf("Loading %s\n", filename)
		data = readFile(filename)
		break

	default:
		log.Fatalf("Unexpected number of arguments: %s. Must be 1 or 2.\nUsage: go run main.go [file]\n", len(os.Args))
	}

	device, err := al.OpenDevice("", nil)
	if err != nil {
		log.Fatal(err)
	}
	defer device.Close()

	var buffers uint32 = 0
	device.GenBuffers(1, &buffers)
	device.BufferData(buffers, al.FORMAT_MONO16, unsafe.Pointer(&data[0]), int32(int(unsafe.Sizeof(data[0]))*len(data)), freq)
	var sources uint32 = 0
	device.GenSources(1, &sources)
	device.Sourcei(sources, al.BUFFER, int32(buffers))
	device.SourcePlay(sources)

	time.Sleep(time.Second * duration)

	device.DeleteSources(1, &sources)
	device.DeleteBuffers(1, &buffers)
	log.Println("Done.")
}
示例#2
0
func main() {
	var duration float64
	var files []string
	switch len(os.Args) {
	case 1:
		log.Panicf("wav player: No files specified.\nUsage: go run main.go file1 file2...\n", len(os.Args))
	default:
		files = os.Args[1:]
	}

	device, err := al.OpenDevice("", nil)
	if err != nil {
		log.Panic(err)
	}
	defer device.Close()

	var buffers = make([]uint32, len(files))
	device.GenBuffers(int32(len(buffers)), &buffers[0])
	var sources = make([]uint32, len(files))
	device.GenSources(int32(len(sources)), &sources[0])

	for i, file := range files {
		data, dur, config := readFile(file)

		if dur > duration {
			duration = dur
		}

		if config.Channels == 1 {
			device.BufferData(buffers[i], al.FORMAT_MONO16, unsafe.Pointer(&data[0]), int32(int(unsafe.Sizeof(data[0]))*len(data)), int32(config.SampleRate))
		} else {
			device.BufferData(buffers[i], al.FORMAT_STEREO16, unsafe.Pointer(&data[0]), int32(int(unsafe.Sizeof(data[0]))*len(data)), int32(config.SampleRate))
		}

		device.Sourcei(sources[i], al.BUFFER, int32(buffers[i]))
	}

	log.Println("Duration:", duration, "seconds")

	device.SourcePlayv(sources)

	for {
		stopped := 0
		for _, source := range sources {
			var state int32
			device.GetSourcei(source, al.SOURCE_STATE, &state)
			if state != al.PLAYING {
				stopped++
			}
		}
		if stopped == len(sources) {
			// everything is stopped
			break
		}
		time.Sleep(time.Second / 2)
	}

	device.DeleteSources(int32(len(sources)), &sources[0])
	device.DeleteBuffers(int32(len(buffers)), &buffers[0])
	log.Println("Done.")
}