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

/home/vlg/develop/gwavmerger/raven/WavPause.cpp

Go to the documentation of this file.
00001 // -*- c++ -*-
00002 // Generated by assa-genesis
00003 //------------------------------------------------------------------------------
00004 // $Id: WavPause.cpp,v 1.4 2003/02/07 05:15:08 vlg Exp $
00005 //------------------------------------------------------------------------------
00006 //                            WavPause.cpp
00007 //------------------------------------------------------------------------------
00008 //
00009 // Author : Vladislav Grinchenko
00010 // Date   : Sun Dec  9 17:14:29 2001
00011 //
00012 //------------------------------------------------------------------------------
00013 
00014 #include <iostream>
00015 #include <limits.h>
00016 
00017 #include "WavPause-main.h"
00018 #include "WavPause.h"
00019 
00020 using ASSA::APP;
00021 using ASSA::ERROR;
00022 using ASSA::TRACE;
00023 
00024 // Static declarations mandated by Singleton class
00025 
00026 WavPause* ASSA::Singleton<WavPause>::m_instance;
00027 ASSA::Destroyer<WavPause> ASSA::Singleton<WavPause>::m_destroyer;
00028 
00029 const unsigned short int WavPause::FRAME_BUFFER_SIZE = USHRT_MAX;
00030 
00031 //------------------------------------------------------------------------------
00032 // class info member functions
00033 //------------------------------------------------------------------------------
00034 void 
00035 
00036 info::dump () const 
00037 {
00038     trace("info::dump");
00039 
00040     DL((APP,"in_file_format = %d\n", in_file_format));
00041     DL((APP,"version        = %d\n", version));
00042     DL((APP,"sample_format  = %d\n", sample_format));
00043     DL((APP,"sample_width   = %d\n", sample_width));
00044     DL((APP,"channel_count  = %d\n", channel_count));
00045     DL((APP,"frame_size     = %d\n", frame_size));
00046     DL((APP,"sample_rate    = %f\n", sample_rate));
00047     DL((APP,"total_frames   = %d\n", total_frames));
00048 }
00049 
00050 //------------------------------------------------------------------------------
00051 // class WavPause member functions
00052 //------------------------------------------------------------------------------
00053 
00054 WavPause::WavPause ()
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 }
00083 
00084 void
00085 WavPause::initServer ()
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 }
00101 
00102 void
00103 WavPause::processServer ()
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 }
00224 
00225 int
00226 
00227 WavPause::read_input_file ()
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 }
00257 

Generated on Tue Feb 11 23:05:19 2003 for gwavmerger by doxygen1.2.14 written by Dimitri van Heesch, © 1997-2002