void DecodingUpdate() { if (s_decodedFrames.Count > 0) { //Take data from the queue to add to the audioclip. DecodeInformation decodedFrame = s_decodedFrames.Dequeue(); short[] saDecodedFrames = decodedFrame.saDecodedData; int numSamples = decodedFrame.iNumSamples; int frequency = decodedFrame.iFrequency; CNetworkViewId senderViewID = decodedFrame.cSenderViewID; float[] faDecodedAudioData = new float[numSamples]; for (int i = 0; i < faDecodedAudioData.Length; ++i) { faDecodedAudioData[i] = (float)((float)saDecodedFrames[i] / 32767.0f); } // Play audio at location of sender GameObject senderNetworkView = CNetworkView.FindUsingViewId(senderViewID).gameObject; AudioSource senderAudioSource = senderNetworkView.gameObject.GetComponent <AudioSource>(); AudioClip newClip = AudioClip.Create("Test", faDecodedAudioData.Length, 1, frequency, true, false); newClip.SetData(faDecodedAudioData, 0); senderAudioSource.clip = newClip; senderAudioSource.volume = 1.0f; //AudioSystem.GetInstance.Play(newClip, senderNetworkView.gameObject.transform.position, 1.0f, 1.0f, false, 0.0f, AudioSystem.SoundType.SOUND_EFFECTS, true); CAudioSystem.Play(senderAudioSource, 1.0f, 1.0f, false, 0.0f, CAudioSystem.SoundType.SOUND_EFFECTS, true); } if (s_framesToDecode.Count > 0) { for (int i = 0; i < m_kiNumDecodeThreads; i++) { if (m_DecodeThreads[i].IsAlive == false) { m_DecodeThreads[i] = new Thread(new ParameterizedThreadStart(DecodeAudio)); m_DecodeThreads[i].Start((object)s_framesToDecode.Dequeue()); break; } } } }
//Will play specified sound if one is set. Otherwise, a random clip will be used. //A random sound can be specified be setting the index to negative one (-1). //Will only work if object already has an audiosource, else use the overloaded method public void Play(float volumeScale, bool loop, int index) { AudioSource newAudioSource = GetComponent <AudioSource>(); //Make sure fade in times are set. If not, assign default values. if (m_fFadeInTimeList.Length < m_arAudioClipPool.Length) { m_fFadeInTimeList = new float[m_arAudioClipPool.Length]; for (int i = 0; i < m_arAudioClipPool.Length; i++) { m_fFadeInTimeList[i] = 0.0f; } } //Make sure fade out times are set. If not, assign default values. if (m_fFadeOutTimeList.Length < m_arAudioClipPool.Length) { m_fFadeOutTimeList = new float[m_arAudioClipPool.Length]; for (int i = 0; i < m_arAudioClipPool.Length; i++) { m_fFadeOutTimeList[i] = 0.0f; } } //Assign a random index if one is not set. if (index == -1) { index = Random.Range(0, m_arAudioClipPool.Length); } newAudioSource.clip = m_arAudioClipPool[index]; //Allow the AudioSystem to handle the new audio source. CAudioSystem.Play(newAudioSource, Random.Range(m_fVolumeMin, m_fVolumeMax) * volumeScale, Random.Range(m_fPitchMin, m_fpitchMax), loop, m_fFadeInTimeList[index], m_eSoundType, true); //Add this to the list of attached audio sources. m_arAttachedAudioSource.Add(newAudioSource); }