示例#1
0
func main() {
	device := openal.OpenDevice("")
	context := device.CreateContext()
	context.Activate()

	//listener := new(openal.Listener)
	//listener.

	source := openal.NewSource()
	source.SetPitch(1)
	source.SetGain(1)
	source.SetPosition(0, 0, 0)
	source.SetVelocity(0, 0, 0)
	source.SetLooping(false)

	buffer := openal.NewBuffer()

	format, data, err := ReadWavFile("welcome.wav")
	if err != nil {
		panic(err)
	}

	switch format.Channels {
	case 1:
		buffer.SetData(openal.FormatMono16, data[:len(data)], int32(format.Samples))
	case 2:
		buffer.SetData(openal.FormatStereo16, data[:len(data)], int32(format.Samples))
	}

	source.SetBuffer(buffer)
	source.Play()
	for source.State() == openal.Playing {

		//loop long enough to let the wave file finish

	}
	fmt.Println(source.State())

	source.Pause()
	source.Stop()
	return
	context.Destroy()
	time.Sleep(time.Second)
}
示例#2
0
文件: sound.go 项目: velovix/paunch
// NewSound returns a new Sound object based on the provided file. As of right
// now, NewSound only supports standard WAV files.
func NewSound(filename string) (*Sound, error) {

	sound := &Sound{}

	tmpSource := al.NewSource()
	sound.source = &tmpSource
	tmpBuffer := al.NewBuffer()
	sound.buffer = &tmpBuffer

	var err error
	var info soundInfo
	split := strings.Split(filename, ".")
	if split[len(split)-1] == "wav" {
		info, err = loadWAV(filename)
	} else if split[len(split)-1] == "ogg" {
		info, err = loadOGG(filename)
	}
	if err != nil {
		return sound, err
	}

	if info.bitRate == 8 {
		if info.numChannels == 1 {
			sound.buffer.SetData(al.FormatMono8, info.data8, info.sampleRate)
		} else if info.numChannels == 2 {
			sound.buffer.SetData(al.FormatStereo8, info.data8, info.sampleRate)
		} else {
			return sound, errors.New("invalid number of channels")
		}
	} else if info.bitRate == 16 {
		if info.numChannels == 1 {
			sound.buffer.SetDataInt(al.FormatMono16, info.data16, info.sampleRate)
		} else if info.numChannels == 2 {
			sound.buffer.SetDataInt(al.FormatStereo16, info.data16, info.sampleRate)
		} else {
			return sound, errors.New("invalid number of channels")
		}
	}

	sound.source.Seti(al.AlBuffer, int32(*sound.buffer))

	return sound, nil
}
示例#3
0
文件: main.go 项目: vova616/ibxmgo
func main() {
	test := "./test2.mod"
	if len(os.Args) > 1 {
		test = os.Args[1]
	}

	f, e := os.Open(test)
	if e != nil {
		panic(e)
	}
	m, e := ibxmgo.Decode(f)
	if e != nil {
		panic(e)
	}

	ibxm, e := ibxmgo.NewIBXM(m, 48000)
	if e != nil {
		panic(e)
	}

	//f2, _ := os.Create("./output.raw")
	//ibxm.Dump(f2)
	//return
	duration := ibxm.SongDuration()

	data := make([]int32, ibxm.MixBufferLength())
	data2 := make([]int16, duration*2)

	s := time.Now()
	index := 0
	for {
		in, ended := ibxm.GetAudio(data)
		in *= 2
		for j := 0; j < in; j++ {
			x := data[j]
			if x > 32767 {
				x = 32767
			} else if x < -32768 {
				x = -32768
			}
			data2[index] = int16(x)
			index++
		}
		if ended {
			break
		}
	}

	fmt.Println(time.Since(s))

	//return
	device := openal.OpenDevice("")
	context := device.CreateContext()
	context.Activate()

	//listener := new(openal.Listener)

	source := openal.NewSource()
	source.SetPitch(1)
	source.SetGain(1)
	source.SetPosition(0, 0, 0)
	source.SetVelocity(0, 0, 0)
	source.SetLooping(false)

	buffer := openal.NewBuffer()

	buffer.SetDataInt(openal.FormatStereo16, data2, 48000)

	source.SetBuffer(buffer)
	source.Play()
	for source.State() == openal.Playing {

		//loop long enough to let the wave file finish

	}
	time.Sleep(time.Second)
	source.Pause()
	source.Stop()
	return
	context.Destroy()
	time.Sleep(time.Second)
}