Пример #1
0
        /// <summary>
        /// Encodes a signal from the specified file.
        /// </summary>
        ///
        /// <param name="fileName">File name to save the signal to.</param>
        /// <param name="signal">The audio signal that should be saved to disk.</param>
        ///
        public static void EncodeToFile(string fileName, Signal signal)
        {
            string fileExtension = FormatHandlerAttribute.GetNormalizedExtension(fileName);

            IAudioEncoder encoder = FormatEncoderAttribute.GetEncoder(fileExtension, encoderTypes, encoders.Value);

            if (encoder != null)
            {
                // open stream
                using (FileStream stream = new FileStream(fileName, FileMode.Create, FileAccess.Write))
                {
                    // open decoder
                    encoder.Open(stream);

                    // write all audio frames
                    encoder.Encode(signal);

                    encoder.Close();
                }

                return;
            }

            throw new ArgumentException(String.Format("No suitable encoder has been found for the file format {0}. If ", fileExtension) +
                                        "you are trying to encode .wav files, please add a reference to Accord.Audio.DirectSound, and make sure you " +
                                        "are using at least one type from assembly in your code (to make sure the assembly is loaded).", "fileName");
        }
Пример #2
0
        private IEnumerator InitializeMicrophone()
        {
            Debug.Log("Trying to initialize microphone");

            yield return(Application.RequestUserAuthorization(UserAuthorization.Microphone));

            if (Application.HasUserAuthorization(UserAuthorization.Microphone))
            {
                Debug.Log("Microphone access was granted");

                foreach (var device in recorder.Devices)
                {
                    Debug.Log(device);
                }

                sampleFrequency = recorder.Start(recorder.Devices.First(), (data, count) =>
                {
                    if (audioEncoder == null)
                    {
                        audioEncoder = new SpeexAudioEncoder(false, BandMode.Wide, false, 10);
                        audioStream  = new EventStream((buffer, bufferOffset, bufferCount) =>
                        {
                            var playerNetworkBehaviour = GetComponent <PlayerNetworkBehaviour>();
                            if (playerNetworkBehaviour != null)
                            {
                                playerNetworkBehaviour.SendVoiceInput(buffer, bufferOffset, bufferCount, sampleFrequency);
                            }
                        });

                        audioEncoder.Open(audioStream, 1, sampleFrequency);
                    }

                    audioEncoder.Write(data, count);
                });
            }
        }
        private IEnumerator InitializeMicrophone()
        {
            Debug.Log("Trying to initialize microphone");

            yield return Application.RequestUserAuthorization(UserAuthorization.Microphone);

            if (Application.HasUserAuthorization(UserAuthorization.Microphone))
            {
                Debug.Log("Microphone access was granted");

                foreach( var device in recorder.Devices)
                {
                    Debug.Log(device);
                }

                sampleFrequency = recorder.Start(recorder.Devices.First(), (data,count) =>
                {
                    if ( audioEncoder == null )
                    {
                        audioEncoder    = new SpeexAudioEncoder(false,BandMode.Wide,false,10);
                        audioStream  = new EventStream((buffer,bufferOffset,bufferCount) =>
                        {
                            var playerNetworkBehaviour = GetComponent<PlayerNetworkBehaviour>();
                            if ( playerNetworkBehaviour != null )
                            {
                                playerNetworkBehaviour.SendVoiceInput(buffer,bufferOffset,bufferCount,sampleFrequency);
                            }
                        });

                        audioEncoder.Open(audioStream,1,sampleFrequency);
                    }

                    audioEncoder.Write(data,count);
                });
            }
        }