home *** CD-ROM | disk | FTP | other *** search
/ Chip 2004 November / CMCD1104.ISO / Software / Complet / ZynAddFX / Setup_ZynAddSubFX-2.1.1.exe / Recorder.C < prev    next >
Encoding:
C/C++ Source or Header  |  2004-03-05  |  3.6 KB  |  139 lines

  1. /*
  2.   ZynAddSubFX - a software synthesizer
  3.  
  4.   Recorder.C - Records sound to a file
  5.   Copyright (C) 2002-2004 Nasca Octavian Paul
  6.   Author: Nasca Octavian Paul
  7.  
  8.   This program is free software; you can redistribute it and/or modify
  9.   it under the terms of version 2 of the GNU General Public License 
  10.   as published by the Free Software Foundation.
  11.  
  12.   This program is distributed in the hope that it will be useful,
  13.   but WITHOUT ANY WARRANTY; without even the implied warranty of
  14.   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
  15.   GNU General Public License (version 2) for more details.
  16.  
  17.   You should have received a copy of the GNU General Public License (version 2)
  18.   along with this program; if not, write to the Free Software Foundation,
  19.   Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307 USA
  20.  
  21. */
  22.  
  23. #include <stdlib.h>
  24. #include <stdio.h>
  25. #include <string.h>
  26.  
  27. #include <sys/types.h>
  28. #include <sys/stat.h>
  29. #include <fcntl.h>
  30. #include <unistd.h>
  31. #include <errno.h>
  32.  
  33. #include "Recorder.h"
  34.  
  35. Recorder::Recorder(){
  36.     recordbuf_16bit=new short int [SOUND_BUFFER_SIZE*2];
  37.     status=0;file=-1;
  38.     sampleswritten=0;
  39.     notetrigger=0;
  40.     for (int i=0;i<SOUND_BUFFER_SIZE*2;i++){
  41.     recordbuf_16bit[i]=0;
  42.     };
  43. };
  44.  
  45. Recorder::~Recorder(){
  46.     if (recording()==1) stop();
  47.     delete [] recordbuf_16bit;
  48. };
  49.  
  50. int Recorder::preparefile(char *filename_,int overwrite){
  51.     if (overwrite==0) file=open(filename_,O_CREAT|O_EXCL|O_WRONLY|O_BINARY,00444+00222);
  52.         else file=open(filename_,O_CREAT|O_WRONLY|O_TRUNC|O_BINARY,00444+00222);//overwrite if the file exists
  53.     if (file==-1) {
  54.     if (errno==EEXIST) return(1);//file exists already
  55.         else return(2);//Access Denied or any other problem
  56.     };    
  57.     status=1;//ready
  58.     
  59.     //prepare the space fot the wav header
  60.     //the header itself, will be written when the file is closed
  61.     unsigned char zerobuf[44]; for (int i=0;i<44;i++) zerobuf[i]=0;
  62.     write(file,zerobuf,44);
  63.  
  64.     return(0);
  65. };
  66.  
  67. void Recorder::start(){
  68.     notetrigger=0;
  69.     status=2;//recording
  70. };
  71.  
  72. void Recorder::stop(){
  73.     unsigned int chunksize;
  74.     lseek(file,0,SEEK_SET);
  75.  
  76.     write(file,"RIFF",4);
  77.     chunksize=sampleswritten*4+36;
  78.     write(file,&chunksize,4);
  79.  
  80.     write(file,"WAVEfmt ",8);
  81.     chunksize=16;
  82.      write(file,&chunksize,4);
  83.     unsigned short int formattag=1;//uncompresed wave
  84.      write(file,&formattag,2);
  85.     unsigned short int nchannels=2;//stereo
  86.      write(file,&nchannels,2);
  87.     unsigned int samplerate=SAMPLE_RATE;//samplerate
  88.      write(file,&samplerate,4);
  89.     unsigned int bytespersec=SAMPLE_RATE*4;//bytes/sec
  90.      write(file,&bytespersec,4);
  91.     unsigned short int blockalign=4;//2 channels * 16 bits/8
  92.      write(file,&blockalign,2);
  93.     unsigned short int bitspersample=16;
  94.      write(file,&bitspersample,2);
  95.     
  96.     write(file,"data",4);
  97.     chunksize=sampleswritten*blockalign;
  98.     write(file,&chunksize,4);
  99.  
  100.     close(file);
  101.     file=-1;
  102.     status=0;
  103.     sampleswritten=0;
  104. };
  105.  
  106. void Recorder::pause(){
  107.     status=0;
  108. };
  109.  
  110. int Recorder::recording(){
  111.     if ((status==2)&&(notetrigger!=0)) return(1);
  112.     else return(0);
  113. };
  114.  
  115. void Recorder::recordbuffer(REALTYPE *outl,REALTYPE *outr){
  116.     int tmp;
  117.     if (status!=2) return;
  118.     for (int i=0;i<SOUND_BUFFER_SIZE;i++){
  119.     tmp=(int)(outl[i]*32767.0);
  120.     if (tmp<-32768) tmp=-32768;
  121.     if (tmp>32767) tmp=32767;
  122.     recordbuf_16bit[i*2]=tmp;
  123.  
  124.     tmp=(int)(outr[i]*32767.0);
  125.     if (tmp<-32768) tmp=-32768;
  126.     if (tmp>32767) tmp=32767;
  127.     recordbuf_16bit[i*2+1]=tmp;
  128.     };
  129.     if (write(file,recordbuf_16bit,SOUND_BUFFER_SIZE*4)<SOUND_BUFFER_SIZE*4) {
  130.     fprintf(stderr,"Error while recording !\n");
  131.     stop();
  132.     };
  133.     sampleswritten+=SOUND_BUFFER_SIZE;
  134. };
  135.  
  136. void Recorder::triggernow(){
  137.     if (status==2) notetrigger=1;
  138. };
  139.