Beispiel #1
0
    private RESULT myDSPCallback(ref DSP_STATE dsp_state, IntPtr inbuffer, IntPtr outbuffer, uint length, int inchannels, ref int outchannels)
    {
        if (btn.interactable)
        {
            return(RESULT.OK);
        }
        buffer            = new float[length * inchannels];
        singleSampleRange = ((currentSampleRate / 2) / length); // nyquist
        float currentSample;

        int i = 0;

        try
        {
            for (i = 0; i < length * inchannels; i++)
            {
                //test dostepu pamieci
                var currentPtr = new IntPtr(inbuffer.ToInt32() + (i * sizeof(float)));
                currentSample = (float)System.Runtime.InteropServices.Marshal.PtrToStructure(
                    currentPtr, typeof(float));

                float sampleOutput;

                sampleOutput = wahwah.ProcessSample(currentSample, (i / inchannels), (i % inchannels), currentSampleRate, inchannels);
                sampleOutput = lowpass.ProcessSample(sampleOutput, (i / inchannels), (i % inchannels), currentSampleRate, inchannels);

                buffer[i] = sampleOutput;
            }

            //kopiujemy caly bufor do wskaznika data
            System.Runtime.InteropServices.Marshal.Copy(buffer, 0, outbuffer, (int)length * inchannels);
            outchannels = inchannels;
        }
        catch (NullReferenceException nre)
        {
            UnityEngine.Debug.Log("Samples broke at sample:" + i);
            throw new NullReferenceException(nre.Message);
        }

        catch (IndexOutOfRangeException ioore)
        {
            UnityEngine.Debug.Log("Samples broke at sample:" + i);
            throw new NullReferenceException(ioore.Message);
        }

        return(RESULT.OK);
    }
Beispiel #2
0
    private RESULT myDSPCallback(ref DSP_STATE dsp_state, IntPtr inbuffer, IntPtr outbuffer, uint length, int inchannels, ref int outchannels)
    {
        buffer            = new float[length * inchannels];
        singleSampleRange = ((currentSampleRate / 2) / length); // nyquist
        float currentSample;

        int i = 0;

        try
        {
            for (i = 0; i < length * inchannels; i++)
            {
                //test dostepu pamieci
                var currentPtr = new IntPtr(inbuffer.ToInt32() + (i * sizeof(float)));
                currentSample = (float)System.Runtime.InteropServices.Marshal.PtrToStructure(
                    currentPtr, typeof(float));

                float sampleOutput;

                //Dla tylko jednego filtru - przerabiamy probke i oddajemy
                if (filters.Count > 0 && filters[0].isActive)
                {
                    sampleOutput = filters[0].ProcessSample(
                        currentSample, (i / inchannels), (i % inchannels), currentSampleRate, inchannels);
                }
                else
                {
                    sampleOutput = currentSample;
                }
                //Gdy filtrow jest wiecej, nakladamy kazdy po kolei
                if (filters.Count > 1)
                {
                    foreach (KeyValuePair <int, FilterBase> filter in filters)
                    {
                        sampleOutput = (filter.Value.isActive ? filter.Value.ProcessSample(
                                            sampleOutput, (i / inchannels), (i % inchannels), currentSampleRate, inchannels)
                            : sampleOutput);
                    }
                }

                buffer[i] = sampleOutput;

                if (currentSample <= totalMinBuff)
                {
                    totalMinBuff = currentSample;
                }
                if (currentSample >= totalMaxBuff)
                {
                    totalMaxBuff = currentSample;
                }
            }

            //kopiujemy caly bufor do wskaznika data
            System.Runtime.InteropServices.Marshal.Copy(buffer, 0, outbuffer, (int)length * inchannels);
            outchannels = inchannels;
        }
        catch (NullReferenceException nre)
        {
            UnityEngine.Debug.Log("Samples broke at sample:" + i);
            throw new NullReferenceException(nre.Message);
        }

        catch (IndexOutOfRangeException ioore)
        {
            UnityEngine.Debug.Log("Samples broke at sample:" + i);
            throw new NullReferenceException(ioore.Message);
        }
        //test

        /*float[] BUFFTEST = new float[length / sizeof(float)]; //Do przegladania w visualu
         *
         *
         * for (i = 0; i < length / sizeof(float); i++)
         * {
         *  var currentPtr = new IntPtr(outbuffer.ToInt32() + (i * sizeof(float)));
         *  BUFFTEST[i] = (float)System.Runtime.InteropServices.Marshal.PtrToStructure(
         *      currentPtr, typeof(float));
         * }*/

        return(RESULT.OK);
    }
        private RESULT DspReadCallback(ref DSP_STATE dspState, IntPtr inBuffer, IntPtr outBuffer, uint samples,
                                       int inChannels, ref int outChannels)
        {
            try
            {
                // Debug.Log($"Received buffer of samples: {samples}, channels: {inChannels}");
                Assert.AreEqual(inChannels, outChannels);

                const int sampleSizeBytes = 4;                           // size of a float
                int       blockSizeFloats = (int)(samples * inChannels); // size of a float
                int       blockSizeBytes  = blockSizeFloats * sampleSizeBytes;

                // Pass the input through to the output, so we can still hear it.
                unsafe
                {
                    Buffer.MemoryCopy(inBuffer.ToPointer(), outBuffer.ToPointer(),
                                      blockSizeBytes,
                                      blockSizeBytes);
                }

                // Copy the audio into our buffer queue
                lock (mixBlockQueue)
                {
                    // If we've queued up too many blocks, without writing them to the encoder, just stop recording audio.
                    // This can happen if the game pauses, and game Update()'s stop.
                    if (mixBlockQueueSize < MaxBlockQueueSize)
                    {
                        float[] buffer;
                        if (mixBlockQueueSize == mixBlockQueue.Count)
                        {
                            // Add a new buffer if there are no empty buffers left in the list.
                            buffer = new float[blockSizeFloats];
                            mixBlockQueue.Add(buffer);
                        }
                        else
                        {
                            // Use the next free buffer.
                            buffer = mixBlockQueue[mixBlockQueueSize];

                            // Reallocate the buffer if the block size has changed (could happen if the audio device changes).
                            if (buffer.Length != blockSizeFloats)
                            {
                                buffer = new float[blockSizeFloats];
                                mixBlockQueue[mixBlockQueueSize] = buffer;
                            }
                        }

                        mixBlockQueueSize++;

                        // Copy the audio to the block list.
                        unsafe
                        {
                            fixed(float *bufferPtr = buffer)
                            {
                                Buffer.MemoryCopy(inBuffer.ToPointer(), bufferPtr,
                                                  blockSizeBytes, blockSizeBytes);
                            }
                        }
                    }
                }
            }
            catch (Exception e)
            {
                Debug.LogError("There was an error with DSP Processing.");
                Debug.LogException(e);
                return(RESULT.ERR_DSP_DONTPROCESS);
            }

            return(RESULT.OK);
        }