不涉及语音识别~~


<Window x:Class="KinectRecordAudio.MainWindow"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
Title="Audio Recorder" Height="226" Width="405">
<Grid Width="369" Height="170">
<Button Content="Play" Height="44" HorizontalAlignment="Left" Margin="12,13,0,0" Name="button1" VerticalAlignment="Top" Width="114" Click="button1_Click" IsEnabled="{Binding IsPlayingEnabled}" FontSize="18"></Button>
<Button Content="Record" Height="44" HorizontalAlignment="Left" Margin="132,13,0,0" Name="button2" VerticalAlignment="Top" Width="110" Click="button2_Click" IsEnabled="{Binding IsRecordingEnabled}" FontSize="18"/>
<Button Content="Stop" Height="44" HorizontalAlignment="Left" Margin="248,13,0,0" Name="button3" VerticalAlignment="Top" Width="107" Click="button3_Click" IsEnabled="{Binding IsStopEnabled}" FontSize="18"/>
<CheckBox Content="Noise Suppression" Height="16" HorizontalAlignment="Left" Margin="16,77,0,0" VerticalAlignment="Top" Width="142" IsChecked="{Binding IsNoiseSuppressionOn}" />
<CheckBox Content="Automatic Gain Control" Height="16" HorizontalAlignment="Left" Margin="16,104,0,0" VerticalAlignment="Top" IsChecked="{Binding IsAutomaticGainOn}"/>
<CheckBox Content="AEC" Height="44" HorizontalAlignment="Left" IsChecked="{Binding IsAECOn}" Margin="16,129,0,0" VerticalAlignment="Top" />
</Grid>
</Window>

namespace KinectRecordAudio
{
/// <summary>
/// Interaction logic for MainWindow.xaml
/// </summary>
public partial class MainWindow : Window, INotifyPropertyChanged
{
// INotifyPropertyChanged 接口用于向客户端(通常是执行绑定的客户端)发出某一属性值已更改的通知。 string _recordingFileName; // 录音文件名
MediaPlayer _mplayer;
bool _isPlaying;
bool _isNoiseSuppressionOn;
bool _isAutomaticGainOn;
bool _isAECOn; // IsEnabled="{Binding IsRecordingEnabled}" 将控件与方法绑定 public MainWindow()
{
InitializeComponent();
this.Loaded += delegate { KinectSensor.KinectSensors[].Start(); };
_mplayer = new MediaPlayer();
_mplayer.MediaEnded += delegate { _mplayer.Close(); IsPlaying = false; };
this.DataContext = this;
} private void Play()
{
IsPlaying = true;
_mplayer.Open(new Uri(_recordingFileName, UriKind.Relative));
_mplayer.Play();
} private void Record()
{
Thread thread = new Thread(new ThreadStart(RecordKinectAudio));
thread.Priority = ThreadPriority.Highest; // 线程优先级
thread.Start();
} private void Stop()
{
KinectSensor.KinectSensors[].AudioSource.Stop();
IsRecording = false;
}
private object lockObj = new object(); private void RecordKinectAudio()
{
lock (lockObj)
{
// 线程加锁
IsRecording = true; var source = CreateAudioSource(); var time = DateTime.Now.ToString("hhmmss");
_recordingFileName = time + ".wav";
using (var fileStream =
new FileStream(_recordingFileName, FileMode.Create))
{
RecorderHelper.WriteWavFile(source, fileStream);
} IsRecording = false;
} } private KinectAudioSource CreateAudioSource()
{
var source = KinectSensor.KinectSensors[].AudioSource;
source.BeamAngleMode = BeamAngleMode.Adaptive;
source.NoiseSuppression = _isNoiseSuppressionOn;
source.AutomaticGainControlEnabled = _isAutomaticGainOn; if (IsAECOn)
{
source.EchoCancellationMode = EchoCancellationMode.CancellationOnly;
source.AutomaticGainControlEnabled = false;
IsAutomaticGainOn = false;
source.EchoCancellationSpeakerIndex = ;
} return source; // 返回经过处理的数据源
} #region user interaction handlers private void button1_Click(object sender, RoutedEventArgs e)
{
Play();
} private void button2_Click(object sender, RoutedEventArgs e)
{
Record();
} private void button3_Click(object sender, RoutedEventArgs e)
{
Stop();
} #endregion #region properties public event PropertyChangedEventHandler PropertyChanged; private void OnPropertyChanged(string propName)
{
if (PropertyChanged != null)
PropertyChanged(this, new PropertyChangedEventArgs(propName));
} private bool IsPlaying
{
get
{
return _isPlaying;
}
set
{
if (_isPlaying != value)
{
_isPlaying = value;
OnPropertyChanged("IsRecordingEnabled");
}
}
} private bool IsRecording
{
get
{
return RecorderHelper.IsRecording;
}
set
{
if (RecorderHelper.IsRecording != value)
{
RecorderHelper.IsRecording = value;
OnPropertyChanged("IsPlayingEnabled");
OnPropertyChanged("IsRecordingEnabled");
OnPropertyChanged("IsStopEnabled");
}
}
}
public bool IsPlayingEnabled
{
get { return !IsRecording; }
} public bool IsRecordingEnabled
{
get { return !IsPlaying && !IsRecording; }
} public bool IsStopEnabled
{
get { return IsRecording; }
} public bool IsNoiseSuppressionOn
{
get
{
return _isNoiseSuppressionOn;
}
set
{
if (_isNoiseSuppressionOn != value)
{
_isNoiseSuppressionOn = value;
OnPropertyChanged("IsNoiseSuppressionOn");
}
}
} public bool IsAutomaticGainOn
{
get
{
return _isAutomaticGainOn;
}
set
{
if (_isAutomaticGainOn != value)
{
_isAutomaticGainOn = value;
OnPropertyChanged("IsAutomaticGainOn");
}
}
} public bool IsAECOn
{
get
{
return _isAECOn;
}
set
{
if (_isAECOn != value)
{
_isAECOn = value;
OnPropertyChanged("IsAECOn");
}
}
} #endregion
}
}

class RecorderHelper
{
static byte[] buffer = new byte[];
static bool _isRecording; public static bool IsRecording
{
get
{
return _isRecording;
}
set
{
_isRecording = value;
}
} public static void WriteWavFile(KinectAudioSource source, FileStream fileStream)
{
var size = ;
//write wav header placeholder
WriteWavHeader(fileStream, size); // size=0;
using (var audioStream = source.Start())
{
// Starts capturing audio from the device. The data can be read using the returned stream.
//chunk audio stream to file
while (audioStream.Read(buffer, , buffer.Length) > && _isRecording)
{
fileStream.Write(buffer, , buffer.Length);
size += buffer.Length; }
} //write real wav header
// 方法开始写入一个假的头文件,然后读取Kinect中的音频数据流,然后填充FileStream对象,直到_isRecoding属性被设置为false。然后检查已经写入到文件中的数据流大小,用这个值来改写之前写入的文件头
long prePosition = fileStream.Position;
fileStream.Seek(, SeekOrigin.Begin);
WriteWavHeader(fileStream, size); // size = file length
fileStream.Seek(prePosition, SeekOrigin.Begin);
fileStream.Flush();
} public static void WriteWavHeader(Stream stream, int dataLength)
{
using (MemoryStream memStream = new MemoryStream())
{
// 基于指定的字节数组初始化 MemoryStream 类的无法调整大小的新实例 int cbFormat = ;
WAVEFORMATEX format = new WAVEFORMATEX()
{
wFormatTag = ,
nChannels = ,
nSamplesPerSec = ,
nAvgBytesPerSec = ,
nBlockAlign = ,
wBitsPerSample = ,
cbSize =
}; using (var bw = new BinaryWriter(memStream))
{ WriteString(memStream, "RIFF");
bw.Write(dataLength + cbFormat + );
WriteString(memStream, "WAVE");
WriteString(memStream, "fmt ");
bw.Write(cbFormat); bw.Write(format.wFormatTag);
bw.Write(format.nChannels);
bw.Write(format.nSamplesPerSec);
bw.Write(format.nAvgBytesPerSec);
bw.Write(format.nBlockAlign);
bw.Write(format.wBitsPerSample);
bw.Write(format.cbSize); WriteString(memStream, "data");
bw.Write(dataLength); memStream.WriteTo(stream);
}
}
} static void WriteString(Stream stream, string s)
{
byte[] bytes = Encoding.ASCII.GetBytes(s);
stream.Write(bytes, , bytes.Length);
} struct WAVEFORMATEX
{
public ushort wFormatTag;
public ushort nChannels;
public uint nSamplesPerSec;
public uint nAvgBytesPerSec;
public ushort nBlockAlign;
public ushort wBitsPerSample;
public ushort cbSize;
}
}

C#不支持直接写wma文件,需要C++的 WAVEFORMATEX 结构体

Kinect 开发 —— 录音的更多相关文章

  1. Kinect开发文章目录

    整理了一下去年为止到现在写的和翻译的Kinect的相关文章,方便大家查看.另外,最近京东上微软在搞活动, 微软 Kinect for Windows 京东十周年专供礼包 ,如果您想从事Kinect开发 ...

  2. Kinect开发资源汇总

    Kinect开发资源汇总   转自: http://www.sigvc.org/bbs/forum.php?mod=viewthread&tid=254&highlight=kinec ...

  3. Kinect开发学习笔记之(一)Kinect介绍和应用

    Kinect开发学习笔记之(一)Kinect介绍和应用 zouxy09@qq.com http://blog.csdn.net/zouxy09 一.Kinect简单介绍 Kinectfor Xbox ...

  4. Kinect开发笔记之二Kinect for Windows 2.0新功能

    这是本博客翻译文档的第一篇文章.笔者已经苦逼的竭尽全力的在翻译了.但无奈英语水平也是非常有限.不正确或者不妥当不准确的地方必定会有,还恳请大家留言或者邮件我以批评指正.我会虚心接受. 谢谢大家.   ...

  5. Kinect 开发 —— 杂一

    Kinect 提供了非托管(C++)和托管(.NET)两种开发方式的SDK,如果您用C++开发的话,需要安装Speech Runtime(V11),Kinect for Windows Runtime ...

  6. Kinect 开发 —— 控制PPT播放

    实现Kinect控制幻灯片播放很简单,主要思路是:使用Kinect捕捉人体动作,然后根据识别出来的动作向系统发出点击向前,向后按键的事件,从而使得幻灯片能够切换. 这里的核心功能在于手势的识别,我们在 ...

  7. Kinect 开发 —— 全息图

    Kinect的另一个有趣的应用是伪全息图(pseudo-hologram).3D图像可以根据人物在Kinect前面的各种位置进行倾斜和移动.如果方法够好,可以营造出3D控件中3D图像的效果,这样可以用 ...

  8. Kinect 开发 —— 进阶指引(上)

    本文将会介绍一些第三方类库如何来帮助处理Kinect传感器提供的数据.使用不同的技术进行Kinect开发,可以发掘出Kinect应用的强大功能.另一方面如果不使用这些为了特定处理目的而开发的一些类库, ...

  9. Kinect开发 —— 基础知识

    转自:http://www.cnblogs.com/yangecnu/archive/2012/04/02/KinectSDK_Application_Fundamentals_Part2.html ...

随机推荐

  1. mybatis如何成功插入后获取自增长的id

    使用mybatis向数据库中插入一条记录,如何获取成功插入记录的自增长id呢? 需要向xml配置中加上一下两个配置: <insert id="add" useGenerate ...

  2. <Sicily>Pythagorean Proposition

    一.题目描述 One day, WXYZ got a wooden stick, he wanted to split it into three sticks and make a right-an ...

  3. php--防止DDos攻击代码

    <?php //查询禁止IP $ip =$_SERVER['REMOTE_ADDR']; $fileht=".htaccess2"; if(!file_exists($fil ...

  4. Linux 中挂载 ISO 文件

    在 Linux 中挂载 ISO 文件 用 mount 命令,在终端中输入如下命令即可: sudo mount -o loop filename.iso /cdrom 其中 filename.iso 是 ...

  5. Win10平台下通过VMware虚拟机安装Win7、Ubuntu、Mac

    1.安装VMware14.1.1 下载地址:https://download.csdn.net/download/jasonczy/10611423 产品秘钥: CG54H-D8D0H-H8DHY-C ...

  6. 【Codeforces Round #423 (Div. 2) A】Restaurant Tables

    [Link]:http://codeforces.com/contest/828/problem/A [Description] 有n个组按照时间顺序来餐馆; 每个组由一个人或两个人组成; 每当有一个 ...

  7. MapReduce实现线性回归

    1. 软件版本号: Hadoop2.6.0(IDEA中源代码编译使用CDH5.7.3,相应Hadoop2.6.0),集群使用原生Hadoop2.6.4.JDK1.8,Intellij IDEA 14 ...

  8. WebKit载入流程 - 概述

    之前写了几篇载入流程的说明,是从下向上看,有点仅仅见树木不见森林的感觉.经过近期一段时间的学习,有了能加以概括抽象的方法. WebKit载入流程和页面组成是直接相关的,页面就是WebKit要载入的对象 ...

  9. 同学们,OpenCV出3.0了,速去围观!

    OpenCV3.0 OpenCV > NEWS > OpenCV 3.0 2015-06-04 With a great pleasure and great relief OpenCV ...

  10. 英语影视台词---二、Inception

    英语影视台词---二.Inception 一.总结 一句话总结:盗梦空间 1.You're waiting for a train..A train that will take you far aw ...