Main Page   Namespace List   Class Hierarchy   Compound List   File List   Namespace Members   Compound Members   File Members  

WavPause Class Reference

#include <WavPause.h>

Collaboration diagram for WavPause:

Collaboration graph
[legend]
List of all members.

Public Methods

 WavPause ()
virtual void initServer ()
virtual void processServer ()
int get_exit_value () const
void set_exit_value (int v_)

Private Methods

int read_input_file ()

Private Attributes

info m_info
string m_output_file
string m_input_file
int m_exit_value
bool m_percent
double m_duration
int m_multiplier
bool m_relative

Static Private Attributes

const unsigned short int FRAME_BUFFER_SIZE = USHRT_MAX

Constructor & Destructor Documentation

WavPause::WavPause  
 

Definition at line 54 of file WavPause.cpp.

References m_duration, m_input_file, m_multiplier, m_output_file, and m_percent.

00055     : m_exit_value (0),
00056       m_percent (false),
00057       m_duration (-1),
00058       m_multiplier (2),     // default
00059       m_relative (true)     // default
00060 {
00061     // ---Configuration---
00062     rm_opt ('f', "config-file"  );
00063     rm_opt ('n', "instance"     );
00064     rm_opt ('p', "port"         );
00065 
00066     // ---Process bookkeeping---
00067     rm_opt ('b', "daemon"       );
00068     rm_opt ('l', "pidfile"      );
00069     rm_opt ('L', "ommit-pidfile");
00070 
00071     // Default debugging is total silence
00072     m_log_file = "/dev/null";
00073     m_debug_mask = 0;
00074 
00075     // ---Project-specific---
00076     add_opt ('o', "output-file", &m_output_file);
00077     add_opt ('i', "input-file",  &m_input_file);
00078     add_opt ('l', "duration",    &m_duration);
00079     add_opt ('M', "multiplier",  &m_multiplier);
00080 
00081     add_flag_opt ('p', "percent", &m_percent);
00082 }


Member Function Documentation

int WavPause::get_exit_value   const [inline]
 

Definition at line 74 of file WavPause.h.

References m_exit_value.

Referenced by main.

00074 { return m_exit_value; }

void WavPause::initServer   [virtual]
 

Definition at line 85 of file WavPause.cpp.

References m_duration, m_multiplier, and m_relative.

Referenced by main.

00086 {
00087     trace("WavPause::initServer");
00088     ASSA::Log::disable_timestamp ();
00089 
00090     if (m_duration > 0) {
00091         m_relative = false; // absolute
00092         DL((TRACE,"Absolute mode is enabled\n"));
00093         DL((TRACE,"Duration = %5.2f seconds\n", m_duration));
00094     }
00095     else {
00096         DL((TRACE,"Relative mode is enabled\n"));
00097         DL((TRACE,"Multiplier = %dx\n", m_multiplier));
00098     }
00099     DL((APP,"Service has been initialized\n"));
00100 }

void WavPause::processServer   [virtual]
 

Definition at line 103 of file WavPause.cpp.

References info::channel_count, FRAME_BUFFER_SIZE, info::frame_size, info::in_file_format, m_duration, m_info, m_input_file, m_multiplier, m_output_file, read_input_file, info::sample_format, info::sample_rate, info::sample_width, set_exit_value, and info::total_frames.

Referenced by main.

00104 {
00105     trace("WavPause::processServer");
00106 
00107     if (stopServer ()) {
00108         return;
00109     }
00110 
00111     /*------------------------------------------------------------------------
00112      * Initialize audio parameters
00113      *------------------------------------------------------------------------*/
00114     if (m_relative) {
00115         if (read_input_file () == -1) {
00116             DL((APP,"Failed reading input file\n"));
00117             set_exit_value (1);
00118             return;
00119         }
00120     }
00121     /*------------------------------------------------------------------------
00122      * Setup audio parameters for output stream
00123      *------------------------------------------------------------------------*/
00124     AFfilesetup out_file_setup = afNewFileSetup ();
00125 
00126     // Configure the file format parameter in an AFfilesetup.
00127     // AF_FILE_WAV: Waveform RIFF.
00128 
00129     afInitFileFormat (out_file_setup, m_info.in_file_format);
00130 
00131     // Mine is  2-bit integer (2's compliment) little endian
00132     afInitSampleFormat (out_file_setup, AF_DEFAULT_TRACK, 
00133                         m_info.sample_format, m_info.sample_width);
00134 
00135     // Number of channels: 2-stereo
00136     afInitChannels (out_file_setup, AF_DEFAULT_TRACK, m_info.channel_count);
00137 
00138     // Rate: 44100 Hz
00139     afInitRate (out_file_setup, AF_DEFAULT_TRACK, m_info.sample_rate);
00140 
00141     // Use default name if it is not specified with --output option
00142     if (m_output_file.size () == 0) {
00143         m_output_file = m_input_file;
00144         m_output_file.replace (m_output_file.find ('.'), 
00145                                m_output_file.size (), 
00146                                "_pause.wav");
00147     }
00148 
00149     /*------------------------------------------------------------------------
00150      * Generate pause file
00151      *------------------------------------------------------------------------*/
00152 
00153     DL((APP,"Output file: \"%s\"\n", m_output_file.c_str ()));
00154     unlink (m_output_file.c_str ());
00155 
00156     AFfilehandle out_file;
00157 
00158     out_file = afOpenFile (m_output_file.c_str (), "w", out_file_setup);
00159 
00160     if (out_file == AF_NULL_FILEHANDLE) {
00161         EL((ERROR,"Could not open file \"%s\" for writing\n",
00162             m_output_file.c_str ()));
00163         set_exit_value (1);
00164         return;
00165     }
00166     // Set the output file's virtual audio format parameters
00167 
00168     afSetVirtualChannels     (out_file, AF_DEFAULT_TRACK, 
00169                               m_info.channel_count);
00170     afSetVirtualSampleFormat (out_file, AF_DEFAULT_TRACK, 
00171                               m_info.sample_format, m_info.sample_width);
00172     afFreeFileSetup (out_file_setup);
00173 
00174     char* frame_buffer = new char [m_info.frame_size * FRAME_BUFFER_SIZE];
00175     memset (frame_buffer, 0, m_info.frame_size * FRAME_BUFFER_SIZE);
00176 
00177     DL((APP,"frame_size   = %d\n", m_info.frame_size));
00178     DL((APP,"total_frames = %d\n", m_info.total_frames));
00179 
00180     int frames_count = 0;
00181     int frames_write = 0;
00182     int frames_written = 0;
00183     bool aborted = false;
00184 
00185     if (m_relative) {       // Output lenght is a multiple of input
00186         m_info.total_frames *= m_multiplier; 
00187     }
00188     else {          // Output lenght is in seconds
00189         m_info.total_frames = long (m_info.sample_rate * m_duration);
00190     }
00191 
00192     DL((APP,"Recalculated total_frames = %d\n", m_info.total_frames));
00193 
00194     frames_write = m_info.total_frames > FRAME_BUFFER_SIZE ?
00195         FRAME_BUFFER_SIZE : m_info.total_frames;
00196 
00197     while (frames_count < m_info.total_frames) {
00198         frames_written = afWriteFrames (out_file, AF_DEFAULT_TRACK, 
00199                                         frame_buffer, frames_write);
00200         if (frames_written < 0) {
00201             aborted = true;
00202             break;
00203         }
00204         frames_count += frames_written;
00205         if (m_info.total_frames - frames_count < FRAME_BUFFER_SIZE) {
00206             frames_write = m_info.total_frames - frames_count;
00207         }
00208         if (m_percent) {    // Report percentage completed so far
00209             std::cout << long (frames_count * 100 / m_info.total_frames) 
00210                       << std::endl;
00211         }
00212     }
00213     if (aborted) {
00214         DL((APP,"afWriteFrames() failed\n"));
00215         set_exit_value (1);
00216     }
00217 
00218     delete [] frame_buffer;
00219     afCloseFile (out_file);
00220 
00221     m_reactor.stopReactor ();
00222     DL((APP,"Service stopped!\n"));
00223 }

int WavPause::read_input_file   [private]
 

Definition at line 227 of file WavPause.cpp.

References info::channel_count, info::dump, info::frame_size, info::in_file_format, m_info, m_input_file, info::sample_format, info::sample_rate, info::sample_width, set_exit_value, info::total_frames, and info::version.

Referenced by processServer.

00228 {
00229     trace("WavPause::read_input_file");
00230 
00231     AFfilehandle in_file;
00232 
00233     in_file = afOpenFile (m_input_file.c_str (), "r", NULL);
00234 
00235     if (in_file == NULL) {
00236         EL((ERROR,"File \"%s\" open error\n", m_input_file.c_str ()));
00237         set_exit_value (1);
00238         return -1;
00239     }
00240 
00241     afGetSampleFormat(in_file, AF_DEFAULT_TRACK, 
00242                       &m_info.sample_format, &m_info.sample_width);
00243 
00244     m_info.in_file_format = afGetFileFormat (in_file, &m_info.version);
00245     m_info.channel_count  = afGetChannels   (in_file, AF_DEFAULT_TRACK);
00246     m_info.sample_rate    = afGetRate       (in_file, AF_DEFAULT_TRACK);
00247     m_info.total_frames   = afGetFrameCount (in_file, AF_DEFAULT_TRACK); 
00248     m_info.frame_size     = 
00249         (int) afGetVirtualFrameSize (in_file, AF_DEFAULT_TRACK, 1);
00250     
00251     DL((APP,"== Input File info ==\n"));
00252     m_info.dump ();
00253 
00254     afCloseFile (in_file);
00255     return 0;
00256 }

void WavPause::set_exit_value int    v_ [inline]
 

Definition at line 97 of file WavPause.h.

References m_exit_value.

Referenced by processServer, and read_input_file.

00098 { 
00099     m_exit_value = v_;
00100     setStopServerFlag ();
00101 }


Member Data Documentation

const unsigned short int WavPause::FRAME_BUFFER_SIZE = USHRT_MAX [static, private]
 

Definition at line 29 of file WavPause.cpp.

Referenced by processServer.

double WavPause::m_duration [private]
 

Definition at line 89 of file WavPause.h.

Referenced by initServer, processServer, and WavPause.

int WavPause::m_exit_value [private]
 

Definition at line 87 of file WavPause.h.

Referenced by get_exit_value, and set_exit_value.

info WavPause::m_info [private]
 

Definition at line 83 of file WavPause.h.

Referenced by processServer, and read_input_file.

string WavPause::m_input_file [private]
 

Definition at line 85 of file WavPause.h.

Referenced by processServer, read_input_file, and WavPause.

int WavPause::m_multiplier [private]
 

Definition at line 90 of file WavPause.h.

Referenced by initServer, processServer, and WavPause.

string WavPause::m_output_file [private]
 

Definition at line 84 of file WavPause.h.

Referenced by processServer, and WavPause.

bool WavPause::m_percent [private]
 

Definition at line 88 of file WavPause.h.

Referenced by WavPause.

bool WavPause::m_relative [private]
 

Definition at line 91 of file WavPause.h.

Referenced by initServer.


The documentation for this class was generated from the following files:
Generated on Tue Feb 11 23:05:34 2003 for gwavmerger by doxygen1.2.14 written by Dimitri van Heesch, © 1997-2002