无缝音频循环到任意位置 [英] Seamless audio loop to an arbitrary position

查看:108
本文介绍了无缝音频循环到任意位置的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我最喜欢MOD格式的一个原因是能够循环回到歌曲中的任何给定点,使其非常适合具有介绍后跟主循环的歌曲。

One of the things I most love about the MOD format is the ability to loop back to any given point in the song, making it perfect for songs that have an "intro" followed by a "main loop".

当然,MP3不能这样做。

Of course, MP3 can't do this.

到目前为止,我做过这样的事情:

Up until now, I've done things like this:

<audio src="/path/to/song.mp3" onEnded="this.currentTime = 12.345;"></audio>

浮点值是主循环开始的时间。

Where the float value is the time at which the main loop starts.

虽然这有效但音频重新启动时会有一个明显的小秒暂停。我可以通过将目标时间设置在它应该的位置之前来减轻这种暂停的影响,因此节拍至少保持更接近时间,但是它并不是非常理想。

While this works there is a noticeable fraction-of-a-second pause as the audio restarts. I can lessen the effect of this pause by setting the target time a little ahead of where it should be, so the beats are at least kept closer in time, however it's not really ideal.

我能想到的主要选择是手动循环音频文件(例如,通过复制粘贴在Audacity中)以产生比最有可能需要更长的歌曲,但问题是这将导致大量浪费的硬盘空间和带宽,并且它无法解决人们喜欢一首歌并停止长时间收听它的问题。

The main alternative I can think of is to manually loop the audio file (eg. in Audacity by copy-pasting) to produce a song that is longer than it would most likely be needed for, but the problem with that is that it would result in a lot of wasted hard drive space and bandwidth, and it wouldn't solve the problem of people liking a song and stopping to listen to it for a long time.

所以我想知道是否有办法循环MP3流。如果我正确理解了格式,我应该能够确定主循环开始的文件中的位置(以字节为单位)(以秒为单位),因此理论上我可以构造一个无限循环的流。但是,HTM5音频会支持这样的流吗?

So I was wondering if there's any way to loop an MP3 stream. If I understand the format correctly, I should be able to determine at what position in the file (in bytes) the main loop starts (in seconds), so in theory I could construct a stream that loops indefinitely. However, would such a stream be supported by HTM5 audio?

推荐答案

每次尝试测量延迟:

function playSeamless(clip, next) {
    if(!next) {
        next = clip.cloneNode(true);
        next.controls = false;
    }

    var start = Date.now();
    clip.play();

    setTimeout(function() {
        var time = (Date.now() - start) / 1000;
        var position = clip.currentTime;
        var delay = time - position;

        setTimeout(function() {
            // Set desired currentTime on next here and adjust delay above
            playSeamless(next, clip);
        }, (clip.duration - clip.currentTime - delay * 2.35) * 1000 | 0);
    }, 200);
}

playSeamless(yourAudioClip);

这是更好,但不完全准确,所以我需要调整 * 2.35 或将其作为减法或其他内容。

It was better, but not entirely accurate, so I need to adjust * 2.35 or make it a subtraction or something.

这篇关于无缝音频循环到任意位置的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆