Playing audiosource stream(s) on other audio source.

Hi, I am trying to play all audio from audio sources within a collider in one other audio source. I have looked at getting the output data using the GetOutputData function. But I am unsure how it could be used to play the audio back in a reasonable manner. I started trying to replay the audio using this set of code as a begin but it just sounds as some hardcore music:

var clip = AudioClip.Create("Replayer", 1000, source.clip.channels, source.clip.frequency, false);

float[] arr = new float[1000];

source.GetOutputData(arr, 0);
clip.SetData(arr, 0);
target.clip = clip;
target.Play();

I thought about using the stream functionality in clips but I am unsure how that works aswell. I tried reading the docs and experimenting but this wont really give any results to the specific cause. Maybe someone here can push me in the right direction? Thankyou in advance!

2 Likes

After more research with the google term “unity audio source recording” (I am trying everything right now!) I found this post and a unity forums both seemingly ending in a dead end.

http://answers.unity.com/answers/1568366/view.html

Please note I only want to directly replay specific audio sources, not all scene audio.

I’m struggling to understand what you’re trying to do. All audio sources play according to their settings. Are you trying to solo a subset of what is in your scene and have the others go silent?

Understandable, Let me try and elaborate further. I have a multiplayer game where people use their mics to talk in game. I now want a selected set of audio sources to replay on another audio source so it would sound like there is a microphone in the other room.

As a visual example:

The cyan/blue audio sources are the targets and the audio source on the left with the red line need to directly play the audio sources are playing. I hope this makes it a bit more clear, it would basically be an in-game microphone and I think using .

We have a similar issue where we’re trying to take audio from a camera’s audioListener and relay it to an audioSource at a different location. Was curious if you had made any headway and wanted to bump this post for further help.

I have not actually, We haven’t experimented further but I wanted to try and just replay all clips.

Hi, Did you find a solution? I want my Recorder to record certain elements in the scene, but AudioListener only records everything. The other way is to output chosen audio to to and audio source and record that audio source

I am not sure about recording, I managed to make it work with real-time replay by just taking all clips inside the range and referencing these in the replay audio sources and matching up the time.

1 Like

Oh nice, that sounds like something I could do, is it possible to share your solution? I would like to achieve an audio source that can take a range of referenced audio sources or just ignores one(metronome sound). Still being able to replay normally in real time. The Recorder can capture the audio source instead of the AudioListener which captures the whole scene.

I can share it yes, Its not very flashed out since the project got abandoned shortly after but I am actually planning on doing it for another project in a short time so I will update when I do.

[SerializeField] private AudioSource target;
        [SerializeField] private float recordingRange = 10, maxVolume = .5f;
        private Dictionary<AudioSource, AudioSource> audioSourcesToReplay = new Dictionary<AudioSource, AudioSource>();
        private Collider[] colliders;
        private int audioSourcesToReplayLastFrameCount;

        private void Start()
        {
            //Get all colliders
            colliders = this.GetComponents<Collider>();
            //Do full loop to check
            GetAllAudioSourcesToReplay();
            //Set clip
            SetupAudiosourceClip();
        }

        private void Update()
        {
            GetAllAudioSourcesToReplay();

            TimeAllAudioSources();
        }

        private void TimeAllAudioSources()
        {
            foreach (var source in audioSourcesToReplay)
            {
                if (audioSourcesToReplay[source.Key] == null)
                {
                    audioSourcesToReplay[source.Key] = target.gameObject.AddComponent<AudioSource>();
                    audioSourcesToReplay[source.Key].clip = source.Key.clip;
                    target.gameObject.SetActive(false);
                    return;
                }
                else
                {
                    target.gameObject.SetActive(true);
                    audioSourcesToReplay[source.Key].volume = Mathf.Lerp(maxVolume, 0, UnityEngine.Vector3.Distance(source.Key.transform.position, transform.position) / recordingRange);
                    audioSourcesToReplay[source.Key].time = source.Key.time;
                }

                return;
            }
        }

        public void OnDrawGizmosSelected()
        {
            //Draw sources
            foreach (var source in audioSourcesToReplay)
            {
                Gizmos.color = Color.cyan - new Color(0, 0, 0, .5f);
                Gizmos.DrawSphere(source.Key.transform.position, .3f);
            }
            //Draw target
            if (target != null)
            {
                Gizmos.color = Color.red - new Color(0, 0, 0, .5f);
                Gizmos.DrawLine(transform.position, target.transform.position);
            }
            //Draw bounds
            Gizmos.color = Color.black - new Color(0, 0, 0, .7f);
            Gizmos.matrix = transform.localToWorldMatrix;
            foreach (Collider collider in colliders)
            {
                Gizmos.DrawWireCube(collider.bounds.center, collider.bounds.size);
            }
        }

        /// <summary>
        /// Gets all audio sources to replay.
        /// </summary>
        private void GetAllAudioSourcesToReplay()
        {
            if (audioSourcesToReplayLastFrameCount != SteamAudioManager.GetSources().Count)
            {
                foreach (SteamAudioSource source in SteamAudio.SteamAudioManager.GetSources())
                {
                    foreach (Collider collider in colliders)
                    {
                        if (collider.bounds.Contains(source.transform.position) && !audioSourcesToReplay.ContainsKey(source.GetComponent<AudioSource>()))
                        {
                            audioSourcesToReplay.Add(source.GetComponent<AudioSource>(), null);
                        }
                    }
                }

                audioSourcesToReplayLastFrameCount = SteamAudioManager.GetSources().Count;
            }
        }
        /// <summary>
        ///
        /// </summary>
        private void SetupAudiosourceClip()
        {
            target.Stop();
        }
1 Like

Thank you, but I struggled to integrate this into replaying audio from other audio clips. Is steam audio a plugin you used?

Yeah I sued steam audio. You can replace that with normal audio sources