@@ -10,26 +10,26 @@ Sink::Sink(QString filename, int codec2_mode, int natural, bool save_uncompresse
10
10
{
11
11
qDebug ()<<" Sink::Sink()" ;
12
12
13
- save_uncompressed_pcm_too= save_uncompressed_pcm_too_;
14
- failed= false ;
15
- laststatusmsg= " " ;
16
- bufremptr= 0 ;
13
+ save_uncompressed_pcm_too = save_uncompressed_pcm_too_;
14
+ failed = false ;
15
+ laststatusmsg = " " ;
16
+ bufremptr = 0 ;
17
17
mode = 0 ;
18
- done= false ;
19
- mode= codec2_mode;
18
+ done = false ;
19
+ mode = codec2_mode;
20
20
21
21
codec2 = codec2_create (mode);
22
- if (codec2== NULL )
22
+ if (codec2 == NULL )
23
23
{
24
- done= true ;
24
+ done = true ;
25
25
qDebug ()<<" cant create codec2. maybe an unsupported mode?" ;
26
- laststatusmsg= " cant create codec2. maybe an unsupported mode?" ;
27
- failed= true ;
26
+ laststatusmsg = " cant create codec2. maybe an unsupported mode?" ;
27
+ failed = true ;
28
28
return ;
29
29
}
30
30
nsam = codec2_samples_per_frame (codec2);
31
31
nbit = codec2_bits_per_frame (codec2);
32
- buf = (short *)malloc (nsam* sizeof (short ));
32
+ buf = (short *)malloc (nsam * sizeof (short ));
33
33
nbyte = (nbit + 7 ) / 8 ;
34
34
bits = (unsigned char *)malloc (nbyte*sizeof (char ));
35
35
codec2_set_natural_or_gray (codec2, !natural);
@@ -38,22 +38,22 @@ Sink::Sink(QString filename, int codec2_mode, int natural, bool save_uncompresse
38
38
file->setFileName (filename);
39
39
if (!file->open (QIODevice::WriteOnly))
40
40
{
41
- done= true ;
41
+ done = true ;
42
42
qDebug ()<<" cant open file" ;
43
- laststatusmsg= " cant open file for saving" ;
44
- failed= true ;
43
+ laststatusmsg = " cant open file for saving" ;
44
+ failed = true ;
45
45
return ;
46
46
}
47
47
48
48
file_pcm->setFileName (filename+" .wav" );
49
49
if (save_uncompressed_pcm_too)
50
50
{
51
- if (!file_pcm->open (QIODevice::WriteOnly))
51
+ if (!file_pcm->open (QIODevice::WriteOnly))
52
52
{
53
- done= true ;
53
+ done = true ;
54
54
qDebug ()<<" cant open file" ;
55
- laststatusmsg= " cant open uncompressed pcm file for saving" ;
56
- failed= true ;
55
+ laststatusmsg = " cant open uncompressed pcm file for saving" ;
56
+ failed = true ;
57
57
return ;
58
58
}
59
59
else writeWavHeader ();// make space
@@ -62,9 +62,9 @@ Sink::Sink(QString filename, int codec2_mode, int natural, bool save_uncompresse
62
62
63
63
Sink::~Sink ()
64
64
{
65
- if (codec2)codec2_destroy (codec2);
66
- if (buf)free (buf);
67
- if (bits)free (bits);
65
+ if (codec2) codec2_destroy (codec2);
66
+ if (buf) free (buf);
67
+ if (bits) free (bits);
68
68
qDebug ()<<" Sink::~Sink()" ;
69
69
}
70
70
@@ -78,7 +78,7 @@ void Sink::stop()
78
78
qDebug ()<<" Sink::stop()" ;
79
79
close ();
80
80
file->close ();
81
- if (save_uncompressed_pcm_too)writeWavHeader ();// fill file sz
81
+ if (save_uncompressed_pcm_too) writeWavHeader ();// fill file sz
82
82
file_pcm->close ();
83
83
}
84
84
@@ -92,50 +92,62 @@ qint64 Sink::readData(char *data, qint64 len)
92
92
93
93
qint64 Sink::writeData (const char *data, qint64 len)
94
94
{
95
+ int i, n, val, bufptr, bytestoread, maxval, minval, db;
96
+ qint64 br, dl;
95
97
96
- if (failed)emit ChannelFailed ();
98
+ if (failed) emit ChannelFailed ();
97
99
98
- if (done)
100
+ if (done)
99
101
{
100
102
QTimer::singleShot (0 ,this ,SLOT (stop ()));
101
103
return 0 ;
102
104
}
103
105
104
106
// emit max volume in %
105
107
const short *ptr = reinterpret_cast <const short *>(data);
106
- int maxval=0 ;
107
- for (int i=0 ; i<len/sizeof (short ); i++)
108
+ maxval = 0 ;
109
+ minval = 32767 ;
110
+ n = (int )(len / sizeof (short ));
111
+ for (i = 0 ; i < n; i++)
108
112
{
109
- int val=abs ((int )(*ptr));
110
- if (val>maxval)maxval=val;
113
+ val = abs ((int )(*ptr));
114
+ maxval = (val > maxval) ? val : maxval;
115
+ minval = (val < minval) ? val : minval;
111
116
ptr++;
112
117
}
113
- emit signal_volume (100 * maxval/ 32767 );
118
+ emit signal_volume (( 100 * maxval) / 32767 );
114
119
115
- if (save_uncompressed_pcm_too)file_pcm->write (data,len);
120
+ if (save_uncompressed_pcm_too) file_pcm->write (data,len);
116
121
117
122
// fill "buf" till we have enough to decode then write to file
118
- for ( int bufptr= 0 ; bufptr< len;)
123
+ for ( bufptr = 0 ; bufptr < len;)
119
124
{
125
+ br = (qint64)(sizeof (short ) * nsam - bufremptr);
126
+ dl = len - bufptr;
127
+ bytestoread = qMin (br, dl);// how much can we read?
120
128
121
- int bytestoread=qMin (((qint64)(sizeof (short )*nsam-bufremptr)),len-bufptr);// how much can we read?
122
-
123
- if (bytestoread!=((qint64)(sizeof (short )*nsam-bufremptr)))// not enough? then save for later
129
+ db = bufremptr / sizeof (short );
130
+ if (bytestoread != br)// not enough? then save for later
124
131
{
125
- memcpy (buf+bufremptr/sizeof (short ), data+bufptr,bytestoread);
126
- bufremptr+=bytestoread;
127
- bufptr+=bytestoread;
132
+ memcpy (buf + db, data + bufptr, bytestoread);
133
+
134
+ bufremptr += bytestoread;
135
+ bufptr += bytestoread;
128
136
break ;
129
137
}
130
138
else // enough? then decode
131
139
{
132
- memcpy (buf+bufremptr/sizeof (short ), data+bufptr, bytestoread);
133
- codec2_encode (codec2,bits,buf);
134
- file->write ((char *)bits,nbyte);
135
- bufremptr=0 ;
140
+ memcpy (buf + db, data + bufptr, bytestoread);
141
+ // trim begin clipping
142
+ if (minval < 32767 )
143
+ {
144
+ codec2_encode (codec2, bits, buf);
145
+ file->write ((char *)bits, nbyte);
146
+ }
147
+ bufremptr = 0 ;
136
148
}
137
149
138
- bufptr+= bytestoread;
150
+ bufptr += bytestoread;
139
151
}
140
152
141
153
return len;
@@ -159,7 +171,7 @@ void Sink::writeWavHeader()
159
171
out << quint16 (1 ); // data format (1 => PCM)
160
172
out << quint16 (1 );
161
173
out << quint32 (8000 );
162
- out << quint32 (8000 * 1 * 16 / 8 ); // bytes per second
174
+ out << quint32 (8000 * 1 * 16 / 8 ); // bytes per second
163
175
out << quint16 (1 * 16 / 8 ); // Block align
164
176
out << quint16 (16 ); // Significant Bits Per Sample
165
177
0 commit comments