public Object Generate(System.IO.Stream audioms, ConditionVariables effects, bool ensureaudio) { try { audioms.Position = 0; IWaveSource s = new CSCore.Codecs.WAV.WaveFileReader(audioms); if (ensureaudio) { s = s.AppendSource(x => new ExtendWaveSource(x, 100)); // SEEMS to help the click at end.. } ApplyEffects(ref s, effects); return(s); } catch { if (ensureaudio) { return(new NullWaveSource(5)); } else { return(null); } } }
// FROM audio stream public AudioData Generate(System.IO.Stream audioms, SoundEffectSettings effects, bool ensureaudio) { try { audioms.Position = 0; IWaveSource s; if (audioms.Length == 0) { if (ensureaudio) { s = new NullWaveSource(50); } else { return(null); } } else { s = new CSCore.Codecs.WAV.WaveFileReader(audioms); //System.Diagnostics.Debug.WriteLine("oRIGINAL length " + s.Length); if (ensureaudio) { s = s.AppendSource(x => new ExtendWaveSource(x, 100)); // SEEMS to help the click at end.. } } //System.Diagnostics.Debug.WriteLine("Sample length " + s.Length); System.Diagnostics.Debug.Assert(s != null); ApplyEffects(ref s, effects); //System.Diagnostics.Debug.WriteLine(".. to length " + s.Length); System.Diagnostics.Debug.Assert(s != null); return(new AudioData(s)); } catch (Exception ex) { System.Diagnostics.Debug.WriteLine("Exception " + ex.Message); if (ensureaudio) { IWaveSource s = new NullWaveSource(100); System.Diagnostics.Debug.Assert(s != null); return(new AudioData(s)); } else { return(null); } } }
private void ReplaceSlideAudioAnnotationForRelationship(PresentationDocument presentationDocument, string slideRelationshipId, byte[] audioFile) { var presentationPart = presentationDocument.PresentationPart; SlidePart slidePart = (SlidePart)presentationPart.GetPartById(slideRelationshipId); MediaDataPart mediaPart = (MediaDataPart)slidePart.DataPartReferenceRelationships .FirstOrDefault(dpr => (dpr.DataPart?.ContentType == "audio/x-wav" || dpr.DataPart?.ContentType == "audio/mp4") && dpr.RelationshipType.EndsWith("/relationships/media"))?.DataPart; if (mediaPart != null) { this._logger.Verbose($"Replacing contents of '{mediaPart.Uri}' media part."); byte[] encodedAAC = new byte[0]; if (mediaPart.ContentType == "audio/mp4") { this._logger.Verbose("Converting replacement audio from Wav to Mp4 AAC."); encodedAAC = _audioEncoder.FromWav(audioFile); } using (var mpStream = mediaPart.GetStream()) using (var writer = new BinaryWriter(mpStream)) { if (encodedAAC.LongLength > 0) { writer.Write(encodedAAC); } else { writer.Write(audioFile); } } this._logger.Verbose("Updating slide duration to match new audio."); using (var memStream = new MemoryStream(audioFile)) using (IWaveSource wavSource = new CSCore.Codecs.WAV.WaveFileReader(memStream)) { TimeSpan totalTime = wavSource.GetLength(); this._logger.Verbose($"New audio length: {totalTime.TotalMilliseconds}ms."); UpdateSlideAdvanceAfterTime(slidePart, totalTime.TotalMilliseconds); } } else { this._logger.Warning("Current slide has no media in it."); this._logger.Verbose("Could not find any parts with '/relationships/media' type and 'audio/x-wav' or 'audio/mp4' content types."); } }
/// <summary> /// Plays the WAVE file data contained in the given MemoryStream using the CSCore library. /// </summary> /// <param name="memoryStream">The MemoryStream containing the WAVE file data.</param> public void PlayWithCSCore(MemoryStream memoryStream) { using (IWaveSource soundSource = new WaveFileReader(memoryStream)) { //SoundOut implementation which plays the sound // WaveOut works. DirectSoundOut works. WasapiOut works. using (ISoundOut soundOut = new CSCore.SoundOut.WasapiOut()) { //Tell the SoundOut which sound it has to play soundOut.Initialize(soundSource); //Play the sound soundOut.Play(); while ((soundOut.PlaybackState == CSCore.SoundOut.PlaybackState.Playing) && (soundNumPlaying >= 0)) { Thread.Sleep(10); } //Stop the playback soundOut.Stop(); } } }