add bgm playback, scrolling based on bgm time

This commit is contained in:
msk
2023-09-29 16:16:44 -07:00
parent 02b7f023fd
commit 2a6e64dbfd
6 changed files with 131 additions and 44 deletions
-1
View File
@@ -91,7 +91,6 @@ namespace WacK.Scenes
var songPath = $"user://songs/{songsButton.Text}";
var chartPath = $"{songPath}/{difficultyButton.Selected}.mer";
var soundPath = $"{songPath}/{soundButton.Text}";
GD.Print($"Song: {songPath}\nChart: {chartPath}\nSound: {soundPath}");
// folder check
using var dir = DirAccess.Open(songPath);
+46
View File
@@ -0,0 +1,46 @@
using Godot;
using Godot.Collections;
using System;
public partial class BGM : AudioStreamPlayer
{
public void LoadFromUser(string path)
{
if (!path.StartsWith("user://"))
{
GD.Print("Tried to load audio that isn't in user directory.");
return;
}
var f = FileAccess.Open(path, FileAccess.ModeFlags.Read);
if (f == null)
{
GD.PrintErr($"Unable to open {path} for loading audio! {FileAccess.GetOpenError()}");
return;
}
GD.Print("hi");
var ext = path.Split('.')[^1].ToLower();
switch (ext)
{
case "mp3":
var mp3 = new AudioStreamMP3()
{
Data = f.GetBuffer((long)f.GetLength())
};
Stream = mp3;
break;
case "wav":
case "wave":
var wav = new AudioStreamWav()
{
Data = f.GetBuffer((long)f.GetLength())
};
Stream = wav;
break;
case "ogg":
// TODO: implement
GD.PrintErr("External OGGs not supported in Godot 4.1...");
break;
}
}
}
+15
View File
@@ -0,0 +1,15 @@
using Godot;
using System;
public partial class SFX : Node
{
// Called when the node enters the scene tree for the first time.
public override void _Ready()
{
}
// Called every frame. 'delta' is the elapsed time since the previous frame.
public override void _Process(double delta)
{
}
}
@@ -22,6 +22,7 @@ namespace WacK.Scenes
{
chartPath = chPath;
soundPath = snPath;
GD.Print($"Chart: {chartPath}\nSound: {soundPath}");
}
}
public partial class Play : Node
@@ -34,6 +35,14 @@ namespace WacK.Scenes
public static PackedScene noteHold = GD.Load<PackedScene>("res://Things/TunnelObjects/Notes/NoteHold.tscn");
public static PackedScene noteChain = GD.Load<PackedScene>("res://Things/TunnelObjects/Notes/NoteChain.tscn");
[ExportCategory("Audio")]
[Export]
private BGM bgmController;
[Export]
private SFX sfxController;
[ExportCategory("2D")]
[ExportSubgroup("2D Things")]
[Export]
public Control noteDisplay;
[Export]
@@ -41,6 +50,7 @@ namespace WacK.Scenes
[Export]
public Background background;
[ExportSubgroup("Out-of-bounds Viewports")]
[Export]
public Viewport mainViewport;
[Export]
@@ -69,6 +79,10 @@ namespace WacK.Scenes
// parse mer and create chart for current play
chart = new(playParams.chartPath);
RealizeChart();
// audio setup
bgmController.LoadFromUser(playParams.soundPath);
bgmController.Play();
}
/// <summary>
@@ -107,8 +121,10 @@ namespace WacK.Scenes
public override void _Process(double delta)
{
double time = bgmController.GetPlaybackPosition() + AudioServer.GetTimeSinceLastMix() - AudioServer.GetOutputLatency();
var nPos = noteDisplay.Position;
nPos.Y += (float)delta * scrollPxPerSec;
nPos.Y = ((float)time * scrollPxPerSec) + 1920;
noteDisplay.Position = nPos;
scrollDisplay.Position = nPos;
}