Guest User

Untitled

a guest
Nov 17th, 2017
235
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 3.51 KB | None | 0 0
  1. using System.Collections;
  2. using System.Collections.Generic;
  3. using UnityEngine;
  4. using System.IO; // for FileStream
  5. using System; // for BitConverter and Byte Type
  6.  
  7. public class TestScript : MonoBehaviour {
  8.  
  9.     private int bufferSize;
  10.     private int numBuffers;
  11.     private int outputRate = 44100;
  12.     private string fileName = "Assets/Music/etc/piano/songoutput1/audiooutput1.wav";
  13.     private int headerSize = 44; //default for uncompressed wav
  14.  
  15.     private bool recOutput;
  16.  
  17.     private FileStream fileStream;
  18.  
  19.     void Awake()
  20.     {
  21.         AudioSettings.outputSampleRate = outputRate;
  22.     }
  23.  
  24.     void Start()
  25.     {
  26.         AudioSettings.GetDSPBufferSize (out bufferSize, out numBuffers);
  27.     }
  28.  
  29.     void Update()
  30.     {
  31.         if(Input.GetKeyDown("0")) //the part that has a grudge against AC
  32.         {
  33.             print("rec");
  34.             if(recOutput == false)
  35.             {
  36.                 StartWriting(fileName);
  37.                 recOutput = true;
  38.             }
  39.             else
  40.             {
  41.                 recOutput = false;
  42.                 WriteHeader();    
  43.                 print("rec stop");
  44.             }
  45.         }
  46.     }
  47.  
  48.     private void StartWriting(string name)
  49.     {
  50.         fileStream = new FileStream(name, FileMode.Create);
  51.         byte emptyByte = new byte();
  52.  
  53.         for(int i = 0; i<headerSize; i++) //preparing the header
  54.         {
  55.             fileStream.WriteByte(emptyByte);
  56.         }
  57.     }
  58.  
  59.     private void OnAudioFilterRead(float[] data, int channels)
  60.     {
  61.         if(recOutput)
  62.         {
  63.             ConvertAndWrite(data); //audio data is interlaced
  64.         }
  65.     }
  66.  
  67.     private void ConvertAndWrite(float[] dataSource)
  68.     {
  69.  
  70.         Int16[] intData = new Int16[dataSource.Length];
  71.         //converting in 2 steps : float[] to Int16[], //then Int16[] to Byte[]
  72.  
  73.         Byte[] bytesData = new Byte[dataSource.Length*2];
  74.         //bytesData array is twice the size of
  75.         //dataSource array because a float converted in Int16 is 2 bytes.
  76.  
  77.         int rescaleFactor = 32767; //to convert float to Int16
  78.  
  79.         for (int i = 0; i<dataSource.Length;i++)
  80.         {
  81.             intData[i] = (short)(dataSource[i]*rescaleFactor);
  82.             Byte[] byteArr = new Byte[2];
  83.             byteArr = BitConverter.GetBytes(intData[i]);
  84.             byteArr.CopyTo(bytesData,i*2);
  85.         }
  86.  
  87.         fileStream.Write(bytesData,0,bytesData.Length);
  88.     }
  89.  
  90.     private void WriteHeader()
  91.     {
  92.  
  93.         fileStream.Seek(0,SeekOrigin.Begin);
  94.  
  95.         Byte[] riff = System.Text.Encoding.UTF8.GetBytes("RIFF");
  96.         fileStream.Write(riff,0,4);
  97.  
  98.         Byte[] chunkSize = BitConverter.GetBytes(fileStream.Length-8);
  99.         fileStream.Write(chunkSize,0,4);
  100.  
  101.         Byte[] wave = System.Text.Encoding.UTF8.GetBytes("WAVE");
  102.         fileStream.Write(wave,0,4);
  103.  
  104.         Byte[] fmt = System.Text.Encoding.UTF8.GetBytes("fmt ");
  105.         fileStream.Write(fmt,0,4);
  106.  
  107.         Byte[] subChunk1 = BitConverter.GetBytes(16);
  108.         fileStream.Write(subChunk1,0,4);
  109.  
  110.         UInt16 two = 2;
  111.         UInt16 one = 1;
  112.  
  113.         Byte[] audioFormat = BitConverter.GetBytes(one);
  114.         fileStream.Write(audioFormat,0,2);
  115.  
  116.         Byte[] numChannels = BitConverter.GetBytes(two);
  117.         fileStream.Write(numChannels,0,2);
  118.  
  119.         Byte[] sampleRate = BitConverter.GetBytes(outputRate);
  120.         fileStream.Write(sampleRate,0,4);
  121.  
  122.         Byte[] byteRate = BitConverter.GetBytes(outputRate*4);
  123.         // sampleRate * bytesPerSample*number of channels, here 44100*2*2
  124.  
  125.         fileStream.Write(byteRate,0,4);
  126.  
  127.         UInt16 four = 4;
  128.         Byte[] blockAlign = BitConverter.GetBytes(four);
  129.         fileStream.Write(blockAlign,0,2);
  130.  
  131.         UInt16 sixteen = 16;
  132.         Byte[] bitsPerSample = BitConverter.GetBytes(sixteen);
  133.         fileStream.Write(bitsPerSample,0,2);
  134.  
  135.         Byte[] dataString = System.Text.Encoding.UTF8.GetBytes("data");
  136.         fileStream.Write(dataString,0,4);
  137.  
  138.         Byte[] subChunk2 = BitConverter.GetBytes(fileStream.Length-headerSize);
  139.         fileStream.Write(subChunk2,0,4);
  140.  
  141.         fileStream.Close();
  142.     }
  143. }
Advertisement
Add Comment
Please, Sign In to add comment