用于web/javascript的自动音频精灵生成器?

用于web/javascript的自动音频精灵生成器?,javascript,audio,html5-audio,Javascript,Audio,Html5 Audio,(在一个音频文件中连接多个音频位)在web上的javascript音频控制中越来越常见。然而,创建和实现音频精灵需要相当多的“愚蠢”工作。有没有一种工具或方法可以让你自动完成而不是“手动”完成 例如,给定一个包含音频文件的文件夹,我需要一个生成音频文件的工具 包含所有内容的音频文件,最好用一点静音隔开 音频文件中每个声音片段的开始和偏移计时(以毫秒为单位)。最好,它会输出javascript sprite代码本身 这个python脚本是一种生成音频精灵的简单方法 以及适合web的javascri

(在一个音频文件中连接多个音频位)在web上的javascript音频控制中越来越常见。然而,创建和实现音频精灵需要相当多的“愚蠢”工作。有没有一种工具或方法可以让你自动完成而不是“手动”完成

例如,给定一个包含音频文件的文件夹,我需要一个生成音频文件的工具

  • 包含所有内容的音频文件,最好用一点静音隔开
  • 音频文件中每个声音片段的开始和偏移计时(以毫秒为单位)。最好,它会输出javascript sprite代码本身
    这个python脚本是一种生成音频精灵的简单方法 以及适合web的javascript代码

    功能

    • 无需安装特殊模块
    • 将音频文件连接到带有静音填充的精灵
    • 生成javascript精灵信息(计时)
    要求:

    silenceDuration = 0.2  # Seconds of silence between merged files
    outfile = "soundsSprite.wav"  # Output file. Will be saved in the path below.
    folder = "C:/Users/Jonas/Dropbox/Documents/cnru/programmering/html5 nback/stimuli_mess/audioletters/"
    
    # Prepare...
    import wave, os, glob
    os.chdir(folder)
    currentTime = 0
    sprite = {}
    
    # Open output file
    output = wave.open(outfile, 'wb')
    
    # Loop through files in folder and append to outfile
    for i, infile in enumerate(glob.glob('*.wav')):
        # Skip the outfile itself
        if infile == outfile: continue
    
        # Open file and get info
        w = wave.open(folder + infile, 'rb')
        soundDuration = w.getnframes() / float(w.getframerate())
    
        # First file: determine general parameters- Create silence.
        if i == 0:
            output.setparams(w.getparams())
            silenceData = [0] * int(w.getframerate() * 2 * silenceDuration)  # N 0's where N are the number of samples corresponding to the duration specified in "silenceDuration"
            silenceFrames = "".join(wave.struct.pack('h', item) for item in silenceData)
    
        # Output sound + silence to file
        output.writeframes(w.readframes(w.getnframes()))
        output.writeframes(silenceFrames)
        w.close()
    
        # Create sprite metadata {'mysound.wav': [start_secs, end_secs]}. Then increment current time
        start = round(currentTime, 3)
        end = round(currentTime + soundDuration, 3)
        sprite[infile[:-4]] = [start, end]
        currentTime += soundDuration + silenceDuration
    
    # Yay, the worst is behind us. Close output file
    output.close()
    
    # Output in the required format. Here for jquery.mb.audio
    for filename, times in sprite.items():
        print '%s: {id: "%s", start: %.3f, end: %.3f, loop: false}, ' % (filename, filename, times[0], times[1])
    
  • 仅在.wav上用作输入和输出。之后转换为其他文件格式
  • 所有音频文件应具有相同的属性(采样器、频道等)
  • Python代码:

    silenceDuration = 0.2  # Seconds of silence between merged files
    outfile = "soundsSprite.wav"  # Output file. Will be saved in the path below.
    folder = "C:/Users/Jonas/Dropbox/Documents/cnru/programmering/html5 nback/stimuli_mess/audioletters/"
    
    # Prepare...
    import wave, os, glob
    os.chdir(folder)
    currentTime = 0
    sprite = {}
    
    # Open output file
    output = wave.open(outfile, 'wb')
    
    # Loop through files in folder and append to outfile
    for i, infile in enumerate(glob.glob('*.wav')):
        # Skip the outfile itself
        if infile == outfile: continue
    
        # Open file and get info
        w = wave.open(folder + infile, 'rb')
        soundDuration = w.getnframes() / float(w.getframerate())
    
        # First file: determine general parameters- Create silence.
        if i == 0:
            output.setparams(w.getparams())
            silenceData = [0] * int(w.getframerate() * 2 * silenceDuration)  # N 0's where N are the number of samples corresponding to the duration specified in "silenceDuration"
            silenceFrames = "".join(wave.struct.pack('h', item) for item in silenceData)
    
        # Output sound + silence to file
        output.writeframes(w.readframes(w.getnframes()))
        output.writeframes(silenceFrames)
        w.close()
    
        # Create sprite metadata {'mysound.wav': [start_secs, end_secs]}. Then increment current time
        start = round(currentTime, 3)
        end = round(currentTime + soundDuration, 3)
        sprite[infile[:-4]] = [start, end]
        currentTime += soundDuration + silenceDuration
    
    # Yay, the worst is behind us. Close output file
    output.close()
    
    # Output in the required format. Here for jquery.mb.audio
    for filename, times in sprite.items():
        print '%s: {id: "%s", start: %.3f, end: %.3f, loop: false}, ' % (filename, filename, times[0], times[1])
    
    **输出** 我对我拥有的许多音频文件(大声朗读字母)运行了此操作,并获得以下输出:

    精灵变量:

    {'AA': [0.449, 0.776], 'E': [3.149, 3.419], 'A': [0.0, 0.249], 'C': [2.113, 2.395], 'B': [1.554, 1.913], 'AE': [0.976, 1.354], 'D': [2.595, 2.949], 'G': [4.132, 4.554], 'F': [3.619, 3.932], 'H': [4.754, 4.972], 'K': [5.957, 6.258], 'J': [5.172, 5.757], 'L': [6.458, 6.719], 'O': [6.919, 7.133], 'Q': [8.488, 8.957], 'P': [7.853, 8.288], 'S': [9.681, 10.057], 'R': [9.157, 9.481], 'U': [10.694, 10.994], 'T': [10.257, 10.494], 'V': [11.194, 11.703], 'Y': [12.601, 12.93], 'X': [11.903, 12.401], 'Z': [13.13, 13.714], 'OE': [7.333, 7.653]}
    
    。。。已转换为样式:

    感谢来自以下方面的灵感:
    (tom10的答案)

    看看这个优秀的Node.js解决方案:

    对我来说太棒了