Exemplo n.º 1
0
func play_music(music openal.Source) {
	file := "music" + slash + "1.wav"
	buffer := openal.NewBuffer()
	data, _ := ioutil.ReadFile(file)
	buffer.SetData(openal.FormatStereo16, data, 44100)
	music.SetBuffer(buffer)
	music.Play()
	for true {
		time.Sleep(1 * time.Second)
		if music.State() == openal.Stopped {
			buffer := openal.NewBuffer()
			last := ""
			switch len(tracklist) {
			case 1:
				file = tracklist[0]
				last = file
				tracklist = []string{"music" + slash + "1.wav", "music" + slash + "2.wav", "music" + slash + "3.wav"}
			default:
				i := randomInt(len(tracklist))
				file = tracklist[i]
				for file == last {
					i := randomInt(len(tracklist))
					file = tracklist[i]
				}
				tracklist = append(tracklist[:i], tracklist[i+1:]...)
			}
			data, _ := ioutil.ReadFile(file)
			buffer.SetData(openal.FormatStereo16, data, 44100)
			music.SetBuffer(buffer)
			music.Play()
		}
	}
}
Exemplo n.º 2
0
func (s *Stream) OnAudioPacket(e *gumble.AudioPacketEvent) {
	packet := e.AudioPacket
	samples := len(packet.PositionalAudioBuffer.AudioBuffer)
	if samples*2 > cap(s.buffer) {
		return
	}
	var source openal.Source
	if userSource, ok := s.userStreams[packet.Sender.Session]; !ok {
		source = openal.NewSource()
		s.userStreams[packet.Sender.Session] = source
	} else {
		source = userSource
	}

	for i, value := range packet.PositionalAudioBuffer.AudioBuffer {
		binary.LittleEndian.PutUint16(s.buffer[i*2:], uint16(value))
	}

	var buffer openal.Buffer
	for source.BuffersProcessed() > 0 {
		openal.DeleteBuffer(source.UnqueueBuffer())
	}
	buffer = openal.NewBuffer()
	buffer.SetData(openal.FormatMono16, s.buffer[0:samples*2], gumble.AudioSampleRate)
	source.QueueBuffer(buffer)

	if source.State() != openal.Playing {
		source.Play()
	}
}
Exemplo n.º 3
0
func main() {
	device := openal.OpenDevice("")
	context := device.CreateContext()
	context.Activate()

	//listener := new(openal.Listener)

	source := openal.NewSource()
	//source.SetPitch(1)
	//source.SetGain(1)
	//position := &openal.Vector{0, 0, 0}
	//velocity := &openal.Vector{0, 0, 0}
	//source.SetPosition(position)
	//source.SetVelocity(velocity)
	source.SetLooping(false)

	buffer := openal.NewBuffer()

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

	buffer.SetData(openal.FormatMono16, data, 44100)

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

		//loop long enough to let the wave file finish

	}
	source.Pause()
	context.Destroy()
}
Exemplo n.º 4
0
func (s *Stream) OnAudioStream(e *gumble.AudioStreamEvent) {
	go func() {
		source := openal.NewSource()
		var raw [gumble.AudioMaximumFrameSize * 2]byte
		for packet := range e.C {
			samples := len(packet.AudioBuffer)
			if samples > cap(raw) {
				continue
			}
			for i, value := range packet.AudioBuffer {
				binary.LittleEndian.PutUint16(raw[i*2:], uint16(value))
			}
			for source.BuffersProcessed() > 0 {
				source.UnqueueBuffer().Delete()
			}
			buffer := openal.NewBuffer()
			buffer.SetData(openal.FormatMono16, raw[:samples*2], gumble.AudioSampleRate)
			source.QueueBuffer(buffer)
			if source.State() != openal.Playing {
				source.Play()
			}
		}
		for source.BuffersProcessed() > 0 {
			source.UnqueueBuffer().Delete()
		}
		source.Delete()
	}()
}
Exemplo n.º 5
0
func play_crash() {
	menu_click := openal.NewSource()
	menu_click.SetLooping(false)
	file := "sounds" + slash + "crash.wav"
	buffer := openal.NewBuffer()
	data, _ := ioutil.ReadFile(file)
	buffer.SetData(openal.FormatStereo16, data, 44100)
	menu_click.SetBuffer(buffer)
	menu_click.Play()
}
Exemplo n.º 6
0
func play_explosion() {
	menu_click := openal.NewSource()
	menu_click.SetLooping(false)
	file := "sounds" + slash + "bomb.wav"
	buffer := openal.NewBuffer()
	data, _ := ioutil.ReadFile(file)
	buffer.SetData(openal.FormatStereo16, data, 11025)
	menu_click.SetBuffer(buffer)
	menu_click.Play()
}
Exemplo n.º 7
0
func main() {
	d := openal.OpenDevice(openal.GetString(openal.DefaultDeviceSpecifier))
	ctx := d.CreateContext()
	ctx.Activate()

	s := openal.NewSource()
	stereo := newStereo()
	var arr = make([][2]int16, sampleRate)
	for i := 0; i < 3; i++ {
		b := openal.NewBuffer()
		stereo.processAudio(arr)
		b.SetDataStereo16(arr, sampleRate)
		s.QueueBuffer(b)
	}

	s.Play()
	time.Sleep(5 * time.Second)
}
Exemplo n.º 8
0
func ExamplePlay() {
	device := openal.OpenDevice("")
	defer device.CloseDevice()

	context := device.CreateContext()
	defer context.Destroy()
	context.Activate()

	source := openal.NewSource()
	defer source.Pause()
	source.SetLooping(false)

	buffer := openal.NewBuffer()

	if err := openal.Err(); err != nil {
		fmt.Println(err)
		return
	}

	data, err := ioutil.ReadFile("data/welcome.wav")
	if err != nil {
		fmt.Println(err)
		return
	}

	buffer.SetData(openal.FormatMono16, data, 44100)

	source.SetBuffer(buffer)
	source.Play()
	for source.State() == openal.Playing {
		// loop long enough to let the wave file finish
		time.Sleep(time.Millisecond * 100)
	}
	source.Delete()
	fmt.Println("sound played")
	// Output: sound played
}