2017-10-03 18:21:08 +08:00
|
|
|
// Copyright (c) 2007-2017 ppy Pty Ltd <contact@ppy.sh>.
|
|
|
|
// Licensed under the MIT Licence - https://raw.githubusercontent.com/ppy/osu/master/LICENCE
|
|
|
|
|
|
|
|
using System;
|
|
|
|
using System.Collections.Generic;
|
|
|
|
using OpenTK;
|
|
|
|
using OpenTK.Graphics;
|
2017-10-03 20:19:38 +08:00
|
|
|
using OpenTK.Graphics.ES30;
|
2017-10-03 18:21:08 +08:00
|
|
|
using osu.Framework.Allocation;
|
|
|
|
using osu.Framework.Audio.Track;
|
|
|
|
using osu.Framework.Configuration;
|
|
|
|
using osu.Framework.Graphics;
|
2017-10-03 20:19:38 +08:00
|
|
|
using osu.Framework.Graphics.Batches;
|
|
|
|
using osu.Framework.Graphics.Colour;
|
2017-10-03 18:21:08 +08:00
|
|
|
using osu.Framework.Graphics.Containers;
|
2017-10-03 20:19:38 +08:00
|
|
|
using osu.Framework.Graphics.OpenGL.Vertices;
|
|
|
|
using osu.Framework.Graphics.Primitives;
|
|
|
|
using osu.Framework.Graphics.Shaders;
|
2017-10-03 18:21:08 +08:00
|
|
|
using osu.Framework.Graphics.Shapes;
|
2017-10-03 20:19:38 +08:00
|
|
|
using osu.Framework.Graphics.Textures;
|
2017-10-03 18:21:08 +08:00
|
|
|
using osu.Game.Beatmaps;
|
|
|
|
using osu.Game.Graphics.Sprites;
|
|
|
|
using osu.Game.Overlays;
|
|
|
|
|
|
|
|
namespace osu.Game.Tests.Visual
|
|
|
|
{
|
|
|
|
internal class TestCaseWaveform : OsuTestCase
|
|
|
|
{
|
|
|
|
private readonly Bindable<WorkingBeatmap> beatmapBacking = new Bindable<WorkingBeatmap>();
|
|
|
|
|
|
|
|
public TestCaseWaveform()
|
|
|
|
{
|
|
|
|
MusicController mc;
|
|
|
|
FillFlowContainer flow;
|
|
|
|
Child = flow = new FillFlowContainer
|
|
|
|
{
|
|
|
|
RelativeSizeAxes = Axes.Both,
|
|
|
|
Direction = FillDirection.Vertical,
|
|
|
|
Spacing = new Vector2(0, 10),
|
|
|
|
Children = new Drawable[]
|
|
|
|
{
|
|
|
|
mc = new MusicController
|
|
|
|
{
|
|
|
|
Anchor = Anchor.TopCentre,
|
|
|
|
Origin = Anchor.TopCentre,
|
|
|
|
Y = 100,
|
|
|
|
State = Visibility.Visible
|
|
|
|
},
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
for (int i = 1; i <= 16; i *= 2)
|
|
|
|
{
|
2017-10-04 13:42:22 +08:00
|
|
|
var newDisplay = new WaveformDisplay
|
|
|
|
{
|
|
|
|
RelativeSizeAxes = Axes.Both,
|
|
|
|
Resolution = 1f / i
|
|
|
|
};
|
2017-10-03 18:21:08 +08:00
|
|
|
|
2017-10-04 13:42:22 +08:00
|
|
|
newDisplay.Beatmap.BindTo(beatmapBacking);
|
2017-10-03 18:21:08 +08:00
|
|
|
|
|
|
|
flow.Add(new Container
|
|
|
|
{
|
|
|
|
RelativeSizeAxes = Axes.X,
|
|
|
|
Height = 100,
|
|
|
|
Children = new Drawable[]
|
|
|
|
{
|
|
|
|
newDisplay,
|
|
|
|
new Container
|
|
|
|
{
|
|
|
|
Anchor = Anchor.Centre,
|
|
|
|
Origin = Anchor.Centre,
|
|
|
|
AutoSizeAxes = Axes.Both,
|
|
|
|
Children = new Drawable[]
|
|
|
|
{
|
|
|
|
new Box
|
|
|
|
{
|
|
|
|
RelativeSizeAxes = Axes.Both,
|
|
|
|
Colour = Color4.Black,
|
|
|
|
Alpha = 0.75f
|
|
|
|
},
|
|
|
|
new OsuSpriteText
|
|
|
|
{
|
|
|
|
Anchor = Anchor.Centre,
|
|
|
|
Origin = Anchor.Centre,
|
|
|
|
Text = $"Resolution: {(1f / i).ToString("0.00")}"
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
});
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
[BackgroundDependencyLoader]
|
2017-10-04 13:42:22 +08:00
|
|
|
private void load(OsuGameBase osuGame) => beatmapBacking.BindTo(osuGame.Beatmap);
|
2017-10-03 18:21:08 +08:00
|
|
|
|
2017-10-03 20:19:38 +08:00
|
|
|
private class WaveformDisplay : Drawable
|
2017-10-03 18:21:08 +08:00
|
|
|
{
|
2017-10-04 13:42:22 +08:00
|
|
|
public readonly Bindable<WorkingBeatmap> Beatmap = new Bindable<WorkingBeatmap>();
|
|
|
|
|
|
|
|
private Waveform waveform;
|
2017-10-03 20:19:38 +08:00
|
|
|
|
|
|
|
private Shader shader;
|
|
|
|
private readonly Texture texture;
|
|
|
|
|
2017-10-04 13:42:22 +08:00
|
|
|
public WaveformDisplay()
|
2017-10-03 18:21:08 +08:00
|
|
|
{
|
2017-10-03 20:19:38 +08:00
|
|
|
texture = Texture.WhitePixel;
|
2017-10-04 13:42:22 +08:00
|
|
|
Beatmap.ValueChanged += generateWaveform;
|
2017-10-03 20:19:38 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
[BackgroundDependencyLoader]
|
|
|
|
private void load(ShaderManager shaders)
|
|
|
|
{
|
|
|
|
shader = shaders?.Load(VertexShaderDescriptor.TEXTURE_2, FragmentShaderDescriptor.TEXTURE_ROUNDED);
|
2017-10-03 18:21:08 +08:00
|
|
|
}
|
|
|
|
|
2017-10-04 13:42:22 +08:00
|
|
|
private float resolution = 1;
|
|
|
|
public float Resolution
|
2017-10-03 18:21:08 +08:00
|
|
|
{
|
2017-10-04 13:42:22 +08:00
|
|
|
get { return resolution; }
|
|
|
|
set
|
|
|
|
{
|
|
|
|
if (resolution == value)
|
|
|
|
return;
|
|
|
|
resolution = value;
|
|
|
|
|
|
|
|
Invalidate(Invalidation.DrawNode);
|
|
|
|
}
|
2017-10-03 20:19:38 +08:00
|
|
|
}
|
|
|
|
|
2017-10-04 13:42:22 +08:00
|
|
|
private Track lastQueriedTrack;
|
|
|
|
|
|
|
|
private void generateWaveform(WorkingBeatmap beatmap)
|
|
|
|
{
|
|
|
|
// Cancel the old query so we don't saturate the audio thread
|
|
|
|
lastQueriedTrack?.CancelWaveformQuery();
|
|
|
|
|
|
|
|
beatmap.Track.QueryWaveform(w =>
|
|
|
|
{
|
|
|
|
if (Beatmap.Value == beatmap)
|
|
|
|
{
|
|
|
|
waveform = w;
|
|
|
|
Invalidate(Invalidation.DrawNode);
|
|
|
|
}
|
|
|
|
});
|
|
|
|
|
|
|
|
lastQueriedTrack = beatmap.Track;
|
|
|
|
}
|
2017-10-03 20:19:38 +08:00
|
|
|
|
|
|
|
private readonly WaveformDrawNodeSharedData sharedData = new WaveformDrawNodeSharedData();
|
2017-10-04 13:42:22 +08:00
|
|
|
protected override DrawNode CreateDrawNode() => new WaveformDrawNode();
|
2017-10-03 20:19:38 +08:00
|
|
|
protected override void ApplyDrawNode(DrawNode node)
|
|
|
|
{
|
|
|
|
var n = (WaveformDrawNode)node;
|
|
|
|
|
|
|
|
n.Shader = shader;
|
|
|
|
n.Texture = texture;
|
|
|
|
n.Size = DrawSize;
|
|
|
|
n.Shared = sharedData;
|
2017-10-04 13:42:22 +08:00
|
|
|
n.Points = waveform.Generate((int)(MathHelper.Clamp(Math.Ceiling(DrawWidth), 0, waveform.TotalPoints) * resolution));
|
|
|
|
n.Channels = waveform.Channels;
|
2017-10-03 20:19:38 +08:00
|
|
|
|
|
|
|
base.ApplyDrawNode(node);
|
|
|
|
}
|
|
|
|
|
|
|
|
private class WaveformDrawNodeSharedData
|
|
|
|
{
|
|
|
|
public readonly QuadBatch<TexturedVertex2D> VertexBatch = new QuadBatch<TexturedVertex2D>(1000, 10);
|
|
|
|
}
|
|
|
|
|
|
|
|
private class WaveformDrawNode : DrawNode
|
|
|
|
{
|
|
|
|
public Shader Shader;
|
|
|
|
public Texture Texture;
|
|
|
|
|
|
|
|
public WaveformDrawNodeSharedData Shared;
|
2017-10-03 18:21:08 +08:00
|
|
|
|
2017-10-03 20:19:38 +08:00
|
|
|
public List<WaveformPoint> Points;
|
|
|
|
public Vector2 Size;
|
|
|
|
public int Channels;
|
2017-10-03 18:21:08 +08:00
|
|
|
|
2017-10-03 20:19:38 +08:00
|
|
|
public override void Draw(Action<TexturedVertex2D> vertexAction)
|
2017-10-03 18:21:08 +08:00
|
|
|
{
|
2017-10-03 20:19:38 +08:00
|
|
|
base.Draw(vertexAction);
|
2017-10-03 18:21:08 +08:00
|
|
|
|
2017-10-03 20:19:38 +08:00
|
|
|
if (Points == null || Points.Count == 0)
|
|
|
|
return;
|
|
|
|
|
|
|
|
Shader.Bind();
|
|
|
|
Texture.TextureGL.Bind();
|
|
|
|
|
|
|
|
float separation = Size.X / (Points.Count - 1);
|
|
|
|
Vector2 localInflationAmount = new Vector2(0, 1) * DrawInfo.MatrixInverse.ExtractScale().Xy;
|
|
|
|
|
|
|
|
for (int i = 0; i < Points.Count - 1; i++)
|
2017-10-03 18:21:08 +08:00
|
|
|
{
|
2017-10-03 20:19:38 +08:00
|
|
|
ColourInfo colour = DrawInfo.Colour;
|
|
|
|
Quad quadToDraw;
|
|
|
|
|
|
|
|
switch (Channels)
|
2017-10-03 18:21:08 +08:00
|
|
|
{
|
2017-10-03 20:19:38 +08:00
|
|
|
default:
|
|
|
|
case 2:
|
|
|
|
{
|
|
|
|
float height = Size.Y / 2;
|
|
|
|
quadToDraw = new Quad(
|
|
|
|
new Vector2(i * separation, height - Points[i].Amplitude[0] * height),
|
|
|
|
new Vector2((i + 1) * separation, height - Points[i + 1].Amplitude[0] * height),
|
|
|
|
new Vector2(i * separation, height + Points[i].Amplitude[1] * height),
|
|
|
|
new Vector2((i + 1) * separation, height + Points[i + 1].Amplitude[1] * height)
|
|
|
|
);
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
case 1:
|
|
|
|
{
|
|
|
|
quadToDraw = new Quad(
|
|
|
|
new Vector2(i * separation, Size.Y - Points[i].Amplitude[0] * Size.Y),
|
|
|
|
new Vector2((i + 1) * separation, Size.Y - Points[i + 1].Amplitude[0] * Size.Y),
|
|
|
|
new Vector2(i * separation, Size.Y),
|
|
|
|
new Vector2((i + 1) * separation, Size.Y)
|
|
|
|
);
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
Texture.DrawQuad(quadToDraw * DrawInfo.Matrix, colour, null, Shared.VertexBatch.Add, Vector2.Divide(localInflationAmount, quadToDraw.Size));
|
2017-10-03 18:21:08 +08:00
|
|
|
}
|
2017-10-03 20:19:38 +08:00
|
|
|
|
|
|
|
Shader.Unbind();
|
2017-10-03 18:21:08 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|