示例#1
0
// NewPlayer returns a new Player.
// It initializes the underlying audio devices and the related resources.
// If zero values are provided for format and sample rate values, the player
// determines them from the source's WAV header.
// An error is returned if the format and sample rate can't be determined.
func NewPlayer(src ReadSeekCloser, format Format, samplesPerSecond int64) (*Player, error) {
	if err := al.OpenDevice(); err != nil {
		return nil, err
	}
	if err := createContext(); err != nil {
		return nil, err
	}
	s := al.GenSources(1)
	if code := al.Error(); code != 0 {
		return nil, fmt.Errorf("audio: cannot generate an audio source [err=%x]", code)
	}
	p := &Player{
		t:      &track{format: format, src: src, samplesPerSecond: samplesPerSecond},
		source: s[0],
	}
	if err := p.discoverHeader(); err != nil {
		return nil, err
	}
	if p.t.format == 0 {
		return nil, errors.New("audio: cannot determine the format")
	}
	if p.t.samplesPerSecond == 0 {
		return nil, errors.New("audio: cannot determine the sample rate")
	}
	return p, nil
}
示例#2
0
// InitializeSound Initializes sound
func InitializeSound(numKeys int) {
	var i float64
	data = []byte{}
	for i = 0; i < 100000; i = i + 8 {
		data = append(data, byte(128+127*math.Sin(i)))
	}

	err := al.OpenDevice()
	if err != nil {
		fmt.Println(err)
	}

	sources = al.GenSources(numKeys)
	buffers = al.GenBuffers(numKeys)
	secSources = al.GenSources(numKeys)
	secBuffers = al.GenBuffers(numKeys)

	for j := 0; j < numKeys; j++ {
		buffers[j].BufferData(al.FormatMono8, data, int32(100*(numKeys-j)))
		secBuffers[j].BufferData(al.FormatMono8, data, 2*int32(100*(numKeys-j)))
		sources[j].QueueBuffers(buffers[j : j+1]...)
		secSources[j].QueueBuffers(buffers[j : j+1]...)
	}

}
示例#3
0
func NewPlayer(sampleRate, channelNum, bytesPerSample int) (*Player, error) {
	var p *Player
	if err := al.OpenDevice(); err != nil {
		return nil, fmt.Errorf("driver: OpenAL initialization failed: %v", err)
	}
	s := al.GenSources(1)
	if e := al.Error(); e != 0 {
		return nil, fmt.Errorf("driver: al.GenSources error: %d", e)
	}
	p = &Player{
		alSource:   s[0],
		alBuffers:  []al.Buffer{},
		sampleRate: sampleRate,
		alFormat:   alFormat(channelNum, bytesPerSample),
	}
	runtime.SetFinalizer(p, (*Player).Close)

	bs := al.GenBuffers(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.BufferData(p.alFormat, emptyBytes, int32(p.sampleRate))
		p.alSource.QueueBuffers(b)
	}
	al.PlaySources(p.alSource)
	return p, nil
}
示例#4
0
文件: al.go 项目: rakyll/GCSolutions
func OpenDevice(buflen int) error {
	if err := al.OpenDevice(); err != nil {
		return fmt.Errorf("snd/al: open device failed: %s", err)
	}
	if buflen == 0 || buflen&(buflen-1) != 0 {
		return fmt.Errorf("snd/al: buflen(%v) not a power of 2", buflen)
	}
	hwa = &openal{buf: &Buffer{size: buflen}}
	return nil
}
示例#5
0
// newPianoKey creates a PianoKey with color and sound.
func newPianoKey(glctx gl.Context, keyColor util.RGBColor, note util.KeyNote) *PianoKey {
	key := new(PianoKey)
	key.keyColor = keyColor
	// Create buffer
	key.glBuf = glctx.CreateBuffer()
	glctx.BindBuffer(gl.ARRAY_BUFFER, key.glBuf)
	// Generate sound
	_ = al.OpenDevice()
	key.soundBuffers = al.GenBuffers(1)
	key.soundSources = al.GenSources(1)
	key.soundBuffers[0].BufferData(al.FormatStereo8, audio.GenSound(note), audio.SampleRate)
	key.soundSources[0].QueueBuffers(key.soundBuffers)
	return key
}
示例#6
0
func NewContext(oscillator model.Oscillator) *Context {
	if err := al.OpenDevice(); err != nil {
		log.Fatal(err)
	}
	s := al.GenSources(1)
	if code := al.Error(); code != 0 {
		log.Fatalln("openal error:", code)
	}
	return &Context{
		source:     s[0],
		queue:      []al.Buffer{},
		oscillator: oscillator,
	}
}
示例#7
0
func NewContext(oscilator Oscilator) *Context {
	if err := al.OpenDevice(); err != nil {
		log.Fatal(err)
	}
	s := al.GenSources(1)
	if code := al.Error(); code != 0 {
		log.Fatalln("openal error:", code)
	}
	//s[0].SetGain(s[0].MaxGain())
	//s[0].SetPosition(al.ListenerPosition())
	return &Context{
		source:    s[0],
		queue:     []al.Buffer{},
		oscilator: oscilator,
	}
}
示例#8
0
func onStart(glctx gl.Context) {
	var err error
	program, err = glutil.CreateProgram(glctx, vertexShader, fragmentShader)
	if err != nil {
		log.Printf("error creating GL program: %v", err)
		return
	}

	glctx.Enable(gl.DEPTH_TEST)

	triBuf = glctx.CreateBuffer()
	glctx.BindBuffer(gl.ARRAY_BUFFER, triBuf)
	glctx.BufferData(gl.ARRAY_BUFFER, triangleData, gl.STATIC_DRAW)

	position = glctx.GetAttribLocation(program, "vPos")
	color = glctx.GetAttribLocation(program, "vCol")
	normals = glctx.GetAttribLocation(program, "vNorm")

	projection = glctx.GetUniformLocation(program, "proj")
	view = glctx.GetUniformLocation(program, "view")
	model = glctx.GetUniformLocation(program, "model")
	tint = glctx.GetUniformLocation(program, "tint")
	normalMatrix = glctx.GetUniformLocation(program, "normalMatrix")
	lightIntensity = glctx.GetUniformLocation(program, "light.intensities")
	lightPos = glctx.GetUniformLocation(program, "light.position")

	arcball = NewArcBall(mgl32.Vec3{0, 0, 0}, mgl32.Vec3{0, 10, 10}, mgl32.Vec3{0, 1, 0})

	white = mgl32.Vec4{1.0, 1.0, 1.0, 1.0}
	red = mgl32.Vec4{1.0, 0.0, 0.0, 1.0}

	lastUpdate = time.Now()

	images = glutil.NewImages(glctx)
	fps = debug.NewFPS(images)

	err = al.OpenDevice()
	if err != nil {
		log.Printf("Err: %+v", err)
	}
	al.SetListenerPosition(al.Vector{0, 0, 0})
	al.SetListenerGain(1.0)
	piano = NewPiano()
}
示例#9
0
func (as *AudioSystem) New(*ecs.World) {
	as.System = ecs.NewSystem()

	if as.HeightModifier == 0 {
		as.HeightModifier = defaultHeightModifier
	}

	if err := al.OpenDevice(); err != nil {
		log.Println("Error initializing AudioSystem:", err)
		return
	}

	Mailbox.Listen("CameraMessage", func(msg Message) {
		_, ok := msg.(CameraMessage)
		if !ok {
			return
		}

		// Hopefully not that much of an issue, when we receive it before the CameraSystem does
		// TODO: but it is when the CameraMessage is not Incremental (i.e. the changes are big)
		al.SetListenerPosition(al.Vector{cam.X() / Width(), cam.Y() / Height(), cam.Z() * as.HeightModifier})
	})
}