不涉及语音识别~~


<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. net实现压缩功能

    public static class Compressor { public static byte[] Compress(byte[] data) { using (MemoryStream ou ...

  2. NodeJS学习笔记 (11)网络UDP-dgram(ok)

    模块概览 dgram模块是对UDP socket的一层封装,相对net模块简单很多,下面看例子. UPD客户端 vs UDP服务端 首先,启动UDP server,监听来自端口33333的请求. se ...

  3. ES6学习5 字符串的扩展

    1.ES6 为字符串添加了遍历器接口,使得字符串可以被for...of循环遍历. for (let codePoint of 'foo') { console.log(codePoint) } // ...

  4. 紫书 例题 10-17 UVa 1639(数学期望+分数处理+处理溢出)

    设当前有k个,那么也就是说拿到其他图案的可能是(n-k)/n 那么要拿到一个就要拿n/(n-k)次 所以答案就是n(1/n + 1/(n-1) ......1/2 + 1 / 1) 看起来很简单,但是 ...

  5. 【Round #36 (Div. 2 only) C】Socks Pairs

    [题目链接]:https://csacademy.com/contest/round-36/task/socks-pairs/ [题意] 给你n种颜色的袜子,每种颜色颜色的袜子有ai只; 假设你在取袜 ...

  6. Map和Collection详解

    Collection     -----List                -----LinkedList    非同步                 ----ArrayList      非同 ...

  7. 使用 gradle 在编译时动态设置 Android resValue / BuildConfig / Manifes中&lt;meta-data&gt;变量的值

    转载请标明出处:http://blog.csdn.net/xx326664162/article/details/49247815 文章出自:薛瑄的博客 你也能够查看我的其它同类文章.也会让你有一定的 ...

  8. JS控制光标定位,定位到文本的某个位置

    这是一个数字密码,要能够智能的跳转到文本的某个位置,就需要通过JS来控制跳转! 1.onkeyup监听 <input class="put" id="number- ...

  9. 洛谷P4093 [HEOI2016/TJOI2016]序列

    题目描述 佳媛姐姐过生日的时候,她的小伙伴从某宝上买了一个有趣的玩具送给他.玩具上有一个数列,数列中某些项的值可能会变化,但同一个时刻最多只有一个值发生变化.现在佳媛姐姐已经研究出了所有变化的可能性, ...

  10. MVC异常过滤器

    MVC过滤器 一般的过滤器执行顺序 IAuthorizationFilter->OnAuthorization(授权) IActionFilter          ->OnActionE ...