예제 #1
0
        //spusti prevod souboru na wav,ktery je mozno vykreslovat - skonci po naplneni zobrazovaciho bufferu, pot
        public bool WaveChunksConversion(string aCesta, string aCestaDocasnychWAV, long aDelkaJednohoSouboruMS)
        {
            BinaryWriter output = null;
            try
            {
                //nulovani promennych
                this.temporaryWaveFiles.Clear();
                this._FileLengthMS = (long)WavReader.ReturnAudioLength(aCesta).TotalMilliseconds;

                if (aCesta == null || aCestaDocasnychWAV == null || FileLengthMS <= 0)
                    return false; //chybne nastaveni nebo delka souboru k prevodu

                this.TemporaryWAVsPath = aCestaDocasnychWAV;
                this.temporaryWaveFiles = new List<string>();

                delkaDocasnehoWav = (aDelkaJednohoSouboruMS / 1000) * this.Frequency;  //pocet vzorku v 1 docasnem souboru
                this.delkaDocasnehoWavMS = aDelkaJednohoSouboruMS;
                int pIndexDocasneho = -1;

                //vytvoren proces zapocato rucni vytvareni prevadeni docasneho souboru a plneni bufferu programu
                //po naplneni zobrazovaciho bufferu lze vykreslit vlnu v programu-

                prPrevod = new Process();
                prPrevod.StartInfo.FileName = FilePaths.FFmpegPath;
                prPrevod.StartInfo.WorkingDirectory = new FileInfo(aCesta).DirectoryName;
                prPrevod.StartInfo.Arguments = "-i \"" + aCesta + "\" -y -vn -ar 16000 -ac 1 -acodec pcm_s16le -f s16le -"; //nova data bez hlavicky
                prPrevod.StartInfo.RedirectStandardInput = true;
                prPrevod.StartInfo.RedirectStandardOutput = true;
                prPrevod.StartInfo.RedirectStandardError = false;
                prPrevod.StartInfo.UseShellExecute = false;
                prPrevod.StartInfo.CreateNoWindow = true;

                prPrevod.Start();
                //nacteni hlavicky - a alokovani pomocnych bufferu pro tuto operaci cteni

                byte[] buffer2 = new byte[4096];
                short[] buffer2s = new short[buffer2.Length / 2];

                ChannelCount = 1;

                this.Frequency = 16000;

                this.SampleSize = 2;

                long pPocetVzorku2Delta = Const.DELKA_PRVNIHO_RAMCE_ZOBRAZOVACIHO_BUFFERU_MS * (this.Frequency / 1000);
                SampleCount = pPocetVzorku2Delta;
                long pPocetVzorku2 = Const.DISPLAY_BUFFER_LENGTH_MS * (this.Frequency / 1000);

                //nacitani jednotlivych dat
                this._LoadedData = new Int16[pPocetVzorku2];

                int i = 0;

                short dato = 0;  //pomocna

                //prvni docasny wav soubor tmp
                pIndexDocasneho = 0;
                temporaryWaveFiles.Add(Path.Combine(TemporaryWAVsPath, pIndexDocasneho.ToString() + ".wav"));
                output = new BinaryWriter(new FileStream(temporaryWaveFiles[pIndexDocasneho], FileMode.Create));
                Stream outputbase = output.BaseStream;
                //prazdna hlavicka
                byte[] emptyHeader = new byte[44];
                output.Write(emptyHeader);
                //

                long pPocetNactenychVzorkuCelkem = 0;

                int pPocetNactenych = 1;    //pocatecni inicializace nacitani,aby neskoncil cyklus
                long j = 0; //pomocna pro indexaci v souborech

                Stream outstream = prPrevod.StandardOutput.BaseStream;
                for (i = 0; pPocetNactenych > 0; )
                {
                    //Termination
                    if (Interlocked.CompareExchange(ref _conversionStopper, 1, 1) == 1)
                        return false;

                    pPocetNactenych = outstream.Read(buffer2, 0, buffer2.Length);
                    Buffer.BlockCopy(buffer2, 0, buffer2s, 0, pPocetNactenych);
                    int writeoffset = 0;
                    int writecnt = 0;
                    for (int k = 0; k < pPocetNactenych / 2; k++, i++)
                    {
                        //dato = BitConverter.ToInt16(buffer2, k*2);
                        dato = buffer2s[k];
                        pPocetNactenychVzorkuCelkem++;

                        if (i == SampleCount) //first wave chunk completed.. -> show them
                        {
                            //poslani zpravy s daty bufferu
                            AudioBufferEventArgs e = new AudioBufferEventArgs(this._LoadedData, this._LoadedData.Length, 0, SampleCount / this.Frequency * 1000, Const.ID_BUFFER_WAVEFORMVISIBLE);
                            if (HaveData != null)
                                HaveData(this, e); // nacten ramec k zobrazeni a poslani tohoto ramce ven z tridy pomoci e
                            this._Loaded = true;
                            if (SampleCount + pPocetVzorku2Delta <= pPocetVzorku2 - 1)
                            {
                                SampleCount += pPocetVzorku2Delta;
                            }
                        }

                        if (i < SampleCount)
                        {
                            _LoadedData[i] = dato;
                        }

                        if (j >= delkaDocasnehoWav)
                        {
                            // zapsani bufferu jako bytu..
                            outputbase.Write(buffer2, writeoffset * 2, writecnt * 2);
                            writeoffset = writecnt;
                            writecnt = 0;

                            j = 0;
                            //zapsani skutecne hlavicky WAV
                            output.Seek(0, SeekOrigin.Begin);
                            output.Write((GetWaveHeader((Int32)output.BaseStream.Length - 44)));

                            output.Dispose();
                            output = null;
                            pIndexDocasneho++;

                            temporaryWaveFiles.Add(Path.Combine(TemporaryWAVsPath, pIndexDocasneho.ToString() + ".wav"));

                            AudioBufferEventArgs2 e2 = new AudioBufferEventArgs2(pIndexDocasneho, (pIndexDocasneho + 1) * this.delkaDocasnehoWavMS);
                            if (HaveFileNumber != null)
                                HaveFileNumber(this, e2);
                            if (Interlocked.CompareExchange(ref _conversionStopper, 1, 1) == 1)
                                return false;
                            output = new BinaryWriter(new FileStream(temporaryWaveFiles[pIndexDocasneho], FileMode.Create));
                            outputbase = output.BaseStream;
                            //pridani hlavicky-prazdne-zatim-pozdeji dodelat
                            output.Write(emptyHeader);
                            //
                        }

                        if (pPocetNactenych > 0)
                        {
                            writecnt++;
                            //output.Write(dato);     //zapsani do docasneho souboru
                        }
                        j++;
                    }

                    // zapsani bufferu jako bytu..
                    outputbase.Write(buffer2, writeoffset * 2, writecnt * 2);
                }

                if (Interlocked.CompareExchange(ref _conversionStopper, 1, 1) == 1)
                    return false;

                prPrevod.Close();
                prPrevod = null;
                this._FileLengthMS = (long)((float)(pPocetNactenychVzorkuCelkem) / (this.Frequency / 1000));
                if (output != null)
                {
                    //zapsani skutecne hlavicky WAV
                    output.Seek(0, SeekOrigin.Begin);
                    output.Write((GetWaveHeader((Int32)output.BaseStream.Length - 44)));

                    output.Close();   //zavreni souboru pro zapis dat
                    output = null;
                    AudioBufferEventArgs2 e2 = new AudioBufferEventArgs2(pIndexDocasneho, this.FileLengthMS);
                    if (HaveFileNumber != null)
                        HaveFileNumber(this, e2);
                }

                this._Loaded = true;
                this._Converted = true;  //dokoncen prevod audio souboru
                this._FilePath = aCesta;

                if (i < SampleCount)  //soubor je kratsi nez zobrazovaci buffer
                {
                    short[] pPole = new short[i];
                    for (int k = 0; k < i - 1; k++)
                    {
                        pPole[k] = this._LoadedData[k];
                    }
                    AudioBufferEventArgs e = new AudioBufferEventArgs(pPole, pPole.Length, 0, (long)(((double)i * 1000) / this.Frequency), Const.ID_BUFFER_WAVEFORMVISIBLE);

                    //pokusne pridano
                    this.SampleCount = i;

                    if (HaveData != null)
                        HaveData(this, e); // nacten ramec k zobrazeni a poslani tohoto ramce ven z tridy pomoci e
                }
                this.SampleCount = i;

                if (TemporaryWavesDone != null)
                {
                    TemporaryWavesDone(this, new EventArgs());
                }
                return true;
            }
            catch (Exception ex)
            {
                if (output != null)
                {
                    output.Close();
                    output = null;
                }
                this._Converted = true; //i pres spadnuti je nacteni ok, doresit preteceni indexu!!!!!!
                if (TemporaryWavesDone != null && !(ex is ThreadAbortException))
                    TemporaryWavesDone(this, new EventArgs());

                return false;
            }
            finally
            {
                if (output != null)
                    output.Dispose();
            }
        }
예제 #2
0
        //spusti prevod souboru na wav,ktery je mozno vykreslovat - skonci po naplneni zobrazovaciho bufferu, pot
        public bool WaveChunksConversion(string aCesta, string aCestaDocasnychWAV, long aDelkaJednohoSouboruMS)
        {
            BinaryWriter output = null;

            try
            {
                //nulovani promennych
                this.temporaryWaveFiles.Clear();
                this._FileLengthMS = (long)WavReader.ReturnAudioLength(aCesta).TotalMilliseconds;


                if (aCesta == null || aCestaDocasnychWAV == null || FileLengthMS <= 0)
                {
                    return(false); //chybne nastaveni nebo delka souboru k prevodu
                }
                this.TemporaryWAVsPath  = aCestaDocasnychWAV;
                this.temporaryWaveFiles = new List <string>();

                delkaDocasnehoWav        = (aDelkaJednohoSouboruMS / 1000) * this.Frequency; //pocet vzorku v 1 docasnem souboru
                this.delkaDocasnehoWavMS = aDelkaJednohoSouboruMS;
                int pIndexDocasneho = -1;

                //vytvoren proces zapocato rucni vytvareni prevadeni docasneho souboru a plneni bufferu programu
                //po naplneni zobrazovaciho bufferu lze vykreslit vlnu v programu-

                prPrevod = new Process();
                prPrevod.StartInfo.FileName               = FilePaths.FFmpegPath;
                prPrevod.StartInfo.WorkingDirectory       = new FileInfo(aCesta).DirectoryName;
                prPrevod.StartInfo.Arguments              = "-i \"" + aCesta + "\" -y -vn -ar 16000 -ac 1 -acodec pcm_s16le -f s16le -"; //nova data bez hlavicky
                prPrevod.StartInfo.RedirectStandardInput  = true;
                prPrevod.StartInfo.RedirectStandardOutput = true;
                prPrevod.StartInfo.RedirectStandardError  = false;
                prPrevod.StartInfo.UseShellExecute        = false;
                prPrevod.StartInfo.CreateNoWindow         = true;


                prPrevod.Start();
                //nacteni hlavicky - a alokovani pomocnych bufferu pro tuto operaci cteni

                byte[]  buffer2  = new byte[4096];
                short[] buffer2s = new short[buffer2.Length / 2];

                ChannelCount = 1;

                this.Frequency = 16000;

                this.SampleSize = 2;

                long pPocetVzorku2Delta = Const.DELKA_PRVNIHO_RAMCE_ZOBRAZOVACIHO_BUFFERU_MS * (this.Frequency / 1000);
                SampleCount = pPocetVzorku2Delta;
                long pPocetVzorku2 = Const.DISPLAY_BUFFER_LENGTH_MS * (this.Frequency / 1000);

                //nacitani jednotlivych dat
                this._LoadedData = new Int16[pPocetVzorku2];

                int i = 0;


                short dato = 0;  //pomocna


                //prvni docasny wav soubor tmp
                pIndexDocasneho = 0;
                temporaryWaveFiles.Add(Path.Combine(TemporaryWAVsPath, pIndexDocasneho.ToString() + ".wav"));
                output = new BinaryWriter(new FileStream(temporaryWaveFiles[pIndexDocasneho], FileMode.Create));
                Stream outputbase = output.BaseStream;
                //prazdna hlavicka
                byte[] emptyHeader = new byte[44];
                output.Write(emptyHeader);
                //

                long pPocetNactenychVzorkuCelkem = 0;

                int  pPocetNactenych = 1; //pocatecni inicializace nacitani,aby neskoncil cyklus
                long j = 0;               //pomocna pro indexaci v souborech

                Stream outstream = prPrevod.StandardOutput.BaseStream;
                for (i = 0; pPocetNactenych > 0;)
                {
                    //Termination
                    if (Interlocked.CompareExchange(ref _conversionStopper, 1, 1) == 1)
                    {
                        return(false);
                    }


                    pPocetNactenych = outstream.Read(buffer2, 0, buffer2.Length);
                    Buffer.BlockCopy(buffer2, 0, buffer2s, 0, pPocetNactenych);
                    int writeoffset = 0;
                    int writecnt    = 0;
                    for (int k = 0; k < pPocetNactenych / 2; k++, i++)
                    {
                        //dato = BitConverter.ToInt16(buffer2, k*2);
                        dato = buffer2s[k];
                        pPocetNactenychVzorkuCelkem++;

                        if (i == SampleCount) //first wave chunk completed.. -> show them
                        {
                            //poslani zpravy s daty bufferu
                            AudioBufferEventArgs e = new AudioBufferEventArgs(this._LoadedData, this._LoadedData.Length, 0, SampleCount / this.Frequency * 1000, Const.ID_BUFFER_WAVEFORMVISIBLE);
                            if (HaveData != null)
                            {
                                HaveData(this, e); // nacten ramec k zobrazeni a poslani tohoto ramce ven z tridy pomoci e
                            }
                            this._Loaded = true;
                            if (SampleCount + pPocetVzorku2Delta <= pPocetVzorku2 - 1)
                            {
                                SampleCount += pPocetVzorku2Delta;
                            }
                        }

                        if (i < SampleCount)
                        {
                            _LoadedData[i] = dato;
                        }


                        if (j >= delkaDocasnehoWav)
                        {
                            // zapsani bufferu jako bytu..
                            outputbase.Write(buffer2, writeoffset * 2, writecnt * 2);
                            writeoffset = writecnt;
                            writecnt    = 0;


                            j = 0;
                            //zapsani skutecne hlavicky WAV
                            output.Seek(0, SeekOrigin.Begin);
                            output.Write((GetWaveHeader((Int32)output.BaseStream.Length - 44)));

                            output.Dispose();
                            output = null;
                            pIndexDocasneho++;

                            temporaryWaveFiles.Add(Path.Combine(TemporaryWAVsPath, pIndexDocasneho.ToString() + ".wav"));

                            AudioBufferEventArgs2 e2 = new AudioBufferEventArgs2(pIndexDocasneho, (pIndexDocasneho + 1) * this.delkaDocasnehoWavMS);
                            if (HaveFileNumber != null)
                            {
                                HaveFileNumber(this, e2);
                            }
                            if (Interlocked.CompareExchange(ref _conversionStopper, 1, 1) == 1)
                            {
                                return(false);
                            }
                            output     = new BinaryWriter(new FileStream(temporaryWaveFiles[pIndexDocasneho], FileMode.Create));
                            outputbase = output.BaseStream;
                            //pridani hlavicky-prazdne-zatim-pozdeji dodelat
                            output.Write(emptyHeader);
                            //
                        }

                        if (pPocetNactenych > 0)
                        {
                            writecnt++;
                            //output.Write(dato);     //zapsani do docasneho souboru
                        }
                        j++;
                    }

                    // zapsani bufferu jako bytu..
                    outputbase.Write(buffer2, writeoffset * 2, writecnt * 2);
                }

                if (Interlocked.CompareExchange(ref _conversionStopper, 1, 1) == 1)
                {
                    return(false);
                }

                prPrevod.Close();
                prPrevod           = null;
                this._FileLengthMS = (long)((float)(pPocetNactenychVzorkuCelkem) / (this.Frequency / 1000));
                if (output != null)
                {
                    //zapsani skutecne hlavicky WAV
                    output.Seek(0, SeekOrigin.Begin);
                    output.Write((GetWaveHeader((Int32)output.BaseStream.Length - 44)));

                    output.Close();   //zavreni souboru pro zapis dat
                    output = null;
                    AudioBufferEventArgs2 e2 = new AudioBufferEventArgs2(pIndexDocasneho, this.FileLengthMS);
                    if (HaveFileNumber != null)
                    {
                        HaveFileNumber(this, e2);
                    }
                }

                this._Loaded    = true;
                this._Converted = true;  //dokoncen prevod audio souboru
                this._FilePath  = aCesta;

                if (i < SampleCount)  //soubor je kratsi nez zobrazovaci buffer
                {
                    short[] pPole = new short[i];
                    for (int k = 0; k < i - 1; k++)
                    {
                        pPole[k] = this._LoadedData[k];
                    }
                    AudioBufferEventArgs e = new AudioBufferEventArgs(pPole, pPole.Length, 0, (long)(((double)i * 1000) / this.Frequency), Const.ID_BUFFER_WAVEFORMVISIBLE);

                    //pokusne pridano
                    this.SampleCount = i;

                    if (HaveData != null)
                    {
                        HaveData(this, e); // nacten ramec k zobrazeni a poslani tohoto ramce ven z tridy pomoci e
                    }
                }
                this.SampleCount = i;

                if (TemporaryWavesDone != null)
                {
                    TemporaryWavesDone(this, new EventArgs());
                }
                return(true);
            }
            catch (Exception ex)
            {
                if (output != null)
                {
                    output.Close();
                    output = null;
                }
                this._Converted = true; //i pres spadnuti je nacteni ok, doresit preteceni indexu!!!!!!
                if (TemporaryWavesDone != null && !(ex is ThreadAbortException))
                {
                    TemporaryWavesDone(this, new EventArgs());
                }

                return(false);
            }
            finally
            {
                if (output != null)
                {
                    output.Dispose();
                }
            }
        }