Kaynağa Gözat

Clean up audio package and prepare for WASM integration

Daniel Salvadori 6 yıl önce
ebeveyn
işleme
dc37b3e479
3 değiştirilmiş dosya ile 79 ekleme ve 106 silme
  1. 69 0
      audio/listener-browser.go
  2. 10 10
      audio/listener.go
  3. 0 96
      audio/wave.go

+ 69 - 0
audio/listener-browser.go

@@ -0,0 +1,69 @@
+// Copyright 2016 The G3N Authors. All rights reserved.
+// Use of this source code is governed by a BSD-style
+// license that can be found in the LICENSE file.
+
+// +build wasm
+
+package audio
+
+import (
+	"github.com/g3n/engine/core"
+	"github.com/g3n/engine/gls"
+	"github.com/g3n/engine/math32"
+)
+
+// Listener is an audio listener positioned in space.
+type Listener struct {
+	core.Node
+}
+
+// NewListener creates a Listener object.
+func NewListener() *Listener {
+
+	l := new(Listener)
+	l.Node.Init(l)
+	return l
+}
+
+// SetVelocity sets the velocity of the listener with x, y, z components.
+func (l *Listener) SetVelocity(vx, vy, vz float32) {
+
+	// TODO
+}
+
+// SetVelocityVec sets the velocity of the listener with a vector.
+func (l *Listener) SetVelocityVec(v *math32.Vector3) {
+
+	// TODO
+}
+
+// Velocity returns the velocity of the listener as x, y, z components.
+func (l *Listener) Velocity() (float32, float32, float32) {
+
+	// TODO
+}
+
+// VelocityVec returns the velocity of the listener as a vector.
+func (l *Listener) VelocityVec() math32.Vector3 {
+
+	// TODO
+}
+
+// SetGain sets the gain of the listener.
+func (l *Listener) SetGain(gain float32) {
+
+	// TODO
+}
+
+// Gain returns the gain of the listener.
+func (l *Listener) Gain() float32 {
+
+	// TODO
+}
+
+// Render is called by the renderer at each frame.
+// Updates the position and orientation of the listener.
+func (l *Listener) Render(gl *gls.GLS) {
+
+	// TODO
+}

+ 10 - 10
audio/listener.go

@@ -13,12 +13,12 @@ import (
 	"github.com/g3n/engine/math32"
 )
 
-// Listener is an audio listener positioned in space
+// Listener is an audio listener positioned in space.
 type Listener struct {
 	core.Node
 }
 
-// NewListener returns a pointer to a new Listener object.
+// NewListener creates a Listener object.
 func NewListener() *Listener {
 
 	l := new(Listener)
@@ -26,45 +26,45 @@ func NewListener() *Listener {
 	return l
 }
 
-// SetVelocity sets the velocity of the listener with x, y, z components
+// SetVelocity sets the velocity of the listener with x, y, z components.
 func (l *Listener) SetVelocity(vx, vy, vz float32) {
 
 	al.Listener3f(al.Velocity, vx, vy, vz)
 }
 
-// SetVelocityVec sets the velocity of the listener with a vector
+// SetVelocityVec sets the velocity of the listener with a vector.
 func (l *Listener) SetVelocityVec(v *math32.Vector3) {
 
 	al.Listener3f(al.Velocity, v.X, v.Y, v.Z)
 }
 
-// Velocity returns the velocity of the listener as x, y, z components
+// Velocity returns the velocity of the listener as x, y, z components.
 func (l *Listener) Velocity() (float32, float32, float32) {
 
 	return al.GetListener3f(al.Velocity)
 }
 
-// VelocityVec returns the velocity of the listener as a vector
+// VelocityVec returns the velocity of the listener as a vector.
 func (l *Listener) VelocityVec() math32.Vector3 {
 
 	vx, vy, vz := al.GetListener3f(al.Velocity)
 	return math32.Vector3{vx, vy, vz}
 }
 
-// SetGain sets the gain of the listener
+// SetGain sets the gain of the listener.
 func (l *Listener) SetGain(gain float32) {
 
 	al.Listenerf(al.Gain, gain)
 }
 
-// Gain returns the gain of the listener
+// Gain returns the gain of the listener.
 func (l *Listener) Gain() float32 {
 
 	return al.GetListenerf(al.Gain)
 }
 
-// Render is called by the renderer at each frame
-// Updates the OpenAL position and orientation of this listener
+// Render is called by the renderer at each frame.
+// Updates the position and orientation of the listener.
 func (l *Listener) Render(gl *gls.GLS) {
 
 	// Sets the listener source world position

+ 0 - 96
audio/wave.go

@@ -1,96 +0,0 @@
-// Copyright 2016 The G3N Authors. All rights reserved.
-// Use of this source code is governed by a BSD-style
-// license that can be found in the LICENSE file.
-
-// +build !wasm
-
-package audio
-
-import (
-	"fmt"
-	"github.com/g3n/engine/audio/al"
-	"os"
-)
-
-// WaveSpecs describes the characteristics of the audio encoded in a wave file.
-type WaveSpecs struct {
-	Format     int     // OpenAl Format
-	Type       int     // Type field from wave header
-	Channels   int     // Number of channels
-	SampleRate int     // Sample rate in hz
-	BitsSample int     // Number of bits per sample (8 or 16)
-	DataSize   int     // Total data size in bytes
-	BytesSec   int     // Bytes per second
-	TotalTime  float64 // Total time in seconds
-}
-
-const (
-	waveHeaderSize = 44
-	fileMark       = "RIFF"
-	fileHead       = "WAVE"
-)
-
-// WaveCheck checks if the specified filepath corresponds to a an audio wave file.
-// If the file is a valid wave file, return a pointer to WaveSpec structure
-// with information about the encoded audio data.
-func WaveCheck(filepath string) (*WaveSpecs, error) {
-
-	// Open file
-	f, err := os.Open(filepath)
-	if err != nil {
-		return nil, err
-	}
-	defer f.Close()
-
-	// Reads header
-	header := make([]uint8, waveHeaderSize)
-	n, err := f.Read(header)
-	if err != nil {
-		return nil, err
-	}
-	if n < waveHeaderSize {
-		return nil, fmt.Errorf("File size less than header")
-	}
-	// Checks file marks
-	if string(header[0:4]) != fileMark {
-		return nil, fmt.Errorf("'RIFF' mark not found")
-	}
-	if string(header[8:12]) != fileHead {
-		return nil, fmt.Errorf("'WAVE' mark not found")
-	}
-
-	// Decodes header fields
-	var ws WaveSpecs
-	ws.Format = -1
-	ws.Type = int(header[20]) + int(header[21])<<8
-	ws.Channels = int(header[22]) + int(header[23])<<8
-	ws.SampleRate = int(header[24]) + int(header[25])<<8 + int(header[26])<<16 + int(header[27])<<24
-	ws.BitsSample = int(header[34]) + int(header[35])<<8
-	ws.DataSize = int(header[40]) + int(header[41])<<8 + int(header[42])<<16 + int(header[43])<<24
-
-	// Sets OpenAL format field if possible
-	if ws.Channels == 1 {
-		if ws.BitsSample == 8 {
-			ws.Format = al.FormatMono8
-		} else if ws.BitsSample == 16 {
-			ws.Format = al.FormatMono16
-		}
-	} else if ws.Channels == 2 {
-		if ws.BitsSample == 8 {
-			ws.Format = al.FormatStereo8
-		} else if ws.BitsSample == 16 {
-			ws.Format = al.FormatStereo16
-		}
-	}
-
-	// Calculates bytes/sec and total time
-	var bytesChannel int
-	if ws.BitsSample == 8 {
-		bytesChannel = 1
-	} else {
-		bytesChannel = 2
-	}
-	ws.BytesSec = ws.SampleRate * ws.Channels * bytesChannel
-	ws.TotalTime = float64(ws.DataSize) / float64(ws.BytesSec)
-	return &ws, nil
-}