Quantcast
Channel: Windows Presentation Foundation (WPF) forum
Viewing all articles
Browse latest Browse all 18858

Wpf Recording from microphone - volume

$
0
0

Hi.

I have code bellow. It's running OK. But volume is very weak. Would you tell me how to adjust the volume?

Thank you.

<Window x:Class="WpfApplication1.MainWindow"
        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
        Title="MainWindow" Height="350" Width="525"><Grid><Button Content="Nahrávanie" Height="23" HorizontalAlignment="Left" Margin="12,276,0,0" Name="button1" VerticalAlignment="Top" Width="75" Click="button1_Click" /><Button Content="Stop" Height="23" HorizontalAlignment="Left" Margin="106,276,0,0" Name="button2" VerticalAlignment="Top" Width="75" Click="button2_Click" /><Button Content="Prehrávanie" Height="23" HorizontalAlignment="Left" Margin="187,276,0,0" Name="button3" VerticalAlignment="Top" Width="75" Click="button3_Click" /><TextBox Height="23" HorizontalAlignment="Left" Margin="280,276,0,0" Name="textBox1" VerticalAlignment="Top" Width="120" /><RichTextBox Height="176" HorizontalAlignment="Left" Margin="29,66,0,0" Name="richTextBox1" VerticalAlignment="Top" Width="387" /></Grid></Window>

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;
using System.Windows.Threading;
using Microsoft.Xna.Framework.Audio;
using System.IO;
using Microsoft.Xna.Framework;

namespace WpfApplication1
{
    /// <summary>
    /// Interaction logic for MainWindow.xaml
    /// </summary>
    public partial class MainWindow : Window
    {
        Microphone m_Microphone = Microphone.Default;
        SoundEffectInstance m_Playback;
        MemoryStream m_Sound = new MemoryStream();
        byte[] m_chunk;
        public MainWindow()
        {
            InitializeComponent();
            m_Microphone.BufferReady += (s, e) =>
            {
                m_Microphone.GetData(m_chunk);
                m_Sound.Write(m_chunk, 0, m_chunk.Length);
            };

            var _Timer = new DispatcherTimer {Interval = TimeSpan.FromMilliseconds(33) };
            _Timer.Tick += (s, arg) =>
                {
                    FrameworkDispatcher.Update();
                    if (m_Microphone.State == MicrophoneState.Started)
                        return;
                    if (m_Playback != null && m_Playback.State == SoundState.Playing)
                        return;
                    textBox1.Text = "Ready";
                };
            _Timer.Start();
        }

        private void button1_Click(object sender, RoutedEventArgs e)
        {
            m_Microphone.BufferDuration = TimeSpan.FromMilliseconds(500);
            m_chunk = new byte[m_Microphone.GetSampleSizeInBytes(m_Microphone.BufferDuration)];
            m_Sound.SetLength(0);
            m_Microphone.Start();
            textBox1.Text = "recording";
        }

        private void button2_Click(object sender, RoutedEventArgs e)
        {
            if (m_Microphone.State == MicrophoneState.Started)
                m_Microphone.Stop();
            else if (m_Playback.State == SoundState.Playing)
                m_Playback.Stop();
        }

        private void button3_Click(object sender, RoutedEventArgs e)
        {
            if (m_Sound.Length == 0)
                return;
            var _Sound = new SoundEffect(m_Sound.ToArray(), m_Microphone.SampleRate, AudioChannels.Mono);
            m_Playback = _Sound.CreateInstance();
            m_Playback.Play();
            textBox1.Text = "playing";
        }
    }
}



Viewing all articles
Browse latest Browse all 18858

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>