rasdaman complete source
rviewSound.hh
Go to the documentation of this file.
1 /*
2 * This file is part of rasdaman community.
3 *
4 * Rasdaman community is free software: you can redistribute it and/or modify
5 * it under the terms of the GNU General Public License as published by
6 * the Free Software Foundation, either version 3 of the License, or
7 * (at your option) any later version.
8 *
9 * Rasdaman community is distributed in the hope that it will be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 * GNU General Public License for more details.
13 *
14 * You should have received a copy of the GNU General Public License
15 * along with rasdaman community. If not, see <http://www.gnu.org/licenses/>.
16 *
17 * Copyright 2003, 2004, 2005, 2006, 2007, 2008, 2009 Peter Baumann /
18 rasdaman GmbH.
19 *
20 * For more information please see <http://www.rasdaman.org>
21 * or contact Peter Baumann via <baumann@rasdaman.com>.
22 /
23 
49 #ifndef _RVIEW_SOUND_H_
50 #define _RVIEW_SOUND_H_
51 
52 
53 #include <stdio.h>
54 #include <signal.h>
55 
56 #ifdef __VISUALC__
57 #include <windows.h>
58 #endif
59 
60 
61 #ifdef __HAL_ONLY__
62 #ifndef __VISUALC__
63 #include "bool.h"
64 #endif
65 typedef bool bool;
66 #ifndef TRUE
67 #define TRUE true
68 #define FALSE false
69 #endif
70 #else
71 #include "rviewUtils.hh"
72 #include "rviewDisplay.hh"
73 #endif // HAL
74 
75 
76 
77 /* Default latency of sound playback */
78 #define RVIEW_SND_LATENCY 100
79 /* Timer interval relative to sample buffer runtime */
80 #define RVIEW_SND_RELPERIOD 0.5
81 
82 /* Number of sample buffers (NT only) */
83 #define RVIEW_SND_BUFFERS 3
84 
85 
86 enum rviewSoundFormat
87 {
88  rsf_none,
89  rsf_lin8,
90  rsf_ulin8,
91  rsf_ulaw8,
92  rsf_lin16
93 };
94 
95 
96 
97 /*
98  * soundPlayer: hardware abstraction layer
99  */
100 
101 class soundPlayer
102 {
103 public:
104 
105  // Base constructor
106  soundPlayer(void);
107  // Constructor for input from file
108  soundPlayer(int frq, int ch, FILE *fp, rviewSoundFormat fmt, int lat=RVIEW_SND_LATENCY);
109  // Constructor for 8bit linear (default)
110  soundPlayer(int frq, int ch, const signed char *data, int len, rviewSoundFormat fmt=rsf_lin8, int lat=RVIEW_SND_LATENCY);
111  // Constructor for 8bit ulaw (default)
112  soundPlayer(int frq, int ch, const unsigned char *data, int len, rviewSoundFormat fmt=rsf_ulaw8, int lat=RVIEW_SND_LATENCY);
113  // Constructor for 16bit linear (default)
114  soundPlayer(int frq, int ch, const short *data, int len, rviewSoundFormat fmt=rsf_lin16, int lat=RVIEW_SND_LATENCY);
115 
116  ~soundPlayer(void);
117 
118  // For changing during playback
119  int newSample(int frq, int ch, FILE *fp, rviewSoundFormat fmt, int lat=RVIEW_SND_LATENCY);
120  int newSample(int frq, int ch, const signed char *data, int len, rviewSoundFormat=rsf_lin8, int lat=RVIEW_SND_LATENCY);
121  int newSample(int frq, int ch, const unsigned char *data, int len, rviewSoundFormat=rsf_ulaw8, int lat=RVIEW_SND_LATENCY);
122  int newSample(int frq, int ch, const short *data, int len, rviewSoundFormat=rsf_lin16, int lat=RVIEW_SND_LATENCY);
123 
124  int playbackGetOffset(void);
125  int playbackActive(void);
126  void playbackSuspend(void);
127  void playbackResume(void);
128  void playbackStop(void);
129  int playbackSetPosition(int position);
130  int playbackLoopMode(int lpMode);
131 
132 #ifdef __VISUALC__
133  void writeSamples(DWORD systime);
134 #else
135  void writeSamples(void);
136 #endif
137 
138 
139 protected:
140 
141  void setupVariables(void);
142  const char *ensureSamplesForDevice(const char *source, int len);
143  const char *ensureSamples(int &num);
144  int configureDevice(int frq, int ch, int len, rviewSoundFormat fmt, int lat);
145  void ensureUlawTable(int ulawsize);
146  void ensureLinearTable(void);
147  char *ensureConvBuff(int size);
148  char *ensureSampleBuff(int size);
149 
150  int setTimerInterval(unsigned int ti);
151  int startTimer(int ap=1);
152  int stopTimer(int ap=1);
153  int handleOutOfData(int dataSize);
154 
155  rviewSoundFormat format;
156  rviewSoundFormat devFormat;
157  int sampleSize, devSampSize;
158  FILE *sampleFile;
159  int dataOffset, inLength;
160  int frequency, channels, latency, samplesWriteahead;
161  int buffSize, cbuffSize;
162  char *buffer, *convBuff;
163  const char *inData;
164  unsigned char *LinToUlaw;
165  short *UlawToLin;
166  int ldUlawSize;
167  int samplesWritten;
168  soundPlayer *suspendedPlayer;
169  int loopMode;
170  bool timerActive;
171 
172  // Unix specifics
173 #ifdef __VISUALC__
174  void freeWaveHeaders(void);
175 
176  HWAVEOUT waveOut;
177  WAVEFORMATEX waveFmt;
178  WAVEHDR waveHdrs[RVIEW_SND_BUFFERS];
179  UINT timerID;
180  DWORD lastSyncTime;
181  int currentHeader;
182  int emptyBuffers;
183 #else
184  int audioDevice;
185  struct timeval lastSyncTime;
186  struct sigaction oact;
187  struct itimerval ovalue;
188 #endif
189 };
190 
191 
192 
193 #ifndef __HAL_ONLY__
194 
195 /*
196  * rviewSoundPlayer: sound player widget
197  */
198 
199 class rviewSoundPlayer: public rviewDisplay
200 {
201 public:
202 
203  rviewSoundPlayer(mdd_frame *mf, unsigned int flags=0);
204  ~rviewSoundPlayer(void);
205 
206  void label(void);
207  int process(wxObject &obj, wxEvent &evt);
208  virtual int openViewer(void);
209 
210  virtual const char *getFrameName(void) const;
211  virtual rviewFrameType getFrameType(void) const;
212  virtual int getViewerType(void) const;
213 
214  void OnSize(int w, int h);
215 
216  int newProjection(void);
217  void prepareToDie(void);
218 
219  typedef struct format_desc
220  {
221  const char *labelName;
222  rviewSoundFormat fmt;
223  int sampleSize;
224  } format_desc;
225 
226  // constants
227  // Sound player window button size
228  static const int sound_bwidth;
229  static const int sound_bheight;
230  // Sound player window slider height
231  static const int sound_sheight;
232  // Sound player window text size
233  static const int sound_twidth;
234  static const int sound_theight;
235  // Sount player choice size
236  static const int sound_cwidth;
237  static const int sound_cheight;
238  // Sound player window ctrl area size
239  static const int sound_ctrly;
240  // Sound player frame size
241  static const int sound_width;
242  static const int sound_height;
243  // Number of latency entries
244  static const int sound_latencies;
245 
246 
247 protected:
248 
249  int buildSample(void);
250  int newSample(void);
251  int startPlayback(void);
252  int stopPlayback(void);
253  void setSlider(int offset);
254  bool setLoopMode(bool lm);
255 
256  soundPlayer player;
257  rviewButton *toStart, *toEnd;
258  rviewButton *pbPause, *pbStart, *pbStop, *pbLoop;
259  rviewSlider *slider;
260  rviewText *frqWidget;
261  rviewChoice *fmtWidget, *latWidget;
262  int frequency, channels, latency;
263  int lastOffset;
264  int *latencies;
265  void *sampleBuffer;
266  int sampleLength;
267  bool paused;
268  bool playbackOn;
269  bool loopMode;
270  int dim1, dim2;
271  int typeLength;
272  int currentFormat;
273  unsigned int freeDims;
274 
275  static const format_desc soundFormatDesc[];
276 };
277 
278 #endif // HAL
279 
280 #endif