2 * This file is part of CameraPlus.
4 * Copyright (C) 2012 Mohammed Sameer <msameer@foolab.org>
6 * This library is free software; you can redistribute it and/or
7 * modify it under the terms of the GNU Lesser General Public
8 * License as published by the Free Software Foundation; either
9 * version 2.1 of the License, or (at your option) any later version.
11 * This library is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14 * Lesser General Public License for more details.
16 * You should have received a copy of the GNU Lesser General Public
17 * License along with this library; if not, write to the Free Software
18 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
23 #include "qtcamconfig.h"
25 #define CAMERA_IMAGE_START_SOUND_ID "camera-image-start"
26 #define CAMERA_IMAGE_END_SOUND_ID "camera-image-end"
27 #define CAMERA_VIDEO_START_SOUND_ID "camera-video-start"
28 #define CAMERA_VIDEO_STOP_SOUND_ID "camera-video-stop"
30 // TODO: video start sound should block
31 // TODO: video end sound should not appear in the video
32 // TODO: monitor pulse audio death and re-upload our samples
33 // TODO: if we are using headphones then sound volume might be loud. Detect and lower it.
34 Sounds::Sounds(QObject *parent) :
40 // No idea why but canberra will not cache without that!!!
41 setenv("CANBERRA_EVENT_LOOKUP", "1", 1);
46 ca_context_destroy(m_ctx);
51 void Sounds::setConfig(QtCamConfig *conf) {
55 void Sounds::imageCaptureStarted() {
56 if (isMuted() || !m_ctx) {
60 play(CAMERA_IMAGE_START_SOUND_ID);
63 void Sounds::imageCaptureEnded() {
64 if (isMuted() || !m_ctx) {
68 play(CAMERA_IMAGE_END_SOUND_ID);
71 void Sounds::videoRecordingStarted() {
72 if (isMuted() || !m_ctx) {
76 play(CAMERA_VIDEO_START_SOUND_ID);
79 void Sounds::videoRecordingEnded() {
80 if (isMuted() || !m_ctx) {
84 play(CAMERA_VIDEO_STOP_SOUND_ID);
87 bool Sounds::isMuted() const {
91 void Sounds::setMuted(bool mute) {
92 if (mute != m_muted) {
98 void Sounds::reload() {
100 ca_context_destroy(m_ctx);
104 if (int code = ca_context_create(&m_ctx) != CA_SUCCESS) {
105 qWarning() << "Failed to create canberra context" << ca_strerror(code) << code;
109 if (int code = ca_context_set_driver(m_ctx, "pulse") != CA_SUCCESS) {
110 qWarning() << "Failed to set canberra driver to pulse audio" << ca_strerror(code) << code;
113 if (int code = ca_context_change_props(m_ctx,
114 CA_PROP_MEDIA_ROLE, "camera-sound-effect",
115 NULL) != CA_SUCCESS) {
116 qWarning() << "Failed to set context properties" << ca_strerror(code) << code;
119 if (int code = ca_context_open(m_ctx) != CA_SUCCESS) {
120 qWarning() << "Failed to open canberra context" << ca_strerror(code) << code;
121 ca_context_destroy(m_ctx);
126 cache(m_conf->imageCaptureStartedSound(), CAMERA_IMAGE_START_SOUND_ID);
127 cache(m_conf->imageCaptureEndedSound(), CAMERA_IMAGE_END_SOUND_ID);
128 cache(m_conf->videoRecordingStartedSound(), CAMERA_VIDEO_START_SOUND_ID);
129 cache(m_conf->videoRecordingEndedSound(), CAMERA_VIDEO_STOP_SOUND_ID);
132 void Sounds::cache(const QString& path, const char *id) {
133 if (path.isEmpty()) {
137 if (int code = ca_context_cache(m_ctx,
138 CA_PROP_EVENT_ID, id,
139 CA_PROP_MEDIA_FILENAME, path.toLocal8Bit().data(),
140 CA_PROP_CANBERRA_CACHE_CONTROL, "permanent",
141 NULL) != CA_SUCCESS) {
142 qWarning() << "Failed to cache" << path << ca_strerror(code) << code;
146 void Sounds::play(const char *id) {
147 if (int code = ca_context_play(m_ctx, 0,
148 CA_PROP_CANBERRA_VOLUME, "0.0", // Odd, volume has to be a char *
149 CA_PROP_EVENT_ID, id,
150 CA_PROP_MEDIA_ROLE, "camera-sound-effect",
151 NULL) != CA_SUCCESS) {
152 qDebug() << "Failed to play sound" << ca_strerror(code) << code;