Пример #1
0
        public void RequestAudio(AudioOutput _outputComponent, MediaPlayer mediaPlayer, float[] data, int channelMask, int totalChannels, AudioOutput.AudioOutputMode audioOutputMode)
        {
            if (mediaPlayer == null || mediaPlayer.Control == null || !mediaPlayer.Control.IsPlaying())
            {
                return;
            }

            int channels = mediaPlayer.Control.GetNumAudioChannels();;

            //total samples requested should be multiple of channels
#if UNITY_5 || UNITY_5_4_OR_NEWER
            Debug.Assert(data.Length % totalChannels == 0);
#endif

            if (!_accessTrackers.ContainsKey(mediaPlayer))
            {
                _accessTrackers[mediaPlayer] = new HashSet <AudioOutput>();
            }

            //requests data if it hasn't been requested yet for the current cycle
            if (_accessTrackers[mediaPlayer].Contains(_outputComponent) || _accessTrackers[mediaPlayer].Count == 0 || _pcmData[mediaPlayer] == null)
            {
                _accessTrackers[mediaPlayer].Clear();

                int actualDataRequired = data.Length / totalChannels * channels;
                _pcmData[mediaPlayer] = new float[actualDataRequired];

                GrabAudio(mediaPlayer, _pcmData[mediaPlayer], channels);

                _accessTrackers[mediaPlayer].Add(_outputComponent);
            }

            //calculate how many samples and what channels are needed and then copy over the data
            int samples      = Math.Min(data.Length / totalChannels, _pcmData[mediaPlayer].Length / channels);
            int storedPos    = 0;
            int requestedPos = 0;

            //multiple mode, copies over audio from desired channels into the same channels on the audiosource
            if (audioOutputMode == AudioOutput.AudioOutputMode.Multiple)
            {
                int lesserChannels = Math.Min(channels, totalChannels);

                for (int i = 0; i < samples; ++i)
                {
                    for (int j = 0; j < lesserChannels; ++j)
                    {
                        if ((1 << j & channelMask) > 0)
                        {
                            data[requestedPos + j] = _pcmData[mediaPlayer][storedPos + j];
                        }
                    }

                    storedPos    += channels;
                    requestedPos += totalChannels;
                }
            }

            //Mono mode, copies over single channel to all output channels
            else if (audioOutputMode == AudioOutput.AudioOutputMode.Single)
            {
                int desiredChannel = 0;

                for (int i = 0; i < 8; ++i)
                {
                    if ((channelMask & (1 << i)) > 0)
                    {
                        desiredChannel = i;
                        break;
                    }
                }

                if (desiredChannel < channels)
                {
                    for (int i = 0; i < samples; ++i)
                    {
                        for (int j = 0; j < totalChannels; ++j)
                        {
                            data[requestedPos + j] = _pcmData[mediaPlayer][storedPos + desiredChannel];
                        }

                        storedPos    += channels;
                        requestedPos += totalChannels;
                    }
                }
            }
        }
Пример #2
0
        public void RequestAudio(AudioOutput outputComponent, MediaPlayer mediaPlayer, float[] audioData, int audioChannelCount, int channelMask, AudioOutput.AudioOutputMode audioOutputMode, bool supportPositionalAudio)
        {
            if (mediaPlayer == null || mediaPlayer.Control == null || !mediaPlayer.Control.IsPlaying())
            {
                if (supportPositionalAudio)
                {
                    ZeroAudio(audioData, 0);
                }
                return;
            }

            int channels = mediaPlayer.Control.GetAudioChannelCount();

            if (channels <= 0)
            {
                if (supportPositionalAudio)
                {
                    ZeroAudio(audioData, 0);
                }
                return;
            }

            // total samples requested should be multiple of channels
            Debug.Assert(audioData.Length % audioChannelCount == 0);

            // Find or create an instance
            PlayerInstance instance = null;

            if (!_instances.TryGetValue(mediaPlayer, out instance))
            {
                instance = _instances[mediaPlayer] = new PlayerInstance()
                {
                    outputs = new HashSet <AudioOutput>(),
                    pcmData = null
                };
            }

            // requests data if it hasn't been requested yet for the current cycle
            if (instance.outputs.Count == 0 || instance.outputs.Contains(outputComponent) || instance.pcmData == null)
            {
                instance.outputs.Clear();

                int actualDataRequired = (audioData.Length * channels) / audioChannelCount;
                if (instance.pcmData == null || actualDataRequired != instance.pcmData.Length)
                {
                    instance.pcmData = new float[actualDataRequired];
                }

                instance.isPcmDataReady = GrabAudio(mediaPlayer, instance.pcmData, channels);

                instance.outputs.Add(outputComponent);
            }

            if (instance.isPcmDataReady)
            {
                // calculate how many samples and what channels are needed and then copy over the data
                int samples      = Math.Min(audioData.Length / audioChannelCount, instance.pcmData.Length / channels);
                int storedPos    = 0;
                int requestedPos = 0;

                // multiple mode, copies over audio from desired channels into the same channels on the audiosource
                if (audioOutputMode == AudioOutput.AudioOutputMode.MultipleChannels)
                {
                    int lesserChannels = Math.Min(channels, audioChannelCount);

                    if (!supportPositionalAudio)
                    {
                        for (int i = 0; i < samples; ++i)
                        {
                            for (int j = 0; j < lesserChannels; ++j)
                            {
                                if ((1 << j & channelMask) > 0)
                                {
                                    audioData[requestedPos + j] = instance.pcmData[storedPos + j];
                                }
                            }

                            storedPos    += channels;
                            requestedPos += audioChannelCount;
                        }
                    }
                    else
                    {
                        for (int i = 0; i < samples; ++i)
                        {
                            for (int j = 0; j < lesserChannels; ++j)
                            {
                                if ((1 << j & channelMask) > 0)
                                {
                                    audioData[requestedPos + j] *= instance.pcmData[storedPos + j];
                                }
                            }

                            storedPos    += channels;
                            requestedPos += audioChannelCount;
                        }
                    }
                }
                //Mono mode, copies over single channel to all output channels
                else if (audioOutputMode == AudioOutput.AudioOutputMode.OneToAllChannels)
                {
                    int desiredChannel = 0;

                    for (int i = 0; i < 8; ++i)
                    {
                        if ((channelMask & (1 << i)) > 0)
                        {
                            desiredChannel = i;
                            break;
                        }
                    }

                    if (desiredChannel < channels)
                    {
                        if (!supportPositionalAudio)
                        {
                            for (int i = 0; i < samples; ++i)
                            {
                                for (int j = 0; j < audioChannelCount; ++j)
                                {
                                    audioData[requestedPos + j] = instance.pcmData[storedPos + desiredChannel];
                                }

                                storedPos    += channels;
                                requestedPos += audioChannelCount;
                            }
                        }
                        else
                        {
                            for (int i = 0; i < samples; ++i)
                            {
                                for (int j = 0; j < audioChannelCount; ++j)
                                {
                                    audioData[requestedPos + j] *= instance.pcmData[storedPos + desiredChannel];
                                }

                                storedPos    += channels;
                                requestedPos += audioChannelCount;
                            }
                        }
                    }
                }

                // If there is left over audio
                if (supportPositionalAudio && requestedPos != audioData.Length)
                {
                    // Zero the remaining audio data otherwise there are pops
                    ZeroAudio(audioData, requestedPos);
                }
            }
            else
            {
                if (supportPositionalAudio)
                {
                    // Zero the remaining audio data otherwise there are pops
                    ZeroAudio(audioData, 0);
                }
            }
        }