/// <summary> /// Load sound data. wavData must not contain WAV Head. /// </summary> /// <param name="wavBytes"></param> /// <returns>Returns duration.</returns> public decimal Load(byte[] wavBytes, int sampleRate, int bitsPerSample, int channelCount) { var format = new SharpDX.Multimedia.WaveFormat(sampleRate, bitsPerSample, channelCount); // Create and set the buffer description. var desc = new SoundBufferDescription(); desc.Format = format; desc.Flags = // Play sound even if application loses focus. BufferFlags.GlobalFocus | // This has to be true to use effects. BufferFlags.ControlEffects; desc.BufferBytes = wavBytes.Length; // Create and set the buffer for playing the sound. ApplicationBuffer = new SecondarySoundBuffer(ApplicationDevice, desc); ApplicationBuffer.Write(wavBytes, 0, LockFlags.None); var duration = AudioHelper.GetDuration(wavBytes.Length, sampleRate, bitsPerSample, channelCount); return(duration); }
/// <summary> /// Convert XML to WAV bytes. WAV won't have the header, so you have to add it separately. /// </summary> static void ConvertXmlToWav(PlayItem item) { var query = System.Web.HttpUtility.ParseQueryString(item.VoiceSourceKeys ?? ""); // Default is local. var source = VoiceSource.Local; Enum.TryParse(query[InstalledVoiceEx._KeySource], true, out source); var voiceId = query[InstalledVoiceEx._KeyVoiceId]; byte[] wavBytes; if (source == VoiceSource.Amazon) { var region = query[InstalledVoiceEx._KeyRegion]; var engine = query[InstalledVoiceEx._KeyEngine]; var client = new Voices.AmazonPolly( SettingsManager.Options.AmazonAccessKey, SettingsManager.Options.AmazonSecretKey, region ); wavBytes = client.SynthesizeSpeech(voiceId, item.Xml, Amazon.Polly.OutputFormat.Mp3, engine); if (wavBytes != null && wavBytes.Length > 0) { var pi = DecodeToPlayItem(wavBytes); item.WavHead = pi.WavHead; item.WavData = pi.WavData; item.Duration = pi.Duration; pi.Dispose(); } } else { wavBytes = ConvertSsmlXmlToWav(voiceId, item.Xml, item.WavHead); if (wavBytes != null && wavBytes.Length > 0) { item.WavData = wavBytes; item.Duration = AudioHelper.GetDuration(wavBytes.Length, item.WavHead.SampleRate, item.WavHead.BitsPerSample, item.WavHead.Channels); } } }