~ubuntu-branches/ubuntu/quantal/linpsk/quantal

« back to all changes in this revision

Viewing changes to linpsk/waveinput.cpp

  • Committer: Bazaar Package Importer
  • Author(s): Bruce Walker
  • Date: 2002-02-06 11:43:38 UTC
  • Revision ID: james.westby@ubuntu.com-20020206114338-xqmjmhh01lpjm0g4
Tags: upstream-0.6.2
ImportĀ upstreamĀ versionĀ 0.6.2

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
/***************************************************************************
 
2
                          waveinput.cpp  -  description
 
3
                             -------------------
 
4
    begin                : Sat May 5 2001
 
5
    copyright            : (C) 2001 by Volker Schroer
 
6
    email                : DL1KSV@gmx.de
 
7
 ***************************************************************************/
 
8
 
 
9
/***************************************************************************
 
10
 *                                                                         *
 
11
 *   This program is free software; you can redistribute it and/or modify  *
 
12
 *   it under the terms of the GNU General Public License as published by  *
 
13
 *   the Free Software Foundation; either version 2 of the License, or     *
 
14
 *   (at your option) any later version.                                   *
 
15
 *    based on the work of  Moe Wheatly, AE4JY                             *  
 
16
 ***************************************************************************/
 
17
 
 
18
#include "waveinput.h"
 
19
 
 
20
WaveInput::WaveInput(int ptt = -1): Input(ptt)
 
21
{
 
22
}
 
23
WaveInput::~WaveInput(){
 
24
}
 
25
 
 
26
/** Opens the Device for writting, for Wavefiles this means write nothing ! */
 
27
bool WaveInput::open_Device_write(QString Device)
 
28
{
 
29
return true;
 
30
}
 
31
 
 
32
/** puts the Samples onto the Device, for a Wavemode Device nothing happens */
 
33
int WaveInput::putSamples(double *sample,int anzahl)
 
34
{
 
35
return anzahl;
 
36
}
 
37
        
 
38
/** Dummy */
 
39
void WaveInput::PTT(bool mode)
 
40
{
 
41
}
 
42
/** Dummy */
 
43
bool WaveInput::setParams(QString *errorstring)
 
44
{
 
45
return true;
 
46
}
 
47
 
 
48
 /** Opens the Device named Device for reading */
 
49
 
 
50
bool WaveInput::open_Device_read(QString Device)
 
51
{
 
52
char header[5];
 
53
char c;
 
54
QString s;
 
55
int i;
 
56
fd = open(Device,O_RDONLY);
 
57
if (fd < 0)
 
58
        return false;
 
59
// Check for RIFF
 
60
i =read(fd,&header[0],4*sizeof(header[0]));
 
61
if ( i != 4)
 
62
        {
 
63
                close(fd);
 
64
                qWarning("Header mismatch\n");
 
65
                return false;
 
66
        }
 
67
header[4]=0;    
 
68
s=QString(header);
 
69
 
 
70
if  ( s != "RIFF")
 
71
        {
 
72
                close(fd);
 
73
                qWarning(Device + " is not a RIFF File\n");
 
74
                return false;
 
75
        }
 
76
//Check for Wave
 
77
lseek(fd,8,SEEK_SET);
 
78
 
 
79
i =read(fd,&header[0],4*sizeof(header[0]));
 
80
header[4]=0;
 
81
if ( i != 4)
 
82
        {
 
83
                close(fd);
 
84
                qWarning("SubType mismatch\n");
 
85
                return false;
 
86
        }
 
87
s=QString(header);
 
88
 
 
89
if  ( s != "WAVE")
 
90
        {
 
91
                close(fd);
 
92
                qWarning(Device + " is not a WAVE File\n");
 
93
                return false;
 
94
        }
 
95
// Now find data
 
96
 
 
97
i=read(fd,&header[0],4*sizeof(header[0]));
 
98
if ( i != 4)            
 
99
        {
 
100
                close(fd);
 
101
                        qWarning("No data found\n");
 
102
        return false;
 
103
        }
 
104
header[5]=0;
 
105
 
 
106
s=QString(header);
 
107
while( ( s != "data" ) && (i > 0) )
 
108
        {
 
109
                i = read (fd,&c,1);
 
110
                s.remove(0,1);  // remove first character
 
111
                s += c;                                 // append new one
 
112
        }               
 
113
if ( s == "data")
 
114
        {               // got it
 
115
                offset = lseek(fd,4,SEEK_CUR);          
 
116
                lseek(fd,offset,SEEK_SET); //Remove 4 bytes length info
 
117
                return true;
 
118
        }
 
119
else
 
120
        {                                                               //no data found
 
121
                close(fd);
 
122
                        qWarning("No data found\n");
 
123
        return false;
 
124
        }
 
125
        
 
126
}
 
127
 
 
128
int WaveInput::getSamples(double *sample,int anzahl)
 
129
{
 
130
unsigned char c[2];
 
131
int i,j;
 
132
j=0;
 
133
double x;
 
134
while (j<anzahl)
 
135
 {
 
136
 
 
137
  i= read(fd,&c[0],2*sizeof(c[0]));
 
138
  if (i == 2)
 
139
        {
 
140
                        if ( c[1] < 128 )
 
141
                        i =  c[0]+ c[1]* 256 ;
 
142
                        else
 
143
                        i = ( c[1]- 256) *256 +c[0];
 
144
                x= i;
 
145
                        *(sample++)= x/8.;
 
146
                        
 
147
                        j++;
 
148
                }               
 
149
   else
 
150
 
 
151
                
 
152
    lseek(fd,offset,SEEK_SET);
 
153
                
 
154
 } // End while
 
155
return j;
 
156
 
 
157
}