Example #1
0
        private void GenerateNoiseBtn_Click(object sender, EventArgs e)
        {
            // plugin does not support processing audio
            if ((PluginContext.PluginInfo.Flags & VstPluginFlags.CanReplacing) == 0)
            {
                MessageBox.Show(this, "This plugin does not process any audio.", this.Text, MessageBoxButtons.OK, MessageBoxIcon.Exclamation);
                return;
            }

            int inputCount  = PluginContext.PluginInfo.AudioInputCount;
            int outputCount = PluginContext.PluginInfo.AudioOutputCount;
            int blockSize   = 1024;

            // wrap these in using statements to automatically call Dispose and cleanup the unmanaged memory.
            using (VstAudioBufferManager inputMgr = new VstAudioBufferManager(inputCount, blockSize))
            {
                using (VstAudioBufferManager outputMgr = new VstAudioBufferManager(outputCount, blockSize))
                {
                    foreach (VstAudioBuffer buffer in inputMgr.ToArray())
                    {
                        Random rnd = new Random((int)DateTime.Now.Ticks);

                        for (int i = 0; i < blockSize; i++)
                        {
                            // generate a value between -1.0 and 1.0
                            buffer[i] = (float)((rnd.NextDouble() * 2.0) - 1.0);
                        }
                    }

                    PluginContext.PluginCommandStub.SetBlockSize(blockSize);
                    PluginContext.PluginCommandStub.SetSampleRate(44100f);

                    VstAudioBuffer[] inputBuffers  = inputMgr.ToArray();
                    VstAudioBuffer[] outputBuffers = outputMgr.ToArray();

                    PluginContext.PluginCommandStub.MainsChanged(true);
                    PluginContext.PluginCommandStub.StartProcess();
                    PluginContext.PluginCommandStub.ProcessReplacing(inputBuffers, outputBuffers);
                    PluginContext.PluginCommandStub.StopProcess();
                    PluginContext.PluginCommandStub.MainsChanged(false);

                    for (int i = 0; i < inputBuffers.Length && i < outputBuffers.Length; i++)
                    {
                        for (int j = 0; j < blockSize; j++)
                        {
                            if (inputBuffers[i][j] != outputBuffers[i][j])
                            {
                                if (outputBuffers[i][j] != 0.0)
                                {
                                    MessageBox.Show(this, "The plugin has processed the audio.", this.Text, MessageBoxButtons.OK, MessageBoxIcon.Information);
                                    return;
                                }
                            }
                        }
                    }

                    MessageBox.Show(this, "The plugin has passed the audio unchanged to its outputs.", this.Text, MessageBoxButtons.OK, MessageBoxIcon.Information);
                }
            }
        }
        public override int Read(float[] buffer, int offset, int samplesRequested)
        {
            int sampleCount = samplesRequested / 2;

            plugin.SetBlockSize(sampleCount);
            plugin.SetSampleRate(WaveFormat.SampleRate);
            plugin.SetProcessPrecision(VstProcessPrecision.Process32);

            using (VstAudioBufferManager inputMgr = new VstAudioBufferManager(info.AudioInputCount, samplesRequested))
                using (VstAudioBufferManager outputMgr = new VstAudioBufferManager(info.AudioOutputCount, samplesRequested))
                {
                    {
                        vstBufIn  = inputMgr.ToArray();
                        vstBufOut = outputMgr.ToArray();

                        plugin.StartProcess();
                        plugin.ProcessReplacing(vstBufIn, vstBufOut);
                        plugin.StopProcess();

                        int i = 0, j = 0;
                        while (j < samplesRequested / 2)
                        {
                            buffer[i++] = vstBufOut[0][j];
                            buffer[i++] = vstBufOut[1][j++];
                        }
                    }
                }
            Debug.Print("Requested: {0}, Returned: {1}, Offset: {2}", samplesRequested, vstBufOut[0].SampleCount, offset);
            return(samplesRequested);
        }
Example #3
0
        public int Read(float[] buffer, int offset, int count)
        {
            int channelsCount = WaveFormat.Channels;
            int size          = count / channelsCount;

            using (var outputMgr = new VstAudioBufferManager(channelsCount, size))
            {
#pragma warning disable 618
                var outputBuffers = outputMgr.ToArray();
#pragma warning restore 618

                _plugin.PluginCommandStub.SetBlockSize(count / channelsCount);
                _plugin.PluginCommandStub.ProcessReplacing(null, outputBuffers);

                int outIndex = 0;
                for (var i = 0; i < count / channelsCount; ++i)
                {
                    foreach (var outputBuffer in outputBuffers)
                    {
                        buffer[outIndex++] = outputBuffer[i];
                    }
                }

                _plugin.PluginCommandStub.EditorIdle();
            }

            return(count);
        }
Example #4
0
        /// <summary>
        /// we need to reflect the end of time in bars, or what-ever loop points are set.
        /// </summary>
        /// <param name="blockSize"></param>
        private void UpdateBlockSize(int blockSize)
        {
            VstAudioBufferManager ii = null, io = null, ei = null, eo = null;

            BlockSize = blockSize;
            ii        = new VstAudioBufferManager(instrument.PluginInfo.AudioInputCount, blockSize);
            io        = new VstAudioBufferManager(instrument.PluginInfo.AudioOutputCount, blockSize);

            if (effect != null)
            {
                ei = new VstAudioBufferManager(effect.PluginInfo.AudioInputCount, blockSize);
                eo = new VstAudioBufferManager(effect.PluginInfo.AudioOutputCount, blockSize);
            }

            UpdateBlock(instrument, blockSize);
            if (effect != null)
            {
                UpdateBlock(effect, blockSize);
            }

            insI = ii.ToArray();
            insO = io.ToArray();
            effI = ei.ToArray();
            effO = eo.ToArray();

            input  = new float[WaveFormat.Channels * blockSize];
            output = new float[WaveFormat.Channels * blockSize];
        }
Example #5
0
        public VSTStream(int sampleRate, int channels, int blockSize, List <IVstPluginCommandStub> plugins, string waveFilePath)
            : base(sampleRate, channels)
        {
            this.plugins      = plugins;
            this.sampleRate   = sampleRate;
            this.channels     = channels;
            this.blockSize    = blockSize;
            this.waveFilePath = waveFilePath;

            plugins[0].SetBlockSize(blockSize);
            plugins[0].SetSampleRate((float)sampleRate);
            plugins[0].SetProcessPrecision(VstProcessPrecision.Process32);

            vstBufManIn  = new VstAudioBufferManager(channels, blockSize);
            vstBufManOut = new VstAudioBufferManager(channels, blockSize);

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

            // 4 bytes per sample (32 bit)
            naudioBuf = new byte[blockSize * channels * 4];

            wavStream        = new WaveChannel32(new WaveFileReader(waveFilePath));
            wavStream.Volume = 1f;
        }
Example #6
0
        private void PrepareBuffers(int count)
        {
            if (count == _totalSamples)
            {
                return;
            }

            System.Diagnostics.Debug.WriteLine($"VstSampleProvider::PrepareBuffers [{_plugin.PluginCommandStub.GetEffectName()}] count changed from {_totalSamples} to {count}. Replacing current buffers.");

            _inBufMgr?.Dispose();
            _outBufMgr?.Dispose();

            int samplesPerBuffer = count / NUM_CHANNEL;

            _tempBuf   = new float[count];
            _inBufMgr  = new VstAudioBufferManager(_inputCount, samplesPerBuffer);
            _outBufMgr = new VstAudioBufferManager(_outputCount, samplesPerBuffer);

#pragma warning disable CS0618 // Type or member is obsolete
            _inBuf  = _inBufMgr.ToArray();
            _outBuf = _outBufMgr.ToArray();
#pragma warning restore CS0618 // Type or member is obsolete

            _totalSamples = count;
        }
Example #7
0
        void UpdateBlockSize(NAudioVST naudiovst, VstPlugin plugin, AudioProcess parent)
        {
            VstAudioBufferManager io = new VstAudioBufferManager(plugin.PluginInfo.AudioOutputCount, BlockSize);

            UpdateBlock(naudiovst, plugin);
            binput  = parent.boutput;
            boutput = io.ToArray();
        }
Example #8
0
        protected void Load(string filename)
        {
            if (File.Exists(filename))
            {
                PluginContext = OpenPlugin(filename);
                if (PluginContext == null)
                {
                    return;
                }

                SetOutputCount(PluginContext.PluginInfo.AudioOutputCount);

                PluginContext.PluginCommandStub.MainsChanged(true);
                PluginContext.PluginCommandStub.SetSampleRate(WaveFormat.SampleRate);
                PluginContext.PluginCommandStub.SetBlockSize(AudioService.Engine.Settings.BufferSize);
                FIsSynth = PluginContext.PluginInfo.Flags.HasFlag(VstPluginFlags.IsSynth);

                PluginContext.PluginCommandStub.StartProcess();

                FInputCount  = PluginContext.PluginInfo.AudioInputCount;
                FOutputCount = PluginContext.PluginInfo.AudioOutputCount;

                FInputMgr  = new VstAudioBufferManager(FInputCount, AudioService.Engine.Settings.BufferSize);
                FOutputMgr = new VstAudioBufferManager(FOutputCount, AudioService.Engine.Settings.BufferSize);

                FInputBuffers  = FInputMgr.ToArray();
                FOutputBuffers = FOutputMgr.ToArray();

                // plugin does not support processing audio
                if ((PluginContext.PluginInfo.Flags & VstPluginFlags.CanReplacing) == 0)
                {
                    MessageBox.Show("This plugin does not process any audio.");
                    return;
                }

                FCanEvents = PluginContext.PluginCommandStub.CanDo("receiveVstMidiEvent") == VstCanDoResult.Yes;

                InfoForm = new PluginInfoForm();
                InfoForm.PluginContext = PluginContext;
                InfoForm.DataToForm();
                InfoForm.Dock            = DockStyle.Fill;
                InfoForm.ParameterCheck += InfoForm_ParameterCheck;

                GetPluginInfo();
                GetProgramNames();

                if (PluginChanged != null)
                {
                    PluginChanged();
                }

                FDoProcess = true;
            }
        }
Example #9
0
        void UpdateBlockSize(NAudioVST naudiovst, VstPlugin plugin)
        {
            VstAudioBufferManager ii = null, io = null;

            ii = new VstAudioBufferManager(plugin.PluginInfo.AudioInputCount, BlockSize);
            io = new VstAudioBufferManager(plugin.PluginInfo.AudioOutputCount, BlockSize);

            UpdateBlock(naudiovst, plugin);

            binput  = ii.ToArray();
            boutput = io.ToArray();
        }
Example #10
0
        static void Method(VstPluginContext PluginContext, int bytesWritten, byte[] destBuffer)
        {
            int inputCount  = PluginContext.PluginInfo.AudioInputCount;
            int outputCount = PluginContext.PluginInfo.AudioOutputCount;
            int blockSize   = bytesWritten;

            VstAudioBufferManager inputMgr  = new VstAudioBufferManager(inputCount, blockSize);
            VstAudioBufferManager outputMgr = new VstAudioBufferManager(outputCount, blockSize);

            foreach (VstAudioBuffer buffer in inputMgr.ToArray())
            {
                for (int i = 0; i < blockSize; i++)
                {
                    buffer[i] = (float)destBuffer[i] / 128.0f - 1.0f;
                }
            }

            PluginContext.PluginCommandStub.SetBlockSize(blockSize);
            PluginContext.PluginCommandStub.SetSampleRate(44.8f);

            PluginContext.PluginCommandStub.StartProcess();
            PluginContext.PluginCommandStub.ProcessReplacing(inputMgr.ToArray(), outputMgr.ToArray());
            PluginContext.PluginCommandStub.StopProcess();

            foreach (VstAudioBuffer buffer in outputMgr.ToArray())
            {
                for (int i = 0; i < blockSize; i++)
                {
                    destBuffer[i] = Convert.ToByte(((float)buffer[i] + 1.0f) * 128.0f);
                }
            }
            inputMgr.ClearBuffer(inputMgr.ToArray()[0]);
            inputMgr.ClearBuffer(inputMgr.ToArray()[1]);
            inputMgr.Dispose();
            outputMgr.ClearBuffer(outputMgr.ToArray()[0]);
            outputMgr.ClearBuffer(outputMgr.ToArray()[1]);
            outputMgr.Dispose();
        }
Example #11
0
        static void Method(VstPluginContext PluginContext, int bytesWritten, byte[] destBuffer)
        {
            int inputCount = PluginContext.PluginInfo.AudioInputCount;
            int outputCount = PluginContext.PluginInfo.AudioOutputCount;
            int blockSize = bytesWritten;

            VstAudioBufferManager inputMgr = new VstAudioBufferManager(inputCount, blockSize);
            VstAudioBufferManager outputMgr = new VstAudioBufferManager(outputCount, blockSize);

            foreach (VstAudioBuffer buffer in inputMgr.ToArray())
            {
                for (int i = 0; i < blockSize; i++)
                {
                    buffer[i] = (float)destBuffer[i] / 128.0f - 1.0f;
                }
            }

            PluginContext.PluginCommandStub.SetBlockSize(blockSize);
            PluginContext.PluginCommandStub.SetSampleRate(44.8f);

            PluginContext.PluginCommandStub.StartProcess();
            PluginContext.PluginCommandStub.ProcessReplacing(inputMgr.ToArray(), outputMgr.ToArray());
            PluginContext.PluginCommandStub.StopProcess();

            foreach (VstAudioBuffer buffer in outputMgr.ToArray())
            {
                for (int i = 0; i < blockSize; i++)
                {
                    destBuffer[i] = Convert.ToByte(((float)buffer[i] + 1.0f) * 128.0f);
                }
            }
            inputMgr.ClearBuffer(inputMgr.ToArray()[0]);
            inputMgr.ClearBuffer(inputMgr.ToArray()[1]);
            inputMgr.Dispose();
            outputMgr.ClearBuffer(outputMgr.ToArray()[0]);
            outputMgr.ClearBuffer(outputMgr.ToArray()[1]);
            outputMgr.Dispose();
        }
Example #12
0
        protected void ManageBuffers(int count)
        {
            if (FInputMgr.BufferSize != count)
            {
                FInputMgr.Dispose();
                FOutputMgr.Dispose();

                FInputMgr  = new VstAudioBufferManager(FInputCount, count);
                FOutputMgr = new VstAudioBufferManager(FOutputCount, count);

                FInputBuffers  = FInputMgr.ToArray();
                FOutputBuffers = FOutputMgr.ToArray();
            }
        }
Example #13
0
        private void InitBuffer(int inputCount, int outputCount, int blockSize, int sampleRate)
        {
            VstAudioBufferManager inputMgr  = new VstAudioBufferManager(inputCount, blockSize);
            VstAudioBufferManager outputMgr = new VstAudioBufferManager(outputCount, blockSize);

            this.vstInputBuffers  = inputMgr.ToArray();
            this.vstOutputBuffers = outputMgr.ToArray();

            this.PluginContext.PluginCommandStub.SetBlockSize(blockSize);
            this.PluginContext.PluginCommandStub.SetSampleRate((float)sampleRate);
            this.PluginContext.PluginCommandStub.SetProcessPrecision(VstProcessPrecision.Process32);

            this.lastProcessedBufferRight = new float[BlockSize];
            this.lastProcessedBufferLeft  = new float[BlockSize];
        }
Example #14
0
        private void GetSound(BinaryReader reader)
        {
            var blockSize   = reader.ReadInt32();
            var eventsCount = reader.ReadInt32();

            int inputCount  = ctx.PluginInfo.AudioInputCount;
            int outputCount = ctx.PluginInfo.AudioOutputCount;

            VstAudioBufferManager inputMgr  = new VstAudioBufferManager(inputCount, blockSize);
            VstAudioBufferManager outputMgr = new VstAudioBufferManager(outputCount, blockSize);

            VstAudioBuffer[] inputBuffers  = inputMgr.ToArray();
            VstAudioBuffer[] outputBuffers = outputMgr.ToArray();

            ctx.PluginCommandStub.MainsChanged(true);

            for (var i = 0; i < eventsCount; i++)
            {
                SetEvent(reader.ReadInt32(), reader.ReadInt32(), reader.ReadInt32(), new byte[] { reader.ReadByte(), reader.ReadByte(), reader.ReadByte(), reader.ReadByte() }, reader.ReadInt16(), reader.ReadByte());
            }

            ctx.PluginCommandStub.StartProcess();
            ctx.PluginCommandStub.ProcessReplacing(inputBuffers, outputBuffers);

            var stream = new BinaryWriter(Console.OpenStandardOutput());

            if (outputCount == 1)
            {
                for (var i = 0; i < blockSize; i++)
                {
                    stream.Write(outputBuffers[0][i]);
                    stream.Write(outputBuffers[0][i]);
                }
            }
            else
            {
                for (var i = 0; i < blockSize; i++)
                {
                    stream.Write(outputBuffers[0][i]);
                    stream.Write(outputBuffers[1][i]);
                }
            }
        }
Example #15
0
 public Plugin(string pluginPath)
 {
     bufferSize = Settings.GetInstance().BufferSize;
     sampleRate = Settings.GetInstance().SampleRate;
     _isMuted = false;
     HostCommandStub hcs = new HostCommandStub();
     _ctx = VstPluginContext.Create(pluginPath, hcs);
     _name = _ctx.PluginCommandStub.GetEffectName();
     _id = _name.GetHashCode() + DateTime.Now.Second;
     inMgr = new VstAudioBufferManager(_ctx.PluginInfo.AudioInputCount, bufferSize);
     outMgr = new VstAudioBufferManager(_ctx.PluginInfo.AudioOutputCount, bufferSize);
     inputBuffer = inMgr.ToArray();
     outputBuffer = outMgr.ToArray();
     _ctx.PluginCommandStub.SetBlockSize(bufferSize);
     _ctx.PluginCommandStub.SetSampleRate(sampleRate);
     pluginForm = new PluginWindow(_ctx);
     _ctx.PluginCommandStub.MainsChanged(true);
     _ctx.PluginCommandStub.StartProcess();
 }
Example #16
0
        public static void IdleLoop(IVstPluginContext ctx, int loops)
        {
            var outputCount = ctx.PluginInfo.AudioOutputCount;
            var inputCount  = ctx.PluginInfo.AudioInputCount;

            using (var inputMgr = new VstAudioBufferManager(inputCount, BlockSize))
            {
                using (var outputMgr = new VstAudioBufferManager(outputCount, BlockSize))
                {
                    var outputBuffers = outputMgr.ToArray();
                    var inputBuffers  = inputMgr.ToArray();
                    int k;

                    for (k = 0; k < loops; k++)
                    {
                        ctx.PluginCommandStub.ProcessReplacing(inputBuffers, outputBuffers);
                    }
                }
            }
        }
        private void UpdateBlockSize(int blockSize, IVstPluginContext plugin)
        {
            BlockSize = blockSize;

            int inputCount  = plugin.PluginInfo.AudioInputCount;
            int outputCount = plugin.PluginInfo.AudioOutputCount;

            VstAudioBufferManager inputMgr  = new VstAudioBufferManager(inputCount, blockSize);
            VstAudioBufferManager outputMgr = new VstAudioBufferManager(outputCount, blockSize);

            plugin.PluginCommandStub.SetBlockSize(blockSize);
            plugin.PluginCommandStub.SetSampleRate(WaveFormat.SampleRate);
            plugin.PluginCommandStub.SetProcessPrecision(VstProcessPrecision.Process32);

            inputBuffers  = inputMgr.ToArray();
            outputBuffers = outputMgr.ToArray();

            input  = new float[WaveFormat.Channels * blockSize];
            output = new float[WaveFormat.Channels * blockSize];
        }
Example #18
0
        public int Read(float[] buffer, int offset, int count)
        {
            VstAudioBufferManager inBufMgr  = new VstAudioBufferManager(2, count);
            VstAudioBufferManager outBufMgr = new VstAudioBufferManager(2, count);

            VstAudioBuffer[] inBuf  = inBufMgr.ToArray();
            VstAudioBuffer[] outBuf = outBufMgr.ToArray();

            _cmdstub.StartProcess();
            _cmdstub.ProcessReplacing(inBuf, outBuf);
            _cmdstub.StopProcess();

            for (int i = 0; i < count; i++)
            {
                buffer[i] = outBuf[i % 2][i];
            }

            inBufMgr.Dispose();
            outBufMgr.Dispose();

            return(count);
        }
Example #19
0
        private void PrepareBuffers(int count)
        {
            if (count == _totalSamples)
            {
                return;
            }

            _inBufMgr?.Dispose();
            _outBufMgr?.Dispose();

            int samplesPerBuffer = count / NUM_CHANNEL;

            _tempBuf   = new float[count];
            _inBufMgr  = new VstAudioBufferManager(NUM_CHANNEL, samplesPerBuffer);
            _outBufMgr = new VstAudioBufferManager(NUM_CHANNEL, samplesPerBuffer);

#pragma warning disable CS0618 // Type or member is obsolete
            _inBuf  = _inBufMgr.ToArray();
            _outBuf = _outBufMgr.ToArray();
#pragma warning restore CS0618 // Type or member is obsolete

            _totalSamples = count;
        }
Example #20
0
        private void GenerateNoiseBtn_Click(object sender, EventArgs e)
        {
            // plugin does not support processing audio
            if (( PluginContext.PluginInfo.Flags & VstPluginFlags.CanReplacing ) == 0)
            {
                MessageBox.Show(this, "This plugin does not process any audio.", this.Text, MessageBoxButtons.OK, MessageBoxIcon.Exclamation);
                return;
            }

            int inputCount = PluginContext.PluginInfo.AudioInputCount;
            int outputCount = PluginContext.PluginInfo.AudioOutputCount;
            int blockSize = 1024;

            // wrap these in using statements to automatically call Dispose and cleanup the unmanaged memory.
            using (VstAudioBufferManager inputMgr = new VstAudioBufferManager(inputCount, blockSize))
            {
                using (VstAudioBufferManager outputMgr = new VstAudioBufferManager(outputCount, blockSize))
                {
                    foreach (VstAudioBuffer buffer in inputMgr.ToArray())
                    {
                        Random rnd = new Random((int)DateTime.Now.Ticks);

                        for (int i = 0; i < blockSize; i++)
                        {
                            // generate a value between -1.0 and 1.0
                            buffer[i] = (float)((rnd.NextDouble() * 2.0) - 1.0);
                        }
                    }

                    PluginContext.PluginCommandStub.SetBlockSize(blockSize);
                    PluginContext.PluginCommandStub.SetSampleRate(44100f);

                    VstAudioBuffer[] inputBuffers = inputMgr.ToArray();
                    VstAudioBuffer[] outputBuffers = outputMgr.ToArray();

                    PluginContext.PluginCommandStub.MainsChanged(true);
                    PluginContext.PluginCommandStub.StartProcess();
                    PluginContext.PluginCommandStub.ProcessReplacing(inputBuffers, outputBuffers);
                    PluginContext.PluginCommandStub.StopProcess();
                    PluginContext.PluginCommandStub.MainsChanged(false);

                    for (int i = 0; i < inputBuffers.Length && i < outputBuffers.Length; i++)
                    {
                        for (int j = 0; j < blockSize; j++)
                        {
                            if (inputBuffers[i][j] != outputBuffers[i][j])
                            {
                                if (outputBuffers[i][j] != 0.0)
                                {
                                    MessageBox.Show(this, "The plugin has processed the audio.", this.Text, MessageBoxButtons.OK, MessageBoxIcon.Information);
                                    return;
                                }
                            }
                        }
                    }

                    MessageBox.Show(this, "The plugin has passed the audio unchanged to its outputs.", this.Text, MessageBoxButtons.OK, MessageBoxIcon.Information);
                }
            }
        }
Example #21
0
 /// <summary>
 /// assign input and output buffers to parameters
 /// </summary>
 /// <param name="ins"></param>
 /// <param name="outs"></param>
 public void SetBuffers(ref VstAudioBuffer[] ins, ref VstAudioBuffer[] outs)
 {
     ins  = Inputs.ToArray();
     outs = Outputs.ToArray();
 }
        public void AudioProcessing(byte[] audio)
        {
            VstPluginContext pluginContext = Plugins[1381135922].PluginContext;

            //foreach (var plugin in Plugins.Values)
            //{
            //	pluginContext = plugin.PluginContext;
            //}
            // plugin does not support processing audio
            if ((pluginContext.PluginInfo.Flags & VstPluginFlags.CanReplacing) == 0)
            {
                Debug.Print("This plugin does not process any audio.");
                return;
            }

            //TODO : converting byte array to float array
            //Console.WriteLine(audio.Length);
            //var floatArray = audio.Select(b => (float)Convert.ToDouble(b)).ToArray();
            //var floatArray = new float[audio.Length / sizeof(float)];
            //Buffer.BlockCopy(audio, 0, floatArray, 0, audio.Length);
            var floatArray = ConvertByteToFloat16(audio);
            //var floatArray2 = audio.Select(b => (float)Convert.ToDouble(b)).ToArray();

            int inputCount = pluginContext.PluginInfo.AudioInputCount;

            Debug.Print(pluginContext.PluginCommandStub.GetProductString() + " inputCount = " + inputCount);
            int outputCount = pluginContext.PluginInfo.AudioOutputCount;

            Debug.Print(pluginContext.PluginCommandStub.GetProductString() + " outputCount = " + inputCount);
            int blockSize = floatArray.Length;

            Console.WriteLine("Bytes = " + audio.Length + " Floats = " + blockSize);

            // wrap these in using statements to automatically call Dispose and cleanup the unmanaged memory.
            using (VstAudioBufferManager inputMgr = new VstAudioBufferManager(inputCount, blockSize))
            {
                using (VstAudioBufferManager outputMgr = new VstAudioBufferManager(outputCount, blockSize))
                {
                    Console.WriteLine(inputMgr.BufferCount + " " + inputMgr.BufferSize);
                    foreach (VstAudioBuffer buffer in inputMgr)
                    {
                        //buffer = ;
                        Random rnd = new Random((int)DateTime.Now.Ticks);

                        for (int i = 0; i < blockSize; i++)
                        {
                            buffer[i] = floatArray[i];
                        }
                    }

                    pluginContext.PluginCommandStub.SetBlockSize(blockSize);
                    pluginContext.PluginCommandStub.SetSampleRate(44100f);

                    //VstAudioBuffer input = new VstAudioBuffer(floatArray,blockSize,true);
                    VstAudioBuffer[] inputBuffers  = inputMgr.ToArray();
                    VstAudioBuffer[] outputBuffers = outputMgr.ToArray();

                    pluginContext.PluginCommandStub.MainsChanged(true);
                    pluginContext.PluginCommandStub.StartProcess();
                    pluginContext.PluginCommandStub.ProcessReplacing(inputBuffers, outputBuffers);
                    pluginContext.PluginCommandStub.StopProcess();
                    pluginContext.PluginCommandStub.MainsChanged(false);

                    //for (int i = 0; i < inputBuffers.Length && i < outputBuffers.Length; i++)
                    //{
                    Console.WriteLine(outputBuffers[0].SampleCount + " " + inputBuffers[0].SampleCount);
                    float[] array = new float[outputBuffers[0].SampleCount];
                    for (int j = 0; j < blockSize; j++)
                    {
                        array[j] = outputBuffers[0][j];
                        //if (inputBuffers[i][j] != outputBuffers[i][j])
                        //{
                        //	if (outputBuffers[i][j] != 0.0)
                        //	{
                        //		Debug.Print("The plugin has processed the audio.");
                        //		Console.WriteLine(outputBuffers.Length+" "+outputBuffers[i]);
                        //		return;
                        //	}
                        //}
                        //Console.WriteLine(inputBuffers[i][j] + " " + outputBuffers[i][j]);
                    }
                    var pcm         = new byte[array.Length * 2];
                    int sampleIndex = 0,
                        pcmIndex    = 0;

                    while (sampleIndex < array.Length)
                    {
                        var outsample = (short)(array[sampleIndex] * short.MaxValue);
                        pcm[pcmIndex]     = (byte)(outsample & 0xff);
                        pcm[pcmIndex + 1] = (byte)((outsample >> 8) & 0xff);

                        sampleIndex++;
                        pcmIndex += 2;
                    }
                    //}
                    //var byteArray = new byte[outputBuffers[0].SampleCount * 4];
                    //Buffer.BlockCopy(array, 0, byteArray, 0, byteArray.Length);
                    //var byteArray = new byte[array.Length * 2];
                    //Buffer.BlockCopy(array, 0, byteArray, 0, byteArray.Length);
                    //Console.WriteLine(byteArray.Length);
                    //var byteArray = array.Select(f => Convert.ToByte(f)).ToArray();
                    File.WriteAllBytes("Audio/ffffa20.mp3", pcm);
                    Debug.Print("The plugin has passed the audio unchanged to its outputs.");
                }
            }
        }
Example #23
0
        private void UpdateBlockSize(int blockSize)
        {
            BlockSize = blockSize;

            int outputCount = pluginContext.PluginInfo.AudioOutputCount;

            VstAudioBufferManager outputMgr = new VstAudioBufferManager(outputCount, blockSize);

            pluginContext.PluginCommandStub.SetSampleRate(44800f);
            pluginContext.PluginCommandStub.SetBlockSize(blockSize);

            inputBuffers = new VstAudioBuffer[0];
            outputBuffers = outputMgr.ToArray();

            output = new float[2 * blockSize];
        }
Example #24
0
        private void InitBuffer(int inputCount, int outputCount, int blockSize, int sampleRate)
        {
            VstAudioBufferManager inputMgr = new VstAudioBufferManager(inputCount, blockSize);
            VstAudioBufferManager outputMgr = new VstAudioBufferManager(outputCount, blockSize);

            this.vstInputBuffers = inputMgr.ToArray();
            this.vstOutputBuffers = outputMgr.ToArray();

            this.PluginContext.PluginCommandStub.SetBlockSize(blockSize);
            this.PluginContext.PluginCommandStub.SetSampleRate((float)sampleRate);
            this.PluginContext.PluginCommandStub.SetProcessPrecision(VstProcessPrecision.Process32);

            this.lastProcessedBufferRight = new float[BlockSize];
            this.lastProcessedBufferLeft = new float[BlockSize];
        }
        public void AudioProcessing(float[] audio)
        {
            VstPluginContext pluginContext = Plugins[1381135922].PluginContext;

            foreach (var plugin in Plugins.Values)
            {
                pluginContext = plugin.PluginContext;
            }

            //plugin does not support processing audio
            if ((pluginContext.PluginInfo.Flags & VstPluginFlags.CanReplacing) == 0)
            {
                Debug.Print("This plugin does not process any audio.");
                return;
            }


            int inputCount = pluginContext.PluginInfo.AudioInputCount;

            Debug.Print(pluginContext.PluginCommandStub.GetProductString() + " inputCount = " + inputCount);
            int outputCount = pluginContext.PluginInfo.AudioOutputCount;

            Debug.Print(pluginContext.PluginCommandStub.GetProductString() + " outputCount = " + inputCount);
            int blockSize = audio.Length;

            using (VstAudioBufferManager inputMgr = new VstAudioBufferManager(inputCount, blockSize))
            {
                using (VstAudioBufferManager outputMgr = new VstAudioBufferManager(outputCount, blockSize))
                {
                    Console.WriteLine(inputMgr.BufferCount + " " + inputMgr.BufferSize);
                    foreach (VstAudioBuffer buffer in inputMgr)
                    {
                        //buffer = ;
                        Random rnd = new Random((int)DateTime.Now.Ticks);

                        for (int i = 0; i < blockSize; i++)
                        {
                            buffer[i] = audio[i];
                        }
                    }

                    pluginContext.PluginCommandStub.SetBlockSize(blockSize);
                    pluginContext.PluginCommandStub.SetSampleRate(44100f);

                    VstAudioBuffer[] inputBuffers  = inputMgr.ToArray();
                    VstAudioBuffer[] outputBuffers = outputMgr.ToArray();

                    pluginContext.PluginCommandStub.MainsChanged(true);
                    pluginContext.PluginCommandStub.StartProcess();
                    pluginContext.PluginCommandStub.ProcessReplacing(inputBuffers, outputBuffers);
                    pluginContext.PluginCommandStub.StopProcess();
                    pluginContext.PluginCommandStub.MainsChanged(false);
                    float[] array = new float[outputBuffers[0].SampleCount];
                    //for (int i = 0; i < inputBuffers.Length && i < outputBuffers.Length; i++)
                    //{

                    for (int j = 0; j < blockSize; j++)
                    {
                        array[j] = outputBuffers[0][j];
                        //if (inputBuffers[i][j] != outputBuffers[i][j])
                        //{
                        //	if (outputBuffers[i][j] != 0.0)
                        //	{
                        //		Debug.Print("The plugin has processed the audio.");
                        //		Console.WriteLine(outputBuffers.Length + " " + outputBuffers[i]);
                        //		return;
                        //	}
                        //}

                        //Console.WriteLine(inputBuffers[i][j] + " " + outputBuffers[i][j]);
                    }
                    //}

                    var pcm         = new byte[array.Length * 2];
                    int sampleIndex = 0,
                        pcmIndex    = 0;

                    while (sampleIndex < array.Length)
                    {
                        var outsample = (short)(audio[sampleIndex] * short.MaxValue);
                        pcm[pcmIndex]     = (byte)(outsample & 0xff);
                        pcm[pcmIndex + 1] = (byte)((outsample >> 8) & 0xff);

                        sampleIndex++;
                        pcmIndex += 2;
                    }

                    //WaveFileWriter writer = new WaveFileWriter("example.mp3", this.);

                    using (FileStream bytetoimage = File.Create("Audio/Music.wav"))
                    {
                        bytetoimage.Write(pcm, 0, pcm.Length);                        //pass byte array here
                    }

                    using (var retMs = new MemoryStream())
                        using (var ms = new MemoryStream(pcm))
                            using (var rdr = new WaveFileReader(ms))
                                using (var wtr = new LameMP3FileWriter(retMs, rdr.WaveFormat, 128))
                                {
                                    rdr.CopyTo(wtr);
                                }

                    File.WriteAllBytes("Audio/someSong.mp3", pcm);
                    Debug.Print("The plugin has passed the audio unchanged to its outputs.");
                }
            }
        }
Example #26
0
        protected void Load(string filename)
        {
            if (File.Exists(filename))
            {

                PluginContext = OpenPlugin(filename);
                if(PluginContext == null) return;

                SetOutputCount(PluginContext.PluginInfo.AudioOutputCount);

                PluginContext.PluginCommandStub.MainsChanged(true);
                PluginContext.PluginCommandStub.SetSampleRate(WaveFormat.SampleRate);
                PluginContext.PluginCommandStub.SetBlockSize(AudioService.Engine.Settings.BufferSize);
                FIsSynth = PluginContext.PluginInfo.Flags.HasFlag(VstPluginFlags.IsSynth);
                
                PluginContext.PluginCommandStub.StartProcess();

                FInputCount = PluginContext.PluginInfo.AudioInputCount;
                FOutputCount = PluginContext.PluginInfo.AudioOutputCount;

                FInputMgr = new VstAudioBufferManager(FInputCount, AudioService.Engine.Settings.BufferSize);
                FOutputMgr = new VstAudioBufferManager(FOutputCount, AudioService.Engine.Settings.BufferSize);

                FInputBuffers = FInputMgr.ToArray();
                FOutputBuffers = FOutputMgr.ToArray();

                // plugin does not support processing audio
                if ((PluginContext.PluginInfo.Flags & VstPluginFlags.CanReplacing) == 0)
                {
                    MessageBox.Show("This plugin does not process any audio.");
                    return;
                }

                FCanEvents = PluginContext.PluginCommandStub.CanDo("receiveVstMidiEvent") == VstCanDoResult.Yes; 

                InfoForm = new PluginInfoForm();
                InfoForm.PluginContext = PluginContext;
                InfoForm.DataToForm();
                InfoForm.Dock = DockStyle.Fill;

                GetPluginInfo();
                GetProgramNames();

                if (PluginChanged != null)
                    PluginChanged();

                FDoProcess = true;
            }
        }
        public int Readx2x(float[] destBuffer, int position, int bytesRequested)
        {
            Debug.Print("VST Read Operation!!! {0}, {1}", position, bytesRequested);
            int inputCount  = PluginContext.PluginInfo.AudioInputCount;
            int outputCount = PluginContext.PluginInfo.AudioOutputCount;

            PluginContext.PluginCommandStub.SetBlockSize(bytesRequested);
            PluginContext.PluginCommandStub.SetSampleRate(WaveFormat.SampleRate);
            PluginContext.PluginCommandStub.SetProcessPrecision(VstProcessPrecision.Process32);

            using (VstAudioBufferManager inputMgr = new VstAudioBufferManager(inputCount, bytesRequested))
                using (VstAudioBufferManager outputMgr = new VstAudioBufferManager(outputCount, bytesRequested))
                {
//				if (IsProcessReplacing)
                    foreach (VstAudioBuffer buffer in inputMgr.ToArray())
                    {
                        for (int i = 0; i < bytesRequested; i++)
                        {
                            buffer[i] = destBuffer[i];
                        }
                    }

                    PluginContext.PluginCommandStub.ProcessReplacing(inputMgr.ToArray(), outputMgr.ToArray());

                    foreach (VstAudioBuffer buffer in outputMgr.ToArray())
                    {
                        for (int i = 0; i < bytesRequested; i++)
                        {
                            destBuffer[i] = (byte)buffer[i];
                            Debug.Print("index: {i}, data: {0}", i, buffer[i]);
                        }
                    }
                    lastPosition += bytesRequested;
                    position     += bytesRequested;
                    return(bytesRequested);
                    // The Original
                    // ---------------------------------------
//				foreach (VstAudioBuffer buffer in outputMgr.ToArray())
//					for (int i = 0; i < blockSize; i++)
//				{
//					destBuffer[i] = Convert.ToByte(((float)buffer[i] + 1.0f) * 128.0f);
//				}
                    // Jakobi's Process
                    // ---------------------------------------
//				for (int i = 0; i < inputBuffers.Length && i < outputBuffers.Length; i++)
//					for (int j = 0; j < blockSize; j++)
//						if (inputBuffers[i][j] != outputBuffers[i][j])
//							if (outputBuffers[i][j] != 0.0)
//				{
//					MessageBox.Show(this, "The plugin has processed the audio.", this.Text, MessageBoxButtons.OK, MessageBoxIcon.Information);
//					return;
//				}
                    // Continued Forum Post Code:
                    // ---------------------------------------
//			inputMgr.ClearBuffer(inputMgr.ToArray()[0]);
//			inputMgr.ClearBuffer(inputMgr.ToArray()[1]);
//			inputMgr.Dispose();
//			outputMgr.ClearBuffer(outputMgr.ToArray()[0]);
//			outputMgr.ClearBuffer(outputMgr.ToArray()[1]);
//			outputMgr.Dispose();
                }
        }
        public int Readx2x(float[] destBuffer, int position, int bytesRequested)
        {
            Debug.Print("VST Read Operation!!! {0}, {1}",position,bytesRequested);
            int inputCount = PluginContext.PluginInfo.AudioInputCount;
            int outputCount = PluginContext.PluginInfo.AudioOutputCount;

            PluginContext.PluginCommandStub.SetBlockSize(bytesRequested);
            PluginContext.PluginCommandStub.SetSampleRate(WaveFormat.SampleRate);
            PluginContext.PluginCommandStub.SetProcessPrecision(VstProcessPrecision.Process32);

            using (VstAudioBufferManager inputMgr = new VstAudioBufferManager(inputCount, bytesRequested))
                using (VstAudioBufferManager outputMgr = new VstAudioBufferManager(outputCount, bytesRequested))
            {
            //				if (IsProcessReplacing)
                foreach (VstAudioBuffer buffer in inputMgr.ToArray())
                    for (int i = 0; i < bytesRequested; i++)
                        buffer[i] = destBuffer[i];

                PluginContext.PluginCommandStub.ProcessReplacing(inputMgr.ToArray(), outputMgr.ToArray());

                foreach (VstAudioBuffer buffer in outputMgr.ToArray())
                    for (int i = 0; i < bytesRequested; i++)
                {
                    destBuffer[i] = (byte)buffer[i];
                    Debug.Print("index: {i}, data: {0}",i,buffer[i]);
                }
                lastPosition += bytesRequested;
                position += bytesRequested;
                return bytesRequested;
                // The Original
                // ---------------------------------------
            //				foreach (VstAudioBuffer buffer in outputMgr.ToArray())
            //					for (int i = 0; i < blockSize; i++)
            //				{
            //					destBuffer[i] = Convert.ToByte(((float)buffer[i] + 1.0f) * 128.0f);
            //				}
                // Jakobi's Process
                // ---------------------------------------
            //				for (int i = 0; i < inputBuffers.Length && i < outputBuffers.Length; i++)
            //					for (int j = 0; j < blockSize; j++)
            //						if (inputBuffers[i][j] != outputBuffers[i][j])
            //							if (outputBuffers[i][j] != 0.0)
            //				{
            //					MessageBox.Show(this, "The plugin has processed the audio.", this.Text, MessageBoxButtons.OK, MessageBoxIcon.Information);
            //					return;
            //				}
                // Continued Forum Post Code:
                // ---------------------------------------
            //			inputMgr.ClearBuffer(inputMgr.ToArray()[0]);
            //			inputMgr.ClearBuffer(inputMgr.ToArray()[1]);
            //			inputMgr.Dispose();
            //			outputMgr.ClearBuffer(outputMgr.ToArray()[0]);
            //			outputMgr.ClearBuffer(outputMgr.ToArray()[1]);
            //			outputMgr.Dispose();
            }
        }
Example #29
0
        /// <summary>
        ///
        /// </summary>
        public void ProcessCallback(int[][] buf, int samples)
        {
            if (samples == 0)
            {
                return;
            }

            using (VstAudioBufferManager bufA = new VstAudioBufferManager(2, samples))
                using (VstAudioBufferManager bufB = new VstAudioBufferManager(2, samples))
                {
                    lock (InstrumentBase.VstPluginContextLockObject)
                    {
                        bool processed = false;
                        foreach (var vp in this)
                        {
                            var ctx = vp.PluginContext;
                            if (ctx != null)
                            {
                                int idx = 0;
                                foreach (VstAudioBuffer vab in bufA)
                                {
                                    Parallel.ForEach(Partitioner.Create(0, samples), range =>
                                    {
                                        for (var i = range.Item1; i < range.Item2; i++)
                                        {
                                            vab[i] = (float)buf[idx][i] / 32767.0f;
                                        }
                                    });
                                    //for (int i = 0; i < samples; i++)
                                    //    vab[i] = (float)buf[idx][i] / (float)int.MaxValue;
                                    idx++;
                                }
                                break;
                            }
                        }

                        VstAudioBufferManager bufa = bufA;
                        VstAudioBufferManager bufb = bufA;
                        foreach (var vp in this)
                        {
                            var ctx = vp.PluginContext;
                            if (ctx != null)
                            {
                                ctx.Context.PluginCommandStub.SetBlockSize(samples);
                                ctx.Context.PluginCommandStub.ProcessReplacing(bufa.ToArray <VstAudioBuffer>(), bufb.ToArray <VstAudioBuffer>());
                                processed = true;
                            }
                            var tmp = bufa;
                            bufa = bufb;
                            bufb = tmp;
                        }

                        if (processed)
                        {
                            int idx = 0;
                            foreach (VstAudioBuffer vab in bufb)
                            {
                                Parallel.ForEach(Partitioner.Create(0, samples), range =>
                                {
                                    for (var i = range.Item1; i < range.Item2; i++)
                                    {
                                        buf[idx][i] = (int)(vab[i] * 32767.0f);
                                    }
                                });
                                //for (int i = 0; i < samples; i++)
                                //    buf[idx][i] = (int)(vab[i] * int.MaxValue);
                                idx++;
                            }
                        }
                    }
                }
        }
Example #30
0
        private void UpdateBlockSize(int blockSize)
        {
            BlockSize = blockSize;

            int inputCount = pluginContext.PluginInfo.AudioInputCount;
            int outputCount = pluginContext.PluginInfo.AudioOutputCount;

            VstAudioBufferManager inputMgr = new VstAudioBufferManager(inputCount, blockSize);
            VstAudioBufferManager outputMgr = new VstAudioBufferManager(outputCount, blockSize);

            pluginContext.PluginCommandStub.SetBlockSize(blockSize);
            pluginContext.PluginCommandStub.SetSampleRate(WaveFormat.SampleRate);
            pluginContext.PluginCommandStub.SetProcessPrecision(VstProcessPrecision.Process32);

            inputBuffers = inputMgr.ToArray();
            outputBuffers = outputMgr.ToArray();

            input = new float[WaveFormat.Channels * blockSize];
            output = new float[WaveFormat.Channels * blockSize];
        }
Example #31
0
        /// <summary>
        /// we need to reflect the end of time in bars, or what-ever loop points are set.
        /// </summary>
        /// <param name="blockSize"></param>
        private void UpdateBlockSize(int blockSize)
        {
            VstAudioBufferManager ii = null, io = null, ei = null, eo = null;
            BlockSize = blockSize;
            ii  = new VstAudioBufferManager(instrument.PluginInfo.AudioInputCount, blockSize);
            io = new VstAudioBufferManager(instrument.PluginInfo.AudioOutputCount, blockSize);

            if (effect!=null)
            {
                ei  = new VstAudioBufferManager(effect.PluginInfo.AudioInputCount, blockSize);
                eo = new VstAudioBufferManager(effect.PluginInfo.AudioOutputCount, blockSize);
            }

            UpdateBlock(instrument,blockSize);
            if (effect!=null) UpdateBlock(effect,blockSize);

            insI = ii.ToArray();
            insO = io.ToArray();
            effI = ei.ToArray();
            effO = eo.ToArray();

            input  = new float[WaveFormat.Channels * blockSize];
            output = new float[WaveFormat.Channels * blockSize];
        }
        public override int Read(float[] buffer, int offset, int samplesRequested)
        {
            int sampleCount = samplesRequested / 2;

            plugin.SetBlockSize(sampleCount);
            plugin.SetSampleRate(WaveFormat.SampleRate);
            plugin.SetProcessPrecision(VstProcessPrecision.Process32);

            using (VstAudioBufferManager inputMgr = new VstAudioBufferManager(info.AudioInputCount, samplesRequested))
                using (VstAudioBufferManager outputMgr = new VstAudioBufferManager(info.AudioOutputCount, samplesRequested))
            {
                {
                    vstBufIn = inputMgr.ToArray();
                    vstBufOut = outputMgr.ToArray();

                    plugin.StartProcess();
                    plugin.ProcessReplacing(vstBufIn,vstBufOut);
                    plugin.StopProcess();

                    int i = 0, j = 0;
                    while (j < samplesRequested/2)
                    {
                        buffer[i++] = vstBufOut[0][j];
                        buffer[i++] = vstBufOut[1][j++];
                    }
                }
            }
            Debug.Print("Requested: {0}, Returned: {1}, Offset: {2}",samplesRequested,vstBufOut[0].SampleCount,offset);
            return samplesRequested;
        }
Example #33
0
        public VSTStream(int sampleRate, int channels, int blockSize, List<IVstPluginCommandStub> plugins, string waveFilePath)
            : base(sampleRate, channels)
        {
            this.plugins = plugins;
            this.sampleRate = sampleRate;
            this.channels = channels;
            this.blockSize = blockSize;
            this.waveFilePath = waveFilePath;

            plugins[0].SetBlockSize(blockSize);
            plugins[0].SetSampleRate((float)sampleRate);
            plugins[0].SetProcessPrecision(VstProcessPrecision.Process32);

            vstBufManIn = new VstAudioBufferManager(channels, blockSize);
            vstBufManOut = new VstAudioBufferManager(channels, blockSize);

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

            // 4 bytes per sample (32 bit)
            naudioBuf = new byte[blockSize * channels * 4];

            wavStream = new WaveChannel32(new WaveFileReader(waveFilePath));
            wavStream.Volume = 1f;
        }
Example #34
0
		protected void ManageBuffers(int count)
		{
			if(FInputMgr.BufferSize != count)
			{
				FInputMgr.Dispose();
				FOutputMgr.Dispose();
				
				FInputMgr = new VstAudioBufferManager(FInputCount, count);
				FOutputMgr = new VstAudioBufferManager(FOutputCount, count);
				
				FInputBuffers = FInputMgr.ToArray();
				FOutputBuffers = FOutputMgr.ToArray();
			}
		}
Example #35
0
        void UpdateBlockSize( NAudioVST naudiovst, VstPlugin plugin )
        {
            VstAudioBufferManager ii = null, io = null;

            ii  = new VstAudioBufferManager(plugin.PluginInfo.AudioInputCount, BlockSize);
            io = new VstAudioBufferManager(plugin.PluginInfo.AudioOutputCount, BlockSize);

            UpdateBlock(naudiovst,plugin);

            binput = ii.ToArray();
            boutput = io.ToArray();
        }
Example #36
0
 void UpdateBlockSize( NAudioVST naudiovst, VstPlugin plugin, AudioProcess parent)
 {
     VstAudioBufferManager io = new VstAudioBufferManager(plugin.PluginInfo.AudioOutputCount, BlockSize);
     UpdateBlock(naudiovst,plugin);
     binput = parent.boutput;
     boutput = io.ToArray();
 }