コード例 #1
0
        private string CreateTextPartFilesFromSnippet(string ExportFolder, TrackSnippetViewModel snip, MessageBoxResult yesIfVoice, ref int index)
        {
            var transcriptionsFileText = "";

            foreach (var tp in snip.TextParts)
            {
                if (!tp.IsOK || string.IsNullOrWhiteSpace(tp.Text))
                {
                    continue;
                }

                var fileNumberString = $"{index.ToString().PadLeft(4, '0')}";
                var fileName         = Path.Combine(ExportFolder, $"{fileNumberString}.wav"); //Path.GetFileName(snip.FilePath);
                if (yesIfVoice == MessageBoxResult.Yes)
                {
                    transcriptionsFileText += $"{fileNumberString}\t{tp.Text.Trim()}{Environment.NewLine}"; // is for Custom Voice
                }
                else
                {
                    transcriptionsFileText += $"{fileNumberString}.wav\t{tp.Text.Trim()}{Environment.NewLine}"; // is for Acoutic Dataset
                }

                WavFileUtils.TakeClipAddSilence(snip.FilePath, TimeSpan.FromMilliseconds(800), TimeSpan.FromMilliseconds(tp.StartMills), TimeSpan.FromMilliseconds(tp.TextWidth), fileName);

                index++;
            }

            return(transcriptionsFileText);
        }
コード例 #2
0
        private void ImportWavFromMedia_Click(object sender, RoutedEventArgs e)
        {
            var dialog = new System.Windows.Forms.OpenFileDialog {
                Multiselect = false
            };

            dialog.ShowDialog();
            var fileToExplode  = dialog.FileName;
            var newWavFileName = fileToExplode.Substring(0, fileToExplode.LastIndexOf(".")) + ".wav";

            WavFileUtils.ExtractWavFromMedia(fileToExplode, newWavFileName);

            var newFilePath = System.IO.Path.GetFullPath(newWavFileName);

            if (!string.IsNullOrWhiteSpace(ViewModel.FilesFolder) && ViewModel.FilesFolder != newFilePath)
            {
                var answ = MessageBox.Show("Do you want to move the new wav file to the above projects folder?", "Import this file to the project folder?", MessageBoxButton.YesNo);
                if (answ == MessageBoxResult.Yes)
                {
                    var fileNamePart = System.IO.Path.GetFileName(newWavFileName);
                    File.Move(newWavFileName, System.IO.Path.Combine(ViewModel.FilesFolder, fileNamePart));
                    ViewModel.LoadFilesFromSelectedFolder();
                }
            }
        }
コード例 #3
0
 /// <summary>
 /// Кнопка обрезки аудиофайла
 /// </summary>
 /// <param name="sender"></param>
 /// <param name="e"></param>
 private void button1_Click(object sender, EventArgs e)
 {
     if (numericUpDown1.Value > numericUpDown2.Value)
     {
         MessageBox.Show("Начальная позиция позже финальной!", "Ошибка", MessageBoxButtons.OK);
     }
     else
     {
         if ((numericUpDown1.Value + numericUpDown2.Value) >= totaltm)
         {
             MessageBox.Show("Длина обрезок больше длины аудиозаписи!", "Ошибка", MessageBoxButtons.OK);
         }
         else
         {
             SaveFileDialog save = new SaveFileDialog();
             save.InitialDirectory = "C:\\";
             save.Filter           = "wav files (*.wav)|*.wav";
             save.FilterIndex      = 1;
             save.Title            = "Сохранить файл";
             if (save.ShowDialog() == DialogResult.Cancel)
             {
                 return;
             }
             string   filename = save.FileName;
             TimeSpan cfs      = new TimeSpan(0, 0, Convert.ToInt32(numericUpDown1.Value));
             TimeSpan cfe      = new TimeSpan(0, 0, Convert.ToInt32(numericUpDown2.Value));
             WavFileUtils.TrimWavFile(rfn, filename, cfs, cfe);
             MessageBox.Show("Файл сохранен!");
             this.Close();
         }
     }
 }
コード例 #4
0
ファイル: Form1.cs プロジェクト: synthdocs/TrimWavOctatrack
        private void Trim_Click(object sender, EventArgs e)
        {
            // Lecture de l'input
            WaveFileReader reader                 = new WaveFileReader(input.Text.ToString());
            TimeSpan       span                   = reader.TotalTime;
            long           span_total_long        = span.Ticks;
            long           nbr_partie_long        = Int64.Parse(nbr_partie.Text.ToString());
            long           variable_saut_wav_long = span_total_long / nbr_partie_long;

            // Variable de saut en TimeSpan
            TimeSpan variable_saut_wav_timespan = TimeSpan.FromTicks(variable_saut_wav_long);

            // boucle de découpe du fichier wav
            for (int i = 0; i < nbr_partie_long; i++)
            {
                string output_string = output.Text.ToString() + i.ToString() + nom_fichier.Text.ToString() + ".wav";
                if (i == nbr_partie_long - 1)
                {
                    WavFileUtils.TrimWavFile(input.Text.ToString(), output_string, TimeSpan.FromTicks(variable_saut_wav_long * i), TimeSpan.FromTicks((variable_saut_wav_long * i) + variable_saut_wav_long));
                    listBox1.Items.Add(i.ToString() + " --- " + TimeSpan.FromTicks(variable_saut_wav_long * i).ToString() + " --- " + TimeSpan.FromTicks((variable_saut_wav_long * i) + variable_saut_wav_long).ToString());
                }
                else
                {
                    WavFileUtils.TrimWavFile(input.Text.ToString(), output_string, TimeSpan.FromTicks(variable_saut_wav_long * i), TimeSpan.FromTicks((variable_saut_wav_long * i) + variable_saut_wav_long - 1));
                    listBox1.Items.Add(i.ToString() + " --- " + TimeSpan.FromTicks(variable_saut_wav_long * i).ToString() + " --- " + TimeSpan.FromTicks((variable_saut_wav_long * i) + variable_saut_wav_long - 1).ToString());
                }
            }
            Form.ActiveForm.Refresh();
        }
コード例 #5
0
        private void MakeSnippetAudioFiles(
            TrackSnippetViewModel snippet,
            int ix)
        {
            var newProjectFolder = Path.Combine(Settings.Default.ProjectsFolder, SelectedProject.Name, $"{DateTime.Now.ToString("ddMMyyyyHHmmss")}");

            Directory.CreateDirectory(newProjectFolder);

            snippet.FilePath = Path.Combine(newProjectFolder, $"{Path.GetFileNameWithoutExtension(AudioFilePath)}_{ix++}.wav");

            var startTime = TimeSpan.FromTicks(snippet.AudioSnippet.OffsetInTicks);

            WavFileUtils.TakeClipAddSilence(AudioFilePath, TimeSpan.Zero, startTime, snippet.AudioSnippet.Duration, snippet.FilePath);
        }
コード例 #6
0
        static void Main(string[] args)
        {
            if (args.Length < 1)
            {
                Console.WriteLine("No File");
                Console.Read();
                return;
            }

            string path = args[0];

            try
            {
                WavFileUtils.TrimWavFile(path, $"{path}_converted.wav", TimeSpan.FromMinutes(10), TimeSpan.FromMinutes(20));
                Console.WriteLine("Done");
            }
            catch (Exception e)
            {
                Console.WriteLine(e.Message);
                Console.WriteLine(e.StackTrace);
            }

            Console.Read();
        }
コード例 #7
0
        private void DoGenerateCustomArtefacts()
        {
            CheckForChanges();
            LockUserInterface = true;

            try
            {
                MessageBoxResult answ = MessageBoxResult.Cancel;
                var numberOfFiles     = SelectedProject.SnippetsOkTextPartCount;

                //   await Task.Delay(50).ConfigureAwait(false);

                if (numberOfFiles > 0)
                {
                    answ = MessageBox.Show($"Click YES to export just the {numberOfFiles} approved text parts with matching audio.{Environment.NewLine}Click NO to use the original full snippet audio and full text.{Environment.NewLine}Click CANCEL to take no action.", "Generate from just approved text?", MessageBoxButton.YesNoCancel);
                    if (answ == MessageBoxResult.Cancel)
                    {
                        //  LockUserInterface = false;
                        return;
                    }
                }

                var formatAnsw = MessageBox.Show($"Click YES to export for Custom Voice.{Environment.NewLine}Click NO to export for Acoustic Dataset.{Environment.NewLine}Click CANCEL to take no action.", "Which index file format?", MessageBoxButton.YesNoCancel);
                if (formatAnsw == MessageBoxResult.Cancel)
                {
                    // LockUserInterface = false;
                    return;
                }

                var dateFolder      = $"{DateTime.Now.ToString("yyyMMdd_HHmmss")}";
                var exportFolder    = Path.Combine(Settings.Default.ProjectsFolder, selectedProject.FolderName, dateFolder);
                var exportRawFolder = Path.Combine(Settings.Default.ProjectsFolder, selectedProject.FolderName, dateFolder, "RAW");

                parentControl.Dispatcher.Invoke(() =>
                {
                    Directory.CreateDirectory(exportRawFolder);

                    var transcriptionsFileText = "";

                    if (answ == MessageBoxResult.Yes)
                    {
                        // Version 2 - Reworking snippets. Taking only where edits were made and ticked as OK
                        ProgressingInfo = $"Generating {numberOfFiles} approved text parts from {SelectedProject.Snippets.Count} snippets...";
                        var index       = 1;
                        foreach (var snip in SelectedProject.Snippets)
                        {
                            transcriptionsFileText += CreateTextPartFilesFromSnippet(exportRawFolder, snip, formatAnsw, ref index);
                        }
                    }
                    else
                    {
                        // Version 1 - Take the raw text before curating
                        ProgressingInfo = $"Generating {SelectedProject.Snippets.Count} snippets with the raw text...";
                        var ix          = 1;
                        foreach (var snip in SelectedProject.Snippets)
                        {
                            var fileNumberString = $"{(ix++).ToString().PadLeft(4, '0')}";
                            var fileName         = $"{fileNumberString}.wav";
                            if (formatAnsw == MessageBoxResult.Yes)
                            {
                                transcriptionsFileText += $"{fileNumberString}\t{snip.RawText}{Environment.NewLine}"; // Custom Voice format is just a number
                            }
                            else
                            {
                                transcriptionsFileText += $"{fileName}\t{snip.RawText}{Environment.NewLine}"; // Acoustic Dataset requires .wav extension with the number
                            }
                            var newFileCopy = Path.Combine(exportRawFolder, fileName);
                            File.Copy(snip.FilePath, newFileCopy); // we're taking the whole snippet, so make a copy
                            WavFileUtils.ChangeWaveFormat(newFileCopy, 16000, 16, 1);
                        }
                    }

                    // create zip file
                    var newZipFile = Path.Combine(exportFolder, $"samples.zip");
                    ZipFile.CreateFromDirectory(exportRawFolder, newZipFile, CompressionLevel.Fastest, false);

                    // create transcription mapping file
                    var newTransFile = Path.Combine(exportFolder, $"speech.txt");
                    File.WriteAllText(newTransFile, transcriptionsFileText);

                    // delete raw temporary files, now they are zipped
                    Directory.Delete(exportRawFolder, true);
                });

                answ = MessageBox.Show("Files generated. Do you want to open the export folder?", "All done", MessageBoxButton.YesNo);
                if (answ == MessageBoxResult.Yes)
                {
                    Process.Start(exportFolder);
                }
            }
            catch (Exception exc)
            {
                MessageBox.Show($"Error: {exc}");
            }
            finally
            {
                LockUserInterface = false;
            }
        }
コード例 #8
0
        private async Task GetTranscriptionFromAzure(TaskCompletionSource <int> stopRecognition, List <TrackSnippetViewModel> newCollection)
        {
            // Creates an instance of a speech config with specified subscription key and service region.
            // Replace with your own subscription key and service region (e.g., "westus").
            var config = SpeechConfig.FromSubscription(TranscribeEndpointsVM.SelectedTranscribeEndpoint.Key, TranscribeEndpointsVM.SelectedTranscribeEndpoint.Region);

            if (!string.IsNullOrWhiteSpace(TranscribeEndpointsVM.SelectedTranscribeEndpoint.Endpoint))
            {
                config.EndpointId = TranscribeEndpointsVM.SelectedTranscribeEndpoint.Endpoint;
            }
            config.OutputFormat = OutputFormat.Detailed;

            var    log        = "";
            double OnePercent = 100D / CurrentAudioVM.Duration.TimeSpan.Ticks;
            var    tLog       = new transLog {
                Started = DateTime.Now, FilePath = AudioFilePath
            };

            try
            {
                if (Path.GetExtension(AudioFilePath).ToLower() != ".wav")
                {
                    // extract the WAV
                    var wavFileName = Path.Combine(Path.GetDirectoryName(AudioFilePath), Path.GetFileNameWithoutExtension(AudioFilePath) + ".wav");
                    WavFileUtils.ExtractWavFromMedia(AudioFilePath, wavFileName);

                    AudioFilePath = wavFileName;
                }

                using (var audioInput = AudioConfig.FromWavFileInput(AudioFilePath))
                {
                    using (var recognizer = new SpeechRecognizer(config, audioInput))
                    {
                        // Subscribes to events.
                        recognizer.Recognizing += (s, e) =>
                        {
                            transLogRow dyn     = new transLogRow();
                            var         txtCnt  = textPartsAll.Count;
                            var         dur     = e.Result.Duration;
                            var         totDone = TimeSpan.FromTicks(e.Result.OffsetInTicks + dur.Ticks);
                            var         perc    = OnePercent * (dur.Ticks + e.Result.OffsetInTicks);

                            TranscribeInfo = $"{Math.Floor(perc)}%: {newCollection.Count + 1}: {totDone.Hours}:{totDone.Minutes}:{totDone.Seconds}: {txtCnt}: {e.Result.Text}";

                            dyn.Args = e;

                            var textBit       = e.Result.Text;
                            var ary           = textBit.Split(' ');
                            var lastGoodIx    = 0;
                            var goodTextParts = new List <TextPart>();
                            var isChange      = false;

                            for (var a = 0; a < ary.Length; a++)
                            {
                                if (a > txtCnt - 1)
                                {
                                    dyn.Notes += $"[new={txtCnt - a}]";
                                    break;
                                }
                                if (ary[a].Trim() != textPartsAll[a].Text.Trim())
                                {
                                    dyn.Notes           += $"[chg={a}:{textPartsAll[a].Text}={ary[a]}]";
                                    textPartsAll[a].Text = ary[a];
                                    isChange             = true;
                                }
                                goodTextParts.Add(textPartsAll[a]);
                                lastGoodIx = a;
                            }

                            if (lastGoodIx < textPartsAll.Count - 1)
                            {
                                dyn.Notes += $"[lth chg]";
                            }

                            double detectedEstimate;

                            try
                            {
                                if (goodTextParts.Count >= ary.Length)
                                {
                                    return; // nothing new
                                }

                                var newBit = "";
                                for (var x = goodTextParts.Count; x < ary.Length; x++)
                                {
                                    newBit += ary[x] + " ";
                                }

                                // 200 magic number - identification time about 200 mills
                                detectedEstimate = e.Result.Duration.TotalMilliseconds - 200;

                                var sinceLastEvent = detectedEstimate - lastMillisecondsTotal;
                                if (sinceLastEvent < 1)
                                {
                                    sinceLastEvent = 1;
                                }

                                var cnt = goodTextParts.Count;

                                if (cnt > 1 && !isChange)
                                {
                                    if (sinceLastEvent < ADJUSTED_SIZE_MAX_WORD_LENGTH)
                                    {
                                        dyn.Notes += $"[adj={cnt - 1}, snc={sinceLastEvent}, lgt={goodTextParts[cnt - 1].TextWidth} - {ADJUSTED_SIZE_MAX_WORD_LENGTH - sinceLastEvent}]";
                                        goodTextParts[cnt - 1].TextWidth -= (ADJUSTED_SIZE_MAX_WORD_LENGTH - sinceLastEvent);
                                    }
                                }

                                // Finally, save any new bit(s)
                                goodTextParts.Add(new TextPart {
                                    Text = newBit.Trim(), StartMills = detectedEstimate, TextWidth = ADJUSTED_SIZE_MAX_WORD_LENGTH
                                });
                            }
                            catch (Exception ex)
                            {
                                throw;
                            }
                            finally
                            {
                                log += JsonConvert.SerializeObject(dyn) + Environment.NewLine;
                            }
                            lastMillisecondsTotal = detectedEstimate; // start point for next text
                                                                      //           lastCharLength = textBit.Length;

                            textPartsAll = goodTextParts;
                        };

                        recognizer.Recognized += (s, e) =>
                        {
                            if (e.Result.Reason == ResultReason.RecognizedSpeech)
                            {
                                //Console.WriteLine($"RECOGNIZED: Text={e.Result.Text}");
                                var snippet = new TrackSnippetViewModel()
                                {
                                    AudioSnippet  = e.Result,
                                    Scale         = scale,
                                    Gain          = gain,
                                    RawText       = e.Result.Text,
                                    OffsetInTicks = e.Result.OffsetInTicks
                                };

                                snippet.DurationMilliseconds = e.Result.Duration.TotalMilliseconds; //WavFileUtils.GetSoundLength(AudioFilePath);

                                //var rawAry = e.Result.Text.Split(' ');
                                //if (rawAry.Length == textPartsAll.Count)
                                //{
                                //    for(var a = 0; a < rawAry.Length; a++)
                                //    {
                                //        textPartsAll[a].Text = rawAry[a];
                                //    }
                                //}
                                //else
                                //{
                                //    // to do, merge tidied text back into recognised text
                                //}

                                snippet.TextParts = new ObservableCollection <TextPart>(textPartsAll);

                                parentControl.Dispatcher.Invoke(() => newCollection.Add(snippet));

                                textPartsAll          = new List <TextPart>();
                                lastMillisecondsTotal = 0;
                            }
                            else if (e.Result.Reason == ResultReason.NoMatch)
                            {
                                Console.WriteLine($"NOMATCH: Speech could not be recognized.");
                            }
                        };

                        recognizer.Canceled += (s, e) =>
                        {
                            Console.WriteLine($"CANCELED: Reason={e.Reason}");

                            if (e.Reason == CancellationReason.Error)
                            {
                                MessageBox.Show($"FAILED: Are you using a valid subscription key and region? ErrorCode = {e.ErrorCode}: ErrorDetails = {e.ErrorDetails}");
                            }

                            stopRecognition.TrySetResult(0);
                        };

                        recognizer.SessionStarted += (s, e) => Console.WriteLine("\n    Session started event.");

                        recognizer.SessionStopped += (s, e) =>
                        {
                            Console.WriteLine("\n    Session stopped event.");
                            Console.WriteLine("\nStop recognition.");
                            stopRecognition.TrySetResult(0);
                        };

                        // Starts continuous recognition. Uses StopContinuousRecognitionAsync() to stop recognition.
                        await recognizer.StartContinuousRecognitionAsync().ConfigureAwait(false);

                        // Waits for completion.
                        // Use Task.WaitAny to keep the task rooted.
                        Task.WaitAny(new[] { stopRecognition.Task });

                        // Stops recognition.
                        await recognizer.StopContinuousRecognitionAsync().ConfigureAwait(false);
                    }
                }
            }
            catch (Exception excp)
            {
                Debug.WriteLine($"{excp}");
            }
            finally
            {
                if (Settings.Default.TranscriptionLogging)
                {
                    try
                    {
                        var logFile = $"TranscriptionLog_{DateTime.Now.ToString($"{DateTime.Now.ToString("ddMMyyyyHHmmss")}")}.txt";
                        logFile = Path.Combine(Path.GetFullPath(SettingsVM.ProjectsFolder), logFile);
                        File.WriteAllText(logFile, log);

                        tLog.LogPath = logFile;
                        SelectedProject.TranscribeLogs.Add(tLog);
                        Debug.WriteLine($"Log file created at {logFile}");
                    }
                    catch (Exception ex)
                    {
                        throw;
                    }
                }
            }
        }