You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
I'm trying to output one audio file loaded from streamingAssets directory in Unity to all available output devices. I was able to loop the file and pass audio to all output devices but I've got some issues with sound quality:
Audio playback speed seems to be at least twice times faster (or even faster) than when I just open in external software
Audio is playing with some constant "popping" sound in background
My code:
publicclassDistortionSoundController:MonoBehaviour{privateAudioFileReader_audioFileReader;privatereadonlyList<WaveOutEvent>_outputs=new();publicboolInitFinished{get;privateset;}privatebool_isPlaying;publicvoidInit(stringpathToAudioFileInStreamingAssets,IEnumerable<int>outputDevicesIndexes){stringfullAudioFilePath= Path.Combine(FileLocation.StreamingAssets.GetPath(), pathToAudioFileInStreamingAssets);if(!DirectoryService.FileExists(pathToAudioFileInStreamingAssets, FileLocation.StreamingAssets)){thrownew FileNotFoundException($"Provided distortion audio file doesn't exist.\nPath from config JSON: {pathToAudioFileInStreamingAssets}\nFull path: {fullAudioFilePath}");}_audioFileReader=new AudioFileReader(fullAudioFilePath);foreach(int outputIndex in outputDevicesIndexes){varoutput=new WaveOutEvent {DeviceNumber=outputIndex};
output.Init(_audioFileReader);
_outputs.Add(output);}InitFinished=true;}publicvoidPlay(){if(!InitFinished)thrownew Exception($"Tried to use {nameof(DistortionSoundController)} before initializing it!");if(_isPlaying)return;foreach(var output in _outputs)
output.Play();_isPlaying=true;}publicvoidStop(){if(!InitFinished)thrownew Exception($"Tried to use {nameof(DistortionSoundController)} before initializing it!");if(!_isPlaying)return;foreach(var output in _outputs)
output.Stop();
_audioFileReader.Position =0;_isPlaying=false;}publicvoidOnApplicationQuit(){foreach(var output in _outputs){
output?.Stop();
output?.Dispose();}
_audioFileReader?.Dispose();}privatevoidUpdate(){if(!_isPlaying)return;boolreachedFileEnd= _audioFileReader.Position >= _audioFileReader.Length;if(reachedFileEnd)
_audioFileReader.Position =0;}}
reacted with thumbs up emoji reacted with thumbs down emoji reacted with laugh emoji reacted with hooray emoji reacted with confused emoji reacted with heart emoji reacted with rocket emoji reacted with eyes emoji
-
Hello,
I'm trying to output one audio file loaded from streamingAssets directory in Unity to all available output devices. I was able to loop the file and pass audio to all output devices but I've got some issues with sound quality:
My code:
Beta Was this translation helpful? Give feedback.
All reactions