This project has moved. For the latest updates, please go here.

Sample File Generator From Large file

Mar 17, 2010 at 3:08 PM

Hi,

I've been scratching my head and searching for like days regarding my problem unless I come across to NAudio. Its pretty awesome to run some test and using some sample code.

My problem may involves different module by NAudio, here is the scenario pseudo code,

Open Existing File WAV
Insert 1 second of silence sample into buffer
Loop until Read 10 seconds of File WAV into buffer = end of file

            Fade in from silence sample to content sample

            Insert 1 second of silence sample into buffer

            Fade out  from content sample to silence sample

            Skip Forward 1 minute

Next

 Save Sample File NEW WAV

So it clearly depicts that I would be playing with different module provided by NAudio to create a solution for my tasks. I just need some starting point for each of the modules such as

Fade in Fade out,
Inserting 1 second of silence,
Capturing specific xx amount of time from a long wave file and
Skipping xx minute of that wave file to continue ahead..

Please help :)

Mar 18, 2010 at 3:06 PM

Mark? Anyone?

Mar 25, 2010 at 11:49 AM

:(

Coordinator
Mar 25, 2010 at 3:53 PM

Hi roswell,

You've asked a lot of questions. Have you managed to do anything with NAudio yet?

To open a wav file use the WavFileReader class

To write to a wav file use the WavFileWriter class

Writing silence is nice and easy since it just involves putting lots of zeroes into the file.

To fade out requires some mathematics on each sample. You will need to work this out for yourself. NAudio only gives you access to the raw PCM samples.

Skipping forwards and backwards is easy with PCM data - just work out the number of bytes for the time you want to skip and then skip forwards that number of bytes

hope this gets you pointed in the right direction.

Mark

 

Jun 18, 2010 at 10:23 AM
Edited Jun 18, 2010 at 10:27 AM

Voila !!

I did it !!

Through a merger of this http://naudio.codeplex.com/Thread/View.aspx?ThreadId=57257 and your trimwave function, I have done it.  here is the rough code. Let me know if someone need any explanation

    private static void TrimWavFile(WaveStream reader, WaveFileWriter writer, int startPos, int endPos)
        {
            reader.Position = startPos;
            byte[] buffer = new byte[1024];
            int staticInterval = endPos;
            // byte[] silence = new byte[1024]; //new byte[524288] 10584000 176400;
            int silenceBytes = 0;

            int bytesPerMillisecond = reader.WaveFormat.AverageBytesPerSecond / 1000;
            int totalTime = reader.TotalTime.Minutes;
            int bytesPerminutes = reader.WaveFormat.AverageBytesPerSecond * 60;
            byte[] silence = new byte[reader.WaveFormat.AverageBytesPerSecond];
            int escape = totalTime / 2;
            int i = 0;
            int check = 0;
            int checkData = 0;

            long threeSeconds = bytesPerMillisecond * 3000;
            long fadoutSeconds = bytesPerMillisecond * 3000;

            do
            {

                fadeouteffect = 0.01;//.001;
                fadeineffect = 0.7;//.001;
                do
                {


                    int bytesRead = reader.Read(buffer, 0, buffer.Length);
                    if (bytesRead > 0)
                    {

                        // byte[] newbuffer = buffer;
                        // newbuffer = GetFadeOutBuffer(newbuffer, threeSecondsCount);

                        for (int j = 0; j < buffer.Length; j += 2)
                        {

                            short snd = ComplementToSigned(ref buffer, j);

                            if (fadeouteffect >= 0.7)
                                fadeouteffect = 0.7;

                            //if (fadeouteffect <= .01)
                            //    fadeouteffect = .01;

                            snd = (short)(snd * fadeouteffect);
                            byte[] changed = SignedToComplement(snd);
                            if ((changed[0] != null) && (changed[1] != null))
                            {
                                buffer[j] = changed[0];
                                buffer[j + 1] = changed[1];

                            }
                        }
                        fadeouteffect = ((fadeouteffect + 0.002) - 0.0001);
                        writer.WriteData(buffer, 0, bytesRead);

                    }

                    if (reader.Position >= fadoutSeconds)
                        break;
                } while (reader.Position < endPos);

                //end fadeout


                int endPosDifference = (int)(endPos - threeSeconds);

                while (reader.Position < endPos)
                {
                    int bytesRequired = (int)(endPos - reader.Position);
                    check = bytesRequired;

                    if (bytesRequired > 0)
                    {
                        int bytesToRead = Math.Min(bytesRequired, buffer.Length);
                        int bytesRead = reader.Read(buffer, 0, bytesToRead);
                        silenceBytes = bytesRead;

                        if (bytesRead > 0)
                        {
                            if (reader.Position > endPosDifference)
                            {
                                for (int j = 0; j < buffer.Length; j += 2)
                                {
                                    short snd = ComplementToSigned(ref buffer, j);

                                    if (fadeineffect <= .01)
                                        fadeineffect = .01;

                                    snd = (short)(snd * fadeineffect);
                                    byte[] changed = SignedToComplement(snd);
                                    if ((changed[0] != null) && (changed[1] != null))
                                    {
                                        buffer[j] = changed[0];
                                        buffer[j + 1] = changed[1];

                                    }
                                }
                                fadeineffect = ((fadeineffect - 0.002)+0.0001);
                            }
                            writer.WriteData(buffer, 0, bytesRead);
                        }
                    }
                }


                writer.WriteData(silence, 0, reader.WaveFormat.AverageBytesPerSecond);


                //i = i + 1;
                totalTime -= 2;


                int startPosMilliValue = startPos / bytesPerMillisecond;


                startPos = (60000 * bytesPerMillisecond) + endPos;  //1 minutes = 60000 milliseconds
                endPos = (bytesPerMillisecond * 10000) + startPos; //(int)cutFromEnd.TotalMilliseconds * bytesPerMillisecond;
                reader.Position = startPos;

                fadoutSeconds = (bytesPerMillisecond * 3000) + startPos;
                //  endPos = (bytesPerMillisecond * 13000) + startPos;

                checkData = reader.Read(buffer, 0, check);

            } while (reader.Length > endPos);

        }

 

Now I'm gonna make a windows service that will run on Win2008 64 bit, I hope building NAudio on x86 along with service on x86 will run things smooth.

Thanks Mark, Im sure I've saved your blog entry? :D

Coordinator
Jun 29, 2010 at 9:18 AM

glad you got it working :)

Mark