This commit is contained in:
Konstantin8105 2022-11-22 18:50:35 +03:00
parent 0bec81c656
commit fe6d2c0ed3
190 changed files with 104835 additions and 5 deletions

View file

@ -0,0 +1,66 @@
package main
import (
"fmt"
"github.com/gen2brain/raylib-go/raylib"
)
func main() {
// Initialization
//--------------------------------------------------------------------------------------
screenWidth := int32(800)
screenHeight := int32(450)
rl.InitWindow(screenWidth, screenHeight, "raylib [audio] example - Multichannel sound playing")
rl.InitAudioDevice() // Initialize audio device
fxWav := rl.LoadSound("sound.wav") // Load WAV audio file
fxOgg := rl.LoadSound("target.ogg") // Load OGG audio file
rl.SetSoundVolume(fxWav, 0.2)
rl.SetTargetFPS(60) // Set our game to run at 60 frames-per-second
//--------------------------------------------------------------------------------------
// Main game loop
for !rl.WindowShouldClose() { // Detect window close button or ESC key
// Update
//----------------------------------------------------------------------------------
if rl.IsKeyPressed(rl.KeyEnter) {
rl.PlaySoundMulti(fxWav)
} // Play a new wav sound instance
if rl.IsKeyPressed(rl.KeySpace) {
rl.PlaySoundMulti(fxOgg)
} // Play a new ogg sound instance
//----------------------------------------------------------------------------------
// Draw
//----------------------------------------------------------------------------------
rl.BeginDrawing()
rl.ClearBackground(rl.RayWhite)
rl.DrawText("MULTICHANNEL SOUND PLAYING", 20, 20, 20, rl.Gray)
rl.DrawText("Press SPACE to play new ogg instance!", 200, 120, 20, rl.LightGray)
rl.DrawText("Press ENTER to play new wav instance!", 200, 180, 20, rl.LightGray)
rl.DrawText(fmt.Sprintf("CONCURRENT SOUNDS PLAYING: %02d", rl.GetSoundsPlaying()), 220, 280, 20, rl.Red)
rl.EndDrawing()
//----------------------------------------------------------------------------------
}
// De-Initialization
//--------------------------------------------------------------------------------------
rl.StopSoundMulti() // We must stop the buffer pool before unloading
rl.UnloadSound(fxWav) // Unload sound data
rl.UnloadSound(fxOgg) // Unload sound data
rl.CloseAudioDevice() // Close audio device
rl.CloseWindow() // Close window and OpenGL context
//--------------------------------------------------------------------------------------
}

Binary file not shown.

Binary file not shown.

View file

@ -0,0 +1,121 @@
package main
import (
"github.com/gen2brain/raylib-go/raylib"
)
const maxCircles = 64
type circleWave struct {
Position rl.Vector2
Radius float32
Alpha float32
Speed float32
Color rl.Color
}
func main() {
screenWidth := int32(800)
screenHeight := int32(450)
rl.SetConfigFlags(rl.FlagMsaa4xHint) // NOTE: Try to enable MSAA 4X
rl.InitWindow(screenWidth, screenHeight, "raylib [audio] example - module playing (streaming)")
rl.InitAudioDevice()
colors := []rl.Color{
rl.Orange, rl.Red, rl.Gold, rl.Lime, rl.Blue,
rl.Violet, rl.Brown, rl.LightGray, rl.Pink,
rl.Yellow, rl.Green, rl.SkyBlue, rl.Purple, rl.Beige,
}
circles := make([]circleWave, maxCircles)
for i := maxCircles - 1; i >= 0; i-- {
c := circleWave{}
c.Alpha = 0
c.Radius = float32(rl.GetRandomValue(10, 40))
x := rl.GetRandomValue(int32(c.Radius), screenWidth-int32(c.Radius))
y := rl.GetRandomValue(int32(c.Radius), screenHeight-int32(c.Radius))
c.Position = rl.NewVector2(float32(x), float32(y))
c.Speed = float32(rl.GetRandomValue(1, 100)) / 20000.0
c.Color = colors[rl.GetRandomValue(0, int32(len(colors)-1))]
circles[i] = c
}
xm := rl.LoadMusicStream("mini1111.xm")
rl.PlayMusicStream(xm)
pause := false
rl.SetTargetFPS(60)
for !rl.WindowShouldClose() {
rl.UpdateMusicStream(xm) // Update music buffer with new stream data
// Restart music playing (stop and play)
if rl.IsKeyPressed(rl.KeySpace) {
rl.StopMusicStream(xm)
rl.PlayMusicStream(xm)
}
// Pause/Resume music playing
if rl.IsKeyPressed(rl.KeyP) {
pause = !pause
if pause {
rl.PauseMusicStream(xm)
} else {
rl.ResumeMusicStream(xm)
}
}
// Get timePlayed scaled to bar dimensions
timePlayed := int32(rl.GetMusicTimePlayed(xm)/rl.GetMusicTimeLength(xm)*float32(screenWidth-40)) * 2
// Color circles animation
for i := maxCircles - 1; (i >= 0) && !pause; i-- {
circles[i].Alpha += circles[i].Speed
circles[i].Radius += circles[i].Speed * 10.0
if circles[i].Alpha > 1.0 {
circles[i].Speed *= -1
}
if circles[i].Alpha <= 0.0 {
circles[i].Alpha = 0.0
circles[i].Radius = float32(rl.GetRandomValue(10, 40))
circles[i].Position.X = float32(rl.GetRandomValue(int32(circles[i].Radius), screenWidth-int32(circles[i].Radius)))
circles[i].Position.Y = float32(rl.GetRandomValue(int32(circles[i].Radius), screenHeight-int32(circles[i].Radius)))
circles[i].Color = colors[rl.GetRandomValue(0, 13)]
circles[i].Speed = float32(rl.GetRandomValue(1, 100)) / 20000.0
}
}
rl.BeginDrawing()
rl.ClearBackground(rl.RayWhite)
for i := maxCircles - 1; i >= 0; i-- {
rl.DrawCircleV(circles[i].Position, float32(circles[i].Radius), rl.Fade(circles[i].Color, circles[i].Alpha))
}
// Draw time bar
rl.DrawRectangle(20, screenHeight-20-12, screenWidth-40, 12, rl.LightGray)
rl.DrawRectangle(20, screenHeight-20-12, timePlayed, 12, rl.Maroon)
rl.DrawRectangleLines(20, screenHeight-20-12, screenWidth-40, 12, rl.Gray)
rl.EndDrawing()
}
rl.UnloadMusicStream(xm)
rl.CloseAudioDevice()
rl.CloseWindow()
}

Binary file not shown.

Binary file not shown.

View file

@ -0,0 +1,60 @@
package main
import (
"github.com/gen2brain/raylib-go/raylib"
)
func main() {
rl.InitWindow(800, 450, "raylib [audio] example - music playing (streaming)")
rl.InitAudioDevice()
music := rl.LoadMusicStream("guitar_noodling.ogg")
pause := false
rl.PlayMusicStream(music)
rl.SetTargetFPS(60)
for !rl.WindowShouldClose() {
rl.UpdateMusicStream(music) // Update music buffer with new stream data
// Restart music playing (stop and play)
if rl.IsKeyPressed(rl.KeySpace) {
rl.StopMusicStream(music)
rl.PlayMusicStream(music)
}
// Pause/Resume music playing
if rl.IsKeyPressed(rl.KeyP) {
pause = !pause
if pause {
rl.PauseMusicStream(music)
} else {
rl.ResumeMusicStream(music)
}
}
// Get timePlayed scaled to bar dimensions (400 pixels)
timePlayed := rl.GetMusicTimePlayed(music) / rl.GetMusicTimeLength(music) * 100 * 4
rl.BeginDrawing()
rl.ClearBackground(rl.RayWhite)
rl.DrawText("MUSIC SHOULD BE PLAYING!", 255, 150, 20, rl.LightGray)
rl.DrawRectangle(200, 200, 400, 12, rl.LightGray)
rl.DrawRectangle(200, 200, int32(timePlayed), 12, rl.Maroon)
rl.DrawRectangleLines(200, 200, 400, 12, rl.Gray)
rl.DrawText("PRESS SPACE TO RESTART MUSIC", 215, 250, 20, rl.LightGray)
rl.DrawText("PRESS P TO PAUSE/RESUME MUSIC", 208, 280, 20, rl.LightGray)
rl.EndDrawing()
}
rl.UnloadMusicStream(music) // Unload music stream buffers from RAM
rl.CloseAudioDevice() // Close audio device (music streaming is automatically stopped)
rl.CloseWindow()
}

View file

@ -0,0 +1,81 @@
package main
import (
"math"
rl "github.com/gen2brain/raylib-go/raylib"
)
const (
maxSamples = 22050
maxSamplesPerUpdate = 4096
)
func main() {
rl.InitWindow(800, 450, "raylib [audio] example - raw audio streaming")
rl.InitAudioDevice()
// Init raw audio stream (sample rate: 22050, sample size: 32bit-float, channels: 1-mono)
stream := rl.LoadAudioStream(22050, 32, 1)
//// Fill audio stream with some samples (sine wave)
data := make([]float32, maxSamples)
for i := 0; i < maxSamples; i++ {
data[i] = float32(math.Sin(float64((2*rl.Pi*float32(i))/2) * rl.Deg2rad))
}
// NOTE: The generated MAX_SAMPLES do not fit to close a perfect loop
// for that reason, there is a clip everytime audio stream is looped
rl.PlayAudioStream(stream)
totalSamples := int32(maxSamples)
samplesLeft := int32(totalSamples)
position := rl.NewVector2(0, 0)
rl.SetTargetFPS(30)
for !rl.WindowShouldClose() {
// Refill audio stream if required
if rl.IsAudioStreamProcessed(stream) {
numSamples := int32(0)
if samplesLeft >= maxSamplesPerUpdate {
numSamples = maxSamplesPerUpdate
} else {
numSamples = samplesLeft
}
rl.UpdateAudioStream(stream, data[totalSamples-samplesLeft:], numSamples)
samplesLeft -= numSamples
// Reset samples feeding (loop audio)
if samplesLeft <= 0 {
samplesLeft = totalSamples
}
}
rl.BeginDrawing()
rl.ClearBackground(rl.RayWhite)
rl.DrawText("SINE WAVE SHOULD BE PLAYING!", 240, 140, 20, rl.LightGray)
// NOTE: Draw a part of the sine wave (only screen width)
for i := 0; i < int(rl.GetScreenWidth()); i++ {
position.X = float32(i)
position.Y = 250 + 50*data[i]
rl.DrawPixelV(position, rl.Red)
}
rl.EndDrawing()
}
rl.UnloadAudioStream(stream) // Close raw audio stream and delete buffers from RAM
rl.CloseAudioDevice() // Close audio device (music streaming is automatically stopped)
rl.CloseWindow()
}

View file

@ -0,0 +1,41 @@
package main
import (
"github.com/gen2brain/raylib-go/raylib"
)
func main() {
rl.InitWindow(800, 450, "raylib [audio] example - sound loading and playing")
rl.InitAudioDevice()
fxWav := rl.LoadSound("weird.wav")
fxOgg := rl.LoadSound("tanatana.ogg")
rl.SetTargetFPS(60)
for !rl.WindowShouldClose() {
if rl.IsKeyPressed(rl.KeySpace) {
rl.PlaySound(fxWav)
}
if rl.IsKeyPressed(rl.KeyEnter) {
rl.PlaySound(fxOgg)
}
rl.BeginDrawing()
rl.ClearBackground(rl.RayWhite)
rl.DrawText("Press SPACE to PLAY the WAV sound!", 200, 180, 20, rl.LightGray)
rl.DrawText("Press ENTER to PLAY the OGG sound!", 200, 220, 20, rl.LightGray)
rl.EndDrawing()
}
rl.UnloadSound(fxWav)
rl.UnloadSound(fxOgg)
rl.CloseAudioDevice()
rl.CloseWindow()
}

Binary file not shown.

Binary file not shown.