using System;
using UnityEngine;
using System.Collections;
using System.Collections.Generic;
using System.IO;
using System.Text;
using UnityEngine.SceneManagement;
using UnityEngine.UI;
public class Mic : MonoBehaviour {
//Singletone
public static Mic me { get; private set; }
private AudioClip myAudioClip;
//Time for record cant be endless in Unity3D
//so we combine several short parts with max duration equal TIME_FOR_TEMP_RECORD
//in one big .wav file
private const int TIME_FOR_TEMP_RECORD = 60;
//Recording frequency. Wav quality setting.
private const int FREQUENCY = 11025;
//Private VAR to allow recording
private bool isCommandToRecord;
//This variable contains path to last saved record.
public string strLastSavedPath;
//This variable contains TRUE if recording in progress and FALSE if not.
public bool isRecordind {
get { return Microphone.IsRecording(null); }
}
//AudioClip container to combine AudioParts after recording
private List pausedClip = new List ();
// Use this for initialization
void Awake() {
DontDestroyOnLoad(this.gameObject);
if (me != null && me != this) {
// First we check if there are any other instances conflicting
Destroy(gameObject); // If that is the case, we destroy other instances
} else {
me = this; // Here we save our singleton instance
}
}
public void RecordStart() {
isCommandToRecord = true;
}
public void RecordUnPause() {
isCommandToRecord = true;
}
void FixedUpdate() {
if (!isCommandToRecord || Microphone.IsRecording(null) ) return;
//Stream record start here
myAudioClip = Microphone.Start(null, false, TIME_FOR_TEMP_RECORD, FREQUENCY);
//this line execute only onve
pausedClip.Add(myAudioClip);
}
public AudioClip RecordStopAndSave(string nameEnd) {
isCommandToRecord = false;
Microphone.End(null);
string filename = string.Format("{0}_{1}", SceneManager.GetActiveScene().name,nameEnd);
pausedClip[pausedClip.Count-1] = SavWav.TrimSilence(pausedClip[pausedClip.Count-1], 0);
AudioClip newClip ;
if (pausedClip.Count > 1) {
newClip = Combine(pausedClip);
} else {
newClip = pausedClip[0];
}
if (newClip == null) {
Debug.Log("Too short probably");
return null;
}
strLastSavedPath = Path.Combine(Application.persistentDataPath, filename);
SavWav.Save(filename,newClip );
pausedClip = new List <AudioClip>();
return newClip ;
}
public AudioClip RecordStop()
{
isCommandToRecord = false;
Microphone.End(null);
//string filename = string.Format("{0}_{1}", SceneManager.GetActiveScene().name, nameEnd);
pausedClip[pausedClip.Count - 1] = SavWav.TrimSilence(pausedClip[pausedClip.Count - 1], 0);
AudioClip newClip;
if (pausedClip.Count > 1)
{
newClip = Combine(pausedClip);
}
else
{
newClip = pausedClip[0];
}
if (newClip == null)
{
Debug.Log("Too short probably");
return null;
}
//strLastSavedPath = Path.Combine(Application.persistentDataPath, filename);
//SavWav.Save(filename, newClip);
pausedClip = new List<AudioClip>();
return newClip;
}
public void RecordPause() {
isCommandToRecord = false;
Microphone.End(null);
pausedClip[pausedClip.Count-1] = SavWav.TrimSilence(pausedClip[pausedClip.Count-1], 0);
}
public IEnumerator LoadAndPlay(string filename, AudioSource aus) {
string fullFileNames = "file:///" + Path.Combine(Application.persistentDataPath, filename);
WWW www = new WWW(fullFileNames);
yield return www;
AudioClip tempClip = www.GetAudioClip(false);
tempClip.name = filename;
aus.clip = tempClip;
aus.Play();
}
public void Rename(string oldName, string newName) {
File.Move(oldName, newName);
}
private AudioClip Combine(List <AudioClip> clips) {
if (clips == null || clips.Count == 0)
return null;
int length = 0;
for (int i = 0; i < clips.Count; ++i) {
if (clips[i] == null)
continue;
length += clips[i].samples * clips[i].channels;
}
float[] data = new float[length];
length = 0;
for (int i = 0; i < clips.Count; ++i) {
if (clips[i] == null)
continue;
float[] buffer = new float[clips[i].samples * clips[i].channels];
clips[i].GetData(buffer, 0);
buffer.CopyTo(data, length);
length += buffer.Length;
}
if (length == 0)
return null;
AudioClip result = AudioClip.Create("Combine", length, clips[0].channels, clips[0].frequency, false);
result.SetData(data, 0);
return result;
}
public FileInfo[] LoadFileNames() {
DirectoryInfo directoryInfo = new DirectoryInfo (Application.persistentDataPath);
return directoryInfo.GetFiles ("*.wav", SearchOption.AllDirectories);
}
}
using System;
using UnityEngine;
using System.Collections;
using System.IO;
using System.Text;
using System.Text.RegularExpressions;
using UnityEngine.SceneManagement;
using UnityEngine.UI;
public class Controller : MonoBehaviour {
private float timeRecording;
private AudioSource _aus;
public Slider TimeLine;
private string path;
// Flag to know if we are draging the Timeline handle
private bool isTimeLineOnDrag = false;
private AudioClip clipToVoice;
void Start() {
_aus = GetComponent <AudioSource>();
// RefreshFiles();
Debug.Log(Application.persistentDataPath);
}
public void PressRecStart() {
Mic.me.RecordStart();
}
public void PressRecPause() {
Mic.me.RecordPause();
}
public void PressRecUnPasue() {
Mic.me.RecordUnPause();
}
public void PressRecStop() {
// You can change name of file here. But path to file in Mic.cs
string clipName = Regex.Replace(DateTime.Now.ToLongTimeString(), "[^0-9]", "");
clipToVoice = Mic.me.RecordStopAndSave(clipName);
path = clipName;
}
public void PressClipPlay() {
_aus.clip = clipToVoice;
_aus.Play();
}
public void PressClipStop() {
if (_aus!=null)
{
_aus.Stop();
}
}
public void PressClipPause() {
_aus.Pause();
}
public void PressClipUnPause() {
_aus.UnPause();
}
void Update() {
// isRecordind - public property. Helps to check rec status.
if (!_aus.isPlaying) return;
if (isTimeLineOnDrag) {
_aus.timeSamples = (int) (_aus.clip.samples * TimeLine.value);
} else {
//if (_aus.clip) TimeLine.value = (float) _aus.timeSamples / (float) _aus.clip.samples;
//txtClipTime.text = (_aus.time / 60).ToString("00") + ":" + (_aus.time % 60).ToString("00");
}
}
public void RecPlay()
{
//string fle = Application.persistentDataPath + "/main_" + path + ".wav";
//File.Delete(fle);
//_aus.Stop();
PressRecStart();
}
public void mainReturn()
{
string fle = Application.persistentDataPath + "/main_" + path + ".wav";
File.Delete(fle);
_aus.Stop();
}
public void PressRename(string str) {
// string fileold = Path.Combine(Application.persistentDataPath, ddWafFiles.captionText.text);
string fleold = Application.persistentDataPath + "/main_" + path + ".wav";
string filenew = Path.Combine(Application.persistentDataPath+"/", str + ".wav");
Debug.Log(fleold); Debug.Log(filenew);
Mic.me.Rename(fleold, filenew);
RefreshFiles();
// SetDropdownByText(ifRename.text);
}
public void PressLoad1()
{
// string fileLoad = Path.Combine(Application.persistentDataPath, ddWafFiles.captionText.text);
// StartCoroutine(Mic.me.LoadAndPlay(fileLoad, _aus));
StartCoroutine(Mic.me.LoadAndPlay("main_"+ path + ".wav", _aus));
}
public void PressLoad(string strpath) {
// string fileLoad = Path.Combine(Application.persistentDataPath, ddWafFiles.captionText.text);
// StartCoroutine(Mic.me.LoadAndPlay(fileLoad, _aus));
//StartCoroutine(Mic.me.LoadAndPlay("main_"+ path + ".wav", _aus));
StartCoroutine(Mic.me.LoadAndPlay(Application.persistentDataPath+"/" +strpath , _aus));
}
public void PressDelete() {
// string fileDel = Path.Combine(Application.persistentDataPath, ddWafFiles.captionText.text);
// File.Delete(fileDel);
RefreshFiles();
}
public void TimeLineOnBeginDrag() {
isTimeLineOnDrag = true;
_aus.Pause();
}
// Called at the end of the drag of the TimeLine
public void TimeLineOnEndDrag() {
_aus.Play();
isTimeLineOnDrag = false;
}
void RefreshFiles() {
FileInfo[] fileInfo = Mic.me.LoadFileNames();
// ddWafFiles.options.Clear();
foreach (FileInfo file in fileInfo) {
Debug.Log(file.Name);
Dropdown.OptionData optionData = new Dropdown.OptionData(file.Name);
// ddWafFiles.options.Add(optionData);
}
// ddWafFiles.value = 0;
// ddWafFiles.Select();
// ddWafFiles.RefreshShownValue();
}
void SetDropdownByText(string txt) {
// int result = ddWafFiles.options.FindIndex(val => val.text == txt);
//ddWafFiles.value = result;
}
public void returnthis()
{
PressRecStop();
mainReturn();
}
}