A game framework written with osu! in mind.
1// Copyright (c) ppy Pty Ltd <contact@ppy.sh>. Licensed under the MIT Licence.
2// See the LICENCE file in the repository root for full licence text.
3
4using System.Linq;
5using osu.Framework.Allocation;
6using osu.Framework.Audio.Sample;
7using osu.Framework.Audio.Track;
8using osu.Framework.Graphics;
9using osu.Framework.Graphics.Audio;
10using osu.Framework.Graphics.Containers;
11using osu.Framework.Graphics.Shapes;
12
13namespace osu.Framework.Tests.Visual.Audio
14{
15 public class TestSceneSampleAmplitudes : FrameworkTestScene
16 {
17 private Box leftChannel;
18 private Box rightChannel;
19
20 private DrawableSample sample;
21 private SampleChannel channel;
22
23 private Container amplitudeBoxes;
24
25 [BackgroundDependencyLoader]
26 private void load(ISampleStore samples)
27 {
28 Children = new Drawable[]
29 {
30 sample = new DrawableSample(samples.Get("long.mp3")),
31 new GridContainer
32 {
33 RelativeSizeAxes = Axes.Both,
34 Content = new[]
35 {
36 new Drawable[]
37 {
38 new Container
39 {
40 RelativeSizeAxes = Axes.Both,
41 Children = new Drawable[]
42 {
43 leftChannel = new Box
44 {
45 RelativeSizeAxes = Axes.Both,
46 Anchor = Anchor.Centre,
47 Origin = Anchor.CentreRight,
48 },
49 rightChannel = new Box
50 {
51 RelativeSizeAxes = Axes.Both,
52 Anchor = Anchor.Centre,
53 Origin = Anchor.CentreLeft,
54 }
55 }
56 },
57 },
58 new Drawable[]
59 {
60 amplitudeBoxes = new Container
61 {
62 RelativeSizeAxes = Axes.Both,
63 ChildrenEnumerable =
64 Enumerable.Range(0, ChannelAmplitudes.AMPLITUDES_SIZE)
65 .Select(i => new Box
66 {
67 RelativeSizeAxes = Axes.Both,
68 RelativePositionAxes = Axes.X,
69 Anchor = Anchor.BottomLeft,
70 Origin = Anchor.BottomLeft,
71 Width = 1f / ChannelAmplitudes.AMPLITUDES_SIZE,
72 X = (float)i / ChannelAmplitudes.AMPLITUDES_SIZE
73 })
74 },
75 }
76 }
77 },
78 };
79 }
80
81 protected override void LoadComplete()
82 {
83 base.LoadComplete();
84
85 AddStep("start sample", () =>
86 {
87 channel = sample.Play();
88 channel.Looping = true;
89 });
90
91 AddStep("stop sample", () => channel.Stop());
92 }
93
94 protected override void Update()
95 {
96 base.Update();
97
98 if (channel == null)
99 return;
100
101 var amplitudes = channel.CurrentAmplitudes;
102
103 rightChannel.Width = amplitudes.RightChannel * 0.5f;
104 leftChannel.Width = amplitudes.LeftChannel * 0.5f;
105
106 var freqAmplitudes = amplitudes.FrequencyAmplitudes.Span;
107
108 for (int i = 0; i < freqAmplitudes.Length; i++)
109 amplitudeBoxes[i].Height = freqAmplitudes[i];
110 }
111 }
112}