Пример #1
0
        /// <summary>
        /// Convert a byte array to an audio clip
        /// </summary>
        /// <param name="data">The byte array representing an audio clip</param>
        /// <param name="channels">How many channels in the audio data</param>
        /// <param name="frequency">The recording frequency of the audio data</param>
        /// <param name="threedimensional">Whether the audio clip should be 3D</param>
        /// <param name="gain">How much to boost the volume (1.0 = unchanged)</param>
        /// <returns>An AudioClip</returns>
        public static AudioClip BytesToAudioClip(byte[] data, int channels, int frequency, bool threedimensional, float gain)
        {
            float[] samples = new float[data.Length];

            for (int i = 0; i < samples.Length; i++)
            {
                //convert to integer in -128 to +128 range
                int c = (int)data[i];
                c         -= 127;
                samples[i] = ((float)c / 128.0f) * gain;
            }

            AudioClip clip = USpeakAudioManager.GetOrCreateAudioClip(data.Length / channels, channels, frequency, threedimensional);

            clip.SetData(samples, 0);
            return(clip);
        }
Пример #2
0
        /// <summary>
        /// Convert a short array to an audio clip
        /// </summary>
        /// <param name="data">The short array representing an audio clip</param>
        /// <param name="channels">How many channels in the audio data</param>
        /// <param name="frequency">The recording frequency of the audio data</param>
        /// <param name="threedimensional">Whether the audio clip should be 3D</param>
        /// <param name="gain">How much to boost the volume (1.0 = unchanged)</param>
        /// <returns>An AudioClip</returns>
        public static AudioClip ShortsToAudioClip(short[] data, int channels, int frequency, bool threedimensional, float gain)
        {
            float[] samples = new float[data.Length];

            for (int i = 0; i < samples.Length; i++)
            {
                //convert to float in the -1 to 1 range
                int c = (int)data[i];
                samples[i] = ((float)c / 32767.0f) * gain;
            }

            AudioClip clip = USpeakAudioManager.GetOrCreateAudioClip(data.Length / channels, channels, frequency, threedimensional);

            if (samples.Length > 0)
            {
                clip.SetData(samples, 0);
            }
            return(clip);
        }