Search Unity

Save Microphone Recording to disk

Discussion in 'Scripting' started by KnightRiderGuy, Jan 6, 2016.

  1. KnightRiderGuy

    KnightRiderGuy

    Joined:
    Nov 23, 2014
    Posts:
    515
    Ok I have a microphone recorder that I want to save the audio file to a local folder located where the Stand alone app will be located so that the user will be able to find the recording easily, right now it saves it somewhere daffy inside of unity.

    Code (CSharp):
    1. public void SaveToDisk ()
    2.         {
    3.         AudioRecorder.Save ("myFile", goAudioSource.clip);
    4.         }
    I would like to save the file from the stand alone build into a folder something like:
    Audio/Recordings

    Can someone please help.
     
    rwang0 likes this.
  2. Magiichan

    Magiichan

    Joined:
    Jan 5, 2014
    Posts:
    403
    First you'll have to know wat an audiofile consists of. you can see the variables used here:
    http://docs.unity3d.com/ScriptReference/AudioClip.html

    Basically you'll have to serialize the bytes into a file in order to save it.
    Simply save this array using a for loop
    http://docs.unity3d.com/ScriptReference/AudioClip.GetData.html
    (You could use just use XML to store the data and parse it to the variables when needed, although XML is nutorious for it's large file size)

    And deserialize it in a new AudioClip file when wanting to load it.
    http://docs.unity3d.com/ScriptReference/AudioClip.Create.html
    http://docs.unity3d.com/ScriptReference/AudioClip.SetData.html

    If you literally have no idea how to do this, I'm sure there are people willing to help you with the code. Also there's an assetstore, I'm sure there's something in there that could help you.

    I hope this'll help!

    [EDIT]
    After a google search I've found this example on Unity Answers
    http://docs.unity3d.com/ScriptReference/AudioSource.SetScheduledEndTime.html

    [EDIT2]

    If you're just looking for a simple solution here's an open source script that handles it for you.
    https://gist.github.com/darktable/2317063
     
    Last edited: Jan 6, 2016
    SassyPantsy, Elecman, rwang0 and 2 others like this.
  3. KnightRiderGuy

    KnightRiderGuy

    Joined:
    Nov 23, 2014
    Posts:
    515

    I have seen all of these links, they did not tell me what I wanted to know which was how to save the file into a folder like:
    Audio/Recordings relative to where my Stand alone app will be running on the desktop?
     
  4. Magiichan

    Magiichan

    Joined:
    Jan 5, 2014
    Posts:
    403
    http://docs.unity3d.com/ScriptReference/Application-dataPath.html
    Code (CSharp):
    1. string filepath = Path.Combine(Application.dataPath, filename);
    This is pretty basic, a simple google search would've answered this question then.
    Also look at my previous post, i edited it again, there's an existing script to save/load your files:
    http://answers.unity3d.com/questions/354401/save-audio-to-a-file.html
     
    SassyPantsy and KnightRiderGuy like this.
  5. KnightRiderGuy

    KnightRiderGuy

    Joined:
    Nov 23, 2014
    Posts:
    515
    Thanks Magiichan,
    I think a lot of my issues is how to convert the code as right now in the unity editor it does save the file to something like StreamingAssets/

    Screen Shot 2016-01-06 at 12.09.24 PM.png

    But when I build my stand alone player I need it to save to something like:
    Audio/Recordings/
     
  6. Magiichan

    Magiichan

    Joined:
    Jan 5, 2014
    Posts:
    403
    Code (CSharp):
    1. Application.dataPath
    When you've built the standalone version it'll return something like this:
    C:/Program Files/MyGame/MyGame_Data

    So all you have to do is this.
    Code (CSharp):
    1. var path = Application.dataPath + "/Audio/Recordings/";
    2. if (System.IO.Directory.Exists(path))
    3. {
    4.     var files = System.IO.Directory.GetFiles(path);
    5.     for (var i = 0; i < files.Length; i++)
    6.     {
    7.         if (files[i].ToLower().EndsWith(".wav"))
    8.         {
    9.             //Load files with path files[i]
    10.         }
    11.     }
    12. }
    The link I provided actually states this, take a closer look.
    http://docs.unity3d.com/ScriptReference/Application-dataPath.html
    Unity Editor: <path to project folder>/Assets
    Win/Linux player: <path to executablename_Data folder> (note that most Linux installations will be case-sensitive!)
     
    KnightRiderGuy likes this.
  7. KnightRiderGuy

    KnightRiderGuy

    Joined:
    Nov 23, 2014
    Posts:
    515
    I'm not really sure how to use that, I'm on a mac.... not that it should matter but my code guy who had done this wrote the save like this:

    Code (CSharp):
    1. public void SaveToDisk ()
    2.         {
    3.         AudioRecorder.Save ("myFile", goAudioSource.clip);
    4.         }
    5.  
     
  8. Magiichan

    Magiichan

    Joined:
    Jan 5, 2014
    Posts:
    403
    Oh, can you show us the AudioRecorder code so we know what it requires in order to save?
     
    rwang0 and KnightRiderGuy like this.
  9. KnightRiderGuy

    KnightRiderGuy

    Joined:
    Nov 23, 2014
    Posts:
    515
    Yup, here is the entire code.

    Code (CSharp):
    1. using UnityEngine;
    2. using UnityEngine.UI;
    3. using System.Collections;
    4. using System;
    5. using System.IO;
    6. using System.Collections.Generic;
    7.  
    8. public class AudioRecorder : MonoBehaviour
    9. {
    10.    
    11.     private string absolutePath = "./Audio/Recordings"; // relative path to where the app is running
    12.  
    13.  
    14.  
    15.         public GameObject RecButton;
    16.         public GameObject RecButtonBG;
    17.         public GameObject PausseButton;
    18.         public GameObject PauseButtonBG;
    19.         public GameObject PlayButton;
    20.         public GameObject PlayButtonBG;
    21.         public GameObject ButtonTimeRecordSelect1m;
    22.         public GameObject ButtonTimeRecordSelect5m;
    23.         public GameObject ButtonTimeRecordSelect15m;
    24.         public GameObject ButtonTimeRecordSelect30m;
    25.         public GameObject ButtonTimeRecordSelect60m;
    26.         public GameObject ButtonTimeRecordSelect2h;
    27.         public GameObject ButtonTimeRecordSelect4h;
    28.         public GameObject ButtonTimeRecordSelect8h;
    29.         public bool powerOn;
    30.         private bool recording;
    31.         private bool Playing;
    32.         private bool Paused;
    33.         public Text RecTimeSelect;
    34.         public float tmeleft;
    35.         public float tmerec;
    36.         public float tmestart;
    37.         public float tmeend;
    38.         private bool done;
    39.         public float tmeprdct;
    40.         public float pctleft;
    41.         public float pctcmplt;
    42.         public Color TextColor;
    43.         private bool micRecording = false;
    44.         private int minFreq;
    45.         private int maxFreq;
    46.         private static int HEADER_SIZE ;
    47.         public AudioSource goAudioSource;
    48.        
    49.         //Use this for initialization
    50.         void Start ()
    51.         {
    52.                 done = false;
    53.                 TextColor = RecTimeSelect.color;
    54.                 HEADER_SIZE = 44;
    55.        
    56.  
    57.         }
    58.  
    59.         void Update ()
    60.         {
    61.                 micRecording = Microphone.IsRecording (" ");
    62.            
    63.                 if (micRecording) {
    64.                         tmeend = Time.time;
    65.                         tmeleft = tmeprdct - tmeend;
    66.                         done = false;
    67.                
    68.                         pctleft = (int)(tmeleft / tmerec * 100);
    69.                         if (pctleft < 0.5f)
    70.                                 pctleft = 0;
    71.                         pctcmplt = 100 - pctleft;
    72.                 } else {
    73.                         RecButtonBG.SetActive (true);
    74.                         recording = false;
    75.  
    76.                         if (!done) {
    77.                                 tmeend = Time.time;
    78.                    
    79.                                 tmeleft = tmeend - tmestart;
    80.                                 done = true;
    81.                         }
    82.                         if (!goAudioSource.isPlaying & !Paused)
    83.                                 PlayButtonBG.SetActive (true);
    84.  
    85.                         tmeend = Time.time;
    86.                         tmeleft = tmeprdct - tmeend;
    87.                
    88.                         pctleft = (int)(tmeleft / tmerec * 100);
    89.                         if (pctleft < 0.5f)
    90.                                 pctleft = 0;
    91.                         pctcmplt = 100 - pctleft;
    92.                 }
    93.         }
    94.  
    95.         public void Record ()
    96.         {
    97.                 micRecording = true;
    98.                 Playing = false;
    99.                 Paused = false;
    100.                 done = false;
    101.                 tmestart = Time.time;  
    102.            
    103.                 if (tmerec == 0f) {
    104.                         RecTimeSelect.text = ("TIME REQUIRED");
    105.                         TextColor = RecTimeSelect.color;
    106.                         RecTimeSelect.color = Color.red;
    107.                 } else {
    108.                         goAudioSource.clip = Microphone.Start (null, false, (int)tmerec, 44100);
    109.                 }
    110.                 RecButtonBG.SetActive (false);
    111.                 PlayButtonBG.SetActive (true);
    112.                 PauseButtonBG.SetActive (true);
    113.            
    114.         }
    115.        
    116.         public void StopRecord ()
    117.         {
    118.                 micRecording = false;
    119.                 tmeend = Time.time;  
    120.                 Microphone.End (null); //Stop the audio recording
    121.  
    122.         }
    123.        
    124.         public void Play ()
    125.         {
    126.                 if (micRecording)
    127.                         StopRecord ();
    128.  
    129.                 Playing = true;
    130.                 if (Paused) {
    131.                         Paused = false;
    132.                         PauseButtonBG.SetActive (true);
    133.                 }
    134.                 goAudioSource.Play (); //Playback the recorded audio
    135.                 PlayButtonBG.SetActive (false);
    136.         }
    137.        
    138.         public void StopPlay ()
    139.         {
    140.                 Playing = false;
    141.                 goAudioSource.Stop (); //Playback the recorded audio
    142.                 PlayButtonBG.SetActive (true);
    143.         }
    144.        
    145.         public void Stop ()
    146.         {
    147.  
    148.                 Playing = false;
    149.                 Paused = false;
    150.                 RecButtonBG.SetActive (true);
    151.                 PauseButtonBG.SetActive (true);
    152.                 PlayButtonBG.SetActive (true);
    153.  
    154.                 if (micRecording)
    155.                         StopRecord ();
    156.                 else {
    157.                         StopPlay ();
    158.                         Playing = false;
    159.                 }
    160.         micRecording = false;
    161.         }
    162.  
    163.         public void Pause ()
    164.         {
    165.                 if (!micRecording & Playing) {
    166.                         if (!Paused) {
    167.                                 goAudioSource.Pause (); //Pause Playback
    168.                                 Paused = true;
    169.                                 PauseButtonBG.SetActive (false);
    170.  
    171.                         } else {
    172.                                 goAudioSource.Play (); //Playback the recorded audio
    173.                                 Paused = false;
    174.                                 PauseButtonBG.SetActive (true);
    175.                         }
    176.                 }
    177.                 if (micRecording) {
    178.                         Stop ();
    179.                         PauseButtonBG.SetActive (true);
    180.                         Paused = false;
    181.                 }
    182.         }
    183.  
    184.         public void SaveToDisk ()
    185.         {
    186.         AudioRecorder.Save ("myFile", goAudioSource.clip);
    187.         }
    188.  
    189.         public void RecordTime1m ()
    190.         {
    191.                 tmerec = 5;  
    192.                 RecTimeSelect.text = ("Time Selected 5 S");
    193.                 TimeSelected ();
    194.                 ButtonTimeRecordSelect1m.SetActive (false);
    195.         }
    196.  
    197.         public void RecordTime5m ()
    198.         {
    199.                 tmerec = 300;  
    200.                 RecTimeSelect.text = ("Time Selected 5 M");
    201.                 TimeSelected ();
    202.                 ButtonTimeRecordSelect5m.SetActive (false);
    203.        
    204.         }
    205.  
    206.         public void RecordTime15m ()
    207.         {
    208.                 tmerec = 900;  
    209.                 RecTimeSelect.text = ("Time Selected 15 M");
    210.                 TimeSelected ();
    211.                 ButtonTimeRecordSelect15m.SetActive (false);
    212.         }
    213.        
    214.         public void RecordTime30m ()
    215.         {
    216.                 tmerec = 1800;  
    217.                 RecTimeSelect.text = ("Time Selected 30 M");
    218.                 TimeSelected ();
    219.                 ButtonTimeRecordSelect30m.SetActive (false);
    220.         }
    221.        
    222.         public void RecordTime60m ()
    223.         {
    224.                 tmerec = 3600;  
    225.                 RecTimeSelect.text = ("Time Selected 60 M");
    226.                 TimeSelected ();
    227.                 ButtonTimeRecordSelect60m.SetActive (false);
    228.         }
    229.        
    230.         public void RecordTime2h ()
    231.         {
    232.                 tmerec = 7200;  
    233.                 RecTimeSelect.text = ("Time Selected 2 H");
    234.                 TimeSelected ();
    235.                 ButtonTimeRecordSelect2h.SetActive (false);
    236.         }
    237.        
    238.         public void RecordTime4h ()
    239.         {
    240.                 tmerec = 14400;  
    241.                 RecTimeSelect.text = ("Time Selected 4 H");
    242.                 TimeSelected ();
    243.                 ButtonTimeRecordSelect4h.SetActive (false);
    244.         }
    245.        
    246.         public void RecordTime8h ()
    247.         {
    248.                 tmerec = 28800;  
    249.                 RecTimeSelect.text = ("Time Selected 8 H");
    250.                 TimeSelected ();
    251.                 ButtonTimeRecordSelect8h.SetActive (false);
    252.         }
    253.  
    254.         public void TimeSelected ()
    255.         {
    256.                 RecTimeSelect.color = TextColor;
    257.         }
    258.  
    259.         public static bool Save (string filename, AudioClip clip)
    260.         {
    261.        
    262.                 if (!filename.ToLower ().EndsWith (".wav")) {
    263.            
    264.                         filename += ".wav";
    265.            
    266.                 }
    267.        
    268.        
    269.        
    270.                 var filepath = Path.Combine (Application.streamingAssetsPath, filename);
    271.        
    272.        
    273.        
    274.                 Debug.Log (filepath);
    275.        
    276.        
    277.        
    278.                 // Make sure directory exists if user is saving to sub dir.
    279.        
    280.                 Directory.CreateDirectory (Path.GetDirectoryName (filepath));
    281.        
    282.        
    283.        
    284.                 using (var fileStream = CreateEmpty(filepath)) {
    285.            
    286.            
    287.            
    288.                         ConvertAndWrite (fileStream, clip);
    289.            
    290.            
    291.            
    292.                         WriteHeader (fileStream, clip);
    293.            
    294.                 }
    295.        
    296.        
    297.        
    298.                 return true; // TODO: return false if there's a failure saving the file
    299.        
    300.         }
    301.    
    302.         public static AudioClip TrimSilence (AudioClip clip, float min)
    303.         {
    304.        
    305.                 var samples = new float[clip.samples];
    306.        
    307.        
    308.        
    309.                 clip.GetData (samples, 0);
    310.        
    311.        
    312.        
    313.                 return TrimSilence (new List<float> (samples), min, clip.channels, clip.frequency);
    314.        
    315.         }
    316.    
    317.         public static AudioClip TrimSilence (List<float> samples, float min, int channels, int hz)
    318.         {
    319.        
    320.                 return TrimSilence (samples, min, channels, hz, false, false);
    321.        
    322.         }
    323.    
    324.         public static AudioClip TrimSilence (List<float> samples, float min, int channels, int hz, bool _3D, bool stream)
    325.         {
    326.        
    327.                 int i;
    328.        
    329.        
    330.        
    331.                 for (i=0; i<samples.Count; i++) {
    332.            
    333.                         if (Mathf.Abs (samples [i]) > min) {
    334.                
    335.                                 break;
    336.                
    337.                         }
    338.            
    339.                 }
    340.        
    341.        
    342.        
    343.                 samples.RemoveRange (0, i);
    344.        
    345.        
    346.        
    347.                 for (i=samples.Count - 1; i>0; i--) {
    348.            
    349.                         if (Mathf.Abs (samples [i]) > min) {
    350.                
    351.                                 break;
    352.                
    353.                         }
    354.            
    355.                 }
    356.        
    357.        
    358.        
    359.                 samples.RemoveRange (i, samples.Count - i);
    360.        
    361.        
    362.        
    363.                 var clip = AudioClip.Create ("TempClip", samples.Count, channels, hz, _3D, stream);
    364.        
    365.        
    366.        
    367.                 clip.SetData (samples.ToArray (), 0);
    368.        
    369.        
    370.        
    371.                 return clip;
    372.        
    373.         }
    374.    
    375.         static FileStream CreateEmpty (string filepath)
    376.         {
    377.        
    378.                 var fileStream = new FileStream (filepath, FileMode.Create);
    379.        
    380.                 byte emptyByte = new byte ();
    381.        
    382.        
    383.        
    384.                 for (int i = 0; i < HEADER_SIZE; i++) { //preparing the header
    385.            
    386.                         fileStream.WriteByte (emptyByte);
    387.            
    388.                 }
    389.        
    390.        
    391.        
    392.                 return fileStream;
    393.        
    394.         }
    395.    
    396.         static void ConvertAndWrite (FileStream fileStream, AudioClip clip)
    397.         {
    398.        
    399.        
    400.        
    401.                 var samples = new float[clip.samples];
    402.        
    403.        
    404.        
    405.                 clip.GetData (samples, 0);
    406.        
    407.        
    408.        
    409.                 Int16[] intData = new Int16[samples.Length];
    410.        
    411.                 //converting in 2 float[] steps to Int16[], //then Int16[] to Byte[]
    412.        
    413.        
    414.        
    415.                 Byte[] bytesData = new Byte[samples.Length * 2];
    416.        
    417.                 //bytesData array is twice the size of
    418.        
    419.                 //dataSource array because a float converted in Int16 is 2 bytes.
    420.        
    421.        
    422.        
    423.                 int rescaleFactor = 32767; //to convert float to Int16
    424.        
    425.        
    426.        
    427.                 for (int i = 0; i<samples.Length; i++) {
    428.            
    429.                         intData [i] = (short)(samples [i] * rescaleFactor);
    430.            
    431.                         Byte[] byteArr = new Byte[2];
    432.            
    433.                         byteArr = BitConverter.GetBytes (intData [i]);
    434.            
    435.                         byteArr.CopyTo (bytesData, i * 2);
    436.            
    437.                 }
    438.        
    439.        
    440.        
    441.                 fileStream.Write (bytesData, 0, bytesData.Length);
    442.        
    443.         }
    444.    
    445.         static void WriteHeader (FileStream fileStream, AudioClip clip)
    446.         {
    447.        
    448.        
    449.        
    450.                 var hz = clip.frequency;
    451.        
    452.                 var channels = clip.channels;
    453.        
    454.                 var samples = clip.samples;
    455.        
    456.        
    457.        
    458.                 fileStream.Seek (0, SeekOrigin.Begin);
    459.        
    460.        
    461.        
    462.                 Byte[] riff = System.Text.Encoding.UTF8.GetBytes ("RIFF");
    463.        
    464.                 fileStream.Write (riff, 0, 4);
    465.        
    466.        
    467.        
    468.                 Byte[] chunkSize = BitConverter.GetBytes (fileStream.Length - 8);
    469.        
    470.                 fileStream.Write (chunkSize, 0, 4);
    471.        
    472.        
    473.        
    474.                 Byte[] wave = System.Text.Encoding.UTF8.GetBytes ("WAVE");
    475.        
    476.                 fileStream.Write (wave, 0, 4);
    477.        
    478.        
    479.        
    480.                 Byte[] fmt = System.Text.Encoding.UTF8.GetBytes ("fmt ");
    481.        
    482.                 fileStream.Write (fmt, 0, 4);
    483.        
    484.        
    485.        
    486.                 Byte[] subChunk1 = BitConverter.GetBytes (16);
    487.        
    488.                 fileStream.Write (subChunk1, 0, 4);
    489.        
    490.        
    491.        
    492.                 UInt16 two = 2;
    493.        
    494.                 UInt16 one = 1;
    495.        
    496.        
    497.        
    498.                 Byte[] audioFormat = BitConverter.GetBytes (one);
    499.        
    500.                 fileStream.Write (audioFormat, 0, 2);
    501.        
    502.        
    503.        
    504.                 Byte[] numChannels = BitConverter.GetBytes (channels);
    505.        
    506.                 fileStream.Write (numChannels, 0, 2);
    507.        
    508.        
    509.        
    510.                 Byte[] sampleRate = BitConverter.GetBytes (hz);
    511.        
    512.                 fileStream.Write (sampleRate, 0, 4);
    513.        
    514.        
    515.        
    516.                 Byte[] byteRate = BitConverter.GetBytes (hz * channels * 2); // sampleRate * bytesPerSample*number of channels, here 44100*2*2
    517.        
    518.                 fileStream.Write (byteRate, 0, 4);
    519.        
    520.        
    521.        
    522.                 UInt16 blockAlign = (ushort)(channels * 2);
    523.        
    524.                 fileStream.Write (BitConverter.GetBytes (blockAlign), 0, 2);
    525.        
    526.        
    527.        
    528.                 UInt16 bps = 16;
    529.        
    530.                 Byte[] bitsPerSample = BitConverter.GetBytes (bps);
    531.        
    532.                 fileStream.Write (bitsPerSample, 0, 2);
    533.        
    534.        
    535.        
    536.                 Byte[] datastring = System.Text.Encoding.UTF8.GetBytes ("data");
    537.        
    538.                 fileStream.Write (datastring, 0, 4);
    539.        
    540.        
    541.        
    542.                 Byte[] subChunk2 = BitConverter.GetBytes (samples * channels * 2);
    543.        
    544.                 fileStream.Write (subChunk2, 0, 4);
    545.        
    546.        
    547.        
    548.                 //        fileStream.Close();
    549.        
    550.         }
    551.  
    552. }
     
  10. Magiichan

    Magiichan

    Joined:
    Jan 5, 2014
    Posts:
    403
    So your problem is that you don't know how to save a file at a certain location on a mac using c#? I think the save method (System.IO) is only available on windows platforms. =/
     
    rwang0 and KnightRiderGuy like this.
  11. Magiichan

    Magiichan

    Joined:
    Jan 5, 2014
    Posts:
    403
    Try to replace the save function with this:
    Code (CSharp):
    1.     public static bool Save(string filename, AudioClip clip) {
    2.         if (!filename.ToLower().EndsWith(".wav")) {
    3.             filename += ".wav";
    4.         }
    5.  
    6.         var filepath = Path.Combine(Application.dataPath, filename);
    7.  
    8.         Debug.Log(filepath);
    9.  
    10.         // Make sure directory exists if user is saving to sub dir.
    11.         Directory.CreateDirectory(Path.GetDirectoryName(filepath));
    12.  
    13.         using (var fileStream = CreateEmpty(filepath)) {
    14.  
    15.             ConvertAndWrite(fileStream, clip);
    16.  
    17.             WriteHeader(fileStream, clip);
    18.         }
    19.  
    20.         return true; // TODO: return false if there's a failure saving the file
    21.     }
    Maybe it'll work, let me know what errors you get, if any.
     
    rwang0 and KnightRiderGuy like this.
  12. KnightRiderGuy

    KnightRiderGuy

    Joined:
    Nov 23, 2014
    Posts:
    515
    Yup, I got one it returned this error:

    Code (CSharp):
    1. Assets/AudioRecorder.cs(289,36): error CS0111: A member `AudioRecorder.Save(string, UnityEngine.AudioClip)' is already defined. Rename this member or use different parameter types
     
  13. Magiichan

    Magiichan

    Joined:
    Jan 5, 2014
    Posts:
    403
    Find the function in the code and replace it with what i gave you.
    You added it instead of replacing it, so there are 2 with the same name, that gives the error.
     
    KnightRiderGuy likes this.
  14. KnightRiderGuy

    KnightRiderGuy

    Joined:
    Nov 23, 2014
    Posts:
    515
    Figures I'd mess it up in the wrong place.
    OK error gone but where will this save the file too?
     
  15. Magiichan

    Magiichan

    Joined:
    Jan 5, 2014
    Posts:
    403
    Build the game as standalone, and it should save it to
    <path to player app bundle>/Contents
     
    KnightRiderGuy likes this.
  16. KnightRiderGuy

    KnightRiderGuy

    Joined:
    Nov 23, 2014
    Posts:
    515
    I have no idea where to even find that, can it not be made to save to a folder that would be on the desktop beside the app called Audio/Recordings?
    A "Recordings" folder nested inside of a folder called "Audio" ?
     
  17. Magiichan

    Magiichan

    Joined:
    Jan 5, 2014
    Posts:
    403
    Honestly, I don't know where it is either, mac is so confusing. Any mac user here that can help?
     
    KnightRiderGuy likes this.
  18. KnightRiderGuy

    KnightRiderGuy

    Joined:
    Nov 23, 2014
    Posts:
    515
    Basically I need this so that when the stand alone player is built it will be located on the desktop with a folder beside it called "Audio" inside of the Audio folder are two other folders one for "music" and one for "Recordings." The microphone recorded audio I would like saved out to the "Recordings" folder.
     
  19. Magiichan

    Magiichan

    Joined:
    Jan 5, 2014
    Posts:
    403
    Build the stand alone player and see where it saves the .wav file. It should be in the same folder I think. if it isn't try to find where it is instead.
     
    KnightRiderGuy likes this.
  20. KnightRiderGuy

    KnightRiderGuy

    Joined:
    Nov 23, 2014
    Posts:
    515
    Nope I tried a build and it did not even save it to the desktop much less in the directory I wanted?
     
  21. Magiichan

    Magiichan

    Joined:
    Jan 5, 2014
    Posts:
    403
    Can you send me the output_log file?
     
    KnightRiderGuy likes this.
  22. KnightRiderGuy

    KnightRiderGuy

    Joined:
    Nov 23, 2014
    Posts:
    515
    Ok How do I do that?
     
  23. Magiichan

    Magiichan

    Joined:
    Jan 5, 2014
    Posts:
    403
    It's in ~/Library/Logs/Unity/Player.log
     
    KnightRiderGuy likes this.
  24. KnightRiderGuy

    KnightRiderGuy

    Joined:
    Nov 23, 2014
    Posts:
    515
    Nope no folder in there called "Logs" ?
     
  25. Magiichan

    Magiichan

    Joined:
    Jan 5, 2014
    Posts:
    403
    This would be so much easier through skype. Can you please add me so we can start a call & share screen? I dont think I can help you otherwise.
     
    KnightRiderGuy likes this.
  26. KnightRiderGuy

    KnightRiderGuy

    Joined:
    Nov 23, 2014
    Posts:
    515
    OK sounds good
     
  27. KnightRiderGuy

    KnightRiderGuy

    Joined:
    Nov 23, 2014
    Posts:
    515
    Awesome Magiichan

    Here is the code area that needed to be tweaked, thanks to Magiichan for helping out with solving that :D

    Code (CSharp):
    1. public static bool Save(string filename, AudioClip clip)
    2.     {
    3.         if (!filename.ToLower().EndsWith(".wav"))
    4.         {
    5.             filename += ".wav";
    6.         }
    7.  
    8.         var _path = Application.dataPath;
    9.         if (Application.platform == RuntimePlatform.OSXPlayer)
    10.         {
    11.             _path = _path.Substring(0, _path.LastIndexOf('/')-1).Substring(0, _path.LastIndexOf('/')-1).Substring(0, _path.LastIndexOf('/')-1);
    12.             _path = _path.Substring(0, _path.LastIndexOf('/'));
    13.         }
    14.         var filepath = _path + filename;
    15.  
    16.         Debug.Log(filepath);
    17.  
    18.         if (!Application.isEditor)
    19.         {
    20.             // Make sure directory exists if user is saving to sub dir.
    21.             Directory.CreateDirectory(Path.GetDirectoryName(filepath));
    22.  
    23.             using (var fileStream = CreateEmpty(filepath))
    24.             {
    25.                 ConvertAndWrite(fileStream, clip);
    26.                 WriteHeader(fileStream, clip);
    27.             }
    28.         }
    29.  
    30.         return true; // TODO: return false if there's a failure saving the file
    31.     }
     
    RoyalCoder, teexiii and Magiichan like this.
  28. teexiii

    teexiii

    Joined:
    Oct 7, 2015
    Posts:
    15
    Both of you guys are brilliant!!!
     
    Magiichan and KnightRiderGuy like this.