Example #1
0
    //TODO: Fix false positive when overwriting existing files
    // Maybe a check between the files date metadata and the current time?
    /// <summary>
    /// This method takes in a AudioClip, file name, and file directory to create a
    /// byte array and write it to the proper directory. Returns true if the file
    /// is successfully saved.
    /// </summary>
    /// <param name="clip"></param>
    /// <param name="fileName"></param>
    /// <param name="fileDir"></param>
    /// <returns>bool</returns>
    private static bool SaveAudioToDir(AudioClip clip, string fileName, string fileDir)
    {
        //Check if the word audio directory exists, and if not then create it. This should only be needed when playing for first time.
        if (!Directory.Exists(Application.persistentDataPath + "/WordAudio"))
        {
            Debug.Log("Creating the /WordAudio directory");
            Directory.CreateDirectory(Application.persistentDataPath + "/WordAudio");
        }

        //Check if
        if (!Directory.Exists(fileDir))
        {
            Debug.Log("Creating new directory for " + fileName + " at: " + fileDir + " for audioclip:" + clip.name);
            Directory.CreateDirectory(fileDir);
        }
        else
        {
            Debug.Log("Error: That directory already exists!");
        }

        // Convert audio clip to wav encoded bytes
        byte[] bytes = WavEncoder.FromAudioClip(clip);

        // Get the file path and write bytes to the file
        string filePath = fileDir + "/" + fileName.ToLower() + ".wav";

        File.WriteAllBytes(filePath, bytes);

        return(File.Exists(filePath));
    }
        private void rewindSaveBtn_Click(object sender, EventArgs e)
        {
            //Get usable buffer and read bytes
            byte[] buffer = new byte[rewind.GetUsableBytes()];
            rewind.ReadBuffer(buffer, 0);

            //Open a file prompt
            SaveFileDialog fd = new SaveFileDialog();

            fd.Title  = "Save Audio Buffer";
            fd.Filter = "Audio File (*.wav)|*.wav";
            DialogResult ar = fd.ShowDialog();

            //Save
            if (ar == DialogResult.OK)
            {
                using (FileStream fs = new FileStream(fd.FileName, FileMode.Create))
                    using (WavEncoder wav = new WavEncoder(fs, rewind))
                    {
                        wav.Write(buffer, 0, buffer.Length);
                        wav.Flush();
                    }
                rewind.ResetBuffer();
            }
        }
Example #3
0
        public void ReadBufferIntoWav(WavEncoder wav)
        {
            //Get usable buffer and read bytes
            byte[] buffer = new byte[GetUsableBytes()];
            ReadBuffer(buffer, 0);

            //Write
            wav.Write(buffer, 0, buffer.Length);
        }
Example #4
0
    /// <summary>
    /// Loads an audio clip from a given directory. Returns null if the audio clip
    /// could not be loaded.
    /// </summary>
    /// <param name="fileName"></param>
    /// <param name="fileDir"></param>
    /// <returns>AudioClip</returns>
    private static AudioClip LoadAudioByDir(string fileName, string fileDir)
    {
        Debug.Log("File access util: attempting to load existing audio clip: " + fileName);
        AudioClip clip     = null;
        string    filePath = string.Format("{0}/{1}", fileDir, fileName.ToLower());

        if (File.Exists(filePath))
        {
            Debug.Log("existing audioclip found! Returning");
            clip = WavEncoder.ToAudioClip(filePath);
        }
        else
        {
            Debug.Log("No existing audioclip found. Returning null");
            return(null);
        }

        return(clip);
    }
    public static FullProjectDataV2 Version1To2(SerializableProjectData dataV1)
    {
        FullProjectDataV2 dataV2 = new FullProjectDataV2
        {
            project   = dataV1.project,
            audioType = ".wav"
        };
        WavEncoder wavEncoder = new WavEncoder
        {
            channel    = dataV1.channel,
            frequency  = dataV1.frequency,
            length     = dataV1.length,
            sampleData = dataV1.sampleData
        };

        wavEncoder.EncodeToWav(out dataV2.audio);
        dataV2.project.songName = "converted audio.wav";
        return(dataV2);
    }
 public override Stream OpenActiveFile(string path)
 {
     fileStream = new FileStream(path, FileMode.Create);
     wavStream  = new WavEncoder(fileStream, audio);
     return(wavStream);
 }
Example #7
0
        // 当客户端发送消息时执行该函数,接收Socket中的数据
        public void ReceiveCallback(IAsyncResult ia)     //接收回调   ReceiveContent为接收数据
        {
            key = 0;
            StateObject _so = ia.AsyncState as StateObject;

            _socket = _so.theSocket;

            if (Pattern == "File sharing")
            {
                try
                {
                    int n = _socket.EndReceive(ia);     // 接收到的字节数
                    ReceiveContent = Encoding.UTF8.GetString(_so.Buffer, 0, n);
                    Console.WriteLine(ReceiveContent, DateTime.Now.ToLongTimeString());
                    string[] type = ReceiveContent.Split('?');
                    if (type[0].Equals("size"))                   //获取数据大小
                    {
                        Console.WriteLine(type[0] + "+" + type[1], DateTime.Now.ToLongTimeString());
                        key = 1;
                        string findContent_Size = cmd.Exec(type[1]);
                        if (findContent_Size.Equals("HQNR"))                                 // 如果数据为Byte[]
                        {
                            int i = cmd.Get_Byte().Length;
                            SendCommand(i);
                        }
                        else if (findContent_Size.Equals("PMTS"))
                        {
                            SendCommand(cmd.Get_Byte());
                        }
                        else
                        {
                            byte[] buffer = System.Text.Encoding.UTF8.GetBytes(findContent_Size);     //如果是字符串
                            SendCommand(buffer.Length);                                               // 发送大小
                        }
                        Console.WriteLine("size+string" + findContent_Size, DateTime.Now.ToLongTimeString());
                    }
                    else if (type[0].Equals("play"))                           //跳转到屏幕传输
                    {
                        PlayBells.StartPlayMedia("DoorBell.wav");
                        key = 1;
                        if (MessageBox.Show("请求分享风景?", "温馨提示", MessageBoxButtons.OKCancel, MessageBoxIcon.Question) == DialogResult.OK)
                        {
                            PlayBells.StopPlayMedia();
                            SendCommand("OK");
                            Pattern = "Scenic sharing";
                            Thread thread_show = new Thread(ShowShow);
                            thread_show.Start();
                            threadClose = new Thread(ShowClose);
                            threadClose.Start();
                        }
                        else
                        {
                            PlayBells.StopPlayMedia();
                            SendCommand("NO");
                        }
                    }
                    else if (type[0].Equals("doorbells"))
                    {
                        PlayBells.StartPlayMedia("DoorBell.wav");
                        key = 1;
                        if (MessageBox.Show("是否接受视频通话?", "客人来访", MessageBoxButtons.OKCancel, MessageBoxIcon.Question) == DialogResult.OK)
                        {
                            PlayBells.StopPlayMedia();
                            SendCommand("OK");
                            Pattern = "Scenic sharing";
                            Thread thread_show = new Thread(ShowShow);//创建新线程
                            thread_show.Start();
                        }
                        else
                        {
                            PlayBells.StopPlayMedia();
                            SendCommand("NO");
                        }
                    }
                    else if (type[0].Equals("voice"))                           //跳转到屏幕传输
                    {
                        PlayBells.StartPlayMedia("DoorBell.wav");
                        key = 1;
                        if (MessageBox.Show("请求语音通话?", "温馨提示", MessageBoxButtons.OKCancel, MessageBoxIcon.Question) == DialogResult.OK)
                        {
                            PlayBells.StopPlayMedia();
                            SendCommand("OK");
                            Pattern     = "Voice Calls";
                            threadVoice = new Thread(ShowVoice);//创建新线程
                            threadVoice.Start();
                        }
                        else
                        {
                            PlayBells.StopPlayMedia();
                            SendCommand("NO");
                        }
                    }
                    else if (type[0].Equals("data"))
                    {
                        key     = 1;
                        Pattern = "DataSharing";
                    }
                    else if (type[0].Equals(""))
                    {
                        return;
                    }
                    else                             //获取数据或执行功能
                    {
                        Console.WriteLine(type[0], DateTime.Now.ToLongTimeString());
                        findContent = cmd.Exec(type[0]);
                    }
                }
                catch (Exception ex)
                {
                }
                if (!findContent.Equals("wcz") && !findContent.Equals("HQNR") && !findContent.Equals("PMTS") && key == 0)
                {
                    Console.WriteLine("string" + findContent, DateTime.Now.ToLongTimeString());
                    SendCommand(findContent);                                               // 发送字符串
                    findContent = "";
                }
                else if (findContent.Equals("HQNR") && key == 0)
                {
                    SendCommand(cmd.Get_Byte());                                           // 发送byte[]
                    findContent = "";
                }
                ReceiveContent = "";
            }
            else if (Pattern == "Scenic sharing")
            {
                try
                {
                    int n = _socket.EndReceive(ia);//接收到的字节数
                    Console.WriteLine(n.ToString(), DateTime.Now.ToLongTimeString());
                    ReceiveContent = Encoding.UTF8.GetString(_so.Buffer, 0, n);
                    if (_so.Buffer == null || ReceiveContent == "|||***|||")
                    {
                        threadClose.Abort();
                        Pattern = "File sharing";
                        shareform.Close();
                    }
                    else
                    {
                        closei = 0;
                        Console.WriteLine("shenjing", DateTime.Now.ToLongTimeString());
                        byte[] buffer = new byte[n];
                        Image  im     = ChangeImageWithByte.BytToImg(_so.Buffer, n); // 位图和字符流之间的转换
                        shareform.Back_Image(im);
                    }
                }
                catch (Exception ex)
                {
                }
                ReceiveContent = "";
            }
            else if (Pattern == "Voice Calls")
            {
                MemoryStream stream = new MemoryStream();
                WavEncoder   wav    = new WavEncoder();
                try
                {
                    int n = _socket.EndReceive(ia);//接收到的字节数
                    stream.Write(_so.Buffer, 0, n);
                    ReceiveContent = Encoding.UTF8.GetString(_so.Buffer, 0, n);
                    if (_so.Buffer == null || ReceiveContent == "|||***|||")
                    {
                        Pattern = "File sharing";
                        Voice_t.Abort();
                        sharevoice.Close();
                    }
                    else if (ReceiveContent == "start")
                    {
                        sharevoice.threadvoi = new Thread(MyAudio.Send_Voice);
                        sharevoice.threadvoi.Start();
                        Voice_t.Abort();
                        sharevoice.Change();
                    }
                    else
                    {
                        sound.Stream = wav.Encode(stream);
                        //System.Threading.Thread.Sleep(50);
                        Voice_t = new Thread(new ThreadStart(sound.Play));
                        Voice_t.Start();
                    }
                }
                catch
                {
                }
                ReceiveContent = "";
            }
            else if (Pattern == "DataSharing")
            {
                //name = "哈哈.mp4";
                try
                {
                    int n = _socket.EndReceive(ia);//接收到的字节数
                    Console.WriteLine(n.ToString(), DateTime.Now.ToLongTimeString());
                    ReceiveContent = Encoding.UTF8.GetString(_so.Buffer, 0, n);
                    if (ReceiveContent != "|||***|||")
                    {
                        string[] data = ReceiveContent.Split('|');
                        if (data[0] == "FileName")
                        {
                            name = data[1];
                        }
                        else
                        {
                            writeFile(_so.Buffer, @"C:\Users\Public\IHomeFiles\" + name, n);
                            MyDos.ExecDosInstructions(@"C:\Users\Public\IHomeFiles\" + name);
                        }
                    }
                    else
                    {
                        Console.WriteLine("shareform.Close();", DateTime.Now.ToLongTimeString());
                        Pattern = "File sharing";
                        //shareform.Close();
                    }
                }
                catch (Exception ex)
                {
                }
                ReceiveContent = "";
            }
            // 继续接收
            _so           = new StateObject();
            _so.theSocket = _socket;
            try
            {
                _socket.BeginReceive(_so.Buffer, 0, _so.Buffer.Length, SocketFlags.None, new AsyncCallback(this.ReceiveCallback), _so);
            }
            catch (Exception ex)
            {
            }
        }