📄 audiorecorder.cpp
字号:
/*
* based on recplay.c
*
* A simple recording and playback program for the Linux Sound Driver 2.0
* or later.
*
* Copyright by Freescale Semiconductors, Suzhou Ltd. 2004
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions are
* met: 1. Redistributions of source code must retain the above copyright
* notice, this list of conditions and the following disclaimer. 2.
* Redistributions in binary form must reproduce the above copyright notice,
* this list of conditions and the following disclaimer in the documentation
* and/or other materials provided with the distribution.
*
* THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND ANY
* EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
* WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
* DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE FOR
* ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
* DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
* SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
* CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
* LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
* OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
* SUCH DAMAGE.
*
*/
#include <stdio.h>
#include <unistd.h>
#include <fcntl.h>
#include <stdlib.h>
#ifdef __STDC__
#include <string.h>
#else
#include <strings.h>
#endif
#include <sys/soundcard.h>
#include <sys/ioctl.h>
#include <sys/stat.h>
#include <sys/time.h>
#include <sys/types.h>
#include <qpe/qpeapplication.h>
#include <qbuttongroup.h>
#include <qlayout.h>
#include <qradiobutton.h>
#include <qgroupbox.h>
#include <qpushbutton.h>
#include <qlineedit.h>
#include <qlabel.h>
#include <qmessagebox.h>
#include <qfiledialog.h>
#include <qfile.h>
#include <qcheckbox.h>
#include <qslider.h>
#include "audiorecorder.h"
AudioRecorder::AudioRecorder(QWidget *parent, const char *name)
: QWidget(parent, name)
{
setSizePolicy( QSizePolicy( (QSizePolicy::SizeType)0, (QSizePolicy::SizeType)0, sizePolicy().hasHeightForWidth() ) );
// Create Widgets which allow easy layouting
vbox = new QVBoxLayout( this, 11, 6 );
box1 = new QHBoxLayout( vbox ); //For Save to
box2 = new QHBoxLayout( vbox ); //For Mode select
box3 = new QHBoxLayout( vbox ); //gary
box4 = new QHBoxLayout( vbox ); //For 3 pushbuttons
// -------------- First Group
// Create a label for "Save to..."
label1 = new QLabel( "Save audio to: ", this );
label1->setGeometry( QRect( ));
box1->addWidget( label1 );
EditFile = new QLineEdit(this);
box1->addWidget( EditFile);
// -------------- Second Group Horizontal
bgrp1 = new QButtonGroup( 1, QGroupBox::Horizontal, "Mode", this);
bgrp2 = new QButtonGroup( 1, QGroupBox::Horizontal, "Speed", this);
bgrp3 = new QButtonGroup( 1, QGroupBox::Horizontal, "Source", this);
box2->addWidget( bgrp1 );
box2->addWidget( bgrp2);
box2->addWidget( bgrp3);
bgrp1->setExclusive( TRUE );
bgrp2->setExclusive( TRUE );
bgrp3->setExclusive( TRUE );
// insert 2 radiobuttons for Mono and Stereo;
rb1 = new QRadioButton( "&Mono", bgrp1 );
rb1->setChecked( TRUE );
dsp_stereo = 0;
rb2 = new QRadioButton( "&Stereo", bgrp1 );
rb3 = new QRadioButton( "&8 k", bgrp2);
rb3->setChecked( TRUE );
dsp_speed = 8000;
rb4 = new QRadioButton( "&44.1 k", bgrp2);
rb5 = new QRadioButton( "&Line in", bgrp3);
rb6 = new QRadioButton( "&Mic in", bgrp3);
rb6->setChecked( TRUE );
source = SOUND_MASK_MIC;
connect( rb1, SIGNAL( clicked() ), this, SLOT( setMono() ) );
connect( rb2, SIGNAL( clicked() ), this, SLOT( setStereo() ) );
connect( rb3, SIGNAL( clicked() ), this, SLOT( set8k() ) );
connect( rb4, SIGNAL( clicked() ), this, SLOT( set44k() ) );
connect( rb5, SIGNAL( clicked() ), this, SLOT( setLinein() ) );
connect( rb6, SIGNAL( clicked() ), this, SLOT( setMicin() ) );
progress = new QSlider( 0, 150, 1, 0, QSlider::Horizontal, this, "Slider" );
progress->setTickmarks( QSlider::Below );
progress->setTickInterval( 10 );
progress->setFocusPolicy( QWidget::TabFocus );
box3->addWidget( progress );
// -------------- Third Group
// Insert 3 pushbuttons
recordPushButton = new QPushButton( "&Record", this );
box4->addWidget( recordPushButton );
playPushButton = new QPushButton( "&Play", this );
box4->addWidget( playPushButton );
chb = new QCheckBox( "&Side Tone", this);
chb->setChecked( TRUE );
box4->addWidget( chb );
connect(recordPushButton, SIGNAL(clicked()), this, SLOT(record_clicked()));
connect(playPushButton, SIGNAL(clicked()), this, SLOT(play_clicked()));
mixer_fd = ::open("/dev/mixer", O_RDONLY);
if (0 != ::ioctl (mixer_fd, SOUND_MIXER_WRITE_RECSRC, &source))
{
perror("SOUND_MIXER_WRITE_RECSRC error!\n");
exit(-1);
}
}
AudioRecorder::~AudioRecorder()
{
if (audio_fd != -1)
::close(audio_fd);
audio_fd = -1;
return;
}
void AudioRecorder::setMono()
{
dsp_stereo = 0;
}
void AudioRecorder::setStereo()
{
dsp_stereo = 1;
}
void AudioRecorder::set8k()
{
dsp_speed = 8000;
}
void AudioRecorder::set44k()
{
dsp_speed = 44100;
}
void AudioRecorder::setLinein()
{
source = SOUND_MASK_LINE;
}
void AudioRecorder::setMicin()
{
source = SOUND_MASK_MIC;
}
void AudioRecorder::record_clicked()
{
char* name;
int output_fd, l;
int count, c;
char filename1[50];
char filename2[50];
char filename3[50];
char filename4[50];
int i=0, j=0, k=0;
int tmp;
int length=0;
int analog_fd;
char buf[1];
int recordfiletype = 0;
char *audiobuf1; //[8192];
int progresscount;
samplesize = 16;
prof = APF_NORMAL;
//record time 15 seconds
count = 15*dsp_speed;
if (dsp_stereo)
count *= 2;
if (samplesize != 8)
count *= 2;
wholecount = count;
//file name operation
Filename = EditFile->text();
if(Filename.isEmpty())
{
name = "/tmp/rec1.mp3";
EditFile->setText( tr("/tmp/rec1.mp3") );
QMessageBox::warning(this, tr("Warning"), tr("You not specify a file, \nsaved as /tmp/rec1.mp3"), QMessageBox::Ok, 0,0);
repaint(true);
return;
}
else
name = (char*) Filename.latin1();
/*if(!(QFile::exists(Filename)))
{
name = "/tmp/rec1.mp3";
EditFile->setText( tr("/tmp/rec1.mp3") );
QMessageBox::warning(this, tr("Warning"), tr("You not specify a file, saved as /tmp/rec1.mp3"), QMessageBox::Ok, 0,0);
repaint(true);
return;
}
*/
//check whether the music file name is valid, only *.pcm, *.wav, *.mp3 are valid.
//if not valid, Warning: File not support! saved as default file: audiorec001.mp3;
//file name * can't include '.', must be *.*
strcpy(filename1, name);
while(filename1[i] != '.' && filename1[i] != '\0')
i++;
if(filename1[i] == '.')
{
if ((filename1[i+1] == 'p') && (filename1[i+2] == 'c') && (filename1[i+3] == 'm'))
recordfiletype = FILE_PCM;
else if ((filename1[i+1] == 'P') && (filename1[i+2] == 'C') && (filename1[i+3] == 'M'))
recordfiletype = FILE_PCM;
else if ((filename1[i+1] == 'w') && (filename1[i+2] == 'a') && (filename1[i+3] == 'v'))
recordfiletype = FILE_WAV;
else if ((filename1[i+1] == 'W') && (filename1[i+2] == 'A') && (filename1[i+3] == 'V'))
recordfiletype = FILE_WAV;
else if ((filename1[i+1] == 'm') && (filename1[i+2] == 'p') && (filename1[i+3] == '3'))
recordfiletype = FILE_MP3;
else if ((filename1[i+1] == 'M') && (filename1[i+2] == 'P') && (filename1[i+3] == '3'))
recordfiletype = FILE_MP3;
else
{
name = "/tmp/rec1.mp3";
strcpy(filename1, name);
EditFile->setText( tr("/tmp/rec1.mp3") );
QMessageBox::warning(this, tr("Warning"), tr("You not specify a valid file name: \n*.pcm,*.wav,*.mp3, \nsaved as /tmp/rec1.mp3"), QMessageBox::Ok, 0,0);
repaint(true);
return;
recordfiletype = FILE_MP3;
}
}
else if(filename1[i] == '\0')
{
name = "/tmp/rec1.mp3";
strcpy(filename1, name);
EditFile->setText( tr("/tmp/rec1.mp3") );
QMessageBox::warning(this, tr("Warning"), tr("You not specify a valid file name: \n*.pcm,*.wav,*.mp3, \nsaved as /tmp/rec1.mp3"), QMessageBox::Ok, 0,0);
repaint(true);
return;
recordfiletype = FILE_MP3;
}
strcpy(filename2, name);
switch(recordfiletype)
{
case FILE_PCM:
break;
case FILE_WAV:
break;
case FILE_MP3:
//name: *.wav, not *.mp3
while(filename1[j] != '.') //name
j++;
if(filename1[j] == '.') //name
{
//name[j+1] = 'w';
//name[j+2] = 'a';
//name[j+3] = 'v';
filename2[j+1] ='w';
filename2[j+2] ='a';
filename2[j+3] ='v';
k=j+3;
while(filename1[j] != '/')
j--;
for (i=0; i<k-j; i++)
{
filename3[i] = filename1[j+i+1];
filename4[i] = filename2[j+i+1];
}
for (i=k-j; i<50; i++)
{
filename3[i] = '\0';
filename4[i] = '\0';
}
}
break;
}
if ((output_fd = ::open (filename2, O_WRONLY | O_CREAT, 0666)) == -1)
{
perror(name);
exit (-1);
}
audio_fd = ::open("/dev/dsp", O_RDONLY, 0);
if (audio_fd == -1)
{
perror("dev/dsp");
exit (-1);
}
tmp = samplesize;
::ioctl(audio_fd, SNDCTL_DSP_SAMPLESIZE, &samplesize);
if (tmp != samplesize)
{
printf("Unable to set the sample size\n");
exit(-1);
}
::ioctl(audio_fd, SNDCTL_DSP_PROFILE, &prof);
if (::ioctl (audio_fd, SNDCTL_DSP_STEREO, &dsp_stereo)==-1)
{
printf("Unable to set mono/stereo\n");
perror("/dev/dsp");
exit (-1);
}
if (::ioctl (audio_fd, SNDCTL_DSP_SPEED, &dsp_speed) == -1)
{
printf("Unable to set audio speed\n");
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -