博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
Unity3D中把AudioClip保存为Wav文件
阅读量:6229 次
发布时间:2019-06-21

本文共 5714 字,大约阅读时间需要 19 分钟。

似乎需要超能力才能看到,所以复制粘贴一份过来。

1 using System;  2 using System.IO;  3 using UnityEngine;  4 using System.Collections.Generic;  5 using System.Threading;  6   7 public class SavWav {  8   9     const int HEADER_SIZE = 44; 10     struct ClipData{ 11  12         public  int samples; 13         public  int channels; 14         public float[] samplesData; 15  16     } 17  18     public bool Save(string filename, AudioClip clip) { 19         if (!filename.ToLower().EndsWith(".wav")) { 20             filename += ".wav"; 21         } 22  23         var filepath = filename; 24  25         Debug.Log(filepath); 26  27         // Make sure directory exists if user is saving to sub dir. 28         Directory.CreateDirectory(Path.GetDirectoryName(filepath)); 29         ClipData clipdata = new ClipData (); 30         clipdata.samples = clip.samples; 31         clipdata.channels = clip.channels; 32         float[] dataFloat = new float[clip.samples*clip.channels]; 33         clip.GetData (dataFloat, 0); 34         clipdata.samplesData = dataFloat; 35         using (var fileStream = CreateEmpty(filepath)) { 36             MemoryStream memstrm = new MemoryStream (); 37             ConvertAndWrite(memstrm, clipdata); 38             memstrm.WriteTo (fileStream); 39             WriteHeader(fileStream, clip); 40         } 41  42         return true; // TODO: return false if there's a failure saving the file 43     } 44  45     public AudioClip TrimSilence(AudioClip clip, float min) { 46         var samples = new float[clip.samples]; 47  48         clip.GetData(samples, 0); 49  50         return TrimSilence(new List
(samples), min, clip.channels, clip.frequency); 51 } 52 53 public AudioClip TrimSilence(List
samples, float min, int channels, int hz) { 54 return TrimSilence(samples, min, channels, hz, false, false); 55 } 56 57 public AudioClip TrimSilence(List
samples, float min, int channels, int hz, bool _3D, bool stream) { 58 int i; 59 60 for (i=0; i
min) { 62 break; 63 } 64 } 65 66 samples.RemoveRange(0, i); 67 68 for (i=samples.Count - 1; i>0; i--) { 69 if (Mathf.Abs(samples[i]) > min) { 70 break; 71 } 72 } 73 74 samples.RemoveRange(i, samples.Count - i); 75 76 var clip = AudioClip.Create("TempClip", samples.Count, channels, hz, _3D, stream); 77 78 clip.SetData(samples.ToArray(), 0); 79 80 return clip; 81 } 82 83 FileStream CreateEmpty(string filepath) { 84 var fileStream = new FileStream(filepath, FileMode.Create); 85 byte emptyByte = new byte(); 86 87 for(int i = 0; i < HEADER_SIZE; i++) //preparing the header 88 { 89 fileStream.WriteByte(emptyByte); 90 } 91 92 return fileStream; 93 } 94 95 void ConvertAndWrite(MemoryStream memStream, ClipData clipData) 96 { 97 float[] samples = new float[clipData.samples*clipData.channels]; 98 99 samples = clipData.samplesData;100 101 Int16[] intData = new Int16[samples.Length];102 103 Byte[] bytesData = new Byte[samples.Length * 2];104 105 const float rescaleFactor = 32767; //to convert float to Int16106 107 for (int i = 0; i < samples.Length; i++)108 {109 intData[i] = (short)(samples[i] * rescaleFactor);110 //Debug.Log (samples [i]);111 }112 Buffer.BlockCopy(intData, 0, bytesData, 0, bytesData.Length);113 memStream.Write(bytesData, 0, bytesData.Length);114 }115 116 void WriteHeader(FileStream fileStream, AudioClip clip) {117 118 var hz = clip.frequency;119 var channels = clip.channels;120 var samples = clip.samples;121 122 fileStream.Seek(0, SeekOrigin.Begin);123 124 Byte[] riff = System.Text.Encoding.UTF8.GetBytes("RIFF");125 fileStream.Write(riff, 0, 4);126 127 Byte[] chunkSize = BitConverter.GetBytes(fileStream.Length - 8);128 fileStream.Write(chunkSize, 0, 4);129 130 Byte[] wave = System.Text.Encoding.UTF8.GetBytes("WAVE");131 fileStream.Write(wave, 0, 4);132 133 Byte[] fmt = System.Text.Encoding.UTF8.GetBytes("fmt ");134 fileStream.Write(fmt, 0, 4);135 136 Byte[] subChunk1 = BitConverter.GetBytes(16);137 fileStream.Write(subChunk1, 0, 4);138 139 UInt16 two = 2;140 UInt16 one = 1;141 142 Byte[] audioFormat = BitConverter.GetBytes(one);143 fileStream.Write(audioFormat, 0, 2);144 145 Byte[] numChannels = BitConverter.GetBytes(channels);146 fileStream.Write(numChannels, 0, 2);147 148 Byte[] sampleRate = BitConverter.GetBytes(hz);149 fileStream.Write(sampleRate, 0, 4);150 151 Byte[] byteRate = BitConverter.GetBytes(hz * channels * 2); // sampleRate * bytesPerSample*number of channels, here 44100*2*2152 fileStream.Write(byteRate, 0, 4);153 154 UInt16 blockAlign = (ushort) (channels * 2);155 fileStream.Write(BitConverter.GetBytes(blockAlign), 0, 2);156 157 UInt16 bps = 16;158 Byte[] bitsPerSample = BitConverter.GetBytes(bps);159 fileStream.Write(bitsPerSample, 0, 2);160 161 Byte[] datastring = System.Text.Encoding.UTF8.GetBytes("data");162 fileStream.Write(datastring, 0, 4);163 164 Byte[] subChunk2 = BitConverter.GetBytes(samples * channels * 2);165 fileStream.Write(subChunk2, 0, 4);166 167 // fileStream.Close();168 }169 }

 

转载于:https://www.cnblogs.com/kileyi/p/11021262.html

你可能感兴趣的文章
排序箭头,升序,降序简单实现
查看>>
BZOJ 3097: Hash Killer I【构造题,思维题】
查看>>
8.2. OpenMediaVault
查看>>
Meanshift filter实现简单图片的卡通化效果
查看>>
关于排序算法的理解(一)
查看>>
(第六天)DOM
查看>>
bootstrap-fileinput使用
查看>>
使用nginx搭建rtmp服务器
查看>>
setTimeout,setInterval你不知道的事
查看>>
Linux系统安装_Centos6.9
查看>>
RX系列一 | ReactiveX根源 | 观察者模式分析
查看>>
一些C++11语言新特性 - Uniform Initialization
查看>>
Redis高级实践之————Redis短连接性能优化
查看>>
CentOS 6.5 安装nginx 1.6.3
查看>>
48.3. 设置Apache实现防盗连
查看>>
项目管理、测试管理、代码bug 管理
查看>>
SAP物料主档关键栏位解释
查看>>
Nim教程【十二】
查看>>
第 30 章 Shell Terminal
查看>>
Data Guard实现故障自动切换(二)(r11笔记第39天)
查看>>