private async Task Text2Audio(string text, StorageFile audio) { if (audio != null && !string.IsNullOrEmpty(text)) { if (synth == null) { synth = new SpeechSynthesizer(); } var voice = SpeechSynthesizer.AllVoices.Where(o => o.DisplayName == (string)cbVoice.SelectedItem); synth.Voice = voice.First(); synth.Options.AudioPitch = sliderPitch.Value; synth.Options.AudioVolume = sliderVolume.Value / 100.0; synth.Options.SpeakingRate = sliderSpeed.Value; //var options = new SpeechSynthesizerOptions(); SpeechSynthesisStream stream = await synth.SynthesizeTextToStreamAsync(text); trans = new MediaTranscoder() { HardwareAccelerationEnabled = true, AlwaysReencode = true, //VideoProcessingAlgorithm = MediaVideoProcessingAlgorithm.MrfCrf444; }; MediaEncodingProfile profile = MediaEncodingProfile.CreateMp3(AudioEncodingQuality.Medium); if (audio.ContentType.EndsWith("wav", StringComparison.CurrentCultureIgnoreCase)) { profile = MediaEncodingProfile.CreateWav(AudioEncodingQuality.Medium); } else if (audio.ContentType.EndsWith("aac", StringComparison.CurrentCultureIgnoreCase)) { profile = MediaEncodingProfile.CreateM4a(AudioEncodingQuality.Medium); } else if (audio.ContentType.EndsWith("m4a", StringComparison.CurrentCultureIgnoreCase)) { profile = MediaEncodingProfile.CreateM4a(AudioEncodingQuality.Medium); } else if (audio.FileType.EndsWith("alac", StringComparison.CurrentCultureIgnoreCase)) { profile = MediaEncodingProfile.CreateAlac(AudioEncodingQuality.Medium); } else if (audio.ContentType.EndsWith("flac", StringComparison.CurrentCultureIgnoreCase)) { profile = MediaEncodingProfile.CreateFlac(AudioEncodingQuality.Medium); } else if (audio.ContentType.EndsWith("mp4", StringComparison.CurrentCultureIgnoreCase)) { profile = MediaEncodingProfile.CreateMp4(VideoEncodingQuality.Pal); } using (IRandomAccessStream fso = await audio.OpenAsync(FileAccessMode.ReadWrite)) { //var trans_result = await trans.PrepareFileTranscodeAsync(InFile, OutFile, profile_mp3); var trans_result = await trans.PrepareStreamTranscodeAsync(stream, fso, profile); if (trans_result.CanTranscode) { if (canceltsrc != null) { canceltsrc.Dispose(); canceltsrc = null; } canceltsrc = new CancellationTokenSource(); var progress = new Progress <double>(ps => { edHearState.Text = $"{AppResources.GetString("ProcessingState")} {ps:N0}%"; //edHearState.Text = AppResources.GetString("ProcessingState"); }); await trans_result.TranscodeAsync().AsTask(canceltsrc.Token, progress); edHearState.Text = AppResources.GetString("ProcessFinished"); } else { edHearState.Text = AppResources.GetString("CanNotTrans"); } } } }
private async Task Covert_music(string audio, string type) { using (SQLiteConnection db = new SQLiteConnection(App.DB_PATH)) { MediaEncodingProfile profile = new MediaEncodingProfile(); if (type == ".mp3") { var Quality = Load_options(Json.Json_mp3); profile = MediaEncodingProfile.CreateMp3(Quality); } else if (type == ".alac") { var Quality = Load_options(Json.Json_alac); profile = MediaEncodingProfile.CreateAlac(Quality); type = ".m4a"; } else if (type == ".flac") { var Quality = Load_options(Json.Json_flac); profile = MediaEncodingProfile.CreateFlac(Quality); } else if (type == ".aac") { var Quality = Load_options(Json.Json_aac); profile = MediaEncodingProfile.CreateM4a(Quality); type = ".m4a"; } convert_type = type.Substring(1); var songs = db.Query <Song>("Select * from Song"); foreach (var song in songs) { var format = db.Find <MusicFormat>(c => c.Id == song.FormatId); var import_name = song.Path + song.NameSong + "." + format.NameFormat; string new_name = song.Path + song.NameSong + type; Main_folder = await Windows.Storage.AccessCache.StorageApplicationPermissions .FutureAccessList.GetFolderAsync("Audio"); StorageFile oldfile = await Main_folder.GetFileAsync(import_name); MusicProperties musicProperties = await oldfile.Properties.GetMusicPropertiesAsync(); if (oldfile.FileType != ".mp3" && oldfile.FileType != type && musicProperties.Bitrate > 500000) { StorageFile newfile = await Main_folder.CreateFileAsync(new_name, CreationCollisionOption.ReplaceExisting); if (oldfile != null && newfile != null) { try { current_song = song; all_convert_song++; SongDeleteAfterConvert.Add(current_song); MediaTranscoder transcoder = new MediaTranscoder(); PrepareTranscodeResult prepareOp = await transcoder.PrepareFileTranscodeAsync(oldfile, newfile, profile); if (prepareOp.CanTranscode) { var transcodeOp = prepareOp.TranscodeAsync(); transcodeOp.Completed += async(IAsyncActionWithProgress <double> asyncInfo, AsyncStatus status) => { asyncInfo.GetResults(); await CoreApplication.MainView.CoreWindow.Dispatcher.RunAsync(CoreDispatcherPriority.Normal, () => { current_convert_song++; }); }; } } catch (Exception exc) { var dialog = new MessageDialog(exc.ToString()); await dialog.ShowAsync(); } } } } do { await Task.Delay(TimeSpan.FromSeconds(3)); }while (all_convert_song != current_convert_song); Delete_oldfile(); } }