示例#1
0
        private static void Update(WaveFormGenerator.WaveFormGeneratorData generatorData, WaveFormRendererData rendererData, bool rms, WaveFormRendererMode mode)
        {
            if (generatorData.Peak == 0)
            {
                return;
            }
            else if (generatorData.Peak != rendererData.Peak)
            {
                rendererData.Position       = 0;
                rendererData.Peak           = generatorData.Peak;
                rendererData.NormalizedPeak = GetPeak(generatorData, rendererData);
            }

            switch (mode)
            {
            case WaveFormRendererMode.Mono:
                UpdateMono(generatorData, rendererData, rms);
                break;

            case WaveFormRendererMode.Seperate:
                UpdateSeperate(generatorData, rendererData, rms);
                break;

            default:
                throw new NotImplementedException();
            }
        }
示例#2
0
        public void Save(IOutputStream stream, WaveFormGenerator.WaveFormGeneratorData data)
        {
            if (!this.Enabled.Value)
            {
                return;
            }

            var id = this.GetDataId(stream, data.Resolution);

            this.Save(id, data);
        }
示例#3
0
        public static float GetPeak(WaveFormGenerator.WaveFormGeneratorData generatorData, WaveFormRendererData rendererData)
        {
            var data             = generatorData.Data;
            var valuesPerElement = rendererData.ValuesPerElement;
            var peak             = rendererData.NormalizedPeak;

            var position = rendererData.Position;

            while (position < rendererData.Capacity)
            {
                var valuePosition = position * rendererData.ValuesPerElement;
                if ((valuePosition + rendererData.ValuesPerElement) > generatorData.Position)
                {
                    if (generatorData.Position <= generatorData.Capacity)
                    {
                        break;
                    }
                    else
                    {
                        valuesPerElement = generatorData.Capacity - valuePosition;
                    }
                }

                var value = default(float);
                for (var a = 0; a < valuesPerElement; a++)
                {
                    for (var b = 0; b < generatorData.Channels; b++)
                    {
                        value += Math.Max(
                            Math.Abs(data[valuePosition + a, b].Min),
                            Math.Abs(data[valuePosition + a, b].Max)
                            );
                    }
                }
                value /= (valuesPerElement * generatorData.Channels);

                peak = Math.Max(peak, value);

                if (peak >= 1)
                {
                    return(1);
                }

                position++;
            }

            return(peak);
        }
示例#4
0
 protected virtual bool TryLoad(string fileName, out WaveFormGenerator.WaveFormGeneratorData data)
 {
     try
     {
         using (var stream = File.OpenRead(fileName))
         {
             data = Serializer.Instance.Read(stream) as WaveFormGenerator.WaveFormGeneratorData;
             return(true);
         }
     }
     catch (Exception e)
     {
         Logger.Write(this, LogLevel.Warn, "Failed to load wave form from file \"{0}\": {1}", fileName, e.Message);
     }
     data = null;
     return(false);
 }
示例#5
0
 protected virtual void Save(string id, WaveFormGenerator.WaveFormGeneratorData data)
 {
     try
     {
         using (var stream = new MemoryStream())
         {
             Serializer.Instance.Write(stream, data);
             stream.Seek(0, SeekOrigin.Begin);
             FileMetaDataStore.Write(PREFIX, id, stream);
             stream.Seek(0, SeekOrigin.Begin);
         }
     }
     catch (Exception e)
     {
         Logger.Write(this, LogLevel.Warn, "Failed to save wave form: {0}", e.Message);
     }
 }
示例#6
0
        public static WaveFormRendererData Create(WaveFormGenerator.WaveFormGeneratorData generatorData, int width, int height)
        {
            var valuesPerElement = Convert.ToInt32(
                Math.Ceiling(
                    Math.Max(
                        (float)generatorData.Capacity / width,
                        1
                        )
                    )
                );

            return(new WaveFormRendererData()
            {
                Width = width,
                Height = height,
                ValuesPerElement = valuesPerElement,
                WaveElements = new Int32Pair[width, generatorData.Channels],
                PowerElements = new Int32Pair[width, generatorData.Channels],
                Channels = generatorData.Channels,
                Position = 0,
                Capacity = width,
                Peak = 0
            });
        }
示例#7
0
        private static void UpdateSeperate(WaveFormGenerator.WaveFormGeneratorData generatorData, WaveFormRendererData rendererData, bool rms)
        {
            var factor = rendererData.NormalizedPeak / generatorData.Channels;

            if (factor == 0)
            {
                //Peak has not been calculated.
                //I don't know how this happens, but it does.
                return;
            }

            var data             = generatorData.Data;
            var waveElements     = rendererData.WaveElements;
            var powerElements    = rendererData.PowerElements;
            var valuesPerElement = rendererData.ValuesPerElement;

            while (rendererData.Position < rendererData.Capacity)
            {
                var valuePosition = rendererData.Position * rendererData.ValuesPerElement;
                if ((valuePosition + rendererData.ValuesPerElement) > generatorData.Position)
                {
                    if (generatorData.Position <= generatorData.Capacity)
                    {
                        break;
                    }
                    else
                    {
                        valuesPerElement = generatorData.Capacity - valuePosition;
                    }
                }

                var waveHeight = rendererData.Height / generatorData.Channels;

                for (var channel = 0; channel < generatorData.Channels; channel++)
                {
                    var waveCenter = (waveHeight * channel) + (waveHeight / 2);

                    {
                        var y      = default(int);
                        var height = default(int);

                        var topValue    = default(float);
                        var bottomValue = default(float);
                        for (var a = 0; a < valuesPerElement; a++)
                        {
                            topValue    += Math.Abs(data[valuePosition + a, channel].Min);
                            bottomValue += Math.Abs(data[valuePosition + a, channel].Max);
                        }
                        topValue    /= (valuesPerElement * generatorData.Channels);
                        bottomValue /= (valuesPerElement * generatorData.Channels);

                        topValue    /= factor;
                        bottomValue /= factor;

                        topValue    = Math.Min(topValue, 1);
                        bottomValue = Math.Min(bottomValue, 1);

                        y      = Convert.ToInt32(waveCenter - (topValue * (waveHeight / 2)));
                        height = Convert.ToInt32((waveCenter - y) + (bottomValue * (waveHeight / 2)));

                        waveElements[rendererData.Position, channel].Y      = y;
                        waveElements[rendererData.Position, channel].Height = height;
                    }

                    if (rms)
                    {
                        var y      = default(int);
                        var height = default(int);

                        var value = default(float);
                        for (var a = 0; a < valuesPerElement; a++)
                        {
                            value += Math.Abs(data[valuePosition + a, channel].Rms);
                        }
                        value /= (valuesPerElement * generatorData.Channels);

                        value /= factor;

                        value = Math.Min(value, 1);

                        y      = Convert.ToInt32(waveCenter - (value * (waveHeight / 2)));
                        height = Convert.ToInt32((waveCenter - y) + (value * (waveHeight / 2)));

                        powerElements[rendererData.Position, channel].Y      = y;
                        powerElements[rendererData.Position, channel].Height = height;
                    }
                }

                rendererData.Position++;
            }
        }