-rw-r--r-- | library/sound.cpp | 63 |
1 files changed, 34 insertions, 29 deletions
diff --git a/library/sound.cpp b/library/sound.cpp index 602fcf0..1ff3b3f 100644 --- a/library/sound.cpp +++ b/library/sound.cpp | |||
@@ -81,113 +81,118 @@ static int WAVsoundDuration(const QString& filename) | |||
81 | (void)input.at(input.at()+chunk.size-4); | 81 | (void)input.at(input.at()+chunk.size-4); |
82 | } | 82 | } |
83 | } else if ( qstrncmp(chunk.id,"fmt ",4) == 0 ) { | 83 | } else if ( qstrncmp(chunk.id,"fmt ",4) == 0 ) { |
84 | if ( input.readBlock((char*)&chunkdata,sizeof(chunkdata)) != sizeof(chunkdata) ) | 84 | if ( input.readBlock((char*)&chunkdata,sizeof(chunkdata)) != sizeof(chunkdata) ) |
85 | return 0; | 85 | return 0; |
86 | #define WAVE_FORMAT_PCM 1 | 86 | #define WAVE_FORMAT_PCM 1 |
87 | if ( chunkdata.formatTag != WAVE_FORMAT_PCM ) { | 87 | if ( chunkdata.formatTag != WAVE_FORMAT_PCM ) { |
88 | //qDebug("WAV file: UNSUPPORTED FORMAT %d",chunkdata.formatTag); | 88 | //qDebug("WAV file: UNSUPPORTED FORMAT %d",chunkdata.formatTag); |
89 | return 0; | 89 | return 0; |
90 | } | 90 | } |
91 | } else { | 91 | } else { |
92 | //qDebug("skip %.4s chunk",chunk.id); | 92 | //qDebug("skip %.4s chunk",chunk.id); |
93 | // ignored chunk | 93 | // ignored chunk |
94 | if ( chunk.size < 10000000 ) | 94 | if ( chunk.size < 10000000 ) |
95 | (void)input.at(input.at()+chunk.size); | 95 | (void)input.at(input.at()+chunk.size); |
96 | } | 96 | } |
97 | } | 97 | } |
98 | 98 | ||
99 | //qDebug("%dms",total); | 99 | //qDebug("%dms",total); |
100 | return total; | 100 | return total; |
101 | } | 101 | } |
102 | 102 | ||
103 | class SoundData : public QSound { | 103 | class SoundData : public QSound { |
104 | public: | 104 | public: |
105 | SoundData(const QString& name) : | 105 | SoundData ( const QString& name ) : |
106 | QSound(Resource::findSound(name)), | 106 | QSound ( Resource::findSound ( name )), |
107 | filename(Resource::findSound(name)) | 107 | filename ( Resource::findSound ( name )) |
108 | { | 108 | { |
109 | loopsleft=0; | 109 | loopsleft=0; |
110 | } | 110 | ms = WAVsoundDuration(filename); |
111 | } | ||
111 | 112 | ||
112 | void playLoop(int loopcnt = -1) | 113 | void playLoop ( int loopcnt = -1 ) |
113 | { | 114 | { |
114 | // needs server support | 115 | // needs server support |
115 | loopsleft = loopcnt; | 116 | loopsleft = loopcnt; |
116 | 117 | ||
117 | int ms = WAVsoundDuration(filename); | 118 | if ( ms ) |
118 | if ( ms ) | 119 | startTimer ( ms > 50 ? ms-50 : 0 ); // 50 for latency |
119 | startTimer(ms > 50 ? ms-50 : 0); // 50 for latency | 120 | play ( ); |
120 | play(); | 121 | } |
121 | } | ||
122 | 122 | ||
123 | void timerEvent ( QTimerEvent *e ) | 123 | void timerEvent ( QTimerEvent *e ) |
124 | { | 124 | { |
125 | if (loopsleft >= 0) { | 125 | if ( loopsleft >= 0 ) { |
126 | if (--loopsleft <= 0) | 126 | if ( --loopsleft <= 0 ) { |
127 | killTimer (e->timerId()); | 127 | killTimer ( e-> timerId ( )); |
128 | return; | 128 | loopsleft = 0; |
129 | } | 129 | return; |
130 | play(); | 130 | } |
131 | } | 131 | } |
132 | play(); | ||
133 | } | ||
132 | 134 | ||
133 | bool isFinished ( ) const | 135 | bool isFinished ( ) const |
134 | { | 136 | { |
135 | return ( loopsleft == 0 ); | 137 | return ( loopsleft == 0 ); |
136 | } | 138 | } |
137 | 139 | ||
138 | private: | 140 | private: |
139 | QString filename; | 141 | QString filename; |
140 | int loopsleft; | 142 | int loopsleft; |
143 | int ms; | ||
141 | }; | 144 | }; |
145 | |||
142 | #endif | 146 | #endif |
143 | 147 | ||
144 | Sound::Sound(const QString& name) | 148 | Sound::Sound(const QString& name) |
145 | { | 149 | { |
146 | #ifndef QT_NO_SOUND | 150 | #ifndef QT_NO_SOUND |
147 | d = new SoundData(name); | 151 | d = new SoundData(name); |
148 | #endif | 152 | #endif |
149 | } | 153 | } |
150 | 154 | ||
151 | Sound::~Sound() | 155 | Sound::~Sound() |
152 | { | 156 | { |
153 | #ifndef QT_NO_SOUND | 157 | #ifndef QT_NO_SOUND |
154 | delete d; | 158 | delete d; |
155 | #endif | 159 | #endif |
156 | } | 160 | } |
157 | 161 | ||
158 | void Sound::play() | 162 | void Sound::play() |
159 | { | 163 | { |
160 | #ifndef QT_NO_SOUND | 164 | #ifndef QT_NO_SOUND |
161 | d->killTimers(); | ||
162 | d->playLoop(1); | 165 | d->playLoop(1); |
163 | #endif | 166 | #endif |
164 | } | 167 | } |
165 | 168 | ||
166 | void Sound::playLoop() | 169 | void Sound::playLoop() |
167 | { | 170 | { |
168 | #ifndef QT_NO_SOUND | 171 | #ifndef QT_NO_SOUND |
169 | d->killTimers(); | 172 | d->killTimers(); |
170 | d->playLoop(); | 173 | d->playLoop(); |
171 | #endif | 174 | #endif |
172 | } | 175 | } |
173 | 176 | ||
174 | void Sound::stop() | 177 | void Sound::stop() |
175 | { | 178 | { |
176 | #ifndef QT_NO_SOUND | 179 | #ifndef QT_NO_SOUND |
177 | d->killTimers(); | 180 | d->killTimers(); |
178 | #endif | 181 | #endif |
179 | } | 182 | } |
180 | 183 | ||
181 | bool Sound::isFinished() const | 184 | bool Sound::isFinished() const |
182 | { | 185 | { |
183 | #ifndef QT_NO_SOUND | 186 | #ifndef QT_NO_SOUND |
184 | return d->isFinished(); | 187 | return d->isFinished(); |
188 | #else | ||
189 | return true; | ||
185 | #endif | 190 | #endif |
186 | } | 191 | } |
187 | 192 | ||
188 | void Sound::soundAlarm() | 193 | void Sound::soundAlarm() |
189 | { | 194 | { |
190 | #ifndef QT_NO_COP | 195 | #ifndef QT_NO_COP |
191 | QCopEnvelope( "QPE/TaskBar", "soundAlarm()" ); | 196 | QCopEnvelope( "QPE/TaskBar", "soundAlarm()" ); |
192 | #endif | 197 | #endif |
193 | } | 198 | } |