Exemplo n.º 1
0
 public float[] boxBlurFloat(float[] input, uint radius, int runs = 1)
 {
     float[] output = new float[input.Length];
     for (int r = 0; r < runs; r++)
     {
         AverageHelper averageHelper = new AverageHelper();
         for (int i = 0; i < input.Length; i++)
         {
             if (i == 0)
             {
                 for (int ii = 0; ii <= radius; ii++)
                 {
                     if ((i + ii) >= input.Length)
                     {
                         break;
                     }
                     averageHelper.totalValue += input[i + ii];
                     averageHelper.multiplier += 1;
                 }
             }
             else
             {
                 if (i > radius)
                 {
                     averageHelper.totalValue -= input[i - radius - 1];
                     averageHelper.multiplier -= 1;
                 }
                 if ((i + radius) < input.Length)
                 {
                     averageHelper.totalValue += input[i + radius];
                     averageHelper.multiplier += 1;
                 }
             }
             output[i] = (float)(averageHelper.totalValue / averageHelper.multiplier);
         }
         input = output;
     }
     return(output);
 }
Exemplo n.º 2
0
        unsafe private void UpdateChartSubFrame(nui.AUDIO_SUBFRAME *pSubFrame)
        {
            if (this.outChart != null)
            {
                IntPtr ptrOut  = new IntPtr(pSubFrame) + cOutOffset;
                float *pFloats = (float *)ptrOut;
                float  average = 0.0f;

                for (uint i = 0; i < nui.Constants.AUDIO_SAMPLES_PER_SUBFRAME; ++i)
                {
                    average += Math.Abs(pFloats[i]);
                }

                average = average / nui.Constants.AUDIO_SAMPLES_PER_SUBFRAME;
                this.outChart.Update(average, pSubFrame->TimeCounter);
            }

            if (this.micCharts != null)
            {
                float[] averages = new float[this.micCharts.Length];
                IntPtr  ptrMic   = new IntPtr(pSubFrame) + cMicOffset;
                float * pFloats  = (float *)ptrMic;

#if OLD // less performant
                fixed(float *pAverages = &averages[0])
                {
                    AverageHelper.CalculateAbsAverageBy4((uint)nui.Constants.AUDIO_SAMPLES_PER_SUBFRAME * 4, pFloats, pAverages);
                }
#endif // OLD
                uint total = nui.Constants.AUDIO_SAMPLES_PER_SUBFRAME * 4;

                for (uint i = 0; i < total; i += 4)
                {
                    averages[0] += Math.Abs(pFloats[i]);
                    averages[1] += Math.Abs(pFloats[i + 1]);
                    averages[2] += Math.Abs(pFloats[i + 2]);
                    averages[3] += Math.Abs(pFloats[i + 3]);
                }

                for (int i = 0; i < 4; ++i)
                {
                    averages[i] /= nui.Constants.AUDIO_SAMPLES_PER_SUBFRAME;
                    this.micCharts[i].Update(averages[i], pSubFrame->TimeCounter);
                }
            }

            if (this.speakerCharts != null)
            {
                float[] averages   = new float[this.speakerCharts.Length];
                IntPtr  ptrSpeaker = new IntPtr(pSubFrame) + cSpeakerOffset;
                float * pFloats    = (float *)ptrSpeaker;
#if OLD
                fixed(float *pAverages = &averages[0])
                {
                    AverageHelper.CalculateAbsAverageBy8((uint)nui.Constants.AUDIO_SAMPLES_PER_SUBFRAME * 8, pFloats, pAverages);
                }
#endif // OLD
                uint total = nui.Constants.AUDIO_SAMPLES_PER_SUBFRAME * 8;
                for (uint i = 0; i < total; i += 8)
                {
                    averages[0] += Math.Abs(pFloats[i]);
                    averages[1] += Math.Abs(pFloats[i + 1]);
                    averages[2] += Math.Abs(pFloats[i + 2]);
                    averages[3] += Math.Abs(pFloats[i + 3]);
                    averages[4] += Math.Abs(pFloats[i + 4]);
                    averages[5] += Math.Abs(pFloats[i + 5]);
                    averages[6] += Math.Abs(pFloats[i + 6]);
                    averages[7] += Math.Abs(pFloats[i + 7]);
                }

                for (int i = 0; i < this.speakerCharts.Length; ++i)
                {
                    averages[i] /= nui.Constants.AUDIO_SAMPLES_PER_SUBFRAME;
                    this.speakerCharts[i].Update(averages[i], pSubFrame->TimeCounter);
                }
            }

            if (this.IsAudible && (this.audioPlayer != null))
            {
                int  offset = 0;
                uint stride = 0;

                switch (this.AudibleTrack)
                {
                case AudioTrack.Output:
                    stride = 1;
                    offset = AudioPlugin.cOutOffset;
                    break;

                case AudioTrack.Mic0:
                case AudioTrack.Mic1:
                case AudioTrack.Mic2:
                case AudioTrack.Mic3:
                    stride = (uint)this.micCharts.Length;
                    offset = AudioPlugin.cMicOffset + (sizeof(float) * (int)(this.AudibleTrack - AudioTrack.Mic0));
                    break;

                case AudioTrack.SpeakerL:
                case AudioTrack.SpeakerR:
                case AudioTrack.SpeakerC:
                case AudioTrack.SpeakerLFE:
                case AudioTrack.SpeakerBL:
                case AudioTrack.SpeakerBR:
                case AudioTrack.SpeakerSL:
                case AudioTrack.SpeakerSR:
                    stride = (uint)this.speakerCharts.Length;
                    offset = AudioPlugin.cSpeakerOffset + (sizeof(float) * (int)(this.AudibleTrack - AudioTrack.SpeakerL));
                    break;
                }

                if (stride != 0)
                {
                    IntPtr ptr     = new IntPtr(pSubFrame) + offset;
                    float *pFloats = (float *)ptr;

                    this.audioPlayer.PlayAudio(nui.Constants.AUDIO_SAMPLES_PER_SUBFRAME, pFloats, nui.Constants.AUDIO_SAMPLERATE, stride);
                }
            }
        }