0

After searching, it seems that there's no one having the same question like this: how to make a sound using erlang language?

I am writing a program using erlang, so whenever there's some errors occur in the program, there should be some kind of alarm sound. Thanks for your help.

zoro
  • 333
  • 3
  • 13

2 Answers2

2

You can ring the terminal bell by outputting the ASCII bell character (code 7):

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

(Note that \a is not an escape sequence for the bell character in Erlang, even though it is in C.)

For some reason, this doesn't work in the "new" Erlang shell; it just outputs ^G. I got it to work in the following ways:

  • by explicitly starting Erlang with the "old" shell, erl -oldshell, and then typing the io:format call in the shell
  • by specifying the call on the command line:

    erl -noinput -eval 'io:format("\007\n")' -eval 'init:stop()'
    
  • in an escript:

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

If the terminal bell is not the type of audio you want, I suspect you'll have to find a system-dependent solution to invoke an audio driver or something. (Or perhaps use os:cmd to play the audio file you want.)

legoscia
  • 39,593
  • 22
  • 116
  • 167
1

I wrote an interface to OpenAL 1.1 (3d sound library). It should work on Linux although if you want to make it work on Windows you may have to make small changes for the compilation to work. Anyway this maybe be overkill for what you are doing however.

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).

You can store the audio as RAW (use audacity to make the file) and just feed it to OpenAL.

For a reference look at https://www.openal.org/documentation/openal-1.1-specification.pdf .

Eric des Courtis
  • 5,135
  • 6
  • 24
  • 37