public static async System.Threading.Tasks.Task <int> SynSpeechWriteToFileAsync(string text, string fileName) { using (Windows.Media.SpeechSynthesis.SpeechSynthesizer synth = new Windows.Media.SpeechSynthesis.SpeechSynthesizer()) { try { using (Windows.Media.SpeechSynthesis.SpeechSynthesisStream synthStream = await synth.SynthesizeTextToStreamAsyncServiceAsync(text, apiArgs)) // doesn't handle special characters such as quotes { // TODO: obsolete to use DataReader? use await Windows.Storage.FileIO.Read...(file); using (Windows.Storage.Streams.DataReader reader = new Windows.Storage.Streams.DataReader(synthStream)) { await reader.LoadAsync((uint)synthStream.Size); Windows.Storage.Streams.IBuffer buffer = reader.ReadBuffer((uint)synthStream.Size); Windows.Storage.StorageFolder tempFolder = await Windows.Storage.StorageFolder.GetFolderFromPathAsync(Options.options.tempFolderPath); Windows.Storage.StorageFile srcFile = await tempFolder.CreateFileAsync(Options.options.audio.speechSynthesisFileName, Windows.Storage.CreationCollisionOption.ReplaceExisting); await Windows.Storage.FileIO.WriteBufferAsync(srcFile, buffer); Windows.Storage.FileProperties.MusicProperties musicProperties = await srcFile.Properties.GetMusicPropertiesAsync(); Log.WriteLine("Bitrate:" + musicProperties.Bitrate); Windows.Media.MediaProperties.MediaEncodingProfile profile = Windows.Media.MediaProperties.MediaEncodingProfile.CreateWav(Windows.Media.MediaProperties.AudioEncodingQuality.Low); Windows.Media.Transcoding.MediaTranscoder transcoder = new Windows.Media.Transcoding.MediaTranscoder(); Windows.Storage.StorageFile destFile = await tempFolder.CreateFileAsync(fileName, Windows.Storage.CreationCollisionOption.ReplaceExisting); Windows.Media.Transcoding.PrepareTranscodeResult result = await transcoder.PrepareFileTranscodeAsync(srcFile, destFile, profile); if (result.CanTranscode) { await result.TranscodeAsync(); } else { Log.WriteLine("can't transcode file:" + result.FailureReason.ToString()); } } } } catch (Exception ex) { Console.WriteLine(ex.Message); } } return(0); }
//Create a profile private void CreateProfile() { profile = Windows.Media.MediaProperties.MediaEncodingProfile.CreateMp4(Windows.Media.MediaProperties.VideoEncodingQuality.Qvga); // Use MediaEncodingProfile to encode the profile System.Guid MFVideoRotationGuild = new System.Guid("C380465D-2271-428C-9B83-ECEA3B4A85C1"); int MFVideoRotation = ConvertVideoRotationToMFRotation(VideoRotation.None); profile.Video.Properties.Add(MFVideoRotationGuild, PropertyValue.CreateInt32(MFVideoRotation)); // add the mediaTranscoder var transcoder = new Windows.Media.Transcoding.MediaTranscoder(); transcoder.AddVideoEffect(Windows.Media.VideoEffects.VideoStabilization); }
/// <summary> /// Initialize Recording function. /// </summary> private async void InitializeRecording() { mediaCapture = new Windows.Media.Capture.MediaCapture(); mediaCapture.Failed += MediaCaptureFailed; var settings = new MediaCaptureInitializationSettings(); settings.StreamingCaptureMode = StreamingCaptureMode.Audio; settings.AudioDeviceId = MediaDevice.GetDefaultAudioCaptureId(AudioDeviceRole.Default); await mediaCapture.InitializeAsync(settings); encodingProfile = Windows.Media.MediaProperties.MediaEncodingProfile.CreateWav( Windows.Media.MediaProperties.AudioEncodingQuality.Auto); recorded = false; //no audio recorded yet recording = false; //recording not started }
private async System.Threading.Tasks.Task AudioFileOutputAsync(string outputFileName) { Windows.Media.MediaProperties.MediaEncodingProfile mediaEncodingProfile = new Windows.Media.MediaProperties.MediaEncodingProfile(); mediaEncodingProfile = Windows.Media.MediaProperties.MediaEncodingProfile.CreateWav(Windows.Media.MediaProperties.AudioEncodingQuality.Low); // Low defaults to mono - good // hmmmm, suspect that any attempt to change ChannelCount really doesn't do anything (AudioEncodingQuality.High) mediaEncodingProfile.Audio.ChannelCount = 1; // careful - stereo not permitted for some speech applications so must use mono Windows.Storage.StorageFolder tempFolder = await Windows.Storage.StorageFolder.GetFolderFromPathAsync(WoundifyShared.Options.options.tempFolderPath); Windows.Storage.StorageFile srcFile = await tempFolder.CreateFileAsync(WoundifyShared.Options.options.audio.speechSynthesisFileName, Windows.Storage.CreationCollisionOption.ReplaceExisting); Windows.Storage.StorageFile file = await tempFolder.CreateFileAsync(outputFileName, Windows.Storage.CreationCollisionOption.ReplaceExisting); Windows.Media.Audio.CreateAudioFileOutputNodeResult fileOutputNodeResult = await graph.CreateFileOutputNodeAsync(file, mediaEncodingProfile); if (fileOutputNodeResult.Status != Windows.Media.Audio.AudioFileNodeCreationStatus.Success) { WoundifyShared.Log.WriteLine("Audio device output unavailable:" + fileOutputNodeResult.Status.ToString()); return; } fileOutputNode = fileOutputNodeResult.FileOutputNode; }
/// <summary> /// StartRecording method /// Start to record audio using the microphone. /// The audio stream in stored in memory with no limit of size. /// </summary> /// <param name="MaxStreamSizeInBytes"> /// This parameter defines the max size of the buffer in memory. When the size of the buffer is over this limit, the /// client create another stream and remove the previouw stream. /// By default the value is 0, in that case the audio stream in stored in memory with no limit of size. /// </param> /// <param name="ThresholdDuration"> /// The duration in milliseconds for the calculation of the average audio level. /// With this parameter you define the period during which the average level is measured. /// If the value is 0, no buffer will be sent to Cognitive Services. /// </param> /// <param name="ThresholdLevel"> /// The minimum audio level average necessary to trigger the recording, /// it's a value between 0 and 65535. You can tune this value after several microphone tests. /// If the value is 0, no buffer will be sent to Cognitive Services. /// </param> /// <return>return true if successful. /// </return> public async System.Threading.Tasks.Task <bool> StartContinuousRecording(ulong MaxStreamSizeInBytes, UInt16 ThresholdDuration, UInt16 ThresholdLevel) { thresholdDuration = ThresholdDuration; thresholdLevel = ThresholdLevel; bool bResult = false; maxStreamSizeInBytes = MaxStreamSizeInBytes; if (isRecordingInitialized != true) { await InitializeRecording(); } if (STTStream != null) { STTStream.BufferReady -= STTStream_BufferReady; STTStream.AudioLevel -= STTStream_AudioLevel; STTStream.Dispose(); STTStream = null; } STTStream = SpeechToTextMainStream.Create(maxStreamSizeInBytes, thresholdDuration, thresholdLevel); STTStream.AudioLevel += STTStream_AudioLevel; STTStream.BufferReady += STTStream_BufferReady; if ((STTStream != null) && (isRecordingInitialized == true)) { try { Windows.Media.MediaProperties.MediaEncodingProfile MEP = Windows.Media.MediaProperties.MediaEncodingProfile.CreateWav(Windows.Media.MediaProperties.AudioEncodingQuality.Auto); if (MEP != null) { if (MEP.Audio != null) { uint framerate = 16000; uint bitsPerSample = 16; uint numChannels = 1; uint bytespersecond = 32000; MEP.Audio.Properties[WAVAttributes.MF_MT_AUDIO_SAMPLES_PER_SECOND] = framerate; MEP.Audio.Properties[WAVAttributes.MF_MT_AUDIO_NUM_CHANNELS] = numChannels; MEP.Audio.Properties[WAVAttributes.MF_MT_AUDIO_BITS_PER_SAMPLE] = bitsPerSample; MEP.Audio.Properties[WAVAttributes.MF_MT_AUDIO_AVG_BYTES_PER_SECOND] = bytespersecond; foreach (var Property in MEP.Audio.Properties) { System.Diagnostics.Debug.WriteLine("Property: " + Property.Key.ToString()); System.Diagnostics.Debug.WriteLine("Value: " + Property.Value.ToString()); if (Property.Key == new Guid("5faeeae7-0290-4c31-9e8a-c534f68d9dba")) { framerate = (uint)Property.Value; } if (Property.Key == new Guid("f2deb57f-40fa-4764-aa33-ed4f2d1ff669")) { bitsPerSample = (uint)Property.Value; } if (Property.Key == new Guid("37e48bf5-645e-4c5b-89de-ada9e29b696a")) { numChannels = (uint)Property.Value; } } } if (MEP.Container != null) { foreach (var Property in MEP.Container.Properties) { System.Diagnostics.Debug.WriteLine("Property: " + Property.Key.ToString()); System.Diagnostics.Debug.WriteLine("Value: " + Property.Value.ToString()); } } } await mediaCapture.StartRecordToStreamAsync(MEP, STTStream); bResult = true; isRecording = true; System.Diagnostics.Debug.WriteLine("Recording in audio stream..."); } catch (Exception e) { System.Diagnostics.Debug.WriteLine("Exception while recording in audio stream:" + e.Message); } } return(bResult); }
// </SnippetMediaCaptureVideo_InitSettingsCS> // <SnippetMediaCaptureVideo_CreateProfileCS> // Create a profile. private void CreateProfile() { _profile = Windows.Media.MediaProperties.MediaEncodingProfile.CreateMp4( Windows.Media.MediaProperties.VideoEncodingQuality.Qvga); }