【问题标题】:how to make a sound using erlang language?如何使用erlang语言发出声音?
【发布时间】:2017-05-25 07:59:41
【问题描述】:

搜了一下,好像没有人有同样的疑问:如何用erlang语言发声?

我正在使用erlang编写程序,所以每当程序出现错误时,应该有某种警报声。 感谢您的帮助。

【问题讨论】:

标签: audio erlang


【解决方案1】:

您可以通过输出 ASCII 响铃字符(代码 7)来响终端响铃:

io:format("\007\n").

(请注意,\a 不是 Erlang 中响铃字符的转义序列,即使它在 C 中也是如此。)

由于某种原因,这在“新”Erlang shell 中不起作用;它只输出^G。我让它通过以下方式工作:

  • 通过使用“旧”shell erl -oldshell 显式启动 Erlang,然后在 shell 中键入 io:format 调用
  • 通过在命令行上指定调用:

    erl -noinput -eval 'io:format("\007\n")' -eval 'init:stop()'
    
  • 在脚本中:

    #!/usr/bin/env escript
    
    -export([main/1]).
    
    main(_) ->
        io:format("\007\n").
    

如果终端铃声不是您想要的音频类型,我怀疑您必须找到一个依赖于系统的解决方案来调用音频驱动程序或其他东西。 (或者也许使用os:cmd 来播放您想要的音频文件。)

【讨论】:

    【解决方案2】:

    我为 OpenAL 1.1(3d 声音库)编写了一个接口。它应该可以在 Linux 上运行,但如果您想让它在 Windows 上运行,您可能需要进行一些小的更改才能使编译工作。无论如何,这对于您正在做的事情来说可能有点过头了。

    https://github.com/edescourtis/eopenal

    l(eopenal).
    D = eopenal:alcOpenDevice(). 
    true = eopenal:alcIsExtensionPresent(alc_enumeration_ext).
    [ADS|_] = eopenal:alcGetString(alc_device_specifier).
    C = eopenal:alcCreateContext(D).
    true = eopenal:alcMakeContextCurrent(C).
    ok = eopenal:alListener3f(al_position, 0.0, 0.0, 1.0).
    ok = eopenal:alListener3f(al_velocity, 0.0, 0.0, 0.0).
    ok = eopenal:alListenerfv(al_orientation, {0.0, 0.0, 1.0, 0.0, 1.0, 0.0}).
    [S] = eopenal:alGenSources(1).
    ok = eopenal:alSourcef(S, al_pitch, 1.0).
    ok = eopenal:alSourcef(S, al_gain, 1.0).
    ok = eopenal:alSource3f(S, al_position, 0.0, 0.0, 0.0).
    ok = eopenal:alSource3f(S, al_velocity, 0.0, 0.0, 0.0).
    ok = eopenal:alSourcei(S, al_looping, 0).
    [B1, B2] = eopenal:alGenBuffers(2).
    Data = element(2, file:read_file("/home/eric/music.raw")).
    ok = eopenal:alBufferData(B1, al_format_mono16, Data, 8000).
    ok = eopenal:alBufferData(B2, al_format_mono16, Data, 8000).
    ok = eopenal:alSourceQueueBuffers(S, [B1, B2]).
    2 = eopenal:alGetSourcei(S, al_buffers_queued).
    ok = eopenal:alSourcePlay(S).
    ok = timer:sleep(500).
    ok = eopenal:alSourceStop(S).
    ok = eopenal:alSourceUnqueueBuffers(S, [B1]).
    1 = eopenal:alGetSourcei(S, al_buffers_queued).
    ok = eopenal:alSourcePlay(S).
    

    您可以将音频存储为 RAW(使用 audacity 制作文件)并将其提供给 OpenAL。

    参考https://www.openal.org/documentation/openal-1.1-specification.pdf

    【讨论】:

      猜你喜欢
      • 2022-01-21
      • 2015-06-12
      • 1970-01-01
      • 1970-01-01
      • 2014-12-22
      • 2010-09-23
      • 1970-01-01
      • 1970-01-01
      • 2012-04-28
      相关资源
      最近更新 更多