⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 audiorecorder.cpp

📁 qt中录音程序,使用C++语言,界面简洁,功能具备,将原始的pcm声音数据读出,生成mp3格式.
💻 CPP
📖 第 1 页 / 共 2 页
字号:
		perror("/dev/dsp");
		exit (-1);
	}

	::ioctl (audio_fd, SNDCTL_DSP_GETBLKSIZE, &abuf_size);
	if (abuf_size < 1)
	{
		perror("GETBLKSIZE");
		exit (-1);
	}
     //printf("abuf_size=%d \n", abuf_size);

	 audiobuf1 = new char [8192];
	/*if ((audiobuf = (char* )malloc (abuf_size)) == NULL)
	{
		printf("Unable to allocate input/output buffer\n");
		exit (-1);
	}*/

//mixer operation:
      if (chb->isChecked() )
            ::ioctl(mixer_fd, SOUND_MIXER_GARY_SIDETONE, 0);             //enable side tone
      else
	      ::ioctl(mixer_fd, SOUND_MIXER_GARY_NOSIDETONE, 0);        //disable side tone.
//stg3699 operation
	if (source == SOUND_MASK_LINE)
	{  
		analog_fd = ::open("/dev/stg3699", O_RDWR);
		if(analog_fd < 0)
		{
			printf("Device Analog switch /dev/stg3699 open error !\n");
			exit(-1);
		}
		buf[0] = '1';
		write(analog_fd, buf, 1);
	}
	
//record operation
      printf("Starting recording......\n");

	while (count)
	{
		c = count;
		if (c > abuf_size)
			c = abuf_size;

		if ((l = ::read (audio_fd, audiobuf1, c)) > 0)
		{
			if (::write (output_fd, audiobuf1, l) != l)
			{
				perror(name);
				exit (-1);
			}
			length += l;
			count -= l;
		}

		progresscount = (length*150)/wholecount;
	      progress->setValue( progresscount );
		  	
		if (l == -1)
		{
		      perror("/dev/dsp");
			exit (-1);
		}
	}			// While count 
	
      ::close(audio_fd);
	if (output_fd != 1)
		::close (output_fd);
	printf("recording over!\n");
	progress->setValue(0);
	//ioctl(mixer_fd, SOUND_MIXER_GARY_NOSIDETONE, 0);       //disable side tone.
	
	//translate *.pcm to *.wav or *.mp3
	switch(recordfiletype)
	{
		case FILE_PCM:
			//printf("file type pcm \n");
			break;
		case FILE_WAV:     //add wave header to name (*.wav)
		      //printf("file type wav \n");
			pcm2wav(length, filename2);
			break;
		case FILE_MP3:
			//printf("file type mp3\n");
			pcm2wav(length, filename2);  //add wave header to name (*.wav)
			usleep(500000);         //delay 0.5 second
			
			int fork_id;
			fork_id = fork();
	             if(!fork_id)           //call script to translate name(*.wav) to *.mp3
				execl("/usr/sbin/mp3.rec",  "/usr/sbin/mp3.rec", (filename4), (filename3), (char*)(0));                      
			                                                                     //filename4: *.wav;   filename3: *.mp3    
			usleep(500000);          //delay 0.5 second
			break;                                                                
	}

      if (audiobuf1)
	    delete[] audiobuf1;
	printf("recording completed!\n");
 }


void AudioRecorder::pcm2wav(int length, char *name)
{
      char wave[44]; 
	unsigned int value;
	int fd;
	
      fd = ::open ("/tmp/waveheader", O_WRONLY | O_CREAT, 0666);
	wave[0] = 'R';
	wave[1] = 'I';
	wave[2] = 'F';
	wave[3] = 'F';
	
	length = length - 8;
	wave[4] = ((length%16777216)%65536)%256;   //file length - 8
      wave[5] =  ((length%16777216)%65536)/256; 
	wave[6] = (length%16777216)/65536; 
	wave[7] = length/16777216; 
	wave[8] = 'W';
	wave[9] = 'A';
	wave[10] = 'V';
	wave[11] = 'E';
	wave[12] = 'f';
	wave[13] = 'm';
	wave[14] = 't';
	wave[15] = ' ';
      wave[16] = 0x10;   //16 bytes: Length of the fmt data chunk
	wave[17] = 0x00;
	wave[18] = 0x00;
	wave[19] = 0x00;
				
      wave[20] = 0x01;   //value2 = 0x0001;    //Format tag: 1 = PCM;
	wave[21] = 0x00;

	wave[22] = (char)(dsp_stereo+1);        //value2 = dsp_stereo + 1;   //Channels: 1 = mono, 2 = stereo;
      wave[23] = 0x00;

	wave[24] = dsp_speed%256;   		  //value = dsp_speed;   //Sample per second: e.g., 44100
       wave[25] = dsp_speed/256;
       wave[26] = 0;
 	 wave[27] = 0;
	
	value = dsp_speed*(dsp_stereo+1)*2;
	wave[28] = (value%65536)%256;                       //sample rate * block align
       wave[29] = (value%65536)/256;
	wave[30] = value/65536;
	wave[31] = 0;

	wave[32] =(char)( (dsp_stereo+1)*2);  //value2 = (dsp_stereo+1)*2;    //channels * bits/sample/8
	wave[33] = 0;
       wave[34] = 16;              //value2 = 16;            // 8  or 16;
       wave[35] = 0;
		   
	wave[36] = 'd';
	wave[37] = 'a';
	wave[38] = 't';
	wave[39] = 'a';

      length -= 36;
	wave[40] = ((length%16777216)%65536)%256;   //file length - 44
      wave[41] =  ((length%16777216)%65536)/256; 
	wave[42] = (length%16777216)/65536; 
	wave[43] = length/16777216; 
	write(fd, wave, 44);
	
	int fork_id;
      fork_id = fork();
      if(!fork_id)                    //call script to translate name(*.wav) to *.mp3
	    execl("/usr/sbin/pcm2wav",  "/usr/sbin/pcm2wav", (char*)(name), ("/tmp/waveheader"), (char*)(0));  
	  
	printf("pcm to wav completed!\n");
}

void AudioRecorder::play_clicked()
{
      int input_fd, l;
	int count, c;
	char* name;
	char filename1[50];
	int i=0;
      int tmp;
	int playfiletype = 0;
	char *audiobuf2;
	int playprogress = 0;
	int playlength = 0;
  
	count = 0x7fffffff;

      progress->setValue( 0 );
	  
	Filename = EditFile->text();
	
	if(!(QFile::exists(Filename)))
	{
	      QMessageBox::warning(this, tr("Warning"), tr("File doesn't exist!"), QMessageBox::Ok, 0,0);
		repaint(true);
		return;
	}
	else
	{
		name = (char*) Filename.latin1();		
	}
	//check whether the music file name is valid, only *.pcm, *.wav, *.mp3 are valid.
	//if not valid, Warning: File not support!  return;

	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'))
			playfiletype = FILE_PCM;
		else if ((filename1[i+1] == 'P') && (filename1[i+2] == 'C') && (filename1[i+3] == 'M'))
			playfiletype = FILE_PCM;
		else if ((filename1[i+1] == 'w') && (filename1[i+2] == 'a') && (filename1[i+3] == 'v'))
			playfiletype = FILE_WAV;
		else if ((filename1[i+1] == 'W') && (filename1[i+2] == 'A') && (filename1[i+3] == 'V'))
			playfiletype = FILE_WAV;
		else if ((filename1[i+1] == 'm') && (filename1[i+2] == 'p') && (filename1[i+3] == '3'))
			playfiletype = FILE_MP3;
		else if ((filename1[i+1] == 'M') && (filename1[i+2] == 'P') && (filename1[i+3] == '3'))
			playfiletype = FILE_MP3;
		else
		{	
			QMessageBox::warning(this, tr("Warning"), tr("You not specify a valid file, \nplease specify the file: \n*.mp3, *.wav, *.pcm "), QMessageBox::Ok, 0,0);
                    repaint(true);
		       return;
		}
	}
	else if(filename1[i] == '\0')
	{	
		QMessageBox::warning(this, tr("Warning"), tr("You not specify a valid file, \nplease specify the file: \n*.mp3, *.wav, *.pcm "), QMessageBox::Ok, 0,0);
             repaint(true);
		return;
	}

	printf("Starting play: %s \n", name);
      switch(playfiletype)
	{
		case FILE_PCM:
			break;
		case FILE_WAV:
			break;
		case FILE_MP3:
			int fork_id;
			fork_id = fork();
	             if(!fork_id)                    //call mp3 decoder to playback.
			      execl("/usr/sbin/mp3.play", "/usr/sbin/mp3.play", (char*)(name), (char*)(0));  
			return;
	}
	
	if ((input_fd = ::open (name, O_RDONLY, 0)) == -1)
	{
		perror(name);
		exit(-1);
	}
	
//mixer operations:
      ::ioctl(mixer_fd, SOUND_MIXER_GARY_NOSIDETONE, 0);        //disable side tone.

//dsp operations:
      audio_fd = ::open("/dev/dsp", O_WRONLY, 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");
		perror("/dev/dsp");
		exit (-1);
	}

	::ioctl (audio_fd, SNDCTL_DSP_GETBLKSIZE, &abuf_size);
	if (abuf_size < 1)
	{
		perror("GETBLKSIZE");
		exit (-1);
	}

	if ((audiobuf2 = (char* )malloc (abuf_size)) == NULL)
	{
		printf("Unable to allocate input/output buffer\n");
		exit (-1);
	}

	while (count)
	{
		c = count;
	
		if (c > abuf_size)
			c = abuf_size;
	
		if ((l = ::read (input_fd, audiobuf2, c))> 0)
		{
			if (::write (audio_fd, audiobuf2, l) != l)
			{
				perror("/dev/dsp");
				exit (-1);
			}
			count -= l;
		}
		else
		{
			if (l == -1)
			{
				perror(name);
				exit (-1);
			}
			count = 0;	// Stop
		}

		playlength += l;
		playprogress = (playlength*150)/wholecount;
	       progress->setValue( playprogress );
		  
	}	

	::close(audio_fd);
      progress->setValue(0); 

	if (input_fd != 0)
		::close (input_fd);
	
	if (audiobuf2)
          ::free(audiobuf2);
}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -