概述
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using System.IO;
using System;
public class SaveWav
{
const int HEADER_SIZE = 44;
public static void Save(string fileName,AudioClip clip)
{
if (!fileName.ToLower().EndsWith(".wav"))
{
fileName += ".wav";
}
//1.创建头
FileStream fs = CreateEmpty(@"C: UsersAidenxlDesktop"+"\"+fileName);
//2.写语音数据
ConvertAndWrite(fs,clip);
//3.重写真正的文件头
WriteHeader(fs,clip);
}
//创建头文件
static FileStream CreateEmpty(string filepath)
{
var fileStream = new FileStream(filepath,FileMode.Create);
byte emptyByte = new byte();
for (int i = 0; i < HEADER_SIZE; i++)
{
fileStream.WriteByte(emptyByte);
}
return fileStream;
}
static void ConvertAndWrite(FileStream fileStream,AudioClip clip)
{
var samples = new float[clip.samples];
clip.GetData(samples,0);
Int16[] intData = new Int16[samples.Length];
Byte[] bytesData = new Byte[samples.Length * 2];
int rescaleFactor = 32767;
for (int i = 0; i < samples.Length; i++)
{
intData[i] = (short)(samples[i] * rescaleFactor);
Byte[] byteArray = new byte[2];
byteArray = BitConverter.GetBytes(intData[i]);
byteArray.CopyTo(bytesData,i*2);
}
fileStream.Write(bytesData, 0, bytesData.Length);
}
static void WriteHeader(FileStream fileStream,AudioClip clip)
{
var hz = clip.frequency;
var channels = clip.channels;
var samples = clip.samples;
fileStream.Seek(0,SeekOrigin.Begin);
Byte[] riff = System.Text.Encoding.UTF8.GetBytes("RIFF");
fileStream.Write(riff,0,4);
Byte[] chunkSize = BitConverter.GetBytes(fileStream.Length-8);
fileStream.Write(chunkSize,0,4);
Byte[] wave = System.Text.Encoding.UTF8.GetBytes("WAVE");
fileStream.Write(wave,0,4);
Byte[] fmt = System.Text.Encoding.UTF8.GetBytes("fmt ");
fileStream.Write(fmt, 0,4);
Byte[] subChunk1 = BitConverter.GetBytes(16);
fileStream.Write(subChunk1,0,4);
Byte[] audioFormat = BitConverter.GetBytes(1);
fileStream.Write(audioFormat,0,2);
Byte[] numChannels = BitConverter.GetBytes(channels);
fileStream.Write(numChannels, 0,2);
Byte[] sampleRate = BitConverter.GetBytes(hz);
fileStream.Write(sampleRate, 0, 4);
Byte[] byRate = BitConverter.GetBytes(hz*channels*2);
fileStream.Write(byRate, 0, 4);
UInt16 blockAlign = (ushort)(channels * 2);
fileStream.Write(BitConverter.GetBytes(blockAlign),0,2);
UInt16 bps = 16;
byte[] bitsPerSample = BitConverter.GetBytes(bps);
fileStream.Write(bitsPerSample,0,2);
Byte[] dataString = System.Text.Encoding.UTF8.GetBytes("data");
fileStream.Write(dataString, 0,4);
Byte[] subChunk2 = BitConverter.GetBytes(samples*channels*2);
fileStream.Write(subChunk2, 0, 4);
fileStream.Close();
}
}
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class Micphone : MonoBehaviour {
bool micConnected = false;
int minFreq, maxFreq;
AudioSource goAudioSource;
// Use this for initialization
void Start () {
if (Microphone.devices.Length <= 0)
{
Debug.LogWarning("该机器上没有麦克风");
}else
{
micConnected = true;
Microphone.GetDeviceCaps(null, out minFreq, out maxFreq);
if (minFreq == 0 && maxFreq == 0)
{
maxFreq = 44100;
}
goAudioSource = GetComponent<AudioSource>();
}
}
// Update is called once per frame
void Update () {
}
private void OnGUI()
{
if (!micConnected) return;
if (!Microphone.IsRecording(null))
{
if (GUI.Button(new Rect(Screen.width / 2 - 100, Screen.height / 2 - 25, 200, 50), "Record"))
{
AudioClip ss = Microphone.Start(null, true, 20, maxFreq);
goAudioSource.clip = ss;
}
}else
{
if(GUI.Button(new Rect(Screen.width / 2 - 100, Screen.height / 2 - 25, 200, 50), "Stop and play"))
{
Microphone.End(null);
SaveWav.Save("text.wav",goAudioSource.clip);
goAudioSource.Play();
}
GUI.Label(new Rect(Screen.width/2-100,Screen.height/2+25,200,50),"Micophone is using... ");
}
}
}
最后
以上就是伶俐唇彩为你收集整理的C# save wav的全部内容,希望文章能够帮你解决C# save wav所遇到的程序开发问题。
如果觉得靠谱客网站的内容还不错,欢迎将靠谱客网站推荐给程序员好友。
本图文内容来源于网友提供,作为学习参考使用,或来自网络收集整理,版权属于原作者所有。
发表评论 取消回复