Open ViMaSter opened 1 year ago
Potential implementation:
Split the samples based on the loop markers (code that trims silence as reference code)
AudioClip trimSilence(AudioClip inputAudio, float threshold = 0.05f)
{
// Copy samples from input audio to an array. AudioClip uses interleaved format so the length in samples is multiplied by channel count
float[] samplesOriginal = new float[inputAudio.samples * inputAudio.channels];
inputAudio.GetData(samplesOriginal, 0);
// Find first and last sample (from any channel) that exceed the threshold
int audioStart = Array.FindIndex(samplesOriginal, sample => sample > threshold),
audioEnd = Array.FindLastIndex(samplesOriginal, sample => sample > threshold);
// Copy trimmed audio data into another array
float[] samplesTrimmed = new float[audioEnd - audioStart];
Array.Copy(samplesOriginal, audioStart, samplesTrimmed, 0, samplesTrimmed.Length);
// Create new AudioClip for trimmed audio data
AudioClip trimmedAudio = AudioClip.Create(inputAudio.name, samplesTrimmed.Length / inputAudio.channels, inputAudio.channels, inputAudio.frequency, false);
trimmedAudio.SetData(samplesTrimmed, 0);
return trimmedAudio;
}
void Start ()
{
musicSource.PlayOneShot(musicStart);
musicSource.PlayScheduled(AudioSettings.dspTime + musicStart.length);
}
So...
I tried to extract the data as late as possible:
.ogg
and .wav
files can only be accessed via AudioClip
:Calls to "AssetDatabase.CreateAsset" are restricted during asset importing."
The "simplest" but most labor-intensive way is adding a MenuItem (like Assets -> Export and store loop markers
) to the editor, which you need to run manually, in the editor, on each music file. I feel there's a lot of room for oversight and I'm not a fan:
There's one potential implementation which works around 1: StreamingAssets
This would allow raw access to the original files. Meaning, there's no need to do anything special in the editor. However:
We'd have to relocate all music files to that specific directory (or a subdirectory thereof, Assets/StreamingAssets/Music/CoolPatrolX.ogg
) for this to work, however, it would offer all advantages:
Opinions on this implementation or alternatives, that offer the same set of advantages?
I don't see any downfalls to using StreamingAssets based on what you've presented. It's always good to go for the route with fewer possibilities for "user error", as you've explained. Having all the music in one place sounds like a good idea too. I say go for it, nice solution!
Description
Music can optionally have loop sections defined. Currently, these files are simply looped from beginning to end.
Acceptance Criteria
This story will be finished when Unity handles two types of
.ogg
and.wav
files:LOOP_START
toLOOP_END
LOOP_END
the next time