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

TestSnd.cc

Go to the documentation of this file.
00001 /*
00002 
00003     Test program for SoundSrv
00004     Copyright (C) 1999-2000 Jussi Laako
00005 
00006     This program is free software; you can redistribute it and/or modify
00007     it under the terms of the GNU General Public License as published by
00008     the Free Software Foundation; either version 2 of the License, or
00009     (at your option) any later version.
00010 
00011     This program is distributed in the hope that it will be useful,
00012     but WITHOUT ANY WARRANTY; without even the implied warranty of
00013     MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
00014     GNU General Public License for more details.
00015 
00016     You should have received a copy of the GNU General Public License
00017     along with this program; if not, write to the Free Software
00018     Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
00019 
00020 */
00021 
00022 
00023 #include <stdio.h>
00024 #include <stdlib.h>
00025 #include <string.h>
00026 #include <signal.h>
00027 
00028 #include <dsp/DSPOp.hh>
00029 
00030 #include "Config.h"
00031 #include "Audio.hh"
00032 #include "Messages.hh"
00033 #include "SockClie.hh"
00034 #include "SockOp.hh"
00035 
00036 
00037 bool bRun = true;
00038 
00039 
00040 void SigHandler(int iSignal)
00041 {
00042     switch (iSignal)
00043     {
00044         case SIGINT:
00045             printf("SIGINT received\n");
00046             bRun = false;
00047             break;
00048         default:
00049             printf("Uknown signal received\n");
00050     }
00051 }
00052 
00053 
00054 int main(int argc, char *argv[])
00055 {
00056     int iFormat = AFMT_S16_LE;
00057     int iSampleRate;
00058     int iSockHandle;
00059     int iBytesRead;
00060     int iLastReadRes;
00061     int iDataCount = 8192 / sizeof(GDT);
00062     const int iMsgSize = 8192;
00063     char cpMsgBuf[iMsgSize];
00064     signed short sspOutBuf[iDataCount];
00065     GDT fDataBuf[iDataCount];
00066     stSoundStart sSndStart;
00067     audio_buf_info sBufInfo;
00068     clSockClie Client;
00069     clSoundMsg SoundMsg;
00070     clDSPOp DSP;
00071     clSockOp *Socket;
00072     clAudio *Audio;
00073 
00074     if (argc < 3)
00075     {
00076         printf("%s <host> <port> [device]\n", argv[0]);
00077         return 1;
00078     }
00079     signal(SIGINT, SigHandler);
00080     printf("Connecting %s:%s...\n", argv[1], argv[2]);
00081     iSockHandle = Client.Connect(argv[1], NULL, atoi(argv[2]));
00082     if (iSockHandle < 0)
00083     {
00084         printf("Unable to connect specified host!\n");
00085         return 1;
00086     }
00087     printf("Connected, getting starting message...\n");
00088     Socket = new clSockOp(iSockHandle);
00089     if (Socket->Read(cpMsgBuf, GLOBAL_HEADER_LEN) < GLOBAL_HEADER_LEN)
00090     {
00091         printf("Error reading first message!\n");
00092         return 1;
00093     }
00094     SoundMsg.GetStart(cpMsgBuf, &sSndStart);
00095     printf("Got, opening audio device...\n");
00096     if (argc > 3)
00097     {
00098         iSampleRate = (int) (sSndStart.dSampleRate + 0.5);
00099         Audio = new clAudio(argv[3], &iFormat, &iSampleRate,
00100             &sSndStart.iChannels, AUDIO_WRITE);
00101     }
00102     else
00103     {
00104         iSampleRate = (int) (sSndStart.dSampleRate + 0.5);
00105         Audio = new clAudio("/dev/dsp", &iFormat, &iSampleRate, 
00106             &sSndStart.iChannels, AUDIO_WRITE);
00107     }
00108     printf("Audio sr: %g  ch: %i  ec: %i\n", sSndStart.dSampleRate, 
00109         sSndStart.iChannels, Audio->GetError());
00110     memset(sspOutBuf, 0x00, iDataCount * sizeof(signed short));
00111     do
00112     {
00113         Audio->Write(sspOutBuf, iDataCount * sizeof(signed short));
00114         Audio->GetOutBufInfo(&sBufInfo);
00115     } while (sBufInfo.bytes >= (int) (iDataCount * sizeof(signed short)));
00116     printf("Now playing data, press Ctrl-C to stop...\n");
00117     while (bRun)
00118     {
00119         iBytesRead = 0;
00120         do {
00121             iLastReadRes = Socket->Read(&cpMsgBuf[iBytesRead], 
00122                 iMsgSize - iBytesRead);
00123             iBytesRead += iLastReadRes;
00124         } while (iBytesRead < iMsgSize && bRun && iLastReadRes > 0);
00125         if (iLastReadRes < 0)
00126         {
00127             printf("Receive error!\n");
00128             bRun = false;
00129         }
00130         SoundMsg.GetData(cpMsgBuf, fDataBuf, iDataCount);
00131         DSP.Convert(sspOutBuf, fDataBuf, iDataCount, false);
00132         Audio->Write(sspOutBuf, iDataCount * sizeof(signed short));
00133     }
00134     printf("Closing down...\n");
00135     Socket->Shutdown(2);
00136     delete Audio;
00137     delete Socket;
00138     printf("Exit.\n");
00139     return 0;
00140 }
00141 

Generated on Sun Oct 26 19:11:23 2003 for HASAS by doxygen 1.3.3