Exemplo n.º 1
0
        public static AudioClipStream LoadClip(AudioClip clip)
        {
            if (string.IsNullOrEmpty(clip.FilePath))
            {
                return(null);
            }

            Log.Debug($"Loading audioClip {clip.FilePath} ...");
            if (!File.Exists(clip.FilePath))
            {
                Log.Error($"AudioClip file '{clip.FilePath}' does not exist.");
                return(null);
            }
            var streamHandle = Bass.CreateStream(clip.FilePath, 0, 0, BassFlags.Prescan);

            Bass.ChannelGetAttribute(streamHandle, ChannelAttribute.Frequency, out var defaultPlaybackFrequency);
            Bass.ChannelSetAttribute(streamHandle, ChannelAttribute.Volume, AudioEngine.IsMuted ? 0 : 1);
            var bytes = Bass.ChannelGetLength(streamHandle);

            if (bytes < 0)
            {
                Log.Error($"Failed to initialize audio playback for {clip.FilePath}.");
            }
            var duration = (float)Bass.ChannelBytes2Seconds(streamHandle, bytes);

            var stream = new AudioClipStream()
            {
                AudioClip                = clip,
                StreamHandle             = streamHandle,
                DefaultPlaybackFrequency = defaultPlaybackFrequency,
                Duration = duration,
            };

            clip.LengthInSeconds = duration;
            return(stream);
        }
Exemplo n.º 2
0
        public static void CompleteFrame(Playback playback)
        {
            if (!_bassInitialized)
            {
                Bass.Free();
                Bass.Init();
                _bassInitialized = true;
            }
            AudioAnalysis.CompleteFrame();

            // Create new streams
            foreach (var(audioClip, time) in _updatedClipTimes)
            {
                if (_clipPlaybacks.TryGetValue(audioClip.Id, out var clip))
                {
                    clip.TargetTime = time;
                }
                else
                {
                    var audioClipStream = AudioClipStream.LoadClip(audioClip);
                    if (audioClipStream != null)
                    {
                        _clipPlaybacks[audioClip.Id] = audioClipStream;
                    }
                }
            }

            List <Guid> obsoleteIds          = new();
            var         playbackSpeedChanged = Math.Abs(_lastPlaybackSpeed - playback.PlaybackSpeed) > 0.001f;

            _lastPlaybackSpeed = playback.PlaybackSpeed;

            var handledMainSoundtrack = false;

            foreach (var(audioClipId, clipStream) in  _clipPlaybacks)
            {
                clipStream.IsInUse = _updatedClipTimes.ContainsKey(clipStream.AudioClip);
                if (!clipStream.IsInUse)
                {
                    obsoleteIds.Add(audioClipId);
                }
                else
                {
                    if (playbackSpeedChanged)
                    {
                        clipStream.UpdatePlaybackSpeed(playback.PlaybackSpeed);
                    }

                    if (!handledMainSoundtrack && clipStream.AudioClip.IsSoundtrack)
                    {
                        UpdateFftBuffer(clipStream.StreamHandle);
                        handledMainSoundtrack = true;
                    }
                    clipStream.UpdateTime();
                }
            }

            foreach (var id in obsoleteIds)
            {
                _clipPlaybacks[id].Disable();
                _clipPlaybacks.Remove(id);
            }
            _updatedClipTimes.Clear();
        }