Ejemplo n.º 1
0
        public async Task <bool> InitializeSound()
        {
            AudioGraphSettings     settings = new AudioGraphSettings(AudioRenderCategory.Media);
            CreateAudioGraphResult result   = await AudioGraph.CreateAsync(settings);

            if (result.Status != AudioGraphCreationStatus.Success)
            {
                return(false);
            }

            graph = result.Graph;
            CreateAudioDeviceOutputNodeResult deviceOutputNodeResult = await graph.CreateDeviceOutputNodeAsync();

            if (deviceOutputNodeResult.Status == AudioDeviceNodeCreationStatus.Success)
            {
                deviceOutput = deviceOutputNodeResult.DeviceOutputNode;
                graph.ResetAllNodes();

                foreach (var soundMapping in soundMappings)
                {
                    await AddFileToSoundDictionary("ms-appx:///Features/Game/Assets/" + soundMapping.Value);
                }

                graph.Start();
            }

            isInitialized = true;
            return(isInitialized);
        }
Ejemplo n.º 2
0
        public async Task InitializeSounds()
        {
            InputNodes = new ObservableCollection <AudioFileInputNode>();
            FileInputNodesDictionary = new Dictionary <string, AudioFileInputNode>();

            AudioGraphSettings     settings = new AudioGraphSettings(AudioRenderCategory.Media);
            CreateAudioGraphResult result   = await AudioGraph.CreateAsync(settings);

            if (result.Status == AudioGraphCreationStatus.Success)
            {
                graph = result.Graph;
                CreateAudioDeviceOutputNodeResult deviceOutputNodeResult = await graph.CreateDeviceOutputNodeAsync();


                // make sure the audio output is available
                if (deviceOutputNodeResult.Status == AudioDeviceNodeCreationStatus.Success)
                {
                    outputNode = deviceOutputNodeResult.DeviceOutputNode;
                    graph.ResetAllNodes();

                    for (int i = 0; i < countdownSoundsNum; i++)
                    {
                        await CreateInputNodeFromFile(baseUri + countdownBaseFileName + "0" + (i + 1).ToString() + "-" + "1" + audioFileExt);
                        await CreateInputNodeFromFile(baseUri + countdownBaseFileName + "0" + (i + 1).ToString() + "-" + "2" + audioFileExt);
                    }

                    for (int j = 0; j < doneSoundsNum; j++)
                    {
                        await CreateInputNodeFromFile(baseUri + doneBaseFileName + ((j >= 9) ? "" : "0") + (j + 1).ToString() + audioFileExt);
                    }

                    graph.Start();
                }
            }
        }
Ejemplo n.º 3
0
        public async Task InitializeSounds()
        {
            soundBankInitializer = new SoundBanksInitializer();

            POneInputNodes = new ObservableCollection <AudioFileInputNode>();

            PTwoInputNodes = new ObservableCollection <AudioFileInputNode>();

            WOneInputNodes = new ObservableCollection <AudioFileInputNode>();

            WTwoInputNodes = new ObservableCollection <AudioFileInputNode>();

            InputNodesList = new ObservableCollection <ObservableCollection <AudioFileInputNode> >();
            InputNodesList.Add(POneInputNodes);
            InputNodesList.Add(PTwoInputNodes);
            InputNodesList.Add(WOneInputNodes);
            InputNodesList.Add(WTwoInputNodes);

            AudioGraphSettings     settings = new AudioGraphSettings(AudioRenderCategory.Media);
            CreateAudioGraphResult result   = await AudioGraph.CreateAsync(settings);

            if (result.Status == AudioGraphCreationStatus.Success)
            {
                graph = result.Graph;


                // create the output device
                CreateAudioDeviceOutputNodeResult deviceOutputNodeResult = await graph.CreateDeviceOutputNodeAsync();

                // make sure the audio output is available
                if (deviceOutputNodeResult.Status == AudioDeviceNodeCreationStatus.Success)
                {
                    deviceOutputNode = deviceOutputNodeResult.DeviceOutputNode;
                    graph.ResetAllNodes();

                    foreach (SoundBank soundBank in soundBankInitializer.SoundBanks)
                    {
                        foreach (string fileName in soundBank.FileNames[0])
                        {
                            await CreateInputNodeFromFile("ms-appx:///Assets/AudioSamples/" + fileName);

                            InputNodesList[soundBankInitializer.SoundBanks.IndexOf(soundBank)].Add(FileInputNodesDictionary[fileName]);
                        }

                        foreach (string fileName in soundBank.FileNames[1])
                        {
                            await CreateInputNodeFromFile("ms-appx:///Assets/AudioSamples/" + fileName);

                            FileInputNodesDictionary[fileName].LoopCount = null;

                            InputNodesList[soundBankInitializer.SoundBanks.IndexOf(soundBank)].Add(FileInputNodesDictionary[fileName]);
                        }
                    }

                    InitializeEffects();
                    graph.Start();
                }
            }
        }
Ejemplo n.º 4
0
		private async Task CreateAudioGraph()
		{
			var settings = new AudioGraphSettings(AudioRenderCategory.Media);
			var result = await AudioGraph.CreateAsync(settings);
			_graph = result.Graph;
			var deviceOutputNodeResult = await _graph.CreateDeviceOutputNodeAsync();
			_deviceOutput = deviceOutputNodeResult.DeviceOutputNode;
			_graph.ResetAllNodes();
			_graph.Start();
		}
Ejemplo n.º 5
0
        private async Task CreateAudioGraph()
        {
            var settings = new AudioGraphSettings(AudioRenderCategory.Media);
            var result   = await AudioGraph.CreateAsync(settings);

            _graph = result.Graph;
            var deviceOutputNodeResult = await _graph.CreateDeviceOutputNodeAsync();

            _deviceOutput = deviceOutputNodeResult.DeviceOutputNode;
            _graph.ResetAllNodes();
            _graph.Start();
        }
Ejemplo n.º 6
0
        private async Task InitializeAsync()
        {
            var settings = new AudioGraphSettings(Windows.Media.Render.AudioRenderCategory.GameEffects);

            var graphCreationResult = await AudioGraph.CreateAsync(settings);
            _graph = graphCreationResult.Graph;

            var deviceOutputCreationResult = await _graph.CreateDeviceOutputNodeAsync();
            _deviceOutput = deviceOutputCreationResult.DeviceOutputNode;

            _graph.ResetAllNodes();
            _graph.Start();
        }
Ejemplo n.º 7
0
        private async Task CreateAudioGraphAsync(bool isHeadset)
        {
            var settings = new AudioGraphSettings(AudioRenderCategory.Communications);

            settings.EncodingProperties = AudioEncodingProperties.CreatePcm(96000, 1, 24);
            var devices = await DeviceInformation.FindAllAsync(MediaDevice.GetAudioRenderSelector());

            settings.PrimaryRenderDevice = devices.First(d => (isHeadset ? (d.Name ?? string.Empty).ToLower().Contains(HEADSET_NAME) : !(d.Name ?? string.Empty).ToLower().Contains(HEADSET_NAME)));
            var result = await AudioGraph.CreateAsync(settings);

            _graph = result.Graph;
            var deviceOutputNodeResult = await _graph.CreateDeviceOutputNodeAsync();

            _deviceOutput = deviceOutputNodeResult.DeviceOutputNode;
            _graph.ResetAllNodes();
            _graph.Start();
        }
Ejemplo n.º 8
0
        public void Stop()
        {
            lock (this)
            {
                _inputNode?.Dispose();
                _timer?.Dispose();
                _timer     = null;
                _inputNode = null;
            }

            try
            {
                _audioGraph?.Stop();
                _audioGraph?.ResetAllNodes();
            }
            catch (Exception)
            {
            }

            State = MediaPlaybackState.None;
        }
Ejemplo n.º 9
0
        //Check: https://docs.microsoft.com/en-us/windows/uwp/gaming/working-with-audio-in-your-directx-game
        public static async Task InitSound()
        {
            AudioGraphSettings     settings = new AudioGraphSettings(AudioRenderCategory.Media);
            CreateAudioGraphResult result   = await AudioGraph.CreateAsync(settings);

            if (result.Status == AudioGraphCreationStatus.Success)
            {
                graph = result.Graph;

                //Create output device for audio playing
                CreateAudioDeviceOutputNodeResult deviceOutputNodeResult = await graph.CreateDeviceOutputNodeAsync();

                //Check for succes
                if (deviceOutputNodeResult.Status == AudioDeviceNodeCreationStatus.Success)
                {
                    output = deviceOutputNodeResult.DeviceOutputNode;
                    graph.ResetAllNodes();

                    graph.Start();
                    initialized = true;
                }
            }
        }
Ejemplo n.º 10
0
 private void Stop()
 {
     // Causes all nodes in the graph to discard any data currently in their audio buffers.
     _audioGraph?.ResetAllNodes();
     IsPlaying = false;
 }
Ejemplo n.º 11
0
        unsafe private void Timer_Tick(object sender, object e)
        {
            try
            {
                AudioFrame audioFrame = frameOutputNode.GetFrame();
                using (AudioBuffer buffer = audioFrame.LockBuffer(AudioBufferAccessMode.Read))
                    using (IMemoryBufferReference memoryBufferReference = buffer.CreateReference())
                    {
                        byte * dataInBytes;
                        uint   capacityInBytes;
                        float *dataInFloat;

                        ((IMemoryBufferByteAccess)memoryBufferReference).GetBuffer(out dataInBytes, out capacityInBytes);
                        dataInFloat = (float *)dataInBytes;

                        Int32   pulseOn             = 0;
                        Int32   pulseOff            = 0;
                        Boolean transitionUpFound   = false;
                        Boolean transitionDownFound = false;
                        Int32   transitionCount     = 0;
                        periodLength = 0;
                        Boolean high = dataInFloat[0] > 0;
                        for (Int32 i = 0; i < capacityInBytes / 8; i++)
                        {
                            if (dataInFloat[i] != 0)
                            {
                                if (dataInFloat[i] < -0.05) // If low
                                {
                                    if (high)               // If was high
                                    {
                                        transitionDownFound = true;
                                        transitionCount++;
                                    }
                                    high = false;
                                }
                                else if (dataInFloat[i] > 0.05) // Is high
                                {
                                    if (!high)                  // If was low
                                    {
                                        transitionUpFound = true;
                                        transitionCount++;
                                    }
                                    high = true;
                                }

                                if (transitionCount > 2)
                                {
                                    periodLength = (int)((pulseOn + pulseOff) / 9.27);
                                    break;
                                }

                                if (high && transitionUpFound)
                                {
                                    pulseOn++;
                                }
                                else if (!high && transitionDownFound)
                                {
                                    pulseOff++;
                                }
                            }
                        }
                        dataInFloat = null;
                        dataInBytes = null;
                        memoryBufferReference.Dispose();
                        buffer.Dispose();
                        audioFrame.Dispose();
                        periodLengthUK101 = periodLength;
                    }
            } catch { }
            //audioFrame = frameOutputNode.GetFrame();
            audioGraph.Stop();
            audioGraph.ResetAllNodes();
            audioGraph.Start();
        }