Created
February 20, 2018 23:14
-
-
Save cabbibo/befe50d142616e92eea5711244201247 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
using System.Collections; | |
using System.Collections.Generic; | |
using UnityEngine; | |
using UnityEngine.Playables; | |
using System.IO; | |
using System.Runtime.Serialization.Formatters.Binary; | |
public class DataAudioBuffer : MonoBehaviour { | |
public string savePath; | |
public string folder; | |
public AudioListenerTexture alt; | |
public PlayableDirector timeline; | |
public bool makeCapture; | |
public bool start=false; | |
public bool oStart=false; | |
public float startTime; | |
public float time; | |
public ComputeBuffer _buffer; | |
public int closestBuffer; | |
public CaptureAudioTexture audioCapture; | |
public int BufferSize; | |
private CaptureAudioTexture.Data data; | |
public int size; | |
private float[] values; | |
// Use this for initialization | |
void Start () { | |
Time.captureFramerate = 60; | |
alt.update = false; | |
// Create the folder | |
System.IO.Directory.CreateDirectory(folder); | |
size = audioCapture.sizePerFrame; | |
_buffer = new ComputeBuffer(size,sizeof(float)); | |
values = new float[size]; | |
_buffer.SetData( values ); | |
audioCapture.loadDataFromDisk(); | |
data = audioCapture.GetData(); | |
for( int i = 0; i < 100; i++ ){ | |
//print( data.timeStamps[i] ); | |
} | |
} | |
// Update is called once per frame | |
void Update () { | |
if( start == true && oStart == false ){ | |
oStart = true; | |
startTime = Time.time; | |
timeline.Play(); | |
print( timeline ); | |
} | |
//print( data.timeStamps[6] ); | |
if( start == true ){ | |
time = Time.time - startTime; | |
float above; | |
float below; | |
int closest = 0; | |
for( int i = 0; i < data.timeStamps.Length; i++ ){ | |
float t = data.timeStamps[i]; | |
if( time > t ){ | |
above = data.timeStamps[i+1]; | |
below = t; | |
float bVal = Mathf.Abs( time - below ); | |
float aVal = Mathf.Abs( time - above ); | |
if( bVal < aVal ){ | |
closest = i; | |
}else{ | |
closest = i+1; | |
} | |
}else{ | |
break; | |
} | |
} | |
closestBuffer = closest; | |
for( int i = 0; i < values.Length; i++ ){ | |
values[i] = data.data[closest * size+i]; | |
} | |
//print(data.data[closest*size + 10]); | |
Color[] pixels = alt.AudioTexture.GetPixels(0,0,alt.width,1 ); | |
//print( pixels.Length ); | |
// draw the waveform | |
for (int i = 0; i < alt.size; i++) { | |
// og = pixels[i];//AudioTexture.GetPixel((int)(width * i / size), (int)(1 * (samples [i])) - 1 ); | |
pixels[i].r = pixels[i].r * .8f + values[ (int)(i * 4) + 0 ] * 128; | |
pixels[i].g = pixels[i].g * .8f + values[ (int)(i * 4) + 1 ] * 128; | |
pixels[i].b = pixels[i].b * .8f + values[ (int)(i * 4) + 2 ] * 128; | |
pixels[i].a = pixels[i].a * .8f + values[ (int)(i * 4) + 3 ] * 128; | |
//pixels[i].Set(r, g, b, a); | |
//AudioTexture.SetPixel((int)(width * i / size), (int)(1 * (samples [i])) - 1, c ); | |
} // upload to the graphics card | |
alt.AudioTexture.SetPixels(pixels); | |
alt.AudioTexture.Apply (); | |
_buffer.SetData( values ); | |
alt._buffer.SetData( values ); | |
} | |
if( makeCapture == true ){ | |
// Append filename to folder name (format is '0005 shot.png"') | |
string name = string.Format("{0}/shot{1:D04}.png", folder, Time.frameCount ); | |
// Capture the screenshot to the specified file. | |
ScreenCapture.CaptureScreenshot(name,1); | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment