using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using AudioSpectrumStudy.Fmod;
using System.Runtime.InteropServices;
namespace AudioSpectrumStudy
{
public partial class Form1 : Form
{
Fmod.System system;
Fmod.Channel channel;
Fmod.Sound sound;
Fmod.ChannelGroup channelGroup;
Fmod.DSP dsp;
Fmod.DSP_DESCRIPTION description;
Timer timer;
public Form1()
{
InitializeComponent();
textBox1.MouseDoubleClick += textBox1_MouseDoubleClick;
system = new Fmod.System(IntPtr.Zero);
system.init(32, INITFLAGS.NORMAL, IntPtr.Zero);
channel = new Channel(IntPtr.Zero);
sound = new Sound(IntPtr.Zero);
sound.setMusicSpeed(1.0f);
sound.setMode(MODE._2D);
description = new DSP_DESCRIPTION();
system.createDSP(ref description, out dsp);
channel.getChannelGroup(out channelGroup);
timer = new Timer();
timer.Tick += timer_Tick;
}
void textBox1_MouseDoubleClick(object sender, MouseEventArgs e)
{
OpenFileDialog ofd = new OpenFileDialog();
ofd.Filter = "File|*.*";
ofd.ShowDialog();
textBox1.Text = ofd.FileName;
}
void timer_Tick(object sender, EventArgs e)
{
system.update();
UpdateSpectrum();
progressBar1.Value = (int)_spectrum[0] > 100 ? 100 : (int)_spectrum[0];
}
private void Form1_Load(object sender, EventArgs e)
{
}
private const int SPECTRUM_SIZE = 512;
private float[][] _spectrumInfo;
public float[] _spectrum;
private void UpdateSpectrum()
{
if (_spectrum == null || _spectrumInfo == null)
{
if(_spectrum == null)
_spectrum = new float[SPECTRUM_SIZE];
if (_spectrumInfo == null)
{
_spectrumInfo = new float[2][];
_spectrumInfo[0] = new float[SPECTRUM_SIZE];
_spectrumInfo[1] = new float[SPECTRUM_SIZE];
}
}
System.IntPtr data = IntPtr.Zero;
uint length = 0;
dsp.getParameterData(3, out data, out length);
//Fmod.DSP_PARAMETER_FFT spectrumBuffer = new DSP_PARAMETER_FFT();
var spectrumBuffer = (Fmod.DSP_PARAMETER_FFT)Marshal.PtrToStructure(data, typeof(Fmod.DSP_PARAMETER_FFT));
_spectrumInfo = spectrumBuffer.spectrum;
for (int i = 0; i < SPECTRUM_SIZE; i++)
{
_spectrum[i] = (_spectrumInfo[0][i] + _spectrumInfo[1][i]) / 2f;
}
}
private void button1_Click(object sender, EventArgs e)
{
system.createSound(textBox1.Text, MODE._2D, out sound);
system.playSound(sound, channelGroup, false, out channel);
system.playDSP(dsp, channelGroup, false, out channel);
system.release();
system.close();
timer.Start();
}
}
}
this is my code
var spectrumBuffer = (Fmod.DSP_PARAMETER_FFT)Marshal.PtrToStructure(data, typeof(Fmod.DSP_PARAMETER_FFT));
this code raise exception ‘ArgumentNullException’ so, I traced with debug why the exception raise.
I found dsp.getParameterData() method was returned ‘ERR_INVALID_PARAM’
why working like that?