Quantcast
Channel: Questions in topic: "channels"
Viewing all articles
Browse latest Browse all 45

How to send audio to a specific channel?

$
0
0
In the article below, I found a script to take an AudioClip, get its data and send it to a new AudioClip to a specific channel. https://thomasmountainborn.com/2016/06/10/sending-audio-directly-to-a-speaker-in-unity/ Here is the code: public AudioClip CreateSpeakerSpecificClip(AudioClip originalClip, int amountOfChannels, int targetChannel) { // Create a new clip with the target amount of channels. AudioClip clip = AudioClip.Create(originalClip.name, originalClip.samples, amountOfChannels, originalClip.frequency, false); // Init audio arrays. float[] audioData = new float[originalClip.samples * amountOfChannels]; float[] originalAudioData = new float[originalClip.samples * originalClip.channels]; if (!originalClip.GetData(originalAudioData, 0)) return null; // Fill in the audio from the original clip into the target channel. Samples are interleaved by channel (L0, R0, L1, R1, etc). int originalClipIndex = 0; for (int i = targetChannel; i < audioData.Length; i += amountOfChannels) { audioData[i] = originalAudioData[originalClipIndex]; originalClipIndex += originalClip.channels; } if (!clip.SetData(audioData, 0)) return null; return clip; } So, if I have a music (stereo) that have 2 channels, I could take this music, and copy only data from the first channel to a new AudioClip of 1 channel to play the music only on the left side. But, when I call CreateSpeakerSpecificClip(audioClip, 1, 0), I think it should create a new AudioClip with 1 channel (2nd parameter) and target the channel at index 0 (the first and only channel, third parameter). But, I continue to hear the music on both side. Is there anything that I don't see? Thanks

Viewing all articles
Browse latest Browse all 45

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>