diff --git a/src/mainwindow.cpp b/src/mainwindow.cpp index b006c8c..a54328a 100644 --- a/src/mainwindow.cpp +++ b/src/mainwindow.cpp @@ -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(); @@ -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(); @@ -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); diff --git a/src/mainwindow.ui b/src/mainwindow.ui index 21af422..93d73c1 100644 --- a/src/mainwindow.ui +++ b/src/mainwindow.ui @@ -23,10 +23,10 @@ - JC2Rec v1.1 + JC2Rec v1.2 - + :/icons/icon.png:/icons/icon.png diff --git a/src/sink.cpp b/src/sink.cpp index ddaefe5..043ec04 100644 --- a/src/sink.cpp +++ b/src/sink.cpp @@ -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); @@ -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 @@ -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()"; } @@ -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(); } @@ -92,10 +92,12 @@ 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; @@ -103,39 +105,49 @@ qint64 Sink::writeData(const char *data, qint64 len) //emit max volume in % const short *ptr = reinterpret_cast(data); - int maxval=0; - for(int i=0; imaxval)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; bufptrwrite((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; @@ -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 diff --git a/src/source.cpp b/src/source.cpp index ea86047..bab5176 100644 --- a/src/source.cpp +++ b/src/source.cpp @@ -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); diff --git a/src/source.h b/src/source.h index d1b0163..3868add 100644 --- a/src/source.h +++ b/src/source.h @@ -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: