Skip to content

Commit

Permalink
1.2: fix jontio#1: trim begin clipping
Browse files Browse the repository at this point in the history
  • Loading branch information
zvezdochiot committed Sep 4, 2022
1 parent 4285060 commit cee8f3e
Show file tree
Hide file tree
Showing 5 changed files with 73 additions and 60 deletions.
27 changes: 14 additions & 13 deletions src/mainwindow.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -82,8 +82,8 @@ int MainWindow::select_mode()
void MainWindow::on_recButton_clicked()
{
on_stopButton_clicked();
if(m_audioInput)m_audioInput->deleteLater();
if(m_sink)m_sink->deleteLater();
if (m_audioInput) m_audioInput->deleteLater();
if (m_sink) m_sink->deleteLater();

int mode = select_mode();

Expand Down Expand Up @@ -116,14 +116,14 @@ void MainWindow::on_recButton_clicked()
void MainWindow::on_playButton_clicked()
{
on_stopButton_clicked();
if(m_audioOutput)m_audioOutput->deleteLater();
if(m_source)m_source->deleteLater();
if (m_audioOutput) m_audioOutput->deleteLater();
if (m_source) m_source->deleteLater();

int mode = select_mode();

int natural=1;
if(ui->encodingcomboBox->currentText()=="Natural") natural=1;
if(ui->encodingcomboBox->currentText()=="Gray") natural=0;
int natural = 1;
// if (ui->encodingcomboBox->currentText()=="Natural") natural = 1;
if (ui->encodingcomboBox->currentText()=="Gray") natural = 0;

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

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

if(m_source->failed)on_stopButton_clicked();
if (m_source->failed) on_stopButton_clicked();
}

void MainWindow::on_stopButton_clicked()
{
if(m_audioOutput)m_audioOutput->stop();
if(m_source)m_source->stop();
if(m_audioInput)m_audioInput->stop();
if(m_sink)m_sink->stop();
if (m_audioOutput) m_audioOutput->stop();
if (m_source) m_source->stop();
if (m_audioInput) m_audioInput->stop();
if (m_sink) m_sink->stop();
max_mic_volume(0);
ui->progressBar->setFormat(" Volume/Progress");
ui->recButton->setEnabled(true);
Expand Down
4 changes: 2 additions & 2 deletions src/mainwindow.ui
Original file line number Diff line number Diff line change
Expand Up @@ -23,10 +23,10 @@
</size>
</property>
<property name="windowTitle">
<string>JC2Rec v1.1</string>
<string>JC2Rec v1.2</string>
</property>
<property name="windowIcon">
<iconset resource="icon.qrc">
<iconset resource="../icon.qrc">
<normaloff>:/icons/icon.png</normaloff>:/icons/icon.png</iconset>
</property>
<widget class="QWidget" name="centralWidget">
Expand Down
98 changes: 55 additions & 43 deletions src/sink.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -10,26 +10,26 @@ Sink::Sink(QString filename, int codec2_mode, int natural, bool save_uncompresse
{
qDebug()<<"Sink::Sink()";

save_uncompressed_pcm_too=save_uncompressed_pcm_too_;
failed=false;
laststatusmsg="";
bufremptr=0;
save_uncompressed_pcm_too = save_uncompressed_pcm_too_;
failed = false;
laststatusmsg = "";
bufremptr = 0;
mode = 0;
done=false;
mode=codec2_mode;
done = false;
mode = codec2_mode;

codec2 = codec2_create(mode);
if(codec2==NULL)
if (codec2 == NULL)
{
done=true;
done = true;
qDebug()<<"cant create codec2. maybe an unsupported mode?";
laststatusmsg="cant create codec2. maybe an unsupported mode?";
failed=true;
laststatusmsg = "cant create codec2. maybe an unsupported mode?";
failed = true;
return;
}
nsam = codec2_samples_per_frame(codec2);
nbit = codec2_bits_per_frame(codec2);
buf = (short*)malloc(nsam*sizeof(short));
buf = (short*)malloc(nsam * sizeof(short));
nbyte = (nbit + 7) / 8;
bits = (unsigned char*)malloc(nbyte*sizeof(char));
codec2_set_natural_or_gray(codec2, !natural);
Expand All @@ -38,22 +38,22 @@ Sink::Sink(QString filename, int codec2_mode, int natural, bool save_uncompresse
file->setFileName(filename);
if (!file->open(QIODevice::WriteOnly))
{
done=true;
done = true;
qDebug()<<"cant open file";
laststatusmsg="cant open file for saving";
failed=true;
laststatusmsg = "cant open file for saving";
failed = true;
return;
}

file_pcm->setFileName(filename+".wav");
if (save_uncompressed_pcm_too)
{
if(!file_pcm->open(QIODevice::WriteOnly))
if (!file_pcm->open(QIODevice::WriteOnly))
{
done=true;
done = true;
qDebug()<<"cant open file";
laststatusmsg="cant open uncompressed pcm file for saving";
failed=true;
laststatusmsg = "cant open uncompressed pcm file for saving";
failed = true;
return;
}
else writeWavHeader();//make space
Expand All @@ -62,9 +62,9 @@ Sink::Sink(QString filename, int codec2_mode, int natural, bool save_uncompresse

Sink::~Sink()
{
if(codec2)codec2_destroy(codec2);
if(buf)free(buf);
if(bits)free(bits);
if (codec2) codec2_destroy(codec2);
if (buf) free(buf);
if (bits) free(bits);
qDebug()<<"Sink::~Sink()";
}

Expand All @@ -78,7 +78,7 @@ void Sink::stop()
qDebug()<<"Sink::stop()";
close();
file->close();
if(save_uncompressed_pcm_too)writeWavHeader();//fill file sz
if (save_uncompressed_pcm_too) writeWavHeader();//fill file sz
file_pcm->close();
}

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

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

if(failed)emit ChannelFailed();
if (failed) emit ChannelFailed();

if(done)
if (done)
{
QTimer::singleShot(0,this,SLOT(stop()));
return 0;
}

//emit max volume in %
const short *ptr = reinterpret_cast<const short *>(data);
int maxval=0;
for(int i=0; i<len/sizeof(short); i++)
maxval = 0;
minval = 32767;
n = (int)(len / sizeof(short));
for (i = 0; i < n; i++)
{
int val=abs((int)(*ptr));
if(val>maxval)maxval=val;
val = abs((int)(*ptr));
maxval = (val > maxval) ? val : maxval;
minval = (val < minval) ? val : minval;
ptr++;
}
emit signal_volume(100*maxval/32767);
emit signal_volume((100 * maxval) / 32767);

if(save_uncompressed_pcm_too)file_pcm->write(data,len);
if (save_uncompressed_pcm_too) file_pcm->write(data,len);

//fill "buf" till we have enough to decode then write to file
for(int bufptr=0; bufptr<len;)
for (bufptr = 0; bufptr < len;)
{
br = (qint64)(sizeof(short) * nsam - bufremptr);
dl = len - bufptr;
bytestoread = qMin(br, dl);//how much can we read?

int bytestoread=qMin(((qint64)(sizeof(short)*nsam-bufremptr)),len-bufptr);//how much can we read?

if(bytestoread!=((qint64)(sizeof(short)*nsam-bufremptr)))//not enough? then save for later
db = bufremptr / sizeof(short);
if (bytestoread != br)//not enough? then save for later
{
memcpy (buf+bufremptr/sizeof(short), data+bufptr,bytestoread);
bufremptr+=bytestoread;
bufptr+=bytestoread;
memcpy (buf + db, data + bufptr, bytestoread);

bufremptr += bytestoread;
bufptr += bytestoread;
break;
}
else//enough? then decode
{
memcpy (buf+bufremptr/sizeof(short), data+bufptr, bytestoread);
codec2_encode(codec2,bits,buf);
file->write((char*)bits,nbyte);
bufremptr=0;
memcpy (buf + db, data + bufptr, bytestoread);
//trim begin clipping
if (minval < 32767)
{
codec2_encode(codec2, bits, buf);
file->write((char*)bits, nbyte);
}
bufremptr = 0;
}

bufptr+=bytestoread;
bufptr += bytestoread;
}

return len;
Expand All @@ -159,7 +171,7 @@ void Sink::writeWavHeader()
out << quint16(1); // data format (1 => PCM)
out << quint16(1);
out << quint32(8000);
out << quint32(8000 * 1 * 16 / 8 ); // bytes per second
out << quint32(8000 * 1 * 16 / 8); // bytes per second
out << quint16(1 * 16 / 8); // Block align
out << quint16(16); // Significant Bits Per Sample

Expand Down
2 changes: 1 addition & 1 deletion src/source.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -81,7 +81,7 @@ qint64 Source::readData(char *data, qint64 len)

int bufptr=0;

while((bufptr+sizeof(short)*nsam)<(len/2))//(len/2) rather than (len) allows smaller sound card buffer
while((qint64)(bufptr+sizeof(short)*nsam) < (len/2))//(len/2) rather than (len) allows smaller sound card buffer
{
//qint64 bytesread=fread(bits, sizeof(char), nbyte, fin);
qint64 bytesread=file->read((char*)bits, nbyte);
Expand Down
2 changes: 1 addition & 1 deletion src/source.h
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ class Source : public QIODevice
qint64 bytesread_sum;

// FILE *fin;
// FILE *fout;
// FILE *fout;
signals:
void percentage_played(int percentage);
public slots:
Expand Down

0 comments on commit cee8f3e

Please sign in to comment.