我想創建一個使用C#語言和.NET Framework的聲音可視化系統。 這可能看起來像在Winamp應用程序中。 也許存在免費的圖書館或一些有趣的文章,描述如何做到這一點? 例子: alt text http://img44.imageshack.us/img44/9982/examplel.pngC#聲音可視化
C#聲音可視化
回答
您可以嘗試以下鏈接
OpenVP(是開發音樂可視化,用C#編寫的自由和開源平臺),請參閱OpenVP Screenshots。
Play and Visualize WAV Files using Managed Direct Sound
再見。
下面是一個腳本,用於計算計算機上使用WASAPI API播放的任何聲音的FFT。從我們建議先鎖定它,因爲這是一個單獨的線程修改不同的腳本檢索barData時
using CSCore;
using CSCore.SoundIn;
using CSCore.Codecs.WAV;
using WinformsVisualization.Visualization;
using CSCore.DSP;
using CSCore.Streams;
using System;
public class SoundCapture
{
public int numBars = 30;
public int minFreq = 5;
public int maxFreq = 4500;
public int barSpacing = 0;
public bool logScale = true;
public bool isAverage = false;
public float highScaleAverage = 2.0f;
public float highScaleNotAverage = 3.0f;
LineSpectrum lineSpectrum;
WasapiCapture capture;
WaveWriter writer;
FftSize fftSize;
float[] fftBuffer;
SingleBlockNotificationStream notificationSource;
BasicSpectrumProvider spectrumProvider;
IWaveSource finalSource;
public SoundCapture()
{
// This uses the wasapi api to get any sound data played by the computer
capture = new WasapiLoopbackCapture();
capture.Initialize();
// Get our capture as a source
IWaveSource source = new SoundInSource(capture);
// From https://github.com/filoe/cscore/blob/master/Samples/WinformsVisualization/Form1.cs
// This is the typical size, you can change this for higher detail as needed
fftSize = FftSize.Fft4096;
// Actual fft data
fftBuffer = new float[(int)fftSize];
// These are the actual classes that give you spectrum data
// The specific vars of lineSpectrum here aren't that important because they can be changed by the user
spectrumProvider = new BasicSpectrumProvider(capture.WaveFormat.Channels,
capture.WaveFormat.SampleRate, fftSize);
lineSpectrum = new LineSpectrum(fftSize)
{
SpectrumProvider = spectrumProvider,
UseAverage = true,
BarCount = numBars,
BarSpacing = 2,
IsXLogScale = false,
ScalingStrategy = ScalingStrategy.Linear
};
// Tells us when data is available to send to our spectrum
var notificationSource = new SingleBlockNotificationStream(source.ToSampleSource());
notificationSource.SingleBlockRead += NotificationSource_SingleBlockRead;
// We use this to request data so it actualy flows through (figuring this out took forever...)
finalSource = notificationSource.ToWaveSource();
capture.DataAvailable += Capture_DataAvailable;
capture.Start();
}
private void Capture_DataAvailable(object sender, DataAvailableEventArgs e)
{
finalSource.Read(e.Data, e.Offset, e.ByteCount);
}
private void NotificationSource_SingleBlockRead(object sender, SingleBlockReadEventArgs e)
{
spectrumProvider.Add(e.Left, e.Right);
}
~SoundCapture()
{
capture.Stop();
capture.Dispose();
}
public float[] barData = new float[20];
public float[] GetFFtData()
{
lock (barData)
{
lineSpectrum.BarCount = numBars;
if (numBars != barData.Length)
{
barData = new float[numBars];
}
}
if (spectrumProvider.IsNewDataAvailable)
{
lineSpectrum.MinimumFrequency = minFreq;
lineSpectrum.MaximumFrequency = maxFreq;
lineSpectrum.IsXLogScale = logScale;
lineSpectrum.BarSpacing = barSpacing;
lineSpectrum.SpectrumProvider.GetFftData(fftBuffer, this);
return lineSpectrum.GetSpectrumPoints(100.0f, fftBuffer);
}
else
{
return null;
}
}
public void ComputeData()
{
float[] resData = GetFFtData();
int numBars = barData.Length;
if (resData == null)
{
return;
}
lock (barData)
{
for (int i = 0; i < numBars && i < resData.Length; i++)
{
// Make the data between 0.0 and 1.0
barData[i] = resData[i]/100.0f;
}
for (int i = 0; i < numBars && i < resData.Length; i++)
{
if (lineSpectrum.UseAverage)
{
// Scale the data because for some reason bass is always loud and treble is soft
barData[i] = barData[i] + highScaleAverage * (float)Math.Sqrt(i/(numBars + 0.0f)) * barData[i];
}
else
{
barData[i] = barData[i] + highScaleNotAverage * (float)Math.Sqrt(i/(numBars + 0.0f)) * barData[i];
}
}
}
}
}
然後:它採用CSCore及其WinformsVisualization例子。
我不知道我在哪裏得到了GetSpectrumPoints
,因爲它似乎不在Github Repo中,但在這裏。只需將其粘貼到該文件中,我的代碼就可以工作。
public float[] GetSpectrumPoints(float height, float[] fftBuffer)
{
SpectrumPointData[] dats = CalculateSpectrumPoints(height, fftBuffer);
float[] res = new float[dats.Length];
for (int i = 0; i < dats.Length; i++)
{
res[i] = (float)dats[i].Value;
}
return res;
}
我試圖使用你的示例代碼,但它似乎不像['GetSpectrumPoints()'是一個函數](https://github.com/filoe/cscore/blob/29410b12ae35321c4556b072c0711a8f289c0544/Samples/ WinformsVisualization/Visualization/LineSpectrum.cs#L10),並檢查git存儲庫歷史記錄也不會顯示它。你介意澄清/更新你的答案嗎? (*我正嘗試將Windows上的音頻捕獲/處理與驅動LED燈的跨平臺控制檯應用程序相集成; 0.0至1.0條頻率數據值就是它所需要的*) – Shane 2016-11-28 16:53:09
@Shane對不起!我現在添加了這些代碼,希望有所幫助 – Phylliida 2016-12-06 00:19:59
- 1. 如何可視化聲音?
- 2. Java聲音可視化器
- 3. 音頻可視化器C#
- 4. ActionScript 2.0中的聲音可視化
- 5. 音頻/語音可視化
- 6. 音樂播放器的聲音水平儀可視化器
- 7. 音頻可視化
- 8. 音樂可視化
- 9. 用Cb-C可視化音頻數據
- 10. 在視圖中播放聲音變化
- 11. 音頻波形可視化
- 12. android音頻可視化
- 13. HTML5音頻可視化?
- 14. 可視化html5音頻
- 15. 適用於Android的聲音可視化庫
- 16. 無法獲取WPF聲音可視化庫工作
- 17. 使用silverlight顯示聲音可視化器
- 18. 非常需要簡單的樣品聲音可視化
- 19. 從C#的電視卡捕捉聲音#
- 20. c中聲音播放後的聲音#
- 21. C#音樂/聲音同步
- 22. C/C++可視化庫
- 23. C++聲音處理
- 24. 如何開始在C++中編寫音樂可視化工具?
- 25. 音樂可視化誤差在GStreamer
- 26. HTML5音頻播放器的可視化
- 27. 錄製時的音頻可視化
- 28. 音頻可視化與畫布
- 29. 獲取音樂可視化數據
- 30. 創建音樂可視化器
我認爲這將是良好的開始,謝謝。 – 2009-08-30 22:42:44