Skip to content

Commit cee8f3e

Browse files
committed
1.2: fix jontio#1: trim begin clipping
1 parent 4285060 commit cee8f3e

File tree

5 files changed

+73
-60
lines changed

5 files changed

+73
-60
lines changed

src/mainwindow.cpp

Lines changed: 14 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -82,8 +82,8 @@ int MainWindow::select_mode()
8282
void MainWindow::on_recButton_clicked()
8383
{
8484
on_stopButton_clicked();
85-
if(m_audioInput)m_audioInput->deleteLater();
86-
if(m_sink)m_sink->deleteLater();
85+
if (m_audioInput) m_audioInput->deleteLater();
86+
if (m_sink) m_sink->deleteLater();
8787

8888
int mode = select_mode();
8989

@@ -116,14 +116,14 @@ void MainWindow::on_recButton_clicked()
116116
void MainWindow::on_playButton_clicked()
117117
{
118118
on_stopButton_clicked();
119-
if(m_audioOutput)m_audioOutput->deleteLater();
120-
if(m_source)m_source->deleteLater();
119+
if (m_audioOutput) m_audioOutput->deleteLater();
120+
if (m_source) m_source->deleteLater();
121121

122122
int mode = select_mode();
123123

124-
int natural=1;
125-
if(ui->encodingcomboBox->currentText()=="Natural") natural=1;
126-
if(ui->encodingcomboBox->currentText()=="Gray") natural=0;
124+
int natural = 1;
125+
// if (ui->encodingcomboBox->currentText()=="Natural") natural = 1;
126+
if (ui->encodingcomboBox->currentText()=="Gray") natural = 0;
127127

128128
//setup
129129
m_device_out=QAudioDeviceInfo::defaultOutputDevice();
@@ -141,17 +141,18 @@ void MainWindow::on_playButton_clicked()
141141
m_audioOutput->start(m_source);
142142

143143
ui->statusBar->clearMessage();
144-
if(!m_source->laststatusmsg.isEmpty())ui->statusBar->showMessage("Play: "+m_source->laststatusmsg);
144+
if (!m_source->laststatusmsg.isEmpty())
145+
ui->statusBar->showMessage("Play: "+m_source->laststatusmsg);
145146

146-
if(m_source->failed)on_stopButton_clicked();
147+
if (m_source->failed) on_stopButton_clicked();
147148
}
148149

149150
void MainWindow::on_stopButton_clicked()
150151
{
151-
if(m_audioOutput)m_audioOutput->stop();
152-
if(m_source)m_source->stop();
153-
if(m_audioInput)m_audioInput->stop();
154-
if(m_sink)m_sink->stop();
152+
if (m_audioOutput) m_audioOutput->stop();
153+
if (m_source) m_source->stop();
154+
if (m_audioInput) m_audioInput->stop();
155+
if (m_sink) m_sink->stop();
155156
max_mic_volume(0);
156157
ui->progressBar->setFormat(" Volume/Progress");
157158
ui->recButton->setEnabled(true);

src/mainwindow.ui

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -23,10 +23,10 @@
2323
</size>
2424
</property>
2525
<property name="windowTitle">
26-
<string>JC2Rec v1.1</string>
26+
<string>JC2Rec v1.2</string>
2727
</property>
2828
<property name="windowIcon">
29-
<iconset resource="icon.qrc">
29+
<iconset resource="../icon.qrc">
3030
<normaloff>:/icons/icon.png</normaloff>:/icons/icon.png</iconset>
3131
</property>
3232
<widget class="QWidget" name="centralWidget">

src/sink.cpp

Lines changed: 55 additions & 43 deletions
Original file line numberDiff line numberDiff line change
@@ -10,26 +10,26 @@ Sink::Sink(QString filename, int codec2_mode, int natural, bool save_uncompresse
1010
{
1111
qDebug()<<"Sink::Sink()";
1212

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;
1717
mode = 0;
18-
done=false;
19-
mode=codec2_mode;
18+
done = false;
19+
mode = codec2_mode;
2020

2121
codec2 = codec2_create(mode);
22-
if(codec2==NULL)
22+
if (codec2 == NULL)
2323
{
24-
done=true;
24+
done = true;
2525
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;
2828
return;
2929
}
3030
nsam = codec2_samples_per_frame(codec2);
3131
nbit = codec2_bits_per_frame(codec2);
32-
buf = (short*)malloc(nsam*sizeof(short));
32+
buf = (short*)malloc(nsam * sizeof(short));
3333
nbyte = (nbit + 7) / 8;
3434
bits = (unsigned char*)malloc(nbyte*sizeof(char));
3535
codec2_set_natural_or_gray(codec2, !natural);
@@ -38,22 +38,22 @@ Sink::Sink(QString filename, int codec2_mode, int natural, bool save_uncompresse
3838
file->setFileName(filename);
3939
if (!file->open(QIODevice::WriteOnly))
4040
{
41-
done=true;
41+
done = true;
4242
qDebug()<<"cant open file";
43-
laststatusmsg="cant open file for saving";
44-
failed=true;
43+
laststatusmsg = "cant open file for saving";
44+
failed = true;
4545
return;
4646
}
4747

4848
file_pcm->setFileName(filename+".wav");
4949
if (save_uncompressed_pcm_too)
5050
{
51-
if(!file_pcm->open(QIODevice::WriteOnly))
51+
if (!file_pcm->open(QIODevice::WriteOnly))
5252
{
53-
done=true;
53+
done = true;
5454
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;
5757
return;
5858
}
5959
else writeWavHeader();//make space
@@ -62,9 +62,9 @@ Sink::Sink(QString filename, int codec2_mode, int natural, bool save_uncompresse
6262

6363
Sink::~Sink()
6464
{
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);
6868
qDebug()<<"Sink::~Sink()";
6969
}
7070

@@ -78,7 +78,7 @@ void Sink::stop()
7878
qDebug()<<"Sink::stop()";
7979
close();
8080
file->close();
81-
if(save_uncompressed_pcm_too)writeWavHeader();//fill file sz
81+
if (save_uncompressed_pcm_too) writeWavHeader();//fill file sz
8282
file_pcm->close();
8383
}
8484

@@ -92,50 +92,62 @@ qint64 Sink::readData(char *data, qint64 len)
9292

9393
qint64 Sink::writeData(const char *data, qint64 len)
9494
{
95+
int i, n, val, bufptr, bytestoread, maxval, minval, db;
96+
qint64 br, dl;
9597

96-
if(failed)emit ChannelFailed();
98+
if (failed) emit ChannelFailed();
9799

98-
if(done)
100+
if (done)
99101
{
100102
QTimer::singleShot(0,this,SLOT(stop()));
101103
return 0;
102104
}
103105

104106
//emit max volume in %
105107
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++)
108112
{
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;
111116
ptr++;
112117
}
113-
emit signal_volume(100*maxval/32767);
118+
emit signal_volume((100 * maxval) / 32767);
114119

115-
if(save_uncompressed_pcm_too)file_pcm->write(data,len);
120+
if (save_uncompressed_pcm_too) file_pcm->write(data,len);
116121

117122
//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;)
119124
{
125+
br = (qint64)(sizeof(short) * nsam - bufremptr);
126+
dl = len - bufptr;
127+
bytestoread = qMin(br, dl);//how much can we read?
120128

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
124131
{
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;
128136
break;
129137
}
130138
else//enough? then decode
131139
{
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;
136148
}
137149

138-
bufptr+=bytestoread;
150+
bufptr += bytestoread;
139151
}
140152

141153
return len;
@@ -159,7 +171,7 @@ void Sink::writeWavHeader()
159171
out << quint16(1); // data format (1 => PCM)
160172
out << quint16(1);
161173
out << quint32(8000);
162-
out << quint32(8000 * 1 * 16 / 8 ); // bytes per second
174+
out << quint32(8000 * 1 * 16 / 8); // bytes per second
163175
out << quint16(1 * 16 / 8); // Block align
164176
out << quint16(16); // Significant Bits Per Sample
165177

src/source.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -81,7 +81,7 @@ qint64 Source::readData(char *data, qint64 len)
8181

8282
int bufptr=0;
8383

84-
while((bufptr+sizeof(short)*nsam)<(len/2))//(len/2) rather than (len) allows smaller sound card buffer
84+
while((qint64)(bufptr+sizeof(short)*nsam) < (len/2))//(len/2) rather than (len) allows smaller sound card buffer
8585
{
8686
//qint64 bytesread=fread(bits, sizeof(char), nbyte, fin);
8787
qint64 bytesread=file->read((char*)bits, nbyte);

src/source.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,7 @@ class Source : public QIODevice
2929
qint64 bytesread_sum;
3030

3131
// FILE *fin;
32-
// FILE *fout;
32+
// FILE *fout;
3333
signals:
3434
void percentage_played(int percentage);
3535
public slots:

0 commit comments

Comments
 (0)