Code Monkey home page Code Monkey logo

Comments (2)

D4rkF4ce avatar D4rkF4ce commented on September 28, 2024

Hy again,

yesterday I found a solution for my issue, but i dont know if its the best way to do this...

The methods GetWAVEData(), WaveHeaderIN() & WaveHeaderOUT() is from codeproject to get Wave datas without the wave header and write it back into the source wave file.
[http://www.codeproject.com/Articles/19590/WAVE-File-Processor-in-C]

This Methode is also from the codeproject sourcecode:

public bool ChangeVolume(string strPath, bool booIncrease, short shtPcnt)
        {
            if (strPath == null) strPath = "";
            if (strPath == "") return false;
            if (shtPcnt > 100) return false;

            CustomWaveProcessor wain = new CustomWaveProcessor();
            CustomWaveProcessor waout = new CustomWaveProcessor();

            waout.DataLength = waout.Length = 0;

            if (!wain.WaveHeaderIN(@strPath)) return false;

            waout.DataLength = wain.DataLength;
            waout.Length = wain.Length;

            waout.BitsPerSample = wain.BitsPerSample;
            waout.Channels = wain.Channels;
            waout.SampleRate = wain.SampleRate;

            byte[] arrfile = this.GetWAVEData(strPath);


            //change volume
            for (int j = 0; j < arrfile.Length; j += 2)
            {
                short snd = this.ComplementToSigned(ref arrfile, j);
                try
                {
                    short p = Convert.ToInt16((snd * shtPcnt) / 100);
                    if (booIncrease)
                        snd += p;
                    else
                        snd -= p;
                }
                catch
                {
                    snd = this.ComplementToSigned(ref arrfile, j);
                }
                byte[] newval = this.SignedToComplement(snd);
                if ((newval[0] != null) && (newval[1] != null))
                {
                    arrfile[j] = newval[0];
                    arrfile[j + 1] = newval[1];
                }

            }

            //write back to the file
            waout.DataLength = arrfile.Length;
            waout.WaveHeaderOUT(@strPath);
            this.WriteWAVEData(strPath, ref arrfile);

            return true;
        }

With this Method i am able to get the current amplitude value of my wave file:

public double GetAmplitudeFromWave(string strPath)
        {
            byte[] _buffer = this.GetWAVEData(strPath);
            double peak = 0;

            for (var i = 0; i < _buffer.Length; i = i + 2)
            {
                var sample = BitConverter.ToInt16(_buffer, i);
                if (sample > peak)
                    peak = sample;
                else if (sample < -peak)
                    peak = -sample;
            }
            var decibel = (20 * Math.Log10(peak / 32768));
            return decibel;
        }

Finally i wrote this Method to set my custom amplitude value for my wave file:

public void ChangeAmplitude(string strPath, double outDb)
        {
            try
            {
                double amp = this.GetAmplitudeFromWave(strPath);

                Console.WriteLine("Current dB:" + amp);

                if (amp > outDb)
                {
                    while (amp > outDb)
                    {
                        this.ChangeVolume(strPath, false, 1);
                        amp = this.GetAmplitudeFromWave(strPath);
                    }
                }
                else if (amp < outDb)
                {
                    while (amp < outDb)
                    {
                        this.ChangeVolume(strPath, true, 1);
                        amp = this.GetAmplitudeFromWave(strPath);
                    }
                }

                Console.WriteLine("dB changed to: " + amp);
            }
            catch (Exception ex)
            {
                Console.WriteLine(ex.Message);
                Console.WriteLine(ex.StackTrace);
            }            
        }

What are you saying, is it OK to do it so or is there any other solution?

Thx,
Greetz

from naudio.

Freefall63 avatar Freefall63 commented on September 28, 2024

Really overcomplicated, I´d simply use a VolumeSampleProvider or would code a custom ISampleProvider.

from naudio.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.