This project has moved. For the latest updates, please go here.

How to monitor and prevent cpu overusage

Jul 15, 2011 at 8:29 PM

Hi

I'm using NAdio in conjuntion with VST.NET to write a C# program that is trying to act like a VST host for VSTI plugins.

My program also has to talk to a webcam and do some image manipulation stuff too and I suspect that it's all too much for my poor old PC and that it ends up in a black hole, always trying to catch up with itself and never succeeding.

Can anyone suggest the most efficient way to monitor and prevent problems when cpu usage gets too much for it.

Many thanks for your consideration.

Relevant bit of my code...

namespace AudioIO
{
    class AudioOutput : IDisposable
    {
        public IWavePlayer playbackDevice = null;
        private VSTStream vstStream = null;
        private IVstPluginCommandStub plugin;
        private int sample_rate, mono_stereo;

        public AudioOutput(IVstPluginCommandStub plugin, int sample_rate, int mono_stereo)
        {
            this.plugin = plugin;
            this.sample_rate = sample_rate;
            this.mono_stereo = mono_stereo;

            Init();
        }

        public void Init()
        {
            vstStream = new VSTStream(this.plugin, sample_rate, mono_stereo);
            vstStream.SetWaveFormat(sample_rate, mono_stereo);
            
            playbackDevice = new WaveOut(WaveCallbackInfo.FunctionCallback());
            playbackDevice.Init(vstStream);
        }

        public void volume(float vol_in)
        {
            playbackDevice.Volume = vol_in;
        }

        public void Dispose()
        {
            if (playbackDevice != null)
            {
                playbackDevice.Pause();
                playbackDevice.Stop();
                playbackDevice.Dispose();
                playbackDevice = null;
            }
        }

        public void Stop()
        {
            if (playbackDevice != null && playbackDevice.PlaybackState != PlaybackState.Stopped)
                playbackDevice.Pause();
                playbackDevice.Stop();
        }

        public void Play()
        {
            if (playbackDevice != null && playbackDevice.PlaybackState != PlaybackState.Playing)
                playbackDevice.Play();
        }

        public void Pause()
        {
            if (playbackDevice != null && playbackDevice.PlaybackState != PlaybackState.Paused)
                playbackDevice.Pause();
        }
    }
    
    class VSTStream : WaveProvider32
    {
        public long Length { get { throw new System.NotSupportedException(); } }
        public long Position { get { throw new System.NotSupportedException(); } set { throw new System.NotSupportedException(); } }

        public IVstPluginCommandStub plugin;
        private int sample_rate, mono_stereo;
        VstAudioBufferManager vstBufManIn, vstBufManOut;
        private VstAudioBuffer[] vstBufIn;
        private VstAudioBuffer[] vstBufOut;
       
        public VSTStream(IVstPluginCommandStub plugin, int sample_rate, int mono_stereo)
        {
            this.plugin = plugin;
            this.sample_rate = sample_rate;
            this.mono_stereo = mono_stereo;
            int block_size = sample_rate / 10;
            
            plugin.SetBlockSize(block_size);
            plugin.SetSampleRate((float)sample_rate);
            plugin.GetOutputProperties(0);
            
            vstBufManIn = new VstAudioBufferManager(2, block_size);
            vstBufManOut = new VstAudioBufferManager(2, block_size);

            vstBufIn = vstBufManIn.ToArray();
            vstBufOut = vstBufManOut.ToArray();
        }

        public override int Read(float[] buffer, int offset, int sampleCount)
        {
            plugin.ProcessEvents(freedom_music_v1.form1.vMidiEvents.ToArray());
            freedom_music_v1.form1.vMidiEvents.Clear();

            plugin.ProcessReplacing(vstBufIn, vstBufOut);

            {
                float this_vol = freedom_music_v1.form1.main_volume;

                unsafe
                {
                    if (mono_stereo == 2)
                    {
                        float* tmpBufL = ((IDirectBufferAccess32)vstBufOut[0]).Buffer;
                        float* tmpBufR = ((IDirectBufferAccess32)vstBufOut[1]).Buffer;
                        int j = 0;
                        for (int i = 0; i < (sampleCount / 2); i++)
                        {
                            buffer[j] = (*(tmpBufL + i)) * this_vol;
                            j++;
                            buffer[j] = (*(tmpBufR + i)) * this_vol;
                            j++;
                        }
                    }
                    else
                    {
                        float* tmpBufL = ((IDirectBufferAccess32)vstBufOut[0]).Buffer;
                        for (int i = 0; i < (sampleCount); i++)
                        {
                            buffer[i] = (*(tmpBufL + i)) * this_vol;
                        }
                    }
                }
                return sampleCount;
            }
        }
    }
}

Coordinator
Jul 26, 2011 at 9:29 AM

I would write an offline processing function that passes a WAV file through the VST effect. Then you can use a profiler to see what parts of your code are in need of optimisation.

Mark