How about sound?

282 views
Skip to first unread message

Bram Moolenaar

unread,
Jun 4, 2019, 9:08:03 AM6/4/19
to vim...@googlegroups.com

I have been wondering if it would help if Vim could play a sound. Think
of a gentle "ping" to indicate the cursor can't move there, or a
"tadaa" when a background build finishes successfully. At least I have
had times where I didn't notice something that was going on, because I
was looking in the wrong place. Currently there only is the annoying
beep (which I usually have disabled).

I wonder how useful this would be. It's probably a matter of taste
anyway. And it only works locally (unless there is an xterm sequence to
play a sound?).

And I wonder if it can be implemented in a way it's portable. There are
some audio libraries floating around, but I don't expect the "-dev"
version to be installed on most systems. Perhaps MS-Windows has
something that's always available?

Another way would be to use a system command (in the background), but
that could have some latency.

--
From "know your smileys":
...---... SOS

/// Bram Moolenaar -- Br...@Moolenaar.net -- http://www.Moolenaar.net \\\
/// sponsor Vim, vote for features -- http://www.Vim.org/sponsor/ \\\
\\\ an exciting new programming language -- http://www.Zimbu.org ///
\\\ help me help AIDS victims -- http://ICCF-Holland.org ///

tooth pik

unread,
Jun 4, 2019, 9:22:43 AM6/4/19
to vim...@googlegroups.com
my first reaction is "useless cruft" -- sorry

my 2nd is, find the "vim way": don't build the sound into vim, use an
external voice
synthesizer that vim would send text messages to with informative
error info -- the
user would be left to assign their own voice to whatever synthesizer
they came up
with, leaving it portable in the sense that surely windows and mac can
come up with
(however expensive) their own dang synthesizer that can accept text and speak it
> --
> --
> You received this message from the "vim_dev" maillist.
> Do not top-post! Type your reply below the text you are replying to.
> For more information, visit http://www.vim.org/maillist.php
>
> ---
> You received this message because you are subscribed to the Google Groups "vim_dev" group.
> To unsubscribe from this group and stop receiving emails from it, send an email to vim_dev+u...@googlegroups.com.
> To view this discussion on the web visit https://groups.google.com/d/msgid/vim_dev/201906041307.x54D7s43025572%40masaka.moolenaar.net.
> For more options, visit https://groups.google.com/d/optout.

Christian Brabandt

unread,
Jun 4, 2019, 9:54:52 AM6/4/19
to vim...@googlegroups.com

On Di, 04 Jun 2019, Bram Moolenaar wrote:

>
> I have been wondering if it would help if Vim could play a sound. Think
> of a gentle "ping" to indicate the cursor can't move there, or a
> "tadaa" when a background build finishes successfully. At least I have
> had times where I didn't notice something that was going on, because I
> was looking in the wrong place. Currently there only is the annoying
> beep (which I usually have disabled).
>
> I wonder how useful this would be. It's probably a matter of taste
> anyway. And it only works locally (unless there is an xterm sequence to
> play a sound?).
>
> And I wonder if it can be implemented in a way it's portable. There are
> some audio libraries floating around, but I don't expect the "-dev"
> version to be installed on most systems. Perhaps MS-Windows has
> something that's always available?
>
> Another way would be to use a system command (in the background), but
> that could have some latency.

Please not, we don't want to become an operating system, do we? How
about something more flexible, some kind of a notification callback,
that could show up (e.g. a small popup) or something that a user can
configure to play a sound if possible (e.g. a shell callback)?

Best,
Christian
--
Eine Religion, die den Menschen finster macht, ist falsch; denn er
muß Gott mit frohem Herzen und nicht aus Zwang dienen.
-- Immanuel Kant

John Little

unread,
Jun 5, 2019, 12:56:57 AM6/5/19
to vim_dev
On Wednesday, June 5, 2019 at 1:08:03 AM UTC+12, Bram Moolenaar wrote:
> I have been wondering if it would help if Vim could play a sound.

I'm a fan. More bandwidth from vim to me is always good. But people wanting to turn off beeps from vim are very vocal, those wanting to turn them on are not.

I got used to relying on beeps using vi on dumb terminals for many years, and using vim with GTK with random DEs getting any kind of beep with vim proved very hit or miss, and losing the beep after some kind of upgrade happened a lot. So I learned about libcanberra, and my vim gives me a 100 ms cowbell. It's only about 15 lines of C. Managing (choosing, distributing) the sound files would be a far larger task than the code I imagine.

I've no idea about libcanberra on MS Windows.

Regards, John Little

Bakudankun

unread,
Jun 5, 2019, 9:52:01 AM6/5/19
to vim_dev
2019年6月4日火曜日 22時08分03秒 UTC+9 Bram Moolenaar:
> I have been wondering if it would help if Vim could play a sound. Think
> of a gentle "ping" to indicate the cursor can't move there, or a
> "tadaa" when a background build finishes successfully.


Hello,

I love Vim and sound, and was wondering just like you about changing
default beep sound or playing sounds all the way. I will be glad if the
beep sound is changeable (even if only on gVim), but I think further
sound handling is not what a portable text editor should do, because
there are too many things to be considered, like audio format, latency,
audio buffer, mixing multiple sounds and so on. Most users wouldn't
want Vim to have such processes, unfortunately.

Adding some autocmd events (CursorCouldntMove?) may be good, if so then
users can call external processes to play sounds or do anything. I think
latency is not a big problem in most purpose.


> At least I have had times where I didn't notice something that was going
> on, because I was looking in the wrong place. Currently there only is
> the annoying beep (which I usually have disabled).


I had had same problem. To solve the problem, I have created a program
which wraps a (build) process and play sounds in some circumstances.
Please have a look if that interests you.

https://github.com/Bakudankun/ErrorWarner


Best regards,
Bakudankun

Bram Moolenaar

unread,
Jun 6, 2019, 5:59:43 AM6/6/19
to vim...@googlegroups.com, John Little
It appears libcanberra is widely available. My system also has the -dev
package installed (I didn't do that, perhaps because of a gtk
dependency). I could not find something for autoconf though. But I
expect it to be simple.

> I've no idea about libcanberra on MS Windows.

It needs a completely different solution. And again I hope it's a
simple thing, since we don't want this to be adding much code to Vim.

--
From "know your smileys":
:-E Has major dental problems

Marius Gedminas

unread,
Jun 6, 2019, 7:05:53 AM6/6/19
to vim...@googlegroups.com
On Thu, Jun 06, 2019 at 11:59:34AM +0200, Bram Moolenaar wrote:
> It appears libcanberra is widely available. My system also has the -dev
> package installed (I didn't do that, perhaps because of a gtk
> dependency). I could not find something for autoconf though. But I
> expect it to be simple.

On my system libcanberra-dev ships a pkg-config file, so it should be
simple to hook it up to autoconf.

$ pkg-config --libs libcanberra
-lcanberra

$ pkg-config --cflags libcanberra
-D_REENTRANT

(I've never used autoconf so I'm afraid I cannot offer a patch.)

Marius Gedminas
--
Be cheerful. Strive to be happy

Oh, and we just set fire to your desktop.
signature.asc

Bram Moolenaar

unread,
Jun 6, 2019, 8:07:53 AM6/6/19
to vim...@googlegroups.com, Marius Gedminas

Marius Gedminas wrote:

> On Thu, Jun 06, 2019 at 11:59:34AM +0200, Bram Moolenaar wrote:
> > It appears libcanberra is widely available. My system also has the -dev
> > package installed (I didn't do that, perhaps because of a gtk
> > dependency). I could not find something for autoconf though. But I
> > expect it to be simple.
>
> On my system libcanberra-dev ships a pkg-config file, so it should be
> simple to hook it up to autoconf.
>
> $ pkg-config --libs libcanberra
> -lcanberra
>
> $ pkg-config --cflags libcanberra
> -D_REENTRANT
>
> (I've never used autoconf so I'm afraid I cannot offer a patch.)

I tried a little example:


#include <unistd.h>
#include <canberra.h>
int main () {
ca_context * hello;
ca_context_create (&hello);
ca_context_play (hello, 0,
CA_PROP_EVENT_ID, "phone-incoming-call",
CA_PROP_EVENT_DESCRIPTION, "hello world",
NULL);
sleep(2);
return 0;
}

gcc -o sound -D_REENTRANT -lcanberra sound.c
/usr/bin/ld: /tmp/ccArSjRM.o: in function `main':
sound.c:(.text+0x1f): undefined reference to `ca_context_create'
/usr/bin/ld: sound.c:(.text+0x57): undefined reference to `ca_context_play'
collect2: error: ld returned 1 exit status

Somehow linking doesn't work, must be missing something...


--
Common sense is what tells you that the world is flat.

Marius Gedminas

unread,
Jun 6, 2019, 1:05:52 PM6/6/19
to vim...@googlegroups.com
On Thu, Jun 06, 2019 at 02:07:46PM +0200, Bram Moolenaar wrote:
> I tried a little example:
>
>
> #include <unistd.h>
> #include <canberra.h>
> int main () {
> ca_context * hello;
> ca_context_create (&hello);
> ca_context_play (hello, 0,
> CA_PROP_EVENT_ID, "phone-incoming-call",
> CA_PROP_EVENT_DESCRIPTION, "hello world",
> NULL);
> sleep(2);
> return 0;
> }
>
> gcc -o sound -D_REENTRANT -lcanberra sound.c
> /usr/bin/ld: /tmp/ccArSjRM.o: in function `main':
> sound.c:(.text+0x1f): undefined reference to `ca_context_create'
> /usr/bin/ld: sound.c:(.text+0x57): undefined reference to `ca_context_play'
> collect2: error: ld returned 1 exit status
>
> Somehow linking doesn't work, must be missing something...

This works:

gcc -o sound -D_REENTRANT sound.c -lcanberra

(At some point back in 2011 Ubuntu changed the linker defaults to use
--as-needed, which means the linker tries to drop unneeded libraries if
it doesn't see any object files using symbols from that library before
it encounters the name of the library. I don't really understand this;
but it caused everyone's build scripts to fail at the time, and the fix
was to move all the libraries to the end of the compiler command line.
It's documented in https://wiki.ubuntu.com/NattyNarwhal/ToolchainTransition#Indirect_Linking_for_Shared_Libraries
but I don't understand a word on that page. I hope it's the right
page.)

Marius Gedminas
--
Perl is hard for most people to write. They write PERL or Pearl.
-- Abigail
signature.asc

John Little

unread,
Jun 7, 2019, 12:43:32 AM6/7/19
to vim_dev
Since others have posted code, here's mine. In gui_gtk_x11.c:

At file scope:

#ifdef CANBERRA
# include <canberra.h>
static ca_context *canberra = 0;
#endif

In gui_mch_beep:

#ifdef CANBERRA
int ca_error;
if (!canberra) // static, initialize on first use
ca_context_create (&canberra);
ca_error = ca_context_play (canberra, 0,
CA_PROP_EVENT_ID, "bell",
CA_PROP_EVENT_DESCRIPTION, "vim beep",
CA_PROP_CANBERRA_CACHE_CONTROL, "volatile",
NULL);
if (ca_error)
fprintf(stderr, "%d error, %s\n", ca_error, ca_strerror(ca_error));
#else
...

But also, in misc1.c, in vim_beep:
#ifdef CANBERRA
// john I use a 100 ms beep, which plays asynchronously anyway
// so let's use 50 instead of 500
if (!did_init || ELAPSED_FUNC(start_tv) > 50)
#else
if (!did_init || ELAPSED_FUNC(start_tv) > 500)
#endif

Because libcanberra plays the sound in another thread, or something like that, it doesn't block, and it handles playing multiple sounds. My keyboard repeat rate is 50 Hz, and if I hold down the escape key I get 10 sounds a second, because the sound is 100 ms (thanks to audacity). Having a very fast (that is, a short attack stage) sound saves me a good fraction of second every time.

Thinking while typing, please excuse me...

The sound themes typically available for the xdg desktop standard have sounds that are one or 2 seconds long, and usually start slowly. For me that wouldn't suit vim.

I'd like a general scheme where a different sound could be set for at least each auto command event. Maybe via an ex command, say "play". F.ex. one could make vim like a smartphone with key sounds on with InsertCharPre. But I'd like other events, too, with fine-grained control possible for errors and every circumstance where vim presently issues a beep.

Python can play sounds, so I suppose I could make a call to python in auto commands. Maybe all we need is an event for the bell, and for errors, maybe passing or setting up some context via v:event. (It was a struggle for me to get all the packages I needed to get my python to use GSound; documentation I found was not quite up to date. )

So, my conclusion is that there could be an event for all the circumstances where vim presently may beep, and the relevant item at :h 'belloff' could be set in v:event. If an error has occurred then v:errmsg gives information. Then some heroic plugin author can put together the python bits and pieces with an appropriate sound theme. The large majority who want nothing to do with sound can continue in their silence, or maybe flash their RGB, or send a command to an odour generator ;).

Regards, John Little

Nicola Vitacolonna

unread,
Jun 8, 2019, 5:53:16 AM6/8/19
to vim...@googlegroups.com
On 4 Jun 2019, at 15:07, Bram Moolenaar <Br...@moolenaar.net> wrote:
>
>
> I have been wondering if it would help if Vim could play a sound. Think
> of a gentle "ping" to indicate the cursor can't move there, or a
> "tadaa" when a background build finishes successfully. At least I have
> had times where I didn't notice something that was going on, because I
> was looking in the wrong place. Currently there only is the annoying
> beep (which I usually have disabled).
>
> I wonder how useful this would be. It's probably a matter of taste
> anyway. And it only works locally (unless there is an xterm sequence to
> play a sound?).
>
> And I wonder if it can be implemented in a way it's portable. There are
> some audio libraries floating around, but I don't expect the "-dev"
> version to be installed on most systems. Perhaps MS-Windows has
> something that's always available?
>
> Another way would be to use a system command (in the background), but
> that could have some latency.

My 2¢ on the topic, as I have been thinking for a while about a plugin for playing mechanical sounds on key presses.

- Make an optional plugin for that
- The plugin has an extremely simple API:

- enableSoundServer(): starts a background process accepting requests to play sounds; don’t use system commands, it’s a waste of resources and likely too slow;
- disableSoundServer(): obviously.
- getSounds(): returns a list of available sound names;
- play(soundName): asynchronously plays the requested sound.
- Perhaps, also stopSounds(), to interrupt all playing sounds.
- Sounds might be searched for in `sounds` directories under packpath (or rtp?) when the server starts.

Vim has all the needed infrastructure, IMO: jobs, timers, autocommands… People not needing/wanting sounds won’t pay anything (except for some disk space for the plugin). It is fundamental that play() returns immediately, so that multiple calls result in overlapping sounds.

I have also been searching for small, portable audio libraries, to make the plugin self-contained, and I recall having found one that seemed apt, although I haven’t tried it. As soon as I find what it was I’ll post the link here.

Life.



Lifepillar

unread,
Jun 8, 2019, 7:08:20 AM6/8/19
to vim...@googlegroups.com
 don’t use system commands, it’s a waste of resources and likely too slow;

To be clear, here I meant tricks like system('play’). I think (but I haven’t really verified) that communication with an efficient server via jobs would be good enough.

I have also been searching for small, portable audio libraries, to make the plugin self-contained, and I recall having found one that seemed apt, although I haven’t tried it. As soon as I find what it was I’ll post the link here.

Found it:


Not really a popular one, but I liked it because it looks simple enough that I can understand it and, perhaps, modify its code if necessary. It seems quite portable (at least, compiles, builds and plays sounds in macOS 10.14) and it does not have a restricted license (it doesn’t have any, AFAICS).

Life.



Lifepillar

unread,
Jun 8, 2019, 7:35:10 AM6/8/19
to vim...@googlegroups.com, John Little
On 6 Jun 2019, at 11:59, Bram Moolenaar <Br...@moolenaar.net> wrote:

It appears libcanberra is widely available.  My system also has the -dev
package installed (I didn't do that, perhaps because of a gtk
dependency).  I could not find something for autoconf though.  But I
expect it to be simple.

I've no idea about libcanberra on MS Windows.

It needs a completely different solution.  And again I hope it's a
simple thing, since we don't want this to be adding much code to Vim.

There is also libao (https://xiph.org/ao/doc/).

Life.

Reply all
Reply to author
Forward
0 new messages