Exemple #1
0
        private AudioNodeEmitter GetEmitter(PlaybackOptions options)
        {
            if (options.Position == null)
            {
                return(null);
            }

            var shape = AudioNodeEmitterShape.CreateOmnidirectional();
            var decay = AudioNodeEmitterDecayModel.CreateNatural(0.1, 1.0, 10, 100);

            var emitter = new AudioNodeEmitter(shape, decay, AudioNodeEmitterSettings.None);

            emitter.SpatialAudioModel = SpatialAudioModel.ObjectBased;
            AdjustEmitter(emitter, options);
            return(emitter);
        }
        public async Task CreateFileInputNodeWithEmitter()
        {
            if (audioGraph == null)
            {
                return;
            }

            FileOpenPicker filePicker = new FileOpenPicker();

            filePicker.SuggestedStartLocation = PickerLocationId.MusicLibrary;
            filePicker.FileTypeFilter.Add(".mp3");
            filePicker.FileTypeFilter.Add(".wav");
            filePicker.FileTypeFilter.Add(".wma");
            filePicker.FileTypeFilter.Add(".m4a");
            filePicker.ViewMode = PickerViewMode.Thumbnail;
            StorageFile file = await filePicker.PickSingleFileAsync();

            // File can be null if cancel is hit in the file picker
            if (file == null)
            {
                return;
            }

            //<SnippetCreateEmitter>
            var emitterShape = AudioNodeEmitterShape.CreateOmnidirectional();
            var decayModel   = AudioNodeEmitterDecayModel.CreateNatural(.1, 1, 10, 100);
            var settings     = AudioNodeEmitterSettings.None;

            var emitter = new AudioNodeEmitter(emitterShape, decayModel, settings);

            emitter.Position = new System.Numerics.Vector3(10, 0, 5);

            CreateAudioFileInputNodeResult result = await audioGraph.CreateFileInputNodeAsync(file, emitter);

            if (result.Status != AudioFileNodeCreationStatus.Success)
            {
                ShowErrorMessage(result.Status.ToString());
            }

            fileInputNode = result.FileInputNode;
            //</SnippetCreateEmitter>
        }
        private async Task CreateEmitter(int spatialSoundIdx)
        {
            if (_graph == null)
            {
                return;
            }

            //Create the AudioNodeEmitter
            AudioNodeEmitter emitter = new AudioNodeEmitter(
                AudioNodeEmitterShape.CreateOmnidirectional(),
                AudioNodeEmitterDecayModels.CreateNatural(),
                AudioNodeEmitterSettings.None);

            emitter.Position = _spatialSounds[spatialSoundIdx].Position;
            //Create a new AudioFileInputNode and add an emitter to it
            var audio = await GetSoundFileAsync(spatialSoundIdx);

            CreateAudioFileInputNodeResult inCreateResult = await _graph.CreateFileInputNodeAsync(audio, emitter);

            inCreateResult.FileInputNode.OutgoingGain = 5; // _spatialSounds[_spatialSoundsIdx].Gain;
                                                           //Add the new AudioFileInputNode to the AudioGraph
            inCreateResult.FileInputNode.AddOutgoingConnection(_deviceOutput);
            inCreateResult.FileInputNode.AddOutgoingConnection(_submixNode);
            //Subscribe to the FileCompleted event so that we can go to the next spatial sound
            //inCreateResult.FileInputNode.FileCompleted += CurrentAudioFileInputNode_FileCompleted;

            if (_currentAudioFileInputNode != null &&
                _currentAudioFileInputNode.OutgoingConnections.Count > 0)
            {
                //Remove the old AudioFileOutputNode from the AudioGraph
                _currentAudioFileInputNode.RemoveOutgoingConnection(_deviceOutput);
                _currentAudioFileInputNode.RemoveOutgoingConnection(_submixNode);
            }

            //Set the current node
            _currentAudioFileInputNode = inCreateResult.FileInputNode;
        }
Exemple #4
0
        /// <summary>
        /// Loads audio file.
        /// </summary>
        /// <param name="PlayPosition"></param>
        /// <returns></returns>
        private async Task LoadAudioFile(TimeSpan PlayPosition)
        {
            if (soundFile != null)
            {
                ClearErrorMessage();
                StartButtonEnabled = false;
                StopButtonEnabled  = false;
                FileName           = "";

                try
                {
                    await BuildAudioGraph();
                }
                catch (Exception ex)
                {
                    ShowErrorMessage(ex.Message);
                    return;
                }
                emitter          = new AudioNodeEmitter(AudioNodeEmitterShape.CreateOmnidirectional(), AudioNodeEmitterDecayModel.CreateCustom(0.1, 1), AudioNodeEmitterSettings.None);
                emitter.Position = new Vector3((float)X, (float)Z, (float)-Y);

                CreateAudioFileInputNodeResult inCreateResult = await graph.CreateFileInputNodeAsync(soundFile, emitter);

                switch (inCreateResult.Status)
                {
                case AudioFileNodeCreationStatus.FormatNotSupported:
                case AudioFileNodeCreationStatus.InvalidFileType:
                    ShowErrorMessage(String.Format("Could not load {0}. \n\nPlease choose a different file - For Windows Spatial Sound processing, audio files must be Mono 48kHz PCM Wav.", soundFile.Name));
                    return;

                case AudioFileNodeCreationStatus.UnknownFailure:
                case AudioFileNodeCreationStatus.FileNotFound:
                    ShowErrorMessage(String.Format("Audio file load error: {0}", inCreateResult.Status.ToString()));
                    return;
                }


                //Dispose of previous input node.
                if (currentAudioFileInputNode != null)
                {
                    try
                    {
                        currentAudioFileInputNode.Dispose();
                    }
                    catch (ObjectDisposedException)
                    {
                        //Do nothing
                    }
                }
                currentAudioFileInputNode = inCreateResult.FileInputNode;
                currentAudioFileInputNode.AddOutgoingConnection(deviceOutput);
                currentAudioFileInputNode.FileCompleted += CurrentAudioFileInputNode_FileCompleted;
                FileName = currentAudioFileInputNode.SourceFile.Name;

                if (PlayPosition != TimeSpan.Zero)
                {
                    currentAudioFileInputNode.Seek(PlayPosition);
                    graph.Start();
                    StopButtonEnabled = true;
                }
                else
                {
                    StartButtonEnabled = true;
                }
            }
        }