summaryrefslogtreecommitdiff
path: root/core/applets/vmemo/vmemo.cpp
authorllornkcor <llornkcor>2002-02-14 01:39:21 (UTC)
committer llornkcor <llornkcor>2002-02-14 01:39:21 (UTC)
commita3b4e55734b9a1d2b1f2f7278caa165138dd2aa2 (patch) (unidiff)
tree4119fd1cccfa4c5fa8bd865ff25f2c66103da2f1 /core/applets/vmemo/vmemo.cpp
parentd804cef9c9ef7f5c4831370cead1392b64a17579 (diff)
downloadopie-a3b4e55734b9a1d2b1f2f7278caa165138dd2aa2.zip
opie-a3b4e55734b9a1d2b1f2f7278caa165138dd2aa2.tar.gz
opie-a3b4e55734b9a1d2b1f2f7278caa165138dd2aa2.tar.bz2
ok.. now i REALLY moved vmemo..
Diffstat (limited to 'core/applets/vmemo/vmemo.cpp') (more/less context) (ignore whitespace changes)
-rw-r--r--core/applets/vmemo/vmemo.cpp360
1 files changed, 360 insertions, 0 deletions
diff --git a/core/applets/vmemo/vmemo.cpp b/core/applets/vmemo/vmemo.cpp
new file mode 100644
index 0000000..6f04c66
--- a/dev/null
+++ b/core/applets/vmemo/vmemo.cpp
@@ -0,0 +1,360 @@
1/**************************************************************************************94x78**
2**
3** This file may be distributed and/or modified under the terms of the
4** GNU General Public License version 2 as published by the Free Software
5** Foundation and appearing in the file LICENSE.GPL included in the
6** packaging of this file.
7**
8** This file is provided AS IS with NO WARRANTY OF ANY KIND, INCLUDING THE
9** WARRANTY OF DESIGN, MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE.
10**
11*********************************************************************************************/
12
13/*
14 * $Id$
15 */
16
17#include <sys/time.h>
18#include <sys/types.h>
19#include <unistd.h>
20#include <stdio.h>
21#include <sys/stat.h>
22#include <fcntl.h>
23#include <sys/ioctl.h>
24#include <linux/soundcard.h>
25#include <string.h>
26#include <stdlib.h>
27
28typedef struct _waveheader {
29 u_long main_chunk;/* 'RIFF' */
30 u_long length; /* filelen */
31 u_long chunk_type;/* 'WAVE' */
32
33 u_long sub_chunk;/* 'fmt ' */
34 u_long sc_len; /* length of sub_chunk, =16 (chunckSize) */
35 u_short format; /* should be 1 for PCM-code (formatTag) */
36 u_short modus; /* 1 Mono, 2 Stereo (channels) */
37 u_long sample_fq;/* samples per second (samplesPerSecond) */
38 u_longbyte_p_sec; /* avg bytes per second (avgBytePerSecond) */
39 u_short byte_p_spl;/* samplesize; 1 or 2 bytes (blockAlign) */
40 u_short bit_p_spl;/* 8, 12 or 16 bit (bitsPerSample) */
41
42 u_long data_chunk;/* 'data' */
43 u_long data_length;/* samplecount */
44} WaveHeader;
45
46 #define RIFF 0x46464952
47 #define WAVE 0x45564157
48 #define FMT 0x20746D66
49 #define DATA 0x61746164
50 #define PCM_CODE1
51 #define WAVE_MONO1
52 #define WAVE_STEREO2
53
54#include "vmemo.h"
55
56#include <qpe/qpeapplication.h>
57#include <qpe/resource.h>
58#include <qpe/config.h>
59
60#include <qpainter.h>
61#include <qdatetime.h>
62#include <qsound.h>
63#include <qfile.h>
64
65int seq = 0;
66
67/* XPM */
68static char * vmemo_xpm[] = {
69"14 14 47 1",
70" c None",
71". c #101010",
72"+ c #0D0D0D",
73"@ c #0B0B0B",
74"# c #393939",
75"$ c #6C6C6C",
76"% c #5D5D5D",
77"& c #4E4E4E",
78"* c #1C1C1C",
79"= c #131313",
80"- c #7B7B7B",
81"; c #303030",
82"> c #383838",
83", c #3F3F3F",
84"' c #343434",
85") c #060606",
86"! c #444444",
87"~ c #646464",
88"{ c #505050",
89"] c #717171",
90"^ c #1B1B1B",
91"/ c #585858",
92"( c #4C4C4C",
93"_ c #353535",
94": c #0E0E0E",
95"< c #080808",
96"[ c #262626",
97"} c #121212",
98"| c #7F7F7F",
99"1 c #464646",
100"2 c #0C0C0C",
101"3 c #727272",
102"4 c #292929",
103"5 c #656565",
104"6 c #565656",
105"7 c #434343",
106"8 c #272727",
107"9 c #0F0F0F",
108"0 c #3A3A3A",
109"a c #090909",
110"b c #535353",
111"c c #545454",
112"d c #494949",
113"e c #7A7A7A",
114"f c #202020",
115"g c #3D3D3D",
116"h c #1F1F1F",
117" .+@ ",
118" #$%&* ",
119" =-;>,')",
120" .$;!~,)",
121" ;#{]!)",
122" ^~/(_)",
123" ./:@<[)",
124" }. .|]1;;2 ",
125" #-$;^/3&;;4@ ",
126".$;;#5:67;89 ",
127":%;0%&ab;8. ",
128"@cd%e!fg49 ",
129" h0,!_;2@ ",
130" ))))) "};
131
132VMemo::VMemo( QWidget *parent, const char *name )
133 : QWidget( parent, name )
134{
135 setFixedHeight( 18 );
136 setFixedWidth( 14 );
137
138 recording = FALSE;
139}
140
141VMemo::~VMemo()
142{
143}
144
145void VMemo::paintEvent( QPaintEvent* )
146{
147 QPainter p(this);
148 p.drawPixmap( 0, 1,( const char** ) vmemo_xpm );
149}
150
151void VMemo::mousePressEvent( QMouseEvent * )
152{
153 qWarning("VMemo::mousePress()");
154 QSound::play(Resource::findSound("vmemob"));
155
156 recording = TRUE;
157 qWarning("VMemo::mousePress() -> Starting to record");
158 if (openDSP() == -1)
159 {
160 // ### Display an error box
161 qWarning("VMemo::mousePress() -> DSP error");
162 recording = FALSE;
163 return;
164 }
165
166 Config vmCfg("VMemo");
167 vmCfg.setGroup("Defaults");
168
169 QDateTime dt = QDateTime::currentDateTime();
170
171#if defined(QT_QWS_EBX) //if zaurus
172 QString fileName(vmCfg.readEntry("Dir", "/mnt/cf/")); // zaurus does not have /mnt/ramfs
173#else
174 QString fileName(vmCfg.readEntry("Dir", "/mnt/ramfs/"));
175#endif
176
177 fileName += "vm_";
178 fileName += dt.toString();
179 fileName += ".wav";
180
181 // No spaces in the filename
182 fileName.replace(QRegExp("'"),"");
183 fileName.replace(QRegExp(" "),"_");
184 fileName.replace(QRegExp(":"),".");
185 fileName.replace(QRegExp(","),"");
186
187 if(openWAV(fileName.latin1()) == -1)
188 {
189 // ### Display an error box
190 qWarning("VMemo::mousePress() -> WAV error");
191 close(dsp);
192 return;
193 }
194
195 QArray<int> cats(1);
196 cats[0] = vmCfg.readNumEntry("Category", 0);
197
198 QString dlName("vm_");
199 dlName += dt.toString();
200 DocLnk l;
201 l.setFile(fileName);
202 l.setName(dlName);
203 l.setType("audio/x-wav");
204 l.setCategories(cats);
205 l.writeLink();
206
207 record();
208}
209
210void VMemo::mouseReleaseEvent( QMouseEvent * )
211{
212 qWarning("VMemo::mouseRelese() -> Done recording");
213 recording = FALSE;
214 QSound::play(Resource::findSound("vmemoe"));
215}
216
217int VMemo::openDSP()
218{
219 Config cfg("Sound");
220 cfg.setGroup("Record");
221
222 speed = cfg.readNumEntry("SampleRate", 11025);
223 channels = cfg.readNumEntry("Stereo", 1) ? 2 : 1; // 1 = stereo(2), 0 = mono(1)
224 if (cfg.readNumEntry("SixteenBit", 1))
225 {
226 format = AFMT_S16_LE;
227 resolution = 16;
228 }
229 else
230 {
231 format = AFMT_U8;
232 resolution = 8;
233 }
234
235#if defined(QT_QWS_EBX) //if zaurus
236 //dsp = open("/dev/dsp1", O_RDONLY); //on the Zaurus this needs to be /dev/dsp1 !!!!
237 dsp = open("/dev/dsp1", O_RDWR); //on the Zaurus this needs to be /dev/dsp1 !!!!
238#else
239 //dsp = open("/dev/dsp", O_RDONLY);
240 dsp = open("/dev/dsp", O_RDWR);
241#endif
242
243 qWarning("speed = %i", speed);
244
245 if(dsp == -1)
246 {
247 perror("open(\"/dev/dsp\")");
248 return -1;
249 }
250
251 if(ioctl(dsp, SNDCTL_DSP_SETFMT , &format)==-1) {
252 perror("ioctl(\"SNDCTL_DSP_SETFMT\")");
253 return -1;
254 }
255 if(ioctl(dsp, SNDCTL_DSP_CHANNELS , &channels)==-1) {
256 perror("ioctl(\"SNDCTL_DSP_CHANNELS\")");
257 return -1;
258 }
259 if(ioctl(dsp, SNDCTL_DSP_SPEED , &speed)==-1) {
260 perror("ioctl(\"SNDCTL_DSP_SPEED\")");
261 return -1;
262 }
263 if(ioctl(dsp, SOUND_PCM_READ_RATE , &rate)==-1) {
264 perror("ioctl(\"SOUND_PCM_READ_RATE\")");
265 return -1;
266 }
267 qWarning("speed = %i", speed);
268
269 return 1;
270}
271
272int VMemo::openWAV(const char *filename)
273{
274 char buffer[256];
275
276 qDebug("Creating %s ",filename);
277 track.setName(filename);
278 if(!track.open(IO_WriteOnly|IO_Truncate|IO_Raw))
279 {
280 qDebug("Could not open file");
281 return -1;
282 }
283 wav=track.handle();
284
285 WaveHeader wh;
286
287 /*
288 wh.main_chunk = RIFF;
289 wh.length = 0;
290 wh.chunk_type = WAVE;
291 wh.sub_chunk = FMT;
292 wh.sc_len = 16;
293 wh.format = PCM_CODE;
294 wh.modus = channels;
295 wh.sample_fq = speed;
296 wh.byte_p_spl = ((resolution == 8) ? 1 : 2) * (channels ? 2 : 1);
297 wh.byte_p_sec = resolution; //speed * wh.modus * wh.byte_p_spl;
298 wh.bit_p_spl = resolution;
299 wh.data_chunk = DATA;
300 wh.data_length= 0;
301 */
302
303 wh.main_chunk = RIFF;
304 wh.length = 0;
305 wh.chunk_type = WAVE;
306
307 wh.sub_chunk = FMT;
308 wh.sc_len = 16;
309 wh.format = PCM_CODE;
310 wh.modus = channels;
311 wh.sample_fq = speed;
312 wh.bit_p_spl = resolution;
313 wh.byte_p_sec = wh.sample_fq * wh.bit_p_spl;
314 wh.byte_p_spl = channels * (wh.bit_p_spl % 8);
315
316 qWarning("channels = %i\n"
317 "samplesPerSecond = %i\n"
318 "avgBytesPerSecond = %i\n"
319 "blockAlign = %i\n"
320 "bitsPerSecond = %i\n", wh.modus, wh.sample_fq, wh.byte_p_sec, wh.byte_p_spl, wh.bit_p_spl);
321
322 wh.data_chunk = DATA;
323 wh.data_length= 0;
324
325 write (wav, &wh, sizeof(WaveHeader));
326
327 return 1;
328}
329
330void VMemo::record(void)
331{
332 int length=0, result, value;
333 char sound[8192];
334
335 qWarning("VMemo::record()");
336
337 while(recording)
338 {
339 result = read(dsp, sound, 8192);
340 write(wav, sound, result);
341 length += result;
342 qApp->processEvents();
343 }
344
345 qWarning("VMemo::record() -> Done recording");
346 qWarning("VMemo::record() -> Closing dsp");
347
348 value = length+36;
349 lseek(wav, 4, SEEK_SET);
350 write(wav, &value, 4);
351 lseek(wav, 40, SEEK_SET);
352 write(wav, &length, 4);
353 track.close();
354
355 if( ioctl( dsp, SNDCTL_DSP_RESET,0) == -1)// ); //tell driver to stop for a while
356 perror("ioctl(\"SNDCTL_DSP_RESET\")");
357 ::close(dsp);
358
359 qWarning("VMemo::record() -> terminating");
360}