CSL  6.0
FileIO.cpp
Go to the documentation of this file.
1 //
2 // FileIO.cpp -- File IO using libsndfile
3 // See the copyright notice and acknowledgment of authors in the file COPYRIGHT
4 //
5 
6 #include "FileIO.h"
7 
8 using namespace csl;
9 
10 FileIO::FileIO(char * path) : IO(), mIsPlaying(false), mIsThreadRunning(false), mFile(0) {
11  if ( ! path)
13  else
14  mPath = path;
15  mFile = new SoundFile(mPath);
16 }
17 
19  if (mFile)
20  delete mFile;
21  if (mThread)
22  delete mThread;
23 }
24 
25 // Open file output file writer
26 
27 void FileIO::open() throw(CException) {
28  int channels = CGestalt::numOutChannels();
29  int blockSize = CGestalt::blockSize();
30  int rate = CGestalt::frameRate();
31  SoundFileFormat format;
32 
33 #ifdef DO_TIMING
34  mThisSec = mTimeVals = mTimeSum = 0;
35 #endif
36  if (mInBuffer.mAreBuffersAllocated) // allocate I/O buffers
38  mInBuffer.setSize(channels, blockSize);
42  mOutBuffer.setSize(channels, blockSize);
44  const char * sPath = mPath.c_str();
45 
46  char * dot = (char *) strrchr(sPath, '.'); // get the file name extension
47  format = kSoundFileFormatAIFF; // default = AIFF files
48  if (strcmp(dot, ".wav") == 0)
49  format = kSoundFileFormatWAV;
50  else if (strcmp(dot, ".snd") == 0)
51  format = kSoundFileFormatSND;
52 
53  mFile->openForWrite(format, channels, rate); // open output file
54  if ( ! mFile->isValid()) {
55  logMsg(kLogError, "Error opening sound file output");
56  return; // THROW AN EXCEPTION
57  }
58  mThread = CThread::MakeThread(); // create background thread
59  mDuration = (int)((float)blockSize / (float)rate * 1000000.0f); // block rate in usec
60  return;
61 }
62 
63 void FileIO::start(float seconds) throw(CException) {
64  mIsPlaying = true;
65  mIsThreadRunning = false;
66  if (seconds) { // full-speed driver version
67  unsigned numFrames = (unsigned) (seconds * CGestalt::frameRate());
68  logMsg("Starting sound file output loop");
69  while (mNumFramesPlayed < numFrames) // note that this does not return until it's done,
70  writeNextBuffer(); // so you can't start and then trigger envelopes!
71  } else { // Start file IO -- loop in the background
72  logMsg("Starting sound file output thread");
73  mThread->createThread(threadFunction, this);
74  }
75 }
76 
77 void FileIO::stop() throw(CException) {
78  mIsPlaying = false; // signal for writer thread to finish
79  while(mIsThreadRunning) // wait around for the feeder thread to die
81 }
82 
83 // close, free, and write output file
84 
85 void FileIO::close() throw(CException) {
90  mFile->close();
91 }
92 
93 // compute and write a buffer
94 
96  if (mGraph != 0)
98  else
100  mFile->writeBuffer(mOutBuffer);
102 }
103 
104 // Background thread function -- loop to read from the DSP graph and write to the file
105 
106 void * FileIO::threadFunction(void * ptr) {
107  FileIO *me = (FileIO *)ptr;
108 // printf("D: %d\n", me->mDuration);
109 
110 #ifdef DO_TIMING
111  int sleepTime;
112  struct timeval *meThen = &me->mThen;
113  struct timeval *meNow = & me->mNow;
114 #endif
115  while (me->mIsPlaying) { // loop until turned off
116 #ifdef DO_TIMING
117  GET_TIME(meThen);
118 #endif
119  me->writeNextBuffer(); // get and write the next buffer
120 #ifdef DO_TIMING
121  GET_TIME(meNow);
122  me->printTimeStatistics(meNow, meThen, & me->mThisSec, & me->mTimeSum, & me->mTimeVals);
123  GET_TIME(meNow);
124  sleepTime = me->mDuration - (((meNow->tv_sec - meThen->tv_sec) * 1000000) + (meNow->tv_usec - meThen->tv_usec));
125  // printf("S: %d\n", sleepTime);
126  if (sleepTime > 0)
127  csl::sleepUsec(sleepTime);
128 #else
130 #endif
131  }
132  // do this to prevent a race condition
133  me->mIsThreadRunning = false;
134  logMsg("Stopping sound file output thread");
135  return 0;
136 }
void logMsg(const char *format,...)
These are the public logging messages.
Definition: CGestalt.cpp:292
#define kSoundFileFormatAIFF
Definition: SoundFile.h:60
long mTimeSum
for printing run-time statistics
Definition: CSL_Core.h:797
unsigned mNumFrames
num frames used in each buffer
Definition: CSL_Core.h:113
bool mAreBuffersAllocated
are the buffers allocated?
Definition: CSL_Core.h:120
AdditiveInstrument.h – Sum-of-sines synthesis instrument class.
Definition: Accessor.h:17
bool mIsPlaying
whether or not it's playing
Definition: FileIO.h:25
static std::string sndFileName()
pick a new sound file name to use based on OUT_SFILE_NAME
Definition: CGestalt.cpp:186
CThread * mThread
Definition: FileIO.h:43
static unsigned numOutChannels()
default number of output channels
Definition: CGestalt.cpp:59
int SoundFileFormat
Definition: SoundFile.h:65
Buffer mOutBuffer
Definition: FileIO.h:47
virtual void nextBuffer(Buffer &outputBuffer)
get a buffer of Frames – this is the core CSL "pull" function; the given buffer can be written into...
Definition: CSL_Core.cpp:726
void freeBuffers()
fcn to free them
Definition: CSL_Core.cpp:141
#define GET_TIME(val)
Definition: CSL_Core.h:700
void zeroBuffers()
fill all data with 0
Definition: CSL_Core.cpp:173
unsigned mDuration
the file's buffer rate;
Definition: FileIO.h:27
void setSize(unsigned numChannels, unsigned numFrames)
Definition: CSL_Core.cpp:75
#define kSoundFileFormatSND
Definition: SoundFile.h:61
void writeNextBuffer()
Definition: FileIO.cpp:95
UnitGenerator * mGraph
the root of my client DSP graph, often a mixer or panner
Definition: CSL_Core.h:777
string mPath
Definition: FileIO.h:38
static unsigned blockSize()
the default block size
Definition: CGestalt.cpp:57
virtual void start()
Definition: CSL_Core.h:761
void open()
Definition: FileIO.cpp:27
IO – the abstract I/O scheduling class; subclasses interface to specific I/O APIs.
Definition: CSL_Core.h:752
#define kSoundFileFormatWAV
Definition: SoundFile.h:59
static unsigned frameRate()
default frame rate
Definition: CGestalt.cpp:51
long mThisSec
Definition: CSL_Core.h:797
void close()
open/close start/stop methods
Definition: FileIO.cpp:85
Buffer mInBuffer
Definition: FileIO.h:46
void stop()
Definition: FileIO.cpp:77
unsigned mNumFramesPlayed
counter of frames I've played
Definition: CSL_Core.h:784
void printTimeStatistics(struct timeval *tthen, struct timeval *tnow, long *tsecond, long *ttimeSum, long *ttimeVals)
Definition: CSL_Core.cpp:1486
static CThread * MakeThread()
factory method
FileIO(char *path=NULL)
< the path name determines the file type, e.g., xx.aiff, zz.snd, or yy.wav
Definition: FileIO.cpp:10
SoundFile * mFile
Definition: FileIO.h:42
bool sleepUsec(float dur)
Misc. global functions in the csl namespace.
Definition: CGestalt.cpp:345
struct timeval mThen mNow
used for getting the real time
Definition: CSL_Core.h:796
void allocateBuffers()
fcn to malloc storage buffers
Definition: CSL_Core.cpp:122
static void * threadFunction(void *)
Definition: FileIO.cpp:106
long mTimeVals
Definition: CSL_Core.h:797
bool mIsThreadRunning
is the background thread running?
Definition: FileIO.h:26
Base class of CSL exceptions (written upper-case). Has a string message.
FileIO.h – IO using a sound file for storing output sample data.
Definition: FileIO.h:19