~ubuntu-branches/ubuntu/gutsy/kdebase-workspace/gutsy

« back to all changes in this revision

Viewing changes to khotkeys/arts/soundrecorder_arts.cpp

  • Committer: Bazaar Package Importer
  • Author(s): Jonathan Riddell
  • Date: 2007-09-05 20:45:14 UTC
  • Revision ID: james.westby@ubuntu.com-20070905204514-632hhspl0nvrc84i
Tags: upstream-3.93.0
ImportĀ upstreamĀ versionĀ 3.93.0

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
/***************************************************************************
 
2
 *   Copyright (C) 2005 by Olivier Goffart   *
 
3
 *   ogoffart@kde.org   *
 
4
 *                                                                         *
 
5
 *   This program is free software; you can redistribute it and/or modify  *
 
6
 *   it under the terms of the GNU General Public License as published by  *
 
7
 *   the Free Software Foundation; either version 2 of the License, or     *
 
8
 *   (at your option) any later version.                                   *
 
9
 *                                                                         *
 
10
 *   This program is distributed in the hope that it will be useful,       *
 
11
 *   but WITHOUT ANY WARRANTY; without even the implied warranty of        *
 
12
 *   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the         *
 
13
 *   GNU General Public License for more details.                          *
 
14
 *                                                                         *
 
15
 *   You should have received a copy of the GNU General Public License     *
 
16
 *   along with this program; if not, write to                             *
 
17
 *   the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,  *
 
18
 *   Boston, MA 02110-1301, USA.                                           *
 
19
 ***************************************************************************/
 
20
 
 
21
#include <config-workspace.h>
 
22
 
 
23
#ifdef HAVE_ARTS
 
24
 
 
25
#include "soundrecorder_arts.h"
 
26
 
 
27
#include <arts/kaudiorecordstream.h>
 
28
#include <arts/kartsserver.h>
 
29
#include <arts/kartsdispatcher.h>
 
30
 
 
31
#include <kdebug.h>
 
32
#include <klocale.h>
 
33
#include <qtimer.h>
 
34
 
 
35
 
 
36
#define FS 11025
 
37
#define BITS 16
 
38
 
 
39
#define ABS(X) ( ((X)>0) ? (X) : -(X) )
 
40
 
 
41
extern "C"
 
42
KDE_EXPORT
 
43
KHotKeys::SoundRecorder* khotkeys_soundrecorder_create( QObject* parent, const char* name )
 
44
{
 
45
    return new KHotKeys::SoundRecorderArts( parent, name );
 
46
}
 
47
 
 
48
namespace KHotKeys
 
49
{
 
50
 
 
51
 
 
52
SoundRecorderArts::SoundRecorderArts(QObject *parent, const char *name)
 
53
        : SoundRecorder(parent, name) ,
 
54
          m_dis(new KArtsDispatcher( this) ),
 
55
          m_server( new KArtsServer( this ) ) ,
 
56
          m_recStream( new KAudioRecordStream( m_server, i18n("khotkeys"), m_server ) )
 
57
{
 
58
        create_ptr check = khotkeys_soundrecorder_create; // check the type matches
 
59
        ( void ) check;
 
60
 
 
61
        m_recStream->usePolling( false );
 
62
        connect( m_recStream, SIGNAL(data (QByteArray &)), this, SLOT(slotDataReceived(QByteArray& )));
 
63
}
 
64
 
 
65
SoundRecorderArts::~SoundRecorderArts()
 
66
{
 
67
        delete m_recStream;
 
68
        delete m_server;
 
69
        delete m_dis;
 
70
}
 
71
 
 
72
void SoundRecorderArts::start()
 
73
{
 
74
        m_recStream->start(FS,BITS,2);
 
75
        m_data.resize(0);
 
76
}
 
77
 
 
78
void SoundRecorderArts::stop()
 
79
{
 
80
        m_recStream->stop();
 
81
        QTimer::singleShot(400,this,SLOT(slotEmitSignal()));
 
82
}
 
83
 
 
84
void SoundRecorderArts::abort()
 
85
{
 
86
        m_recStream->stop();
 
87
        m_data.resize(0);
 
88
}
 
89
 
 
90
 
 
91
Sound SoundRecorderArts::sound()
 
92
{
 
93
        Sound s;
 
94
        uint BytePS=BITS/8;
 
95
        uint length=m_data.size()/BytePS;
 
96
        QMemArray<Q_INT32> da(length);
 
97
        s.max=0;
 
98
        s._fs=FS;
 
99
        for(uint f=0;f<length; f++)
 
100
        {
 
101
#if BITS==8
 
102
                int nb=(unsigned char)(m_data[f])  -128;
 
103
#elif BITS==16
 
104
                int nb=(m_data[2*f] &0x000000FF )  |  ( (m_data[2*f+1] &0x000000FF ) << 8 )    ;
 
105
                if(nb & (1<< 15)) 
 
106
                        nb = nb-(1<<16);
 
107
#else
 
108
        #error  BITS is not 16 or 8
 
109
#endif
 
110
                if(s.max < (uint)ABS(nb))
 
111
                {
 
112
                        s.max= (uint)ABS(nb);
 
113
                }
 
114
                da[f]=nb;
 
115
        }
 
116
        s.data=da;
 
117
        return s;
 
118
}
 
119
 
 
120
void SoundRecorderArts::slotDataReceived(QByteArray & data)
 
121
{
 
122
        uint pos=m_data.size();
 
123
        m_data.resize(pos + data.size());
 
124
        for(uint f=0;f<data.size(); f++)
 
125
        {
 
126
                m_data[pos+f]=data[f];
 
127
        }
 
128
}
 
129
 
 
130
void SoundRecorderArts::slotEmitSignal()
 
131
{
 
132
        emit recorded( sound() );
 
133
}
 
134
 
 
135
}
 
136
 
 
137
#include "soundrecorder_arts.moc"
 
138
 
 
139
#endif