File: File.cpp

package info (click to toggle)
hamfax 0.5.1-1
  • links: PTS
  • area: main
  • in suites: woody
  • size: 416 kB
  • ctags: 441
  • sloc: cpp: 3,188; sh: 197; makefile: 124
file content (114 lines) | stat: -rw-r--r-- 2,865 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
// HamFax -- an application for sending and receiving amateur radio facsimiles
// Copyright (C) 2001 Christof Schmitt, DH1CS <cschmitt@users.sourceforge.net>
//  
// This program is free software; you can redistribute it and/or
// modify it under the terms of the GNU General Public License
// as published by the Free Software Foundation; either version 2
// of the License, or (at your option) any later version.
//
// This program is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
// GNU General Public License for more details.
//  
// You should have received a copy of the GNU General Public License
// along with this program; if not, write to the Free Software
// Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.

#include "File.hpp"
#include "Error.hpp"

File::File(QObject* parent)
	: QObject(parent), aFile(0)
{
	afSetErrorHandler(0);
	timer=new QTimer(this);
}

File::~File(void)
{
	end();
}

void File::startOutput(const QString& fileName)
{
	try {
		AFfilesetup fs;
		if((fs=afNewFileSetup())==AF_NULL_FILESETUP) {
			throw Error(tr("could not allocate AFfilesetup"));
		}
		afInitFileFormat(fs,AF_FILE_NEXTSND);
		afInitSampleFormat(fs,AF_DEFAULT_TRACK,AF_SAMPFMT_TWOSCOMP,16);
		afInitByteOrder(fs,AF_DEFAULT_TRACK,AF_BYTEORDER_BIGENDIAN);
		afInitChannels(fs,AF_DEFAULT_TRACK,1);
		afInitRate(fs,AF_DEFAULT_TRACK,8000);
		
		if((aFile=afOpenFile(fileName,"w",fs))==AF_NULL_FILEHANDLE) {
			aFile=0;
			throw Error(tr("could not open file"));
		}
		afFreeFileSetup(fs);
		timer->start(0);
		connect(timer,SIGNAL(timeout()),this,SLOT(timerSignal()));
		emit newSampleRate(8000);
	} catch(Error) {
		end();
		throw;
	}
}

void File::startInput(const QString& fileName)
{
	try {
		AFfilesetup fs=0;
		if((aFile=afOpenFile(fileName,"r",fs))==AF_NULL_FILEHANDLE) {
			throw Error(tr("could not open file"));
		}
		if(afGetFrameSize(aFile,AF_DEFAULT_TRACK,0)!=2) {
			throw Error(tr("samples size not 16 bit"));
		}
		if(afGetRate(aFile,AF_DEFAULT_TRACK)!=8000) {
			throw Error(tr("sample rate is not 8000 Hz"));
		}
		emit newSampleRate(8000);
		timer->start(0);
		connect(timer,SIGNAL(timeout()),this,SLOT(read()));
	} catch(Error) {
		end();
		throw;
	}
}

void File::end(void)
{
	timer->stop();
	disconnect(timer,SIGNAL(timeout()),this,SLOT(timerSignal()));
	if(aFile!=0) {
		afCloseFile(aFile);
		aFile=0;
		emit deviceClosed();
	}
}

void File::write(short* samples, int number)
{
	if(aFile!=0) {
		if((afWriteFrames(aFile,AF_DEFAULT_TRACK,samples,number))
		   !=blockSize) {
			end();
			}
	}
}

void File::read(void)
{
	int n=blockSize;
	short buffer[n];
	n=afReadFrames(aFile,AF_DEFAULT_TRACK,buffer,n);
	emit data(buffer,n);
}

void File::timerSignal(void)
{
	emit next(blockSize);
}