/// <summary>
        ///     Starts every time when audio frame is read from a file
        /// </summary>
        private void FileInput_QuantumStarted(AudioGraph sender, object args)
        {
            // to not report too many times
            if (sender.CompletedQuantumCount % 100 == 0)
            {
                var numOfSamples =
                    0.0000001
                    * _fileInputNode.Duration.Ticks
                    * _fileInputNode.EncodingProperties.SampleRate;
                var dProgress =
                    100 *
                    (int)sender.CompletedQuantumCount
                    * sender.SamplesPerQuantum /
                    numOfSamples;
                _ioProgress?.Report(dProgress);
            }

            if (_audioDataCurrentPosition == 0)
            {
                _frameOutputNode.Start();
            }

            var frame = _frameOutputNode.GetFrame();

            ProcessInputFrame(frame);

            if (_finished)
            {
                _frameOutputNode?.Stop();
                _audioGraph?.Stop();
            }
        }
Ejemplo n.º 2
0
        public async Task StartAudioInput(IAudioClient audioClient)
        {
            StartMicConnectWatcher();

            if (_AudioOutStream != null)
            {
                _AudioOutStream.Dispose();
                _AudioOutStream = null;
            }

            if (InputDeviceState != InputDeviceState.Avairable)
            {
                if (!await ResetAudioInput())
                {
                    return;
                }
            }

            _AudioOutStream = audioClient.CreatePCMStream(AudioApplication.Voice, 1920, 100);

            _FrameOutputNode.Stop();

            _AudioGraph.QuantumStarted += AudioGraph_QuantumStarted;

            _FrameOutputNode.Start();

            _AudioGraph.Start();
        }
Ejemplo n.º 3
0
        private async Task StartAudioGraph()
        {
            if (_audioGraph == null)
            {
                await InitAudioGraph();

                var success = await CreateMicrophoneInputNode();

                if (!success)
                {
                    IsMuted = true;
                    return;
                }

                CreateToxOutputNode();

                CreateToxInputNode();
                await CreateSpeakerOutputNode();
            }
            else
            {
                if (IsMuted)
                {
                    _microphoneInputNode.Stop();
                    _toxOutputNode.Stop();
                }
                else
                {
                    _microphoneInputNode.Start();
                    _toxOutputNode.Start();
                }
            }

            _audioGraph.Start();
        }
Ejemplo n.º 4
0
        public static async Task CreateDeviceInputNode()
        {
            Console.WriteLine("Creating AudioGraphs");
            // Create an AudioGraph with default settings
            AudioGraphSettings graphsettings = new AudioGraphSettings(AudioRenderCategory.GameChat);

            graphsettings.EncodingProperties               = new AudioEncodingProperties();
            graphsettings.EncodingProperties.Subtype       = "Float";
            graphsettings.EncodingProperties.SampleRate    = 48000;
            graphsettings.EncodingProperties.ChannelCount  = 2;
            graphsettings.EncodingProperties.BitsPerSample = 32;
            graphsettings.EncodingProperties.Bitrate       = 3072000;
            //settings.DesiredSamplesPerQuantum = 960;
            //settings.QuantumSizeSelectionMode = QuantumSizeSelectionMode.ClosestToDesired;
            CreateAudioGraphResult graphresult = await AudioGraph.CreateAsync(graphsettings);

            if (graphresult.Status != AudioGraphCreationStatus.Success)
            {
                // Cannot create graph
                return;
            }

            ingraph = graphresult.Graph;


            AudioGraphSettings nodesettings = new AudioGraphSettings(AudioRenderCategory.GameChat);

            nodesettings.EncodingProperties       = AudioEncodingProperties.CreatePcm(48000, 2, 16);
            nodesettings.DesiredSamplesPerQuantum = 960;
            nodesettings.QuantumSizeSelectionMode = QuantumSizeSelectionMode.ClosestToDesired;
            frameOutputNode         = ingraph.CreateFrameOutputNode(outgraph.EncodingProperties);
            quantum                 = 0;
            ingraph.QuantumStarted += Graph_QuantumStarted;

            Windows.Devices.Enumeration.DeviceInformation selectedDevice =
                await Windows.Devices.Enumeration.DeviceInformation.CreateFromIdAsync(Windows.Media.Devices.MediaDevice.GetDefaultAudioCaptureId(Windows.Media.Devices.AudioDeviceRole.Default));

            CreateAudioDeviceInputNodeResult result =
                await ingraph.CreateDeviceInputNodeAsync(MediaCategory.Media, nodesettings.EncodingProperties, selectedDevice);

            if (result.Status != AudioDeviceNodeCreationStatus.Success)
            {
                // Cannot create device output node
                return;
            }

            deviceInputNode = result.DeviceInputNode;
            deviceInputNode.AddOutgoingConnection(frameOutputNode);
            frameOutputNode.Start();
            ingraph.Start();
        }
Ejemplo n.º 5
0
        /// <summary>
        /// Starts the microphone input.
        /// </summary>
        /// <returns>Whether or not the start was successful</returns>
        private static async Task <bool> StartInput()
        {
            isCreating = true;

            bool successAudioGraph         = true;
            bool successOutgoingConnection = true;

            if (isDisposed)
            {
                successAudioGraph = await CreateAudioGraph();

                successOutgoingConnection = await CreateNodes();

                isDisposed = false;
            }
            else
            {
                deviceInputNode.Start();
                frameOutputNode.Start();
            }

            if (successAudioGraph && successOutgoingConnection)
            {
                graph.Start();

                isListing  = true;
                isCreating = false;

                if (stopOnCreation)
                {
                    _ = StopInput();
                    stopOnCreation = false;
                }
                return(true);
            }
            else
            {
                isCreating = false;
                isListing  = false;
                return(false);
            }
        }
Ejemplo n.º 6
0
        private async void CreateAudioGraphAsync()
        {
            AudioGraphSettings settings = new AudioGraphSettings(AudioRenderCategory.Media)
            {
                //settings.DesiredSamplesPerQuantum = fftLength;
                DesiredRenderDeviceAudioProcessing = AudioProcessing.Default,
                QuantumSizeSelectionMode           = QuantumSizeSelectionMode.ClosestToDesired
            };

            CreateAudioGraphResult graphResult = await AudioGraph.CreateAsync(settings);

            if (graphResult.Status != AudioGraphCreationStatus.Success)
            {
                throw new InvalidOperationException($"Graph creation failed {graphResult.Status}");
            }

            _graph = graphResult.Graph;

            //CreateAudioDeviceInputNodeResult inputNodeResult = await _graph.CreateDeviceInputNodeAsync(MediaCategory.Media);
            CreateAudioDeviceInputNodeResult inputNodeResult = await _graph.CreateDeviceInputNodeAsync(MediaCategory.Other);

            if (inputNodeResult.Status == AudioDeviceNodeCreationStatus.Success)
            {
                _inputNode = inputNodeResult.DeviceInputNode;


                _frameOutputNode = _graph.CreateFrameOutputNode();
                _inputNode.AddOutgoingConnection(_frameOutputNode);
                _frameOutputNode.Start();
                _graph.QuantumProcessed += AudioGraph_QuantumProcessed;

                // Because we are using lowest latency setting, we need to handle device disconnection errors
                _graph.UnrecoverableErrorOccurred += Graph_UnrecoverableErrorOccurred;

                _graph.Start();
            }
            else
            {
                MessageDialog md = new MessageDialog("Cannot access microphone");
                await md.ShowAsync();
            }
        }
Ejemplo n.º 7
0
        private async void Init()
        {
            AudioGraphSettings settings = new AudioGraphSettings(AudioRenderCategory.Media);

            settings.QuantumSizeSelectionMode = QuantumSizeSelectionMode.LowestLatency;

            CreateAudioGraphResult result = await AudioGraph.CreateAsync(settings);

            if (result.Status != AudioGraphCreationStatus.Success)
            {
                mainPage.MessageBox("Could not create input device for Mic To MIDI!");
                return;
            }

            audioGraph = result.Graph;
            CreateAudioDeviceInputNodeResult deviceInputNodeResult = await audioGraph.CreateDeviceInputNodeAsync(MediaCategory.Other);

            if (deviceInputNodeResult.Status != AudioDeviceNodeCreationStatus.Success)
            {
                mainPage.MessageBox(String.Format("Audio Device Input unavailable because {0}", deviceInputNodeResult.Status.ToString()));
                return;
            }

            deviceInputNode = deviceInputNodeResult.DeviceInputNode;

            frameOutputNode = audioGraph.CreateFrameOutputNode();
            deviceInputNode.AddOutgoingConnection(frameOutputNode);
            //audioGraph.QuantumStarted += AudioGraph_QuantumStarted;

            audioGraph.Start();
            deviceInputNode.Start();
            frameOutputNode.Start();

            timer          = new DispatcherTimer();
            timer.Interval = new TimeSpan(0, 0, 0, 0, 1); // 1 ms
            timer.Tick    += Timer_Tick;
            timer.Start();
            periodLengthUK101 = 0;
        }
Ejemplo n.º 8
0
        /// <summary>
        /// Create input audio graph
        /// </summary>
        /// <param name="deviceId">Override for default input device id</param>
        public static async Task <bool> CreateInputDeviceNode(string deviceId = null)
        {
            // If not in use, redo dispose
            if (ingraph != null && deviceId != InputDeviceID)
            {
                HeavyDisposeInGraph();
            }
            // Increment use counter
            else
            {
                inGraphCount++;
            }

            Console.WriteLine("Creating AudioGraphs");

            // Create an AudioGraph with default settings
            AudioGraphSettings graphsettings = new AudioGraphSettings(AudioRenderCategory.Media);

            graphsettings.EncodingProperties               = new AudioEncodingProperties();
            graphsettings.EncodingProperties.Subtype       = "Float";
            graphsettings.EncodingProperties.SampleRate    = 48000;
            graphsettings.EncodingProperties.ChannelCount  = 2;
            graphsettings.EncodingProperties.BitsPerSample = 32;
            graphsettings.EncodingProperties.Bitrate       = 3072000;
            CreateAudioGraphResult graphresult = await AudioGraph.CreateAsync(graphsettings);

            if (graphresult.Status != AudioGraphCreationStatus.Success)
            {
                // Cannot create graph
                inGraphCount--;
                LocalState.VoiceState.SelfMute = true;
                VoiceManager.lockMute          = true;
                return(false);
            }

            // "Save" graph
            ingraph = graphresult.Graph;

            // Create frameOutputNode
            AudioGraphSettings nodesettings = new AudioGraphSettings(AudioRenderCategory.GameChat);

            nodesettings.EncodingProperties       = AudioEncodingProperties.CreatePcm(48000, 2, 32);
            nodesettings.DesiredSamplesPerQuantum = 960;
            nodesettings.QuantumSizeSelectionMode = QuantumSizeSelectionMode.ClosestToDesired;
            frameOutputNode         = ingraph.CreateFrameOutputNode(ingraph.EncodingProperties);
            quantum                 = 0;
            ingraph.QuantumStarted += Graph_QuantumStarted;

            // Determine selected device
            DeviceInformation selectedDevice;

            if (deviceId == "Default" || deviceId == null)
            {
                string device = Windows.Media.Devices.MediaDevice.GetDefaultAudioCaptureId(Windows.Media.Devices.AudioDeviceRole.Default);
                if (!string.IsNullOrEmpty(device))
                {
                    selectedDevice = await DeviceInformation.CreateFromIdAsync(device);

                    Windows.Media.Devices.MediaDevice.DefaultAudioCaptureDeviceChanged += MediaDevice_DefaultAudioCaptureDeviceChanged;
                }
                else
                {
                    inGraphCount--;
                    LocalState.VoiceState.SelfMute = true;
                    VoiceManager.lockMute          = true;
                    return(false);
                }
            }
            else
            {
                try
                {
                    selectedDevice = await DeviceInformation.CreateFromIdAsync(deviceId);
                }
                catch
                {
                    selectedDevice = await DeviceInformation.CreateFromIdAsync(Windows.Media.Devices.MediaDevice.GetDefaultAudioCaptureId(Windows.Media.Devices.AudioDeviceRole.Default));

                    deviceId = "Default";
                }
            }

            CreateAudioDeviceInputNodeResult result =
                await ingraph.CreateDeviceInputNodeAsync(MediaCategory.Media, nodesettings.EncodingProperties, selectedDevice);

            if (result.Status != AudioDeviceNodeCreationStatus.Success)
            {
                // Cannot create device output node
                inGraphCount--;
                LocalState.VoiceState.SelfMute = true;
                VoiceManager.lockMute          = true;
                return(false);
            }


            // Attach input device
            deviceInputNode = result.DeviceInputNode;
            deviceInputNode.AddOutgoingConnection(frameOutputNode);
            InputDeviceID = deviceId;

            // Begin playing
            frameOutputNode.Start();
            ingraph.Start();
            return(true);
        }
Ejemplo n.º 9
0
        protected override async void OnNavigatedTo(NavigationEventArgs e)
        {
            var audioInputDevices = await DeviceInformation.FindAllAsync(DeviceClass.AudioCapture);

            foreach (var device in audioInputDevices)
            {
                if (device.Name.ToLower().Contains("usb"))
                {
                    audioInput = device;
                    break;
                }
            }
            if (audioInput == null)
            {
                Debug.WriteLine("Could not find USB audio card");
                return;
            }
            var audioOutputDevices = await DeviceInformation.FindAllAsync(DeviceClass.AudioRender);

            foreach (var device in audioOutputDevices)
            {
                if (device.Name.ToLower().Contains("usb"))
                {
                    audioOutput = device;
                }
                else
                {
                    raspiAudioOutput = device;
                }
            }
            if (audioOutput == null)
            {
                Debug.WriteLine("Could not find USB audio card");
                return;
            }

            // Set up LED strips
            await leftStrip.Begin();

            await rightStrip.Begin();

            //await AudioTest();
            AudioGraphSettings audioGraphSettings = new AudioGraphSettings(AudioRenderCategory.Media);

            audioGraphSettings.DesiredSamplesPerQuantum           = 440;
            audioGraphSettings.DesiredRenderDeviceAudioProcessing = AudioProcessing.Default;
            audioGraphSettings.QuantumSizeSelectionMode           = QuantumSizeSelectionMode.ClosestToDesired;
            audioGraphSettings.PrimaryRenderDevice = raspiAudioOutput;
            CreateAudioGraphResult audioGraphResult = await AudioGraph.CreateAsync(audioGraphSettings);

            if (audioGraphResult.Status != AudioGraphCreationStatus.Success)
            {
                Debug.WriteLine("AudioGraph creation failed! " + audioGraphResult.Status);
                return;
            }
            audioGraph = audioGraphResult.Graph;
            //Debug.WriteLine(audioGraph.SamplesPerQuantum);
            CreateAudioDeviceInputNodeResult inputNodeResult = await audioGraph.CreateDeviceInputNodeAsync(MediaCategory.Media, audioGraph.EncodingProperties, audioInput);

            if (inputNodeResult.Status != AudioDeviceNodeCreationStatus.Success)
            {
                Debug.WriteLine("AudioDeviceInputNode creation failed! " + inputNodeResult.Status);
                return;
            }
            AudioDeviceInputNode inputNode = inputNodeResult.DeviceInputNode;
            CreateAudioDeviceOutputNodeResult outputNodeResult = await audioGraph.CreateDeviceOutputNodeAsync();

            if (outputNodeResult.Status != AudioDeviceNodeCreationStatus.Success)
            {
                Debug.WriteLine("AudioDeviceOutputNode creation failed!" + outputNodeResult.Status);
            }
            AudioDeviceOutputNode outputNode = outputNodeResult.DeviceOutputNode;

            frameOutputNode = audioGraph.CreateFrameOutputNode();
            inputNode.AddOutgoingConnection(frameOutputNode);
            inputNode.AddOutgoingConnection(outputNode);
            cube.SetSpeedStripLedColors(LedColorLists.rainbowColors);
            audioGraph.QuantumProcessed           += AudioGraph_QuantumProcessed;
            audioGraph.UnrecoverableErrorOccurred += AudioGraph_UnrecoverableErrorOccurred;
            audioGraph.Start();
            outputNode.Start();
            inputNode.Start();
            frameOutputNode.Start();
            cube.Reset();
            cube.Update();
            //await MathFunc();
            //cube.ApplyColorFunction((x, y, z) =>
            //{
            //    Color c = Color.FromArgb(255,
            //        (byte)((x / 14.0) * 255.0),
            //        (byte)((y / 14.0) * 255.0),
            //        (byte)((z / 14.0) * 255.0));
            //    return c;
            //});
            //cube.SetLedColors();
            //cube.Update();
            //cube.bottomFrontEdge.SetColor(Colors.Red);
            //cube.bottomRightEdge.SetColor(Colors.OrangeRed);
            //cube.bottomBackEdge.SetColor(Colors.Yellow);
            //cube.bottomLeftEdge.SetColor(Colors.Green);
            //cube.frontLeftEdge.SetColor(Colors.Blue);
            //cube.frontTopEdge.SetColor(Colors.Purple);
            //cube.rightLeftEdge.Brightness = 10;
            //cube.rightLeftEdge.SetColor(Colors.Red);
            //cube.rightTopEdge.Brightness = 10;
            //cube.rightTopEdge.SetColor(Colors.OrangeRed);
            //cube.backLeftEdge.Brightness = 10;
            //cube.backLeftEdge.SetColor(Colors.Yellow);
            //cube.backTopEdge.Brightness = 10;
            //cube.backTopEdge.SetColor(Colors.Green);
            //cube.leftLeftEdge.Brightness = 10;
            //cube.leftLeftEdge.SetColor(Colors.Blue);
            //cube.leftTopEdge.Brightness = 10;
            //cube.leftTopEdge.SetColor(Colors.Purple);
            //cube.Update();
            //await RainbowTest();
            //cube.Brightness = 30;
            //await FlashTest();
            //SetAll();
            //await FadeTest();
            //cube.Reset();
            //cube.Update();
            //await cube.rightLeftEdge.DoLine();
            //ZackTest();
        }