Difference between revisions of "Libcanberra"

From ArchWiki
Jump to: navigation, search
(simplify summary)
(Usage in programming: add Ruby example)
 
(15 intermediate revisions by 5 users not shown)
Line 1: Line 1:
 
[[Category:Development]]
 
[[Category:Development]]
{{Article summary start|Summary}}
+
[[ja:Libcanberra]]
{{Article summary text|This article discusses how to install and configure libcanberra.}}
+
{{Related articles start}}
{{Article summary heading|Related}}
+
{{Related|GTK+}}
{{Article summary wiki|GTK+}}
+
{{Related|Desktop notifications}}
{{Article summary wiki|Libnotify}}
+
{{Related articles end}}
{{Article summary end}}
+
 
+
 
'''Libcanberra''' is a simple abstract interface for playing event sounds. It implements the [http://freedesktop.org/wiki/Specifications/sound-theme-spec XDG Sound Theme and Naming Specifications] for generating event sounds on free desktops, such as GNOME. Further description [http://0pointer.de/lennart/projects/libcanberra/ here]
 
'''Libcanberra''' is a simple abstract interface for playing event sounds. It implements the [http://freedesktop.org/wiki/Specifications/sound-theme-spec XDG Sound Theme and Naming Specifications] for generating event sounds on free desktops, such as GNOME. Further description [http://0pointer.de/lennart/projects/libcanberra/ here]
  
 
==Installation==
 
==Installation==
  
Libcanberra can be installed with the package {{Pkg|libcanberra}}, available in the [[Official Repositories]]. It contains the library and a GTK+ module.
+
Libcanberra can be installed with the package {{Pkg|libcanberra}}, available in the [[Official repositories]]. It contains the library and a GTK+ module.
  
 
You have to choose a backend to play sounds:
 
You have to choose a backend to play sounds:
 
*[[ALSA]] backend is included in {{Pkg|libcanberra}} package
 
*[[ALSA]] backend is included in {{Pkg|libcanberra}} package
*[[GStreamer]] backend can be installed with package {{Pkg|libcanberra-gstreamer}}, available in the [[Official Repositories]].
+
*[[GStreamer]] backend can be installed with package {{Pkg|libcanberra-gstreamer}}, available in the [[Official repositories]].
*[[PulseAudio]] backend can be installed with package {{Pkg|libcanberra-pulse}}, available in the [[Official Repositories]].
+
*[[PulseAudio]] backend can be installed with package {{Pkg|libcanberra-pulse}}, available in the [[Official repositories]].
*[[OSS]] backend can be installed with the package {{AUR|libcanberra-oss}}, available in the [[Arch User Repository]].
+
  
 
Also, you have to install a sound theme in order to hear any event sound:
 
Also, you have to install a sound theme in order to hear any event sound:
*The default sound theme is 'freedesktop', which can be installed with the package {{Pkg|sound-theme-freedesktop}}, available in the [[Official Repositories]].
+
*The default sound theme is 'freedesktop', which can be installed with the package {{Pkg|sound-theme-freedesktop}}, available in the [[Official repositories]].
 
*Alternatively, you can install {{AUR|ubuntu-sounds}}, available in the [[Arch User Repository]].
 
*Alternatively, you can install {{AUR|ubuntu-sounds}}, available in the [[Arch User Repository]].
  
Line 27: Line 24:
 
{{hc|$HOME/.gtkrc-2.0 and $XDG_CONFIG_HOME/gtk-3.0/settings.ini|<nowiki>gtk-enable-event-sounds=true
 
{{hc|$HOME/.gtkrc-2.0 and $XDG_CONFIG_HOME/gtk-3.0/settings.ini|<nowiki>gtk-enable-event-sounds=true
 
gtk-enable-input-feedback-sounds=true
 
gtk-enable-input-feedback-sounds=true
gtk-sound-theme-name=freedestop</nowiki>}}
+
gtk-sound-theme-name=freedesktop</nowiki>}}
  
 
In GNOME, these settings are managed by gnome-settings-daemon, and the configuration is available in GSettings under org.gnome.desktop.sound schema.
 
In GNOME, these settings are managed by gnome-settings-daemon, and the configuration is available in GSettings under org.gnome.desktop.sound schema.
  
==Tips and tricks==
+
==Systemd==
 +
To enable bootup, shutdown and reboot sounds using canberra, [[enable]] {{ic|canberra-system-bootup}} service.
  
===Write your own canberra app===
+
==Usage in programming==
You can write your own libcanberra sound events easily in some programming languages, or you can simply use bash.
+
You can write your own libcanberra sound events easily in many programming languages using [https://wiki.gnome.org/Projects/GSound GSound] through GObject-Introspection, or you can simply use bash.
 +
 
 +
'''Bash'''
  
====Bash====
 
 
*Dependency: {{Pkg|libcanberra}}
 
*Dependency: {{Pkg|libcanberra}}
{{hc|hallo_world.sh|<nowiki>#!/bin/bash
+
{{hc|hello_world.sh|2=
canberra-gtk-play -i phone-incoming-call -d "Hallo world"</nowiki>}}
+
#!/bin/bash
 +
canberra-gtk-play -i phone-incoming-call -d "hello world"
 +
}}
 +
 
 +
'''C'''
  
====C====
 
 
*Dependency: {{Pkg|libcanberra}}
 
*Dependency: {{Pkg|libcanberra}}
*Build with: {{ic|gcc hallo_world.c -o hallo_world `pkg-config --cflags --libs glib-2.0 libcanberra`}}
+
*Build with: {{ic|gcc -o hello_world `pkg-config --cflags --libs glib-2.0 libcanberra` hello_world.c}}
{{hc|hallo_world.c|<nowiki>#include <glib.h>
+
{{hc|hello_world.c|2=
 +
#include <glib.h>
 
#include <canberra.h>
 
#include <canberra.h>
void main () {
+
int main () {
ca_context * Hallo;
+
ca_context * hello;
ca_context_create (&Hallo);
+
ca_context_create (&hello);
ca_context_play (Hallo, 0,
+
ca_context_play (hello, 0,
 
CA_PROP_EVENT_ID, "phone-incoming-call",
 
CA_PROP_EVENT_ID, "phone-incoming-call",
CA_PROP_EVENT_DESCRIPTION, "Hallo world",
+
CA_PROP_EVENT_DESCRIPTION, "hello world",
 
NULL);
 
NULL);
 
g_usleep (2000000);
 
g_usleep (2000000);
}</nowiki>}}
+
return 0;
 +
}
 +
}}
 +
 
 +
*Dependency: {{Pkg|gsound}}
 +
*Build with: {{ic|gcc -o hello_world `pkg-config --cflags --libs glib-2.0 gsound` hello_world.c}}
 +
{{hc|hello_world.c|2=
 +
#include <glib.h>
 +
#include <gsound.h>
 +
int main () {
 +
GSoundContext *hello = gsound_context_new(NULL, NULL);
 +
gsound_context_play_simple(hello, NULL, NULL,
 +
GSOUND_ATTR_EVENT_ID, "phone-incoming-call",
 +
GSOUND_ATTR_EVENT_DESCRIPTION, "hello world",
 +
NULL);
 +
g_usleep (2000000);
 +
return 0;
 +
}
 +
}}
 +
 
 +
'''Genie'''
  
====Genie====
 
 
*Dependency: {{Pkg|libcanberra}}
 
*Dependency: {{Pkg|libcanberra}}
 
*Makedependency: {{Pkg|vala}}
 
*Makedependency: {{Pkg|vala}}
*Build with: {{ic|valac --pkg libcanberra hallo_world.gs}}
+
*Build with: {{ic|valac --pkg libcanberra hello_world.gs}}
{{hc|hallo_world.gs|<nowiki>uses
+
{{hc|hello_world.gs|2=
 +
uses
 
Canberra
 
Canberra
 
init
 
init
Hallo: Context
+
hello: Context
Context.create(out Hallo)
+
Context.create(out hello)
Hallo.play (0,
+
hello.play (0,
 
PROP_EVENT_ID, "phone-incoming-call",
 
PROP_EVENT_ID, "phone-incoming-call",
PROP_EVENT_DESCRIPTION, "Hallo world")
+
PROP_EVENT_DESCRIPTION, "hello world")
Thread.usleep (2000000)</nowiki>}}
+
Thread.usleep (2000000)
 +
}}
 +
 
 +
*Dependency: {{Pkg|gsound}}
 +
*Makedependency: {{Pkg|vala}}
 +
*Build with: {{ic|valac --pkg gsound hello_world.gs}}
 +
{{hc|hello_world.gs|2=
 +
uses
 +
GSound
 +
init
 +
var hello = new GSound.Context
 +
hello.init()
 +
hello.play_simple(null,
 +
GSound.Attribute.EVENT_ID, "phone-incoming-call",
 +
GSound.Attribute.EVENT_DESCRIPTION, "hello world")
 +
Thread.usleep (2000000)
 +
}}
 +
 
 +
'''JavaScript'''
 +
 
 +
* Dependencies: {{Pkg|gsound}}, {{Pkg|gjs}}
 +
 
 +
{{hc|hello_world.js|2=
 +
#!/usr/bin/gjs
 +
const GLib = imports.gi.GLib;
 +
const GSound = imports.gi.GSound;
 +
 
 +
let hello = new GSound.Context();
 +
hello.init(null);
 +
hello.play_simple({ "event.id" : "phone-incoming-call",
 +
                    "event.description" : "hello world" }, null);
 +
GLib.usleep (2000000);
 +
}}
 +
 
 +
'''Lua'''
 +
 
 +
* Dependencies: {{Pkg|gsound}}, {{Pkg|lua-lgi}}
 +
 
 +
{{hc|hello_world.lua|2=
 +
#!/usr/bin/lua
 +
lgi = require 'lgi'
 +
GLib = lgi.require('GLib')
 +
GSound = lgi.require('GSound')
 +
 
 +
hello = GSound.Context()
 +
hello:play_simple({ [GSound.ATTR_EVENT_ID] = "phone-incoming-call",
 +
                    [GSound.ATTR_EVENT_DESCRIPTION] = "hello world" })
 +
GLib.usleep (2000000)
 +
}}
 +
 
 +
'''Perl'''
 +
 
 +
* Dependencies: {{Pkg|gsound}}, {{AUR|perl-glib-object-introspection}}
 +
{{hc|hello_world.pl|2=
 +
#!/usr/bin/perl
 +
use Glib::Object::Introspection;
 +
Glib::Object::Introspection->setup (
 +
basename => 'GSound',
 +
version => '1.0',
 +
package => 'GSound');
 +
my $hello = GSound::Context->new;
 +
$hello->play_simple({ "event.id" => "phone-incoming-call",
 +
                      "event.description" => "hello world" });
 +
sleep (2);
 +
}}
 +
 
 +
'''Python'''
 +
 
 +
* Dependencies: {{Pkg|gsound}}, {{Pkg|python-gobject}}
 +
 
 +
{{hc|hello_world.py|2=
 +
#!/usr/bin/python
 +
import gi
 +
gi.require_version('GSound', '1.0')
 +
from gi.repository import GLib, GSound
 +
 
 +
hello = GSound.Context()
 +
hello.init()
 +
hello.play_simple({ GSound.ATTR_EVENT_ID : "phone-incoming-call",
 +
                    GSound.ATTR_EVENT_DESCRIPTION : "hello world" })
 +
GLib.usleep (2000000);
 +
}}
 +
 
 +
'''Ruby'''
 +
 
 +
* Dependencies: {{Pkg|gsound}}, {{AUR|ruby-gir_ffi}}
 +
 
 +
{{hc|hello_world.rb|2=
 +
#!/usr/bin/ruby
 +
require 'gir_ffi'
 +
GirFFI.setup :GSound
 +
Hello = GSound::Context.new
 +
Hello.play_simple("event.id" => "phone-incoming-call",
 +
                  "event.description" => "hello world")
 +
sleep (2)
 +
}}
 +
 
 +
'''Vala'''
  
====Vala====
 
 
*Dependency: {{Pkg|libcanberra}}
 
*Dependency: {{Pkg|libcanberra}}
 
*Makedependency: {{Pkg|vala}}
 
*Makedependency: {{Pkg|vala}}
*Build with: {{ic|valac --pkg libcanberra hallo_world.vala}}
+
*Build with: {{ic|valac --pkg libcanberra hello_world.vala}}
{{hc|hallo_world.vala|<nowiki>using Canberra;
+
{{hc|hello_world.vala|2=
public class HalloWorld {
+
using Canberra;
 +
public class HelloWorld {
 
static void main () {
 
static void main () {
Context Hallo;
+
Context hello;
Context.create(out Hallo);
+
Context.create(out hello);
Hallo.play (0,
+
hello.play (0,
 
PROP_EVENT_ID, "phone-incoming-call",
 
PROP_EVENT_ID, "phone-incoming-call",
PROP_EVENT_DESCRIPTION, "Hallo world");
+
PROP_EVENT_DESCRIPTION, "hello world");
 +
Thread.usleep (2000000);
 +
}
 +
}
 +
}}
 +
 
 +
*Dependency: {{Pkg|gsound}}
 +
*Makedependency: {{Pkg|vala}}
 +
*Build with: {{ic|valac --pkg gsound hello_world.vala}}
 +
{{hc|hello_world.vala|2=
 +
using GSound;
 +
public class HelloWorld {
 +
static void main () {
 +
var hello = new GSound.Context();
 +
hello.init();
 +
        hello.play_simple(null,
 +
GSound.Attribute.EVENT_ID, "phone-incoming-call",
 +
GSound.Attribute.EVENT_DESCRIPTION, "hello world");
 
Thread.usleep (2000000);
 
Thread.usleep (2000000);
 
}
 
}
}</nowiki>}}
+
}
 +
}}
  
 
==See also==
 
==See also==
 
*[http://0pointer.de/lennart/projects/libcanberra/gtkdoc/ Libcanberra Reference Manual]
 
*[http://0pointer.de/lennart/projects/libcanberra/gtkdoc/ Libcanberra Reference Manual]
 +
*[https://developer.gnome.org/gsound/ GSound Reference Manual]

Latest revision as of 13:27, 9 August 2016

Related articles

Libcanberra is a simple abstract interface for playing event sounds. It implements the XDG Sound Theme and Naming Specifications for generating event sounds on free desktops, such as GNOME. Further description here

Installation

Libcanberra can be installed with the package libcanberra, available in the Official repositories. It contains the library and a GTK+ module.

You have to choose a backend to play sounds:

Also, you have to install a sound theme in order to hear any event sound:

Configuration

By default, the GTK+ module is loaded automatically, when a GTK+ application launched. You can overwrite default settings in user's GtkSettings file:

$HOME/.gtkrc-2.0 and $XDG_CONFIG_HOME/gtk-3.0/settings.ini
gtk-enable-event-sounds=true
gtk-enable-input-feedback-sounds=true
gtk-sound-theme-name=freedesktop

In GNOME, these settings are managed by gnome-settings-daemon, and the configuration is available in GSettings under org.gnome.desktop.sound schema.

Systemd

To enable bootup, shutdown and reboot sounds using canberra, enable canberra-system-bootup service.

Usage in programming

You can write your own libcanberra sound events easily in many programming languages using GSound through GObject-Introspection, or you can simply use bash.

Bash

hello_world.sh
#!/bin/bash
canberra-gtk-play -i phone-incoming-call -d "hello world"

C

  • Dependency: libcanberra
  • Build with: gcc -o hello_world `pkg-config --cflags --libs glib-2.0 libcanberra` hello_world.c
hello_world.c
#include <glib.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);
	g_usleep (2000000);
	return 0;
}
  • Dependency: gsound
  • Build with: gcc -o hello_world `pkg-config --cflags --libs glib-2.0 gsound` hello_world.c
hello_world.c
#include <glib.h>
#include <gsound.h>
int main () {
	GSoundContext *hello = gsound_context_new(NULL, NULL);
	gsound_context_play_simple(hello, NULL, NULL,
		GSOUND_ATTR_EVENT_ID, "phone-incoming-call",
		GSOUND_ATTR_EVENT_DESCRIPTION, "hello world",
		NULL);
	g_usleep (2000000);
	return 0;
}

Genie

  • Dependency: libcanberra
  • Makedependency: vala
  • Build with: valac --pkg libcanberra hello_world.gs
hello_world.gs
uses
	Canberra
init
	hello: Context
	Context.create(out hello)
	hello.play (0,
		PROP_EVENT_ID, "phone-incoming-call",
		PROP_EVENT_DESCRIPTION, "hello world")
	Thread.usleep (2000000)
  • Dependency: gsound
  • Makedependency: vala
  • Build with: valac --pkg gsound hello_world.gs
hello_world.gs
uses
	GSound
init
	var hello = new GSound.Context
	hello.init()
	hello.play_simple(null,
		GSound.Attribute.EVENT_ID, "phone-incoming-call",
		GSound.Attribute.EVENT_DESCRIPTION, "hello world")
	Thread.usleep (2000000)

JavaScript

hello_world.js
#!/usr/bin/gjs
const GLib = imports.gi.GLib;
const GSound = imports.gi.GSound;

let hello = new GSound.Context();
hello.init(null);
hello.play_simple({ "event.id" : "phone-incoming-call", 
                    "event.description" : "hello world" }, null);
GLib.usleep (2000000);

Lua

hello_world.lua
#!/usr/bin/lua
lgi = require 'lgi'
GLib = lgi.require('GLib')
GSound = lgi.require('GSound')

hello = GSound.Context()
hello:play_simple({ [GSound.ATTR_EVENT_ID] = "phone-incoming-call",
                    [GSound.ATTR_EVENT_DESCRIPTION] = "hello world" })
GLib.usleep (2000000)

Perl

hello_world.pl
#!/usr/bin/perl
use Glib::Object::Introspection;
Glib::Object::Introspection->setup (
	basename => 'GSound',
	version => '1.0',
	package => 'GSound');
my $hello = GSound::Context->new;
$hello->play_simple({ "event.id" => "phone-incoming-call",
                      "event.description" => "hello world" });
sleep (2);

Python

hello_world.py
#!/usr/bin/python
import gi
gi.require_version('GSound', '1.0')
from gi.repository import GLib, GSound

hello = GSound.Context()
hello.init()
hello.play_simple({ GSound.ATTR_EVENT_ID : "phone-incoming-call",
                    GSound.ATTR_EVENT_DESCRIPTION : "hello world" })
GLib.usleep (2000000);

Ruby

hello_world.rb
#!/usr/bin/ruby
require 'gir_ffi'
GirFFI.setup :GSound
Hello = GSound::Context.new
Hello.play_simple("event.id" => "phone-incoming-call", 
                  "event.description" => "hello world")
sleep (2)

Vala

  • Dependency: libcanberra
  • Makedependency: vala
  • Build with: valac --pkg libcanberra hello_world.vala
hello_world.vala
using Canberra;
public class HelloWorld {
	static void main () {
	Context hello;
	Context.create(out hello);
	hello.play (0,
		PROP_EVENT_ID, "phone-incoming-call",
		PROP_EVENT_DESCRIPTION, "hello world");
	Thread.usleep (2000000);
	}
}
  • Dependency: gsound
  • Makedependency: vala
  • Build with: valac --pkg gsound hello_world.vala
hello_world.vala
using GSound;
public class HelloWorld {
	static void main () {
	var hello = new GSound.Context();
	hello.init();
        hello.play_simple(null,
		GSound.Attribute.EVENT_ID, "phone-incoming-call",
		GSound.Attribute.EVENT_DESCRIPTION, "hello world");
	Thread.usleep (2000000);
	}
}

See also