progress on untested music controller (base music controller using NAudio added, created intermediate sample provider that allows to obtain spectrum for current buffer);

This commit is contained in:
Harrison Deng 2018-11-19 01:37:39 -06:00
parent 6086c6491b
commit 02d814b4f7
3 changed files with 171 additions and 1 deletions

View File

@ -50,6 +50,7 @@
<Compile Include="Zer01HD\Audio\MusicController.cs" /> <Compile Include="Zer01HD\Audio\MusicController.cs" />
<Compile Include="Zer01HD\Audio\MusicList.cs" /> <Compile Include="Zer01HD\Audio\MusicList.cs" />
<Compile Include="Zer01HD\Audio\SupportedFormats.cs" /> <Compile Include="Zer01HD\Audio\SupportedFormats.cs" />
<Compile Include="Zer01HD\Audio\TransparentSampleProvider.cs" />
<Compile Include="Zer01HD\Screens\MainMenu\MainPage.cs" /> <Compile Include="Zer01HD\Screens\MainMenu\MainPage.cs" />
<Compile Include="Zer01HD\Screens\Transitions\FadeAwayTransition.cs" /> <Compile Include="Zer01HD\Screens\Transitions\FadeAwayTransition.cs" />
<Compile Include="Zer01HD\Utilities\Camera\Camera2D.cs" /> <Compile Include="Zer01HD\Utilities\Camera\Camera2D.cs" />

View File

@ -1,4 +1,6 @@
using Microsoft.Xna.Framework.Media; using Microsoft.Xna.Framework.Media;
using NAudio.Dsp;
using NAudio.Wave;
using System; using System;
using System.Collections.Generic; using System.Collections.Generic;
using System.Linq; using System.Linq;
@ -10,17 +12,98 @@ namespace RhythmBullet.Zer01HD.Audio
internal class MusicController internal class MusicController
{ {
MusicList musicList; MusicList musicList;
WaveOutEvent outputDevice;
TransparentSampleProvider transparentSampleProvider;
AudioFileReader audioInput;
Random random;
float volume;
private float Volume
{
get
{
return volume;
}
set
{
if (outputDevice != null)
{
outputDevice.Volume = value;
}
volume = value;
}
}
private int PlayingIndex
{
get
{
return musicList.List.IndexOf(audioInput.FileName);
}
set
{
if (value < 0) value = musicList.List.Count -1;
if (value >= musicList.List.Count) value = 0;
LoadMusic(musicList.List[value]);
}
}
public MusicController(MusicList musicList) public MusicController(MusicList musicList)
{ {
this.musicList = musicList; this.musicList = musicList;
random = new Random();
} }
public void LoadMusic(string path) private void LoadMusic(string path)
{ {
outputDevice?.Dispose();
audioInput?.Dispose();
audioInput = new AudioFileReader(path);
transparentSampleProvider = new TransparentSampleProvider(audioInput);
outputDevice = new WaveOutEvent();
outputDevice.Volume = Volume;
outputDevice.Init(transparentSampleProvider);
} }
public void Skip()
{
PlayingIndex++;
}
public void Previous()
{
PlayingIndex--;
}
public void Shuffle()
{
PlayingIndex = random.Next(0, musicList.List.Count);
}
public void Play()
{
outputDevice.Play();
}
public void Pause()
{
outputDevice.Pause();
}
public void Stop()
{
outputDevice.Stop();
}
public TimeSpan Position()
{
return audioInput.CurrentTime;
}
} }
} }

View File

@ -0,0 +1,86 @@
using Microsoft.Xna.Framework;
using NAudio.Dsp;
using NAudio.Wave;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace RhythmBullet.Zer01HD.Audio
{
public class TransparentSampleProvider : ISampleProvider
{
private const int FFT_SIZE = 2048;
private readonly int m;
private ISampleProvider source;
private Complex[] fftBuffer;
private float[] spectrum;
private int channelCount;
public bool performFFT;
public TransparentSampleProvider(ISampleProvider sampleProvider)
{
m = (int)Math.Log(FFT_SIZE);
source = sampleProvider;
channelCount = sampleProvider.WaveFormat.Channels;
fftBuffer = new Complex[FFT_SIZE];
spectrum = new float[FFT_SIZE];
}
public WaveFormat WaveFormat
{
get
{
return source.WaveFormat;
}
}
public float GetSpectrumBin(int bin)
{
lock (spectrum)
{
return spectrum[bin];
}
}
public int Read(float[] buffer, int offset, int count)
{
int sampleCount = source.Read(buffer, offset, count);
if (performFFT)
{
int fftOffset = 0;
for (int s = 0; s < sampleCount; s += channelCount)
{
if (s >= FFT_SIZE)
{
FastFourierTransform.FFT(true, m, fftBuffer);
lock (spectrum)
{
for (int binID = 0; binID < spectrum.Length; binID++)
{
spectrum[binID] = fftBuffer[binID].X;
}
}
fftOffset -= FFT_SIZE;
}
float greatestVal = 0;
for (int c = 0; c < channelCount; c++)
{
greatestVal = MathHelper.Max(buffer[s + offset + c], greatestVal);
}
fftBuffer[s + fftOffset].X = (float)(buffer[s + offset] * FastFourierTransform.HammingWindow(s, FFT_SIZE));
fftBuffer[s + fftOffset].Y = 0;
}
}
return sampleCount;
}
}
}