Esempio n. 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()
	}
}
Esempio n. 2
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()
	}()
}
Esempio 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()
}
Esempio n. 4
0
func ExampleMonitor() {
	const (
		frequency    = 44100
		format       = openal.FormatStereo16
		captureSize  = 512
		buffersCount = 10
	)
	mic := openal.CaptureOpenDevice("", frequency, format, frequency*2)
	mic.CaptureStart()
	defer mic.CloseDevice()

	device := openal.OpenDevice("")
	defer device.CloseDevice()

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

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

	buffers := openal.NewBuffers(buffersCount)
	samples := make([]byte, captureSize*format.SampleSize())

	start := time.Now()
	for time.Since(start) < time.Second { // play for 1 second
		if err := openal.Err(); err != nil {
			fmt.Println("error:", err)
			return
		}
		// Get any free buffers
		if prcessed := source.BuffersProcessed(); prcessed > 0 {
			buffersNew := make(openal.Buffers, prcessed)
			source.UnqueueBuffers(buffersNew)
			buffers = append(buffers, buffersNew...)
		}
		if len(buffers) == 0 {
			continue
		}

		if mic.CapturedSamples() >= captureSize {
			mic.CaptureTo(samples)
			buffer := buffers[len(buffers)-1]
			buffers = buffers[:len(buffers)-1]
			buffer.SetData(format, samples, frequency)
			source.QueueBuffer(buffer)

			// If we have enough buffers, start playing
			if source.State() != openal.Playing {
				if source.BuffersQueued() > 2 {
					source.Play()
				}
			}
		}
	}
	fmt.Println(source.State())
	// Output: Playing
}
Esempio 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()
}
Esempio 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()
}
Esempio n. 7
0
func main() {
	music := openal.NewSource()
	music.SetLooping(false)
	if game.music {
		go play_music(music)
		musicThread = true
	}
	for game.loop {
		get_event()
		logic(music)
		renderer.Clear()
		render()
		renderer.Present()
		sdl.Delay(delay)
	}
}
Esempio n. 8
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)
}
Esempio n. 9
0
func NewPlayer(sampleRate, channelNum, bytesPerSample int) (*Player, error) {
	d := openal.OpenDevice("")
	if d == nil {
		return nil, fmt.Errorf("driver: OpenDevice must not return nil")
	}
	c := d.CreateContext()
	if c == nil {
		return nil, fmt.Errorf("driver: CreateContext must not return nil")
	}
	// Don't check openal.Err until making the current context is done.
	// Linux might fail this check even though it succeeds (#204).
	c.Activate()
	if err := openal.Err(); err != nil {
		return nil, fmt.Errorf("driver: Activate: %v", err)
	}
	s := openal.NewSource()
	if err := openal.Err(); err != nil {
		return nil, fmt.Errorf("driver: NewSource: %v", err)
	}
	p := &Player{
		alDevice:   d,
		alSource:   s,
		alBuffers:  []openal.Buffer{},
		sampleRate: sampleRate,
		alFormat:   alFormat(channelNum, bytesPerSample),
	}
	runtime.SetFinalizer(p, (*Player).Close)

	bs := openal.NewBuffers(maxBufferNum)
	const bufferSize = 1024
	emptyBytes := make([]byte, bufferSize)
	for _, b := range bs {
		// Note that the third argument of only the first buffer is used.
		b.SetData(p.alFormat, emptyBytes, int32(p.sampleRate))
		p.alBuffers = append(p.alBuffers, b)
	}
	p.alSource.Play()
	return p, nil
}
Esempio n. 10
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
}