static void playNode(AudioFileInputNode node) { if (node == null) { return; } node.Reset(); node.Start(); }
private async void MainGraph_QuantumProcessed(AudioGraph sender, object args) { try { this.Position = mainInputNode.Position; if (NextJump != null) { if (NextJump != null && Position >= NextJump.Origin - NextJump.CrossFade && Position <= NextJump.Origin && !IsFading && NextJump.Song == mainSong) { IsFading = true; subInputNode = mainInputNode; subSong = mainSong; mainSong = NextJump.TargetSong ?? NextJump.Song; mainInputNode = await mainSong.Song.CreateNode(graph); mainInputNode.AddOutgoingConnection(outputNode); mainInputNode.StartTime = NextJump.TargetTime - (NextJump.Origin - subInputNode.Position); mainInputNode.OutgoingGain = 0; mainInputNode.Start(); } if (IsFading && subInputNode != null) { var fadePosition = (NextJump.Origin - subInputNode.Position); var fadeTime = NextJump.CrossFade; var percentage = Math.Min(1.0, Math.Max(0.0, fadePosition.TotalSeconds / fadeTime.TotalSeconds)); subInputNode.OutgoingGain = percentage; mainInputNode.OutgoingGain = 1.0 - percentage; } if (Position > NextJump.Origin && IsFading && subInputNode != null) { mainInputNode.OutgoingGain = 1.0; subInputNode.Stop(); subInputNode.RemoveOutgoingConnection(outputNode); subInputNode.Dispose(); subInputNode = null; subSong = null; NextJump = NextJump.NextDefaultJump; IsFading = false; } } } catch (Exception e) { App.Log(e); Recover(); } }
// Async Speak output of a text private async Task PlayAsyncLow(SoundBite soundBite) { _playing = true; // locks additional calls for Speak until finished talking this bit if (!_canPlay || _audioGraph == null || _deviceOutputNode == null) { LOG.LogError($"PlayAsyncLow: Some items do not exist: cannot play..\n [{_audioGraph}] [{_deviceOutputNode}]"); await EndOfSound( ); return; } // don't reload if the sound is already in use if (soundBite.Melody != _soundInUse?.Melody) { // if a prev. Node exists, remove it if (_fileInputNode != null) { _audioGraph.Stop( ); _fileInputNode.FileCompleted -= _fileInputNode_FileCompleted; _fileInputNode.RemoveOutgoingConnection(_deviceOutputNode); _fileInputNode.Dispose( ); _fileInputNode = null; } // set new sound _sound = _installedSounds.Where(x => x.Melody == soundBite.Melody).FirstOrDefault( ); if (_sound == null) { LOG.LogError($"PlayAsyncLow: Melody has no Audiofile: {soundBite.Melody} - cannot play"); await EndOfSound( ); return; } StorageFile file = await StorageFile.CreateStreamedFileAsync($"{_sound.Id}.{_sound.SType}", StreamedFileWriter, null); // create the InputNode var resultAF = await _audioGraph.CreateFileInputNodeAsync(file); if (resultAF.Status != AudioFileNodeCreationStatus.Success) { LOG.LogError($"PlayAsyncLow: AudioFileNodeCreationStatus creation: {resultAF.Status}" + $"\nExtError: {resultAF.ExtendedError}"); await EndOfSound( ); return; } _fileInputNode = resultAF.FileInputNode; _fileInputNode.FileCompleted += _fileInputNode_FileCompleted; _fileInputNode.AddOutgoingConnection(_deviceOutputNode); _audioGraph.Start( ); _soundInUse = _sound.AsCopy( ); } // we capture problems through Exceptions here - the settings and restrictions seem not complete in the Docs try { // Play it // cannot start after prev end - so set it null and seek to start of file _fileInputNode.StartTime = null; _fileInputNode.EndTime = null; // cannot start after prev end - so set it null _fileInputNode.Seek(new TimeSpan(0)); // have to seek to Start, we cannot assign a StartTime before the current Position // only now we can set any new start and end... (that is not in the documents...) _fileInputNode.StartTime = TimeSpan.FromSeconds(soundBite.Tone * _soundInUse.ToneStep_sec); _fileInputNode.EndTime = _fileInputNode.StartTime + TimeSpan.FromSeconds((soundBite.Duration < 0) ? _soundInUse.ToneDuration_sec : soundBite.Duration); _fileInputNode.OutgoingGain = soundBite.Volume; _fileInputNode.LoopCount = (int)soundBite.Loops; // counts down in the Completed Callback - track completeness there (not in docu...) _fileInputNode.PlaybackSpeedFactor = soundBite.SpeedFact; // Plays in the current Thread - cannot be waited for in the same thread _fileInputNode.Start( ); //_audioGraph.Start( ); } catch (Exception e) { LOG.LogError($"PlayAsyncLow: Sample Setup caused an Exception\n{e.Message}"); await EndOfSound( ); } }