Beispiel #1
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()
	}
}
Beispiel #2
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()
		}
	}
}