1 /*
2    exportAudio.c
3    code for writing audio files
4    Depends on:
5    exportAudio.h
6    BinauralBeat.h
7 
8    Copyright (C) 2008  Bret Logan
9 
10    This program is free software; you can redistribute it and/or modify
11    it under the terms of the GNU General Public License as published by
12    the Free Software Foundation; either version 2 of the License, or
13    (at your option) any later version.
14 
15    This program is distributed in the hope that it will be useful,
16    but WITHOUT ANY WARRANTY; without even the implied warranty of
17    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
18    GNU General Public License for more details.
19 
20    You should have received a copy of the GNU General Public License
21    along with this program; if not, write to the Free Software
22    Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
23  */
24 
25 #include <sndfile.h>
26 #include "BinauralBeat.h"
27 
28 ////////////////////////////////////
29 //Typical major format types might be:
30 //SF_FORMAT_WAV
31 //SF_FORMAT_FLAC
32 //SF_FORMAT_AIFF
33 //SF_FORMAT_AU
34 //SF_FORMAT_RAW
35 //SF_FORMAT_OGG
36 //But the SubTypes can only be one of these:
37 //SF_FORMAT_PCM_16
38 //SF_FORMAT_VORBIS [only used with SF_FORMAT_OGG majortype]
39 
exportAudioToFile(char * filename,int format)40 int exportAudioToFile (char *filename, int format)
41 {
42  const int EA_FRAMES_TO_WRITE = 16;
43  const int EA_BYTES_TO_FILL = EA_FRAMES_TO_WRITE * 4;
44  SNDFILE *file;
45  SF_INFO sfinfo;
46  int *buffer[EA_FRAMES_TO_WRITE];
47 
48  sfinfo.samplerate = BB_AUDIOSAMPLERATE;
49  sfinfo.frames =
50   (unsigned int) (BB_Loops * BB_TotalDuration * BB_AUDIOSAMPLERATE + 0.5);
51  sfinfo.channels = 2;
52  sfinfo.format = format;
53  sfinfo.sections = 1;
54  sfinfo.seekable = 0;
55 
56  if (!(file = sf_open (filename, SFM_WRITE, &sfinfo)))
57  {
58   printf ("Unexpected Error : Not able to write that format, sorry.\n");
59   return 1;
60  };
61 
62  BB_WriteStopFlag = 0;
63 
64  BB_Reset ();   //make sure we start at absolute beginning
65 
66  while (!(BB_InfoFlag & BB_COMPLETED) && FALSE == BB_WriteStopFlag)
67  {
68   BB_MainLoop (buffer, EA_BYTES_TO_FILL);
69   sf_writef_short (file, (short *) buffer, EA_FRAMES_TO_WRITE);
70  }
71 
72  sf_close (file);
73  return 0;
74 }
75