From fca7c33794c7b909041c77e417f4da6d65e38783 Mon Sep 17 00:00:00 2001 From: Audric Date: Fri, 20 Mar 2026 11:54:18 +0100 Subject: [PATCH] Security hardening: fix 19 of 22 vulnerabilities found in audit Critical: - Trunk HMAC-SHA1 auth: MsgTrunkHello now carries nonce+digest, peer disconnected on secret mismatch (was loaded but never verified) High: - Pre-hello guard: reject all trunk messages before handshake completes - CSPRNG for password generator: random.choice -> secrets.choice Medium: - Path traversal protection on callsign-based file operations - Replace assert() on network-derived UDP data with error handling - HTTP server connection throttling (matching main TCP server) - try/catch on regex compilation in callsignOk()/emailOk() - Per-link trunk talker tracking (clearAllTrunkTalkers scoped to peer) - Regenerate priority nonce on each new trunk connection - Cap active TGs and trunk talkers at 10000 each - Don't reset heartbeat for unauthenticated client app messages - Fix RENEW_AFTER integer division (2/3 -> 2.0/3.0) Low: - JSON-escape HTTP method in error response - snprintf for pidfile error message - MsgAuthResponse::digest() null check on empty vector - Check unpack() return values in AsyncMsg.h vector/map/string - Replace stack VLA with std::string; reserve+push_back vs resize - Require TalkerStart before accepting trunk audio --- .gitignore | 34 + Dockerfile | 41 + README.md | 237 ++ docker-compose.yml | 15 + docs/DEPLOYMENT_ITALY.md | 277 ++ docs/DEPLOYMENT_ITALY_IT.md | 281 ++ docs/DOCKER.md | 149 + docs/INSTALL.md | 206 ++ docs/TRUNK_PROTOCOL.md | 328 ++ src/CMakeLists.txt | 121 + src/async/.gitignore | 2 + src/async/CMakeLists.txt | 7 + src/async/ChangeLog | 602 ++++ src/async/audio/.gitignore | 3 + src/async/audio/AsyncAudioAmp.h | 166 ++ src/async/audio/AsyncAudioClipper.h | 173 ++ src/async/audio/AsyncAudioCompressor.cpp | 269 ++ src/async/audio/AsyncAudioCompressor.h | 257 ++ src/async/audio/AsyncAudioContainer.cpp | 140 + src/async/audio/AsyncAudioContainer.h | 265 ++ src/async/audio/AsyncAudioContainerOpus.cpp | 387 +++ src/async/audio/AsyncAudioContainerOpus.h | 218 ++ src/async/audio/AsyncAudioContainerPcm.cpp | 190 ++ src/async/audio/AsyncAudioContainerPcm.h | 194 ++ src/async/audio/AsyncAudioContainerWav.cpp | 253 ++ src/async/audio/AsyncAudioContainerWav.h | 258 ++ src/async/audio/AsyncAudioDebugger.h | 265 ++ src/async/audio/AsyncAudioDecimator.cpp | 183 ++ src/async/audio/AsyncAudioDecimator.h | 170 ++ src/async/audio/AsyncAudioDecoder.cpp | 197 ++ src/async/audio/AsyncAudioDecoder.h | 210 ++ src/async/audio/AsyncAudioDecoderDummy.h | 162 + src/async/audio/AsyncAudioDecoderGsm.cpp | 171 ++ src/async/audio/AsyncAudioDecoderGsm.h | 173 ++ src/async/audio/AsyncAudioDecoderNull.h | 180 ++ src/async/audio/AsyncAudioDecoderOpus.cpp | 264 ++ src/async/audio/AsyncAudioDecoderOpus.h | 192 ++ src/async/audio/AsyncAudioDecoderRaw.h | 162 + src/async/audio/AsyncAudioDecoderS16.cpp | 159 + src/async/audio/AsyncAudioDecoderS16.h | 157 + src/async/audio/AsyncAudioDecoderSpeex.cpp | 220 ++ src/async/audio/AsyncAudioDecoderSpeex.h | 193 ++ src/async/audio/AsyncAudioDelayLine.cpp | 345 +++ src/async/audio/AsyncAudioDelayLine.h | 265 ++ src/async/audio/AsyncAudioDevice.cpp | 424 +++ src/async/audio/AsyncAudioDevice.h | 390 +++ src/async/audio/AsyncAudioDeviceAlsa.cpp | 825 +++++ src/async/audio/AsyncAudioDeviceAlsa.h | 223 ++ src/async/audio/AsyncAudioDeviceFactory.cpp | 187 ++ src/async/audio/AsyncAudioDeviceFactory.h | 197 ++ src/async/audio/AsyncAudioDeviceOSS.cpp | 502 ++++ src/async/audio/AsyncAudioDeviceOSS.h | 215 ++ src/async/audio/AsyncAudioDeviceUDP.cpp | 387 +++ src/async/audio/AsyncAudioDeviceUDP.h | 220 ++ src/async/audio/AsyncAudioEncoder.cpp | 197 ++ src/async/audio/AsyncAudioEncoder.h | 202 ++ src/async/audio/AsyncAudioEncoderDummy.h | 177 ++ src/async/audio/AsyncAudioEncoderGsm.cpp | 187 ++ src/async/audio/AsyncAudioEncoderGsm.h | 182 ++ src/async/audio/AsyncAudioEncoderNull.h | 181 ++ src/async/audio/AsyncAudioEncoderOpus.cpp | 683 +++++ src/async/audio/AsyncAudioEncoderOpus.h | 439 +++ src/async/audio/AsyncAudioEncoderRaw.h | 167 ++ src/async/audio/AsyncAudioEncoderS16.cpp | 177 ++ src/async/audio/AsyncAudioEncoderS16.h | 163 + src/async/audio/AsyncAudioEncoderSpeex.cpp | 376 +++ src/async/audio/AsyncAudioEncoderSpeex.h | 290 ++ src/async/audio/AsyncAudioFifo.cpp | 414 +++ src/async/audio/AsyncAudioFifo.h | 300 ++ src/async/audio/AsyncAudioFilter.cpp | 245 ++ src/async/audio/AsyncAudioFilter.h | 213 ++ src/async/audio/AsyncAudioFsf.cpp | 258 ++ src/async/audio/AsyncAudioFsf.h | 225 ++ src/async/audio/AsyncAudioGenerator.h | 329 ++ src/async/audio/AsyncAudioIO.cpp | 480 +++ src/async/audio/AsyncAudioIO.h | 379 +++ src/async/audio/AsyncAudioInterpolator.cpp | 190 ++ src/async/audio/AsyncAudioInterpolator.h | 171 ++ src/async/audio/AsyncAudioJitterFifo.cpp | 325 ++ src/async/audio/AsyncAudioJitterFifo.h | 229 ++ src/async/audio/AsyncAudioLADSPAPlugin.cpp | 732 +++++ src/async/audio/AsyncAudioLADSPAPlugin.h | 392 +++ src/async/audio/AsyncAudioMixer.cpp | 410 +++ src/async/audio/AsyncAudioMixer.h | 189 ++ src/async/audio/AsyncAudioNoiseAdder.cpp | 185 ++ src/async/audio/AsyncAudioNoiseAdder.h | 176 ++ src/async/audio/AsyncAudioPacer.cpp | 316 ++ src/async/audio/AsyncAudioPacer.h | 201 ++ src/async/audio/AsyncAudioPassthrough.h | 201 ++ src/async/audio/AsyncAudioProcessor.cpp | 366 +++ src/async/audio/AsyncAudioProcessor.h | 207 ++ src/async/audio/AsyncAudioReader.cpp | 230 ++ src/async/audio/AsyncAudioReader.h | 181 ++ src/async/audio/AsyncAudioRecorder.cpp | 415 +++ src/async/audio/AsyncAudioRecorder.h | 280 ++ src/async/audio/AsyncAudioSelector.cpp | 508 ++++ src/async/audio/AsyncAudioSelector.h | 241 ++ src/async/audio/AsyncAudioSink.cpp | 290 ++ src/async/audio/AsyncAudioSink.h | 244 ++ src/async/audio/AsyncAudioSource.cpp | 341 +++ src/async/audio/AsyncAudioSource.h | 283 ++ src/async/audio/AsyncAudioSplitter.cpp | 545 ++++ src/async/audio/AsyncAudioSplitter.h | 214 ++ .../audio/AsyncAudioStreamStateDetector.h | 291 ++ src/async/audio/AsyncAudioValve.h | 341 +++ src/async/audio/AsyncSigCAudioSink.h | 216 ++ src/async/audio/AsyncSigCAudioSource.h | 204 ++ src/async/audio/CMakeLists.txt | 155 + src/async/audio/fidlib.c | 2321 +++++++++++++++ src/async/audio/fidlib.h | 74 + src/async/audio/fidlib.txt | 642 ++++ src/async/audio/fidmkf.h | 831 ++++++ src/async/audio/fidrf_cmdlist.h | 479 +++ src/async/core/.gitignore | 3 + src/async/core/AsyncApplication.cpp | 202 ++ src/async/core/AsyncApplication.h | 215 ++ src/async/core/AsyncAtTimer.cpp | 274 ++ src/async/core/AsyncAtTimer.h | 229 ++ src/async/core/AsyncConfig.cpp | 548 ++++ src/async/core/AsyncConfig.h | 779 +++++ src/async/core/AsyncDigest.h | 490 +++ src/async/core/AsyncDnsLookup.cpp | 274 ++ src/async/core/AsyncDnsLookup.h | 393 +++ src/async/core/AsyncDnsLookupWorker.h | 469 +++ src/async/core/AsyncDnsResourceRecord.h | 697 +++++ src/async/core/AsyncEncryptedUdpSocket.cpp | 447 +++ src/async/core/AsyncEncryptedUdpSocket.h | 368 +++ src/async/core/AsyncExec.cpp | 603 ++++ src/async/core/AsyncExec.h | 389 +++ src/async/core/AsyncFactory.h | 317 ++ src/async/core/AsyncFdWatch.cpp | 203 ++ src/async/core/AsyncFdWatch.h | 229 ++ src/async/core/AsyncFileReader.cpp | 271 ++ src/async/core/AsyncFileReader.h | 187 ++ src/async/core/AsyncFramedTcpConnection.cpp | 342 +++ src/async/core/AsyncFramedTcpConnection.h | 335 +++ src/async/core/AsyncHttpServerConnection.cpp | 480 +++ src/async/core/AsyncHttpServerConnection.h | 367 +++ src/async/core/AsyncIpAddress.cpp | 284 ++ src/async/core/AsyncIpAddress.h | 275 ++ src/async/core/AsyncMsg.h | 743 +++++ src/async/core/AsyncPlugin.cpp | 196 ++ src/async/core/AsyncPlugin.h | 205 ++ src/async/core/AsyncPty.cpp | 389 +++ src/async/core/AsyncPty.h | 254 ++ src/async/core/AsyncPtyStreamBuf.cpp | 180 ++ src/async/core/AsyncPtyStreamBuf.h | 169 ++ src/async/core/AsyncSerial.cpp | 585 ++++ src/async/core/AsyncSerial.h | 341 +++ src/async/core/AsyncSerialDevice.cpp | 324 ++ src/async/core/AsyncSerialDevice.h | 203 ++ src/async/core/AsyncSslCertSigningReq.h | 669 +++++ src/async/core/AsyncSslContext.h | 278 ++ src/async/core/AsyncSslKeypair.h | 470 +++ src/async/core/AsyncSslX509.h | 999 +++++++ .../core/AsyncSslX509ExtSubjectAltName.h | 380 +++ src/async/core/AsyncSslX509Extensions.h | 300 ++ src/async/core/AsyncStateMachine.h | 655 ++++ src/async/core/AsyncTcpClient.h | 241 ++ src/async/core/AsyncTcpClientBase.cpp | 399 +++ src/async/core/AsyncTcpClientBase.h | 329 ++ src/async/core/AsyncTcpConnection.cpp | 822 +++++ src/async/core/AsyncTcpConnection.h | 567 ++++ src/async/core/AsyncTcpPrioClient.h | 234 ++ src/async/core/AsyncTcpPrioClientBase.cpp | 826 +++++ src/async/core/AsyncTcpPrioClientBase.h | 354 +++ src/async/core/AsyncTcpServer.h | 233 ++ src/async/core/AsyncTcpServerBase.cpp | 492 +++ src/async/core/AsyncTcpServerBase.h | 253 ++ src/async/core/AsyncTimer.cpp | 198 ++ src/async/core/AsyncTimer.h | 232 ++ src/async/core/AsyncUdpSocket.cpp | 408 +++ src/async/core/AsyncUdpSocket.h | 223 ++ src/async/core/CMakeLists.txt | 66 + src/async/cpp/.gitignore | 3 + src/async/cpp/AsyncCppApplication.cpp | 567 ++++ src/async/cpp/AsyncCppApplication.h | 238 ++ src/async/cpp/AsyncCppDnsLookupWorker.cpp | 600 ++++ src/async/cpp/AsyncCppDnsLookupWorker.h | 203 ++ src/async/cpp/CMakeLists.txt | 45 + src/async/demo/.gitignore | 4 + src/async/demo/AsyncAtTimer_demo.cpp | 56 + src/async/demo/AsyncAudioContainer_demo.cpp | 147 + src/async/demo/AsyncAudioFsf_demo.cpp | 45 + src/async/demo/AsyncAudioIO_demo.cpp | 81 + .../demo/AsyncAudioLADSPAPlugin_demo.cpp | 47 + src/async/demo/AsyncAudioSelector_demo.cpp | 31 + src/async/demo/AsyncConfig_demo.cpp | 95 + src/async/demo/AsyncCppApplication_demo.cpp | 9 + src/async/demo/AsyncDigest_demo.cpp | 118 + src/async/demo/AsyncDnsLookup_demo.cpp | 124 + src/async/demo/AsyncExec_demo.cpp | 65 + src/async/demo/AsyncFactory_demo.cpp | 140 + src/async/demo/AsyncFdWatch_demo.cpp | 47 + src/async/demo/AsyncFramedTcpClient_demo.cpp | 64 + src/async/demo/AsyncFramedTcpServer_demo.cpp | 77 + src/async/demo/AsyncHttpServer_demo.cpp | 115 + src/async/demo/AsyncMsg_demo.cpp | 140 + src/async/demo/AsyncPlugin_demo.cpp | 16 + src/async/demo/AsyncPtyStreamBuf_demo.cpp | 59 + src/async/demo/AsyncQtApplication_demo.cpp | 13 + src/async/demo/AsyncSerial_demo.cpp | 51 + src/async/demo/AsyncSslTcpClient_demo.cpp | 89 + src/async/demo/AsyncSslTcpServer_demo.cpp | 182 ++ src/async/demo/AsyncSslX509_demo.cpp | 121 + src/async/demo/AsyncStateMachine_demo.cpp | 128 + src/async/demo/AsyncTcpClient_demo.cpp | 57 + src/async/demo/AsyncTcpPrioClient_demo.cpp | 62 + src/async/demo/AsyncTcpServer_demo.cpp | 86 + src/async/demo/AsyncTimer_demo.cpp | 41 + src/async/demo/AsyncUdpSocket_demo.cpp | 42 + src/async/demo/CMakeLists.txt | 76 + src/async/demo/DemoPlugin.cpp | 25 + src/async/demo/DemoPluginBase.h | 26 + src/async/demo/test.cfg | 10 + src/async/mainpage.dox | 407 +++ src/async/qt/.gitignore | 4 + src/async/qt/AsyncQtApplication.cpp | 305 ++ src/async/qt/AsyncQtApplication.h | 227 ++ src/async/qt/AsyncQtDnsLookupWorker.cpp | 206 ++ src/async/qt/AsyncQtDnsLookupWorker.h | 169 ++ src/async/qt/AsyncQtTimer.h | 164 + src/async/qt/CMakeLists.txt | 73 + src/cmake/Modules/FindALSA.cmake | 36 + src/cmake/Modules/FindCHOWN.cmake | 17 + src/cmake/Modules/FindCURL.cmake | 56 + src/cmake/Modules/FindDL.cmake | 54 + src/cmake/Modules/FindGCrypt.cmake | 63 + src/cmake/Modules/FindGPIOD.cmake | 94 + src/cmake/Modules/FindGROFF.cmake | 17 + src/cmake/Modules/FindGSM.cmake | 43 + src/cmake/Modules/FindGZIP.cmake | 17 + src/cmake/Modules/FindLADSPA.cmake | 84 + src/cmake/Modules/FindOGG.cmake | 92 + src/cmake/Modules/FindOpus.cmake | 92 + src/cmake/Modules/FindPopt.cmake | 87 + src/cmake/Modules/FindRT.cmake | 54 + src/cmake/Modules/FindRtlSdr.cmake | 66 + src/cmake/Modules/FindSIGC.cmake | 43 + src/cmake/Modules/FindSIGC2.cmake | 40 + src/cmake/Modules/FindSpeex.cmake | 92 + src/cmake/Modules/LibFindMacros.cmake | 99 + src/config.h.in | 13 + src/include/CMakeLists.txt | 46 + src/include/version_include_file.in | 9 + src/misc/.gitignore | 2 + src/misc/CMakeLists.txt | 17 + src/misc/CppStdCompat.h | 10 + src/misc/LogWriter.cpp | 504 ++++ src/misc/LogWriter.h | 200 ++ src/misc/common.cpp | 14 + src/misc/common.h | 287 ++ src/svxlink/CMakeLists.txt | 1 + src/svxlink/reflector/CMakeLists.txt | 55 + src/svxlink/reflector/ProtoVer.h | 215 ++ src/svxlink/reflector/Reflector.cpp | 2650 +++++++++++++++++ src/svxlink/reflector/Reflector.h | 331 ++ src/svxlink/reflector/ReflectorClient.cpp | 1464 +++++++++ src/svxlink/reflector/ReflectorClient.h | 616 ++++ src/svxlink/reflector/ReflectorMsg.h | 1822 ++++++++++++ src/svxlink/reflector/TGHandler.cpp | 497 ++++ src/svxlink/reflector/TGHandler.h | 242 ++ src/svxlink/reflector/TrunkLink.cpp | 587 ++++ src/svxlink/reflector/TrunkLink.h | 163 + src/svxlink/reflector/ca-hook.py.in | 366 +++ src/svxlink/reflector/ca-hook.yaml | 153 + src/svxlink/reflector/genpw/.gitignore | 2 + src/svxlink/reflector/genpw/README.adoc | 103 + src/svxlink/reflector/genpw/genpw.py | 76 + src/svxlink/reflector/genpw/genpw.wsgi | 1 + src/svxlink/reflector/genpw/getpw.py | 15 + src/svxlink/reflector/svxreflector-ca | 300 ++ src/svxlink/reflector/svxreflector-status | 120 + src/svxlink/reflector/svxreflector.conf.in | 101 + src/svxlink/reflector/svxreflector.cpp | 587 ++++ src/versions | 40 + 276 files changed, 73533 insertions(+) create mode 100644 .gitignore create mode 100644 Dockerfile create mode 100644 README.md create mode 100644 docker-compose.yml create mode 100644 docs/DEPLOYMENT_ITALY.md create mode 100644 docs/DEPLOYMENT_ITALY_IT.md create mode 100644 docs/DOCKER.md create mode 100644 docs/INSTALL.md create mode 100644 docs/TRUNK_PROTOCOL.md create mode 100644 src/CMakeLists.txt create mode 100644 src/async/.gitignore create mode 100644 src/async/CMakeLists.txt create mode 100644 src/async/ChangeLog create mode 100644 src/async/audio/.gitignore create mode 100644 src/async/audio/AsyncAudioAmp.h create mode 100644 src/async/audio/AsyncAudioClipper.h create mode 100644 src/async/audio/AsyncAudioCompressor.cpp create mode 100644 src/async/audio/AsyncAudioCompressor.h create mode 100644 src/async/audio/AsyncAudioContainer.cpp create mode 100644 src/async/audio/AsyncAudioContainer.h create mode 100644 src/async/audio/AsyncAudioContainerOpus.cpp create mode 100644 src/async/audio/AsyncAudioContainerOpus.h create mode 100644 src/async/audio/AsyncAudioContainerPcm.cpp create mode 100644 src/async/audio/AsyncAudioContainerPcm.h create mode 100644 src/async/audio/AsyncAudioContainerWav.cpp create mode 100644 src/async/audio/AsyncAudioContainerWav.h create mode 100644 src/async/audio/AsyncAudioDebugger.h create mode 100644 src/async/audio/AsyncAudioDecimator.cpp create mode 100644 src/async/audio/AsyncAudioDecimator.h create mode 100644 src/async/audio/AsyncAudioDecoder.cpp create mode 100644 src/async/audio/AsyncAudioDecoder.h create mode 100644 src/async/audio/AsyncAudioDecoderDummy.h create mode 100644 src/async/audio/AsyncAudioDecoderGsm.cpp create mode 100644 src/async/audio/AsyncAudioDecoderGsm.h create mode 100644 src/async/audio/AsyncAudioDecoderNull.h create mode 100644 src/async/audio/AsyncAudioDecoderOpus.cpp create mode 100644 src/async/audio/AsyncAudioDecoderOpus.h create mode 100644 src/async/audio/AsyncAudioDecoderRaw.h create mode 100644 src/async/audio/AsyncAudioDecoderS16.cpp create mode 100644 src/async/audio/AsyncAudioDecoderS16.h create mode 100644 src/async/audio/AsyncAudioDecoderSpeex.cpp create mode 100644 src/async/audio/AsyncAudioDecoderSpeex.h create mode 100644 src/async/audio/AsyncAudioDelayLine.cpp create mode 100644 src/async/audio/AsyncAudioDelayLine.h create mode 100644 src/async/audio/AsyncAudioDevice.cpp create mode 100644 src/async/audio/AsyncAudioDevice.h create mode 100644 src/async/audio/AsyncAudioDeviceAlsa.cpp create mode 100644 src/async/audio/AsyncAudioDeviceAlsa.h create mode 100644 src/async/audio/AsyncAudioDeviceFactory.cpp create mode 100644 src/async/audio/AsyncAudioDeviceFactory.h create mode 100644 src/async/audio/AsyncAudioDeviceOSS.cpp create mode 100644 src/async/audio/AsyncAudioDeviceOSS.h create mode 100644 src/async/audio/AsyncAudioDeviceUDP.cpp create mode 100644 src/async/audio/AsyncAudioDeviceUDP.h create mode 100644 src/async/audio/AsyncAudioEncoder.cpp create mode 100644 src/async/audio/AsyncAudioEncoder.h create mode 100644 src/async/audio/AsyncAudioEncoderDummy.h create mode 100644 src/async/audio/AsyncAudioEncoderGsm.cpp create mode 100644 src/async/audio/AsyncAudioEncoderGsm.h create mode 100644 src/async/audio/AsyncAudioEncoderNull.h create mode 100644 src/async/audio/AsyncAudioEncoderOpus.cpp create mode 100644 src/async/audio/AsyncAudioEncoderOpus.h create mode 100644 src/async/audio/AsyncAudioEncoderRaw.h create mode 100644 src/async/audio/AsyncAudioEncoderS16.cpp create mode 100644 src/async/audio/AsyncAudioEncoderS16.h create mode 100644 src/async/audio/AsyncAudioEncoderSpeex.cpp create mode 100644 src/async/audio/AsyncAudioEncoderSpeex.h create mode 100644 src/async/audio/AsyncAudioFifo.cpp create mode 100644 src/async/audio/AsyncAudioFifo.h create mode 100644 src/async/audio/AsyncAudioFilter.cpp create mode 100644 src/async/audio/AsyncAudioFilter.h create mode 100644 src/async/audio/AsyncAudioFsf.cpp create mode 100644 src/async/audio/AsyncAudioFsf.h create mode 100644 src/async/audio/AsyncAudioGenerator.h create mode 100644 src/async/audio/AsyncAudioIO.cpp create mode 100644 src/async/audio/AsyncAudioIO.h create mode 100644 src/async/audio/AsyncAudioInterpolator.cpp create mode 100644 src/async/audio/AsyncAudioInterpolator.h create mode 100644 src/async/audio/AsyncAudioJitterFifo.cpp create mode 100644 src/async/audio/AsyncAudioJitterFifo.h create mode 100644 src/async/audio/AsyncAudioLADSPAPlugin.cpp create mode 100644 src/async/audio/AsyncAudioLADSPAPlugin.h create mode 100644 src/async/audio/AsyncAudioMixer.cpp create mode 100644 src/async/audio/AsyncAudioMixer.h create mode 100644 src/async/audio/AsyncAudioNoiseAdder.cpp create mode 100644 src/async/audio/AsyncAudioNoiseAdder.h create mode 100644 src/async/audio/AsyncAudioPacer.cpp create mode 100644 src/async/audio/AsyncAudioPacer.h create mode 100644 src/async/audio/AsyncAudioPassthrough.h create mode 100644 src/async/audio/AsyncAudioProcessor.cpp create mode 100644 src/async/audio/AsyncAudioProcessor.h create mode 100644 src/async/audio/AsyncAudioReader.cpp create mode 100644 src/async/audio/AsyncAudioReader.h create mode 100644 src/async/audio/AsyncAudioRecorder.cpp create mode 100644 src/async/audio/AsyncAudioRecorder.h create mode 100644 src/async/audio/AsyncAudioSelector.cpp create mode 100644 src/async/audio/AsyncAudioSelector.h create mode 100644 src/async/audio/AsyncAudioSink.cpp create mode 100644 src/async/audio/AsyncAudioSink.h create mode 100644 src/async/audio/AsyncAudioSource.cpp create mode 100644 src/async/audio/AsyncAudioSource.h create mode 100644 src/async/audio/AsyncAudioSplitter.cpp create mode 100644 src/async/audio/AsyncAudioSplitter.h create mode 100644 src/async/audio/AsyncAudioStreamStateDetector.h create mode 100644 src/async/audio/AsyncAudioValve.h create mode 100644 src/async/audio/AsyncSigCAudioSink.h create mode 100644 src/async/audio/AsyncSigCAudioSource.h create mode 100644 src/async/audio/CMakeLists.txt create mode 100644 src/async/audio/fidlib.c create mode 100644 src/async/audio/fidlib.h create mode 100644 src/async/audio/fidlib.txt create mode 100644 src/async/audio/fidmkf.h create mode 100644 src/async/audio/fidrf_cmdlist.h create mode 100644 src/async/core/.gitignore create mode 100644 src/async/core/AsyncApplication.cpp create mode 100644 src/async/core/AsyncApplication.h create mode 100644 src/async/core/AsyncAtTimer.cpp create mode 100644 src/async/core/AsyncAtTimer.h create mode 100644 src/async/core/AsyncConfig.cpp create mode 100644 src/async/core/AsyncConfig.h create mode 100644 src/async/core/AsyncDigest.h create mode 100644 src/async/core/AsyncDnsLookup.cpp create mode 100644 src/async/core/AsyncDnsLookup.h create mode 100644 src/async/core/AsyncDnsLookupWorker.h create mode 100644 src/async/core/AsyncDnsResourceRecord.h create mode 100644 src/async/core/AsyncEncryptedUdpSocket.cpp create mode 100644 src/async/core/AsyncEncryptedUdpSocket.h create mode 100644 src/async/core/AsyncExec.cpp create mode 100644 src/async/core/AsyncExec.h create mode 100644 src/async/core/AsyncFactory.h create mode 100644 src/async/core/AsyncFdWatch.cpp create mode 100644 src/async/core/AsyncFdWatch.h create mode 100644 src/async/core/AsyncFileReader.cpp create mode 100644 src/async/core/AsyncFileReader.h create mode 100644 src/async/core/AsyncFramedTcpConnection.cpp create mode 100644 src/async/core/AsyncFramedTcpConnection.h create mode 100644 src/async/core/AsyncHttpServerConnection.cpp create mode 100644 src/async/core/AsyncHttpServerConnection.h create mode 100644 src/async/core/AsyncIpAddress.cpp create mode 100644 src/async/core/AsyncIpAddress.h create mode 100644 src/async/core/AsyncMsg.h create mode 100644 src/async/core/AsyncPlugin.cpp create mode 100644 src/async/core/AsyncPlugin.h create mode 100644 src/async/core/AsyncPty.cpp create mode 100644 src/async/core/AsyncPty.h create mode 100644 src/async/core/AsyncPtyStreamBuf.cpp create mode 100644 src/async/core/AsyncPtyStreamBuf.h create mode 100644 src/async/core/AsyncSerial.cpp create mode 100644 src/async/core/AsyncSerial.h create mode 100644 src/async/core/AsyncSerialDevice.cpp create mode 100644 src/async/core/AsyncSerialDevice.h create mode 100644 src/async/core/AsyncSslCertSigningReq.h create mode 100644 src/async/core/AsyncSslContext.h create mode 100644 src/async/core/AsyncSslKeypair.h create mode 100644 src/async/core/AsyncSslX509.h create mode 100644 src/async/core/AsyncSslX509ExtSubjectAltName.h create mode 100644 src/async/core/AsyncSslX509Extensions.h create mode 100644 src/async/core/AsyncStateMachine.h create mode 100644 src/async/core/AsyncTcpClient.h create mode 100644 src/async/core/AsyncTcpClientBase.cpp create mode 100644 src/async/core/AsyncTcpClientBase.h create mode 100644 src/async/core/AsyncTcpConnection.cpp create mode 100644 src/async/core/AsyncTcpConnection.h create mode 100644 src/async/core/AsyncTcpPrioClient.h create mode 100644 src/async/core/AsyncTcpPrioClientBase.cpp create mode 100644 src/async/core/AsyncTcpPrioClientBase.h create mode 100644 src/async/core/AsyncTcpServer.h create mode 100644 src/async/core/AsyncTcpServerBase.cpp create mode 100644 src/async/core/AsyncTcpServerBase.h create mode 100644 src/async/core/AsyncTimer.cpp create mode 100644 src/async/core/AsyncTimer.h create mode 100644 src/async/core/AsyncUdpSocket.cpp create mode 100644 src/async/core/AsyncUdpSocket.h create mode 100644 src/async/core/CMakeLists.txt create mode 100644 src/async/cpp/.gitignore create mode 100644 src/async/cpp/AsyncCppApplication.cpp create mode 100644 src/async/cpp/AsyncCppApplication.h create mode 100644 src/async/cpp/AsyncCppDnsLookupWorker.cpp create mode 100644 src/async/cpp/AsyncCppDnsLookupWorker.h create mode 100644 src/async/cpp/CMakeLists.txt create mode 100644 src/async/demo/.gitignore create mode 100644 src/async/demo/AsyncAtTimer_demo.cpp create mode 100644 src/async/demo/AsyncAudioContainer_demo.cpp create mode 100644 src/async/demo/AsyncAudioFsf_demo.cpp create mode 100644 src/async/demo/AsyncAudioIO_demo.cpp create mode 100644 src/async/demo/AsyncAudioLADSPAPlugin_demo.cpp create mode 100644 src/async/demo/AsyncAudioSelector_demo.cpp create mode 100644 src/async/demo/AsyncConfig_demo.cpp create mode 100644 src/async/demo/AsyncCppApplication_demo.cpp create mode 100644 src/async/demo/AsyncDigest_demo.cpp create mode 100644 src/async/demo/AsyncDnsLookup_demo.cpp create mode 100644 src/async/demo/AsyncExec_demo.cpp create mode 100644 src/async/demo/AsyncFactory_demo.cpp create mode 100644 src/async/demo/AsyncFdWatch_demo.cpp create mode 100644 src/async/demo/AsyncFramedTcpClient_demo.cpp create mode 100644 src/async/demo/AsyncFramedTcpServer_demo.cpp create mode 100644 src/async/demo/AsyncHttpServer_demo.cpp create mode 100644 src/async/demo/AsyncMsg_demo.cpp create mode 100644 src/async/demo/AsyncPlugin_demo.cpp create mode 100644 src/async/demo/AsyncPtyStreamBuf_demo.cpp create mode 100644 src/async/demo/AsyncQtApplication_demo.cpp create mode 100644 src/async/demo/AsyncSerial_demo.cpp create mode 100644 src/async/demo/AsyncSslTcpClient_demo.cpp create mode 100644 src/async/demo/AsyncSslTcpServer_demo.cpp create mode 100644 src/async/demo/AsyncSslX509_demo.cpp create mode 100644 src/async/demo/AsyncStateMachine_demo.cpp create mode 100644 src/async/demo/AsyncTcpClient_demo.cpp create mode 100644 src/async/demo/AsyncTcpPrioClient_demo.cpp create mode 100644 src/async/demo/AsyncTcpServer_demo.cpp create mode 100644 src/async/demo/AsyncTimer_demo.cpp create mode 100644 src/async/demo/AsyncUdpSocket_demo.cpp create mode 100644 src/async/demo/CMakeLists.txt create mode 100644 src/async/demo/DemoPlugin.cpp create mode 100644 src/async/demo/DemoPluginBase.h create mode 100644 src/async/demo/test.cfg create mode 100644 src/async/mainpage.dox create mode 100644 src/async/qt/.gitignore create mode 100644 src/async/qt/AsyncQtApplication.cpp create mode 100644 src/async/qt/AsyncQtApplication.h create mode 100644 src/async/qt/AsyncQtDnsLookupWorker.cpp create mode 100644 src/async/qt/AsyncQtDnsLookupWorker.h create mode 100644 src/async/qt/AsyncQtTimer.h create mode 100644 src/async/qt/CMakeLists.txt create mode 100644 src/cmake/Modules/FindALSA.cmake create mode 100644 src/cmake/Modules/FindCHOWN.cmake create mode 100644 src/cmake/Modules/FindCURL.cmake create mode 100644 src/cmake/Modules/FindDL.cmake create mode 100644 src/cmake/Modules/FindGCrypt.cmake create mode 100644 src/cmake/Modules/FindGPIOD.cmake create mode 100644 src/cmake/Modules/FindGROFF.cmake create mode 100644 src/cmake/Modules/FindGSM.cmake create mode 100644 src/cmake/Modules/FindGZIP.cmake create mode 100644 src/cmake/Modules/FindLADSPA.cmake create mode 100644 src/cmake/Modules/FindOGG.cmake create mode 100644 src/cmake/Modules/FindOpus.cmake create mode 100644 src/cmake/Modules/FindPopt.cmake create mode 100644 src/cmake/Modules/FindRT.cmake create mode 100644 src/cmake/Modules/FindRtlSdr.cmake create mode 100644 src/cmake/Modules/FindSIGC.cmake create mode 100644 src/cmake/Modules/FindSIGC2.cmake create mode 100644 src/cmake/Modules/FindSpeex.cmake create mode 100644 src/cmake/Modules/LibFindMacros.cmake create mode 100644 src/config.h.in create mode 100644 src/include/CMakeLists.txt create mode 100644 src/include/version_include_file.in create mode 100644 src/misc/.gitignore create mode 100644 src/misc/CMakeLists.txt create mode 100644 src/misc/CppStdCompat.h create mode 100644 src/misc/LogWriter.cpp create mode 100644 src/misc/LogWriter.h create mode 100644 src/misc/common.cpp create mode 100644 src/misc/common.h create mode 100644 src/svxlink/CMakeLists.txt create mode 100644 src/svxlink/reflector/CMakeLists.txt create mode 100644 src/svxlink/reflector/ProtoVer.h create mode 100644 src/svxlink/reflector/Reflector.cpp create mode 100644 src/svxlink/reflector/Reflector.h create mode 100644 src/svxlink/reflector/ReflectorClient.cpp create mode 100644 src/svxlink/reflector/ReflectorClient.h create mode 100644 src/svxlink/reflector/ReflectorMsg.h create mode 100644 src/svxlink/reflector/TGHandler.cpp create mode 100644 src/svxlink/reflector/TGHandler.h create mode 100644 src/svxlink/reflector/TrunkLink.cpp create mode 100644 src/svxlink/reflector/TrunkLink.h create mode 100755 src/svxlink/reflector/ca-hook.py.in create mode 100644 src/svxlink/reflector/ca-hook.yaml create mode 100644 src/svxlink/reflector/genpw/.gitignore create mode 100644 src/svxlink/reflector/genpw/README.adoc create mode 100755 src/svxlink/reflector/genpw/genpw.py create mode 100644 src/svxlink/reflector/genpw/genpw.wsgi create mode 100755 src/svxlink/reflector/genpw/getpw.py create mode 100755 src/svxlink/reflector/svxreflector-ca create mode 100755 src/svxlink/reflector/svxreflector-status create mode 100644 src/svxlink/reflector/svxreflector.conf.in create mode 100644 src/svxlink/reflector/svxreflector.cpp create mode 100644 src/versions diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..5a37a98 --- /dev/null +++ b/.gitignore @@ -0,0 +1,34 @@ +# Build output +build/ + +# Compiled object files and binaries +*.o +*.a +*.so +*.so.* +*.dylib +*.out +*.elf + +# CMake generated files +CMakeCache.txt +CMakeFiles/ +cmake_install.cmake +install_manifest.txt +CTestTestfile.cmake +_deps/ + +# Claude +.claude/ +CLAUDE.md + +# Editor and IDE +.vscode/ +.idea/ +*.swp +*.swo +*~ + +# OS +.DS_Store +Thumbs.db diff --git a/Dockerfile b/Dockerfile new file mode 100644 index 0000000..2b915c1 --- /dev/null +++ b/Dockerfile @@ -0,0 +1,41 @@ +# Stage 1: Build +FROM debian:bookworm AS builder + +RUN apt-get update && apt-get install -y --no-install-recommends \ + build-essential \ + cmake \ + libsigc++-2.0-dev \ + libssl-dev \ + libjsoncpp-dev \ + libpopt-dev \ + libopus-dev \ + libgsm1-dev \ + && rm -rf /var/lib/apt/lists/* + +WORKDIR /src +COPY src/ src/ +RUN cmake -S src -B build -DLOCAL_STATE_DIR=/var \ + && cmake --build build + + +# Stage 2: Runtime +FROM debian:bookworm-slim + +RUN apt-get update && apt-get install -y --no-install-recommends \ + libsigc++-2.0-0v5 \ + libssl3 \ + libjsoncpp25 \ + libpopt0 \ + libopus0 \ + libgsm1 \ + && rm -rf /var/lib/apt/lists/* + +COPY --from=builder /src/build/bin/svxreflector /usr/bin/svxreflector + +# 5300 tcp+udp: SvxLink client connections and audio +# 5302 tcp: server-to-server trunk links +# 8080 tcp: HTTP status endpoint (optional, enable via HTTP_SRV_PORT) +EXPOSE 5300/tcp 5300/udp 5302/tcp 8080/tcp + +ENTRYPOINT ["svxreflector"] +CMD ["--config", "/etc/svxlink/svxreflector.conf"] diff --git a/README.md b/README.md new file mode 100644 index 0000000..2c4453e --- /dev/null +++ b/README.md @@ -0,0 +1,237 @@ +# GeuReflector + +GeuReflector is a fork of [SvxReflector](https://github.com/sm0svx/svxlink) +extended with a **server-to-server trunk protocol** that lets multiple reflector +instances share talk groups (TGs) as independent parallel voice channels — +analogous to telephone trunk lines between telephone exchanges. + +Original SvxReflector by Tobias Blomberg / SM0SVX. +Trunk extension by IW1GEU. + +--- + +## Why trunking? + +A standard SvxReflector is a single centralized server. Without trunking, a +multi-site national deployment has two unsatisfying options: + +- **One national server** — only one TG can be active at a time across the + entire network; one outage takes everyone down. +- **Independent regional servers bridged by SvxLink instances** — technically + possible, but each bridge instance handles only one TG at a time and one + instance is needed per pair of servers. For 20 regions in full mesh that is + 190 SvxLink bridge processes for a single shared TG, multiplied by the number + of TGs to share. Unmanageable at scale. + +GeuReflector solves this: each region runs its own independent reflector +(resilience, local autonomy) and the trunk links connect them so they can share +TGs selectively. All regional TGs can carry simultaneous independent QSOs while +a national TG remains available to all. Talker arbitration is handled +automatically. + +--- + +## What it adds over SvxReflector + +- **Prefix-based TG ownership** — each reflector is authoritative for all TGs + whose decimal representation starts with a configured digit string (e.g. + `LOCAL_PREFIX=1` → owns TGs 1, 10, 100, 1000, 12345 …). Both `LOCAL_PREFIX` + and `REMOTE_PREFIX` accept comma-separated lists for reflectors that own + multiple prefix groups (e.g. `LOCAL_PREFIX=11,12,13`) +- **Server-to-server trunk links** — persistent TCP connections between pairs of + reflectors carry talker state and audio for shared TGs +- **Full-mesh topology** — any number of reflectors can be trunked together; each + pair has a direct link so no multi-hop routing is needed +- **Unlimited concurrent conversations** — the trunk TCP connection multiplexes + all active TGs simultaneously; the only per-TG rule is one talker at a time +- **Independent talker arbitration** — each reflector arbitrates its own clients; + the trunk adds a tie-breaking layer (random priority nonce) for simultaneous + claims +- **HTTP `/status` endpoint** — existing JSON status now includes a `trunks` + object with per-link state and active talkers +- SvxLink client nodes are **unmodified** — they connect to their local + reflector as normal and are unaware of the trunk + +--- + +## How TG ownership works + +TG numbers work like telephone numbers. The reflector that owns a TG prefix is +the authoritative home for every TG in that prefix group: + +``` +Reflector 1 (LOCAL_PREFIX=1) → TGs 1, 10, 11, 100, 1000, 12345, … +Reflector 2 (LOCAL_PREFIX=2) → TGs 2, 20, 21, 200, 2000, 25000, … +Reflector 3 (LOCAL_PREFIX=3) → TGs 3, 30, … +``` + +When a client on Reflector 1 joins TG 25, Reflector 1 forwards the conversation +over its trunk to Reflector 2 (which owns the "2" prefix). Clients on both +reflectors hear the same audio on TG 25 with no extra configuration. + +--- + +## Build + +Requires the same dependencies as SvxReflector: libsigc++, OpenSSL, libjsoncpp, +libpopt. Optional: libopus, libgsm, libspeex. + +```bash +cd geureflector +cmake -S src -B build -DLOCAL_STATE_DIR=/var +cmake --build build +# binary at build/bin/svxreflector +``` + +--- + +## Configuration + +GeuReflector uses the same `svxreflector.conf` format as SvxReflector with two +additions. + +### 1. Declare this reflector's TG prefix + +Add `LOCAL_PREFIX` to the `[GLOBAL]` section. A comma-separated list is accepted +when a single reflector owns multiple prefix groups: + +```ini +[GLOBAL] +LISTEN_PORT=5300 +LOCAL_PREFIX=1 # owns TGs 1, 10, 100, 1000, ... +# LOCAL_PREFIX=11,12,13 # multiple prefixes on one instance +``` + +### 2. Add a trunk section per peer + +```ini +[TRUNK_2] +HOST=reflector-b.example.com +PORT=5302 # trunk port (default 5302, separate from client port 5300) +SECRET=shared_secret +REMOTE_PREFIX=2 # peer owns TGs 2, 20, 200, 2000, ... +# REMOTE_PREFIX=11,12,13 # comma-separated list accepted here too +``` + +Repeat for each peer (`[TRUNK_3]`, `[TRUNK_4]`, …). + +### Full-mesh example — three reflectors + +**Reflector A** — owns prefix "1": +```ini +[GLOBAL] +LOCAL_PREFIX=1 + +[TRUNK_2] +HOST=reflector-b.example.com +SECRET=secret_ab +REMOTE_PREFIX=2 + +[TRUNK_3] +HOST=reflector-c.example.com +SECRET=secret_ac +REMOTE_PREFIX=3 +``` + +**Reflector B** — owns prefix "2": +```ini +[GLOBAL] +LOCAL_PREFIX=2 + +[TRUNK_1] +HOST=reflector-a.example.com +SECRET=secret_ab +REMOTE_PREFIX=1 + +[TRUNK_3] +HOST=reflector-c.example.com +SECRET=secret_bc +REMOTE_PREFIX=3 +``` + +**Reflector C** — owns prefix "3": +```ini +[GLOBAL] +LOCAL_PREFIX=3 + +[TRUNK_1] +HOST=reflector-a.example.com +SECRET=secret_ac +REMOTE_PREFIX=1 + +[TRUNK_2] +HOST=reflector-b.example.com +SECRET=secret_bc +REMOTE_PREFIX=2 +``` + +Both sides of each trunk must point at each other and share the same `SECRET`. + +--- + +## HTTP Status + +Enable with `HTTP_SRV_PORT=8080` in `[GLOBAL]`. The `/status` endpoint returns +JSON that now includes a `trunks` key. `local_prefix` and `remote_prefix` are +returned as arrays to reflect multi-prefix support: + +```json +{ + "nodes": { ... }, + "trunks": { + "TRUNK_2": { + "host": "reflector-b.example.com", + "port": 5302, + "connected": true, + "local_prefix": ["1"], + "remote_prefix": ["2"], + "active_talkers": { + "25": "SM0ABC", + "200": "LA8PV" + } + } + } +} +``` + +`active_talkers` lists only TGs with an active remote talker at query time. + +--- + +## Live user/password reload + +New users and passwords written to `svxreflector.conf` are picked up **without +restarting** via the command PTY interface (enabled by default): + +```bash +# Reload users list +echo "CFG USERS section_name key value" > /dev/shm/reflector_ctrl + +# Or reload passwords +echo "CFG PASSWORDS section_name key value" > /dev/shm/reflector_ctrl +``` + +The PTY path is set by `COMMAND_PTY` in `[GLOBAL]` (default +`/dev/shm/reflector_ctrl`). + +--- + +## Documentation + +- [`docs/INSTALL.md`](docs/INSTALL.md) — how to build and install GeuReflector + as a drop-in replacement for an existing SvxReflector installation +- [`docs/DOCKER.md`](docs/DOCKER.md) — running GeuReflector in a Docker container + as a drop-in replacement for an existing SvxReflector installation +- [`docs/TRUNK_PROTOCOL.md`](docs/TRUNK_PROTOCOL.md) — full wire protocol + specification: message format, handshake sequence, talker arbitration + tie-breaking, heartbeat, and the complete message type table +- [`docs/DEPLOYMENT_ITALY.md`](docs/DEPLOYMENT_ITALY.md) — complete national + deployment example for Italy (20 regions, full mesh) +- [`docs/DEPLOYMENT_ITALY_IT.md`](docs/DEPLOYMENT_ITALY_IT.md) — same document + in Italian + +--- + +## License + +GNU General Public License v2 or later — same as SvxLink upstream. diff --git a/docker-compose.yml b/docker-compose.yml new file mode 100644 index 0000000..937b199 --- /dev/null +++ b/docker-compose.yml @@ -0,0 +1,15 @@ +services: + svxreflector: + build: . + restart: unless-stopped + ports: + - "5300:5300/tcp" + - "5300:5300/udp" + - "5302:5302/tcp" + - "8080:8080/tcp" # remove if HTTP_SRV_PORT is not set in config + volumes: + - ./config/svxreflector.conf:/etc/svxlink/svxreflector.conf:ro + - pki:/etc/svxlink/pki # persistent storage for certificates + +volumes: + pki: diff --git a/docs/DEPLOYMENT_ITALY.md b/docs/DEPLOYMENT_ITALY.md new file mode 100644 index 0000000..1b8c983 --- /dev/null +++ b/docs/DEPLOYMENT_ITALY.md @@ -0,0 +1,277 @@ +# Italy Deployment Example + +This document describes a full national deployment for Italy using one GeuReflector +instance per region, trunked together in a full-mesh topology. + +## Why the trunk is necessary + +A standard SvxReflector instance is a single centralized server. Without trunking, +a national deployment has only two options: + +**Option A — one national reflector:** +all 20 regions connect their nodes to a single server. This works, but only +**one talk group can be active at a time across the entire country**. While Lazio +is in QSO on the national TG, no other region can have a simultaneous independent +conversation. Every transmission from every region travels through the same server, +and a single server outage takes the whole network down. + +**Option B — independent regional reflectors bridged by SvxLink instances:** +it is possible to link two reflectors together by running a SvxLink instance that +connects to both as a client node, acting as an audio bridge. However, each such +bridge instance can only handle one TG at a time, and one instance is needed per +pair of reflectors. For 20 regions in full mesh that is 190 SvxLink bridge +instances just to cover one shared TG — and multiplied again for every additional +TG that needs to be shared. The operational complexity, resource usage, and +failure surface of managing hundreds of bridge processes makes this approach +impractical at national scale. + +**With GeuReflector trunks:** +each region runs its own independent reflector (resilience, local autonomy), and +the trunk links connect them so they can share TGs selectively. All 20 regional +TGs can carry simultaneous independent QSOs, while a dedicated national TG is +available to all. The trunk handles talker arbitration automatically when two +regions try to use a shared TG at the same time. + +| | Single reflector | Independent reflectors | GeuReflector trunk mesh | +|---|---|---|---| +| Simultaneous regional QSOs | No — one TG at a time | Yes, but one bridge per TG per pair | Yes | +| Inter-region communication | Yes | Yes, but 190+ bridge processes | Yes | +| Regional autonomy | No | Yes | Yes | +| Single point of failure | Yes | No | No | + +--- + +## TG numbering + +The TG numbers used here are **inspired by the Italian DMR TG numbering scheme**, +which is already well thought out, widely adopted, and recognised across the +amateur radio community. Reusing this structure avoids reinventing a numbering +plan and makes the allocation immediately familiar to Italian operators. + +**This SvxLink/GeuReflector system is entirely independent of DMR and is not +connected to any DMR network.** The TG numbers are used here purely as a +convenient and meaningful identifier space — no DMR radio, hotspot, or repeater +is involved. + +The Italian DMR TG numbers all start with the national prefix `222`. That prefix +is stripped when setting `LOCAL_PREFIX`, so each reflector is configured with the +2-digit regional code only (e.g. TG `22201` → `LOCAL_PREFIX=01`). + +Both `LOCAL_PREFIX` and `REMOTE_PREFIX` accept a comma-separated list of prefixes, +so a single reflector instance can own multiple regions +(e.g. `LOCAL_PREFIX=11,12,13` for Liguria, Piemonte and Valle d'Aosta). + +--- + +## Reflector inventory + +| Region | TG | LOCAL_PREFIX | Suggested hostname | +|---------------------------|---------|--------------|-------------------------------------| +| LAZIO | 22201 | 01 | svxref-lazio.example.it | +| SARDEGNA | 22202 | 02 | svxref-sardegna.example.it | +| UMBRIA | 22203 | 03 | svxref-umbria.example.it | +| LIGURIA | 22211 | 11 | svxref-liguria.example.it | +| PIEMONTE | 22212 | 12 | svxref-piemonte.example.it | +| VALLE D'AOSTA | 22213 | 13 | svxref-valledaosta.example.it | +| LOMBARDIA | 22221 | 21 | svxref-lombardia.example.it | +| FRIULI VENEZIA GIULIA | 22231 | 31 | svxref-friuli.example.it | +| TRENTINO ALTO ADIGE | 22232 | 32 | svxref-trentino.example.it | +| VENETO | 22233 | 33 | svxref-veneto.example.it | +| EMILIA ROMAGNA | 22241 | 41 | svxref-emilia.example.it | +| TOSCANA | 22251 | 51 | svxref-toscana.example.it | +| ABRUZZO | 22261 | 61 | svxref-abruzzo.example.it | +| MARCHE | 22262 | 62 | svxref-marche.example.it | +| PUGLIA | 22271 | 71 | svxref-puglia.example.it | +| BASILICATA | 22281 | 81 | svxref-basilicata.example.it | +| CALABRIA | 22282 | 82 | svxref-calabria.example.it | +| CAMPANIA | 22283 | 83 | svxref-campania.example.it | +| MOLISE | 22284 | 84 | svxref-molise.example.it | +| SICILIA | 22291 | 91 | svxref-sicilia.example.it | + +--- + +## Topology + +20 reflectors × 19 trunk links each = **190 trunk TCP connections** total (full mesh). +Every pair shares a unique `SECRET`. Port `5302` is used for all trunk connections; +port `5300` remains for SvxLink client nodes. + +--- + +## Shared secrets convention + +Each pair of reflectors shares one secret. Use a naming convention to keep them +organised, for example `IT__` (lower prefix first): + +``` +IT_01_02 # LAZIO ↔ SARDEGNA +IT_01_03 # LAZIO ↔ UMBRIA +… +IT_84_91 # MOLISE ↔ SICILIA +``` + +--- + +## Configuration: LAZIO (complete example) + +```ini +[GLOBAL] +LISTEN_PORT=5300 +LOCAL_PREFIX=01 +HTTP_SRV_PORT=8080 +COMMAND_PTY=/dev/shm/reflector_ctrl + +[TRUNK_02] +HOST=svxref-sardegna.example.it +PORT=5302 +SECRET=IT_01_02 +REMOTE_PREFIX=02 + +[TRUNK_03] +HOST=svxref-umbria.example.it +PORT=5302 +SECRET=IT_01_03 +REMOTE_PREFIX=03 + +[TRUNK_11] +HOST=svxref-liguria.example.it +PORT=5302 +SECRET=IT_01_11 +REMOTE_PREFIX=11 + +[TRUNK_12] +HOST=svxref-piemonte.example.it +PORT=5302 +SECRET=IT_01_12 +REMOTE_PREFIX=12 + +[TRUNK_13] +HOST=svxref-valledaosta.example.it +PORT=5302 +SECRET=IT_01_13 +REMOTE_PREFIX=13 + +[TRUNK_21] +HOST=svxref-lombardia.example.it +PORT=5302 +SECRET=IT_01_21 +REMOTE_PREFIX=21 + +[TRUNK_31] +HOST=svxref-friuli.example.it +PORT=5302 +SECRET=IT_01_31 +REMOTE_PREFIX=31 + +[TRUNK_32] +HOST=svxref-trentino.example.it +PORT=5302 +SECRET=IT_01_32 +REMOTE_PREFIX=32 + +[TRUNK_33] +HOST=svxref-veneto.example.it +PORT=5302 +SECRET=IT_01_33 +REMOTE_PREFIX=33 + +[TRUNK_41] +HOST=svxref-emilia.example.it +PORT=5302 +SECRET=IT_01_41 +REMOTE_PREFIX=41 + +[TRUNK_51] +HOST=svxref-toscana.example.it +PORT=5302 +SECRET=IT_01_51 +REMOTE_PREFIX=51 + +[TRUNK_61] +HOST=svxref-abruzzo.example.it +PORT=5302 +SECRET=IT_01_61 +REMOTE_PREFIX=61 + +[TRUNK_62] +HOST=svxref-marche.example.it +PORT=5302 +SECRET=IT_01_62 +REMOTE_PREFIX=62 + +[TRUNK_71] +HOST=svxref-puglia.example.it +PORT=5302 +SECRET=IT_01_71 +REMOTE_PREFIX=71 + +[TRUNK_81] +HOST=svxref-basilicata.example.it +PORT=5302 +SECRET=IT_01_81 +REMOTE_PREFIX=81 + +[TRUNK_82] +HOST=svxref-calabria.example.it +PORT=5302 +SECRET=IT_01_82 +REMOTE_PREFIX=82 + +[TRUNK_83] +HOST=svxref-campania.example.it +PORT=5302 +SECRET=IT_01_83 +REMOTE_PREFIX=83 + +[TRUNK_84] +HOST=svxref-molise.example.it +PORT=5302 +SECRET=IT_01_84 +REMOTE_PREFIX=84 + +[TRUNK_91] +HOST=svxref-sicilia.example.it +PORT=5302 +SECRET=IT_01_91 +REMOTE_PREFIX=91 +``` + +All other regional configs follow the same pattern: set `LOCAL_PREFIX` to the +region's 2-digit code and add one `[TRUNK_xx]` section for each of the other +19 regions, using the matching `SECRET` from the convention above. + +--- + +## Concurrent conversations + +There is **no hardcoded limit** on concurrent QSOs over the trunk. The trunk +between each pair of reflectors is a single TCP connection that multiplexes all +active TGs simultaneously — each `MsgTrunkAudio` frame is tagged with the TG +number, so any number of TGs can carry audio at the same time. + +The only per-TG rule is that **one talker is allowed per TG at a time** +(enforced by the arbitration logic). Multiple TGs can all have active talkers +simultaneously without interfering. + +### Practical bandwidth estimate + +| Codec | Bitrate per active TG | Concurrent TGs on a 1 Mbps trunk | +|-------|-----------------------|-----------------------------------| +| OPUS | ~8–16 kbps | ~60–125 | +| GSM | ~13 kbps | ~75 | + +In this Italian deployment, even if all 20 regional TGs carried a simultaneous +QSO, the total trunk load per reflector would be well under 1 Mbps. Bandwidth +is not a concern at this scale; the limiting factor in practice is the number of +licensed operators active at any given time. + +--- + +## Per-region config checklist + +1. Set `LOCAL_PREFIX` to the 2-digit code from the table above. +2. Add one `[TRUNK_xx]` section for **every other region** (19 sections total). +3. Use the same `SECRET` value as the matching section on the peer — mismatched + secrets will prevent the trunk from connecting. +4. Open TCP port `5302` inbound in the firewall (trunk) and `5300` inbound (clients). +5. Ensure `HOST` resolves to the peer's public IP from the server's network. diff --git a/docs/DEPLOYMENT_ITALY_IT.md b/docs/DEPLOYMENT_ITALY_IT.md new file mode 100644 index 0000000..7869169 --- /dev/null +++ b/docs/DEPLOYMENT_ITALY_IT.md @@ -0,0 +1,281 @@ +# Esempio di deployment nazionale — Italia + +Questo documento descrive un deployment nazionale completo per l'Italia utilizzando +un'istanza di GeuReflector per regione, interconnesse in una topologia a maglia +piena (full-mesh). + +## Perché il trunk è necessario + +Un'istanza standard di SvxReflector è un server centralizzato unico. Senza il +trunk, un deployment nazionale dispone di sole due opzioni: + +**Opzione A — un unico reflector nazionale:** +tutte e 20 le regioni collegano i propri nodi a un singolo server. Funziona, ma +**un solo talk group può essere attivo alla volta sull'intera rete nazionale**. +Mentre il Lazio è in QSO sul TG nazionale, nessun'altra regione può avere una +conversazione indipendente simultanea. Ogni trasmissione di ogni regione transita +per lo stesso server, e un'interruzione di quel server abbatte l'intera rete. + +**Opzione B — reflector regionali indipendenti collegati da istanze SvxLink:** +è possibile collegare due reflector facendo girare un'istanza di SvxLink che si +connette ad entrambi come nodo client, fungendo da ponte audio. Tuttavia, ogni +istanza-ponte può gestire un solo TG alla volta, e serve un'istanza per ogni +coppia di reflector. Per 20 regioni in full mesh occorrono già 190 istanze SvxLink +solo per coprire un singolo TG condiviso — moltiplicate ulteriormente per ogni TG +aggiuntivo che si vuole condividere. La complessità operativa, il consumo di +risorse e la superficie di guasto derivanti dalla gestione di centinaia di processi +ponte rendono questo approccio impraticabile su scala nazionale. + +**Con il trunk GeuReflector:** +ogni regione gestisce il proprio reflector indipendente (resilienza, autonomia +locale) e i link trunk li interconnettono permettendo di condividere i TG in modo +selettivo. Tutti e 20 i TG regionali possono ospitare QSO indipendenti simultanei, +mentre un TG nazionale dedicato è accessibile a tutti. Il trunk gestisce +automaticamente l'arbitraggio quando due regioni tentano di usare un TG condiviso +nello stesso momento. + +| | Reflector unico | Reflector indipendenti | Trunk mesh GeuReflector | +|---|---|---|---| +| QSO regionali simultanei | No — un TG alla volta | Sì, ma un ponte per TG per coppia | Sì | +| Comunicazione inter-regionale | Sì | Sì, ma 190+ processi ponte | Sì | +| Autonomia regionale | No | Sì | Sì | +| Singolo punto di guasto | Sì | No | No | + +--- + +## Numerazione dei TG + +La numerazione dei TG utilizzata in questo documento è **ispirata al piano di +numerazione DMR italiano**, già ben strutturato, ampiamente adottato e riconosciuto +dalla comunità dei radioamatori. Riutilizzare questa struttura evita di dover +inventare un piano di numerazione alternativo e rende l'allocazione immediatamente +familiare agli operatori italiani. + +**Questo sistema SvxLink/GeuReflector è completamente indipendente dal DMR e non +è connesso ad alcuna rete DMR.** I numeri di TG sono utilizzati qui esclusivamente +come spazio di identificatori comodo e significativo — non sono coinvolti radio, +hotspot o ripetitori DMR di alcun tipo. + +I TG DMR italiani iniziano tutti con il prefisso nazionale `222`. Questo prefisso +viene rimosso quando si imposta `LOCAL_PREFIX`, quindi ogni reflector viene +configurato con il solo codice regionale a 2 cifre (es. TG `22201` → `LOCAL_PREFIX=01`). + +Sia `LOCAL_PREFIX` che `REMOTE_PREFIX` accettano un elenco separato da virgole, +quindi una singola istanza può gestire più regioni +(es. `LOCAL_PREFIX=11,12,13` per Liguria, Piemonte e Valle d'Aosta). + +--- + +## Inventario dei reflector + +| Regione | TG | LOCAL_PREFIX | Hostname suggerito | +|---------------------------|---------|--------------|-------------------------------------| +| LAZIO | 22201 | 01 | svxref-lazio.example.it | +| SARDEGNA | 22202 | 02 | svxref-sardegna.example.it | +| UMBRIA | 22203 | 03 | svxref-umbria.example.it | +| LIGURIA | 22211 | 11 | svxref-liguria.example.it | +| PIEMONTE | 22212 | 12 | svxref-piemonte.example.it | +| VALLE D'AOSTA | 22213 | 13 | svxref-valledaosta.example.it | +| LOMBARDIA | 22221 | 21 | svxref-lombardia.example.it | +| FRIULI VENEZIA GIULIA | 22231 | 31 | svxref-friuli.example.it | +| TRENTINO ALTO ADIGE | 22232 | 32 | svxref-trentino.example.it | +| VENETO | 22233 | 33 | svxref-veneto.example.it | +| EMILIA ROMAGNA | 22241 | 41 | svxref-emilia.example.it | +| TOSCANA | 22251 | 51 | svxref-toscana.example.it | +| ABRUZZO | 22261 | 61 | svxref-abruzzo.example.it | +| MARCHE | 22262 | 62 | svxref-marche.example.it | +| PUGLIA | 22271 | 71 | svxref-puglia.example.it | +| BASILICATA | 22281 | 81 | svxref-basilicata.example.it | +| CALABRIA | 22282 | 82 | svxref-calabria.example.it | +| CAMPANIA | 22283 | 83 | svxref-campania.example.it | +| MOLISE | 22284 | 84 | svxref-molise.example.it | +| SICILIA | 22291 | 91 | svxref-sicilia.example.it | + +--- + +## Topologia + +20 reflector × 19 trunk link ciascuno = **190 connessioni TCP trunk** in totale (full mesh). +Ogni coppia condivide un `SECRET` univoco. La porta `5302` è utilizzata per tutte +le connessioni trunk; la porta `5300` rimane riservata ai nodi client SvxLink. + +--- + +## Convenzione per i secret condivisi + +Ogni coppia di reflector condivide un secret. Si consiglia una convenzione di +denominazione come `IT__` (prefisso minore per primo): + +``` +IT_01_02 # LAZIO ↔ SARDEGNA +IT_01_03 # LAZIO ↔ UMBRIA +… +IT_84_91 # MOLISE ↔ SICILIA +``` + +--- + +## Configurazione: LAZIO (esempio completo) + +```ini +[GLOBAL] +LISTEN_PORT=5300 +LOCAL_PREFIX=01 +HTTP_SRV_PORT=8080 +COMMAND_PTY=/dev/shm/reflector_ctrl + +[TRUNK_02] +HOST=svxref-sardegna.example.it +PORT=5302 +SECRET=IT_01_02 +REMOTE_PREFIX=02 + +[TRUNK_03] +HOST=svxref-umbria.example.it +PORT=5302 +SECRET=IT_01_03 +REMOTE_PREFIX=03 + +[TRUNK_11] +HOST=svxref-liguria.example.it +PORT=5302 +SECRET=IT_01_11 +REMOTE_PREFIX=11 + +[TRUNK_12] +HOST=svxref-piemonte.example.it +PORT=5302 +SECRET=IT_01_12 +REMOTE_PREFIX=12 + +[TRUNK_13] +HOST=svxref-valledaosta.example.it +PORT=5302 +SECRET=IT_01_13 +REMOTE_PREFIX=13 + +[TRUNK_21] +HOST=svxref-lombardia.example.it +PORT=5302 +SECRET=IT_01_21 +REMOTE_PREFIX=21 + +[TRUNK_31] +HOST=svxref-friuli.example.it +PORT=5302 +SECRET=IT_01_31 +REMOTE_PREFIX=31 + +[TRUNK_32] +HOST=svxref-trentino.example.it +PORT=5302 +SECRET=IT_01_32 +REMOTE_PREFIX=32 + +[TRUNK_33] +HOST=svxref-veneto.example.it +PORT=5302 +SECRET=IT_01_33 +REMOTE_PREFIX=33 + +[TRUNK_41] +HOST=svxref-emilia.example.it +PORT=5302 +SECRET=IT_01_41 +REMOTE_PREFIX=41 + +[TRUNK_51] +HOST=svxref-toscana.example.it +PORT=5302 +SECRET=IT_01_51 +REMOTE_PREFIX=51 + +[TRUNK_61] +HOST=svxref-abruzzo.example.it +PORT=5302 +SECRET=IT_01_61 +REMOTE_PREFIX=61 + +[TRUNK_62] +HOST=svxref-marche.example.it +PORT=5302 +SECRET=IT_01_62 +REMOTE_PREFIX=62 + +[TRUNK_71] +HOST=svxref-puglia.example.it +PORT=5302 +SECRET=IT_01_71 +REMOTE_PREFIX=71 + +[TRUNK_81] +HOST=svxref-basilicata.example.it +PORT=5302 +SECRET=IT_01_81 +REMOTE_PREFIX=81 + +[TRUNK_82] +HOST=svxref-calabria.example.it +PORT=5302 +SECRET=IT_01_82 +REMOTE_PREFIX=82 + +[TRUNK_83] +HOST=svxref-campania.example.it +PORT=5302 +SECRET=IT_01_83 +REMOTE_PREFIX=83 + +[TRUNK_84] +HOST=svxref-molise.example.it +PORT=5302 +SECRET=IT_01_84 +REMOTE_PREFIX=84 + +[TRUNK_91] +HOST=svxref-sicilia.example.it +PORT=5302 +SECRET=IT_01_91 +REMOTE_PREFIX=91 +``` + +Tutte le altre configurazioni regionali seguono lo stesso schema: impostare +`LOCAL_PREFIX` al codice a 2 cifre della propria regione e aggiungere una sezione +`[TRUNK_xx]` per ciascuna delle altre 19 regioni, usando il `SECRET` corrispondente +secondo la convenzione sopra indicata. + +--- + +## Conversazioni simultanee + +Non esiste **nessun limite fisso** al numero di QSO simultanei sul trunk. Il trunk +tra ogni coppia di reflector è una singola connessione TCP che multiplexa tutti i +TG attivi contemporaneamente — ogni frame `MsgTrunkAudio` è etichettato con il +numero di TG, quindi qualsiasi numero di TG può trasmettere audio nello stesso momento. + +L'unica regola per TG è che **un solo operatore alla volta può trasmettere su un +dato TG** (gestito dalla logica di arbitraggio). Più TG possono avere trasmissioni +attive simultaneamente senza interferire. + +### Stima della banda necessaria + +| Codec | Bitrate per TG attivo | TG simultanei su un trunk da 1 Mbps | +|-------|-----------------------|--------------------------------------| +| OPUS | ~8–16 kbps | ~60–125 | +| GSM | ~13 kbps | ~75 | + +In questo deployment italiano, anche se tutti e 20 i TG regionali fossero +simultaneamente in QSO, il carico totale sul trunk per ogni reflector rimarrebbe +ampiamente sotto 1 Mbps. La banda non è un fattore limitante a questa scala; +il vero limite pratico è il numero di operatori patentati attivi in un dato momento. + +--- + +## Lista di controllo per ogni regione + +1. Impostare `LOCAL_PREFIX` al codice a 2 cifre dalla tabella sopra. +2. Aggiungere una sezione `[TRUNK_xx]` per **ciascuna delle altre regioni** (19 sezioni in totale). +3. Usare lo stesso valore di `SECRET` della sezione corrispondente sul peer — secret + non corrispondenti impediranno la connessione del trunk. +4. Aprire la porta TCP `5302` in ingresso nel firewall (trunk) e la porta `5300` (client). +5. Verificare che `HOST` risolva all'IP pubblico del peer dalla rete del server. diff --git a/docs/DOCKER.md b/docs/DOCKER.md new file mode 100644 index 0000000..af8ad70 --- /dev/null +++ b/docs/DOCKER.md @@ -0,0 +1,149 @@ +# Running GeuReflector in Docker + +This guide covers building a Docker image and running the reflector as a +container. The image uses a two-stage build: a full build environment compiles +the binary, then only the runtime libraries and the binary are copied into a +minimal `debian:bookworm-slim` image. + +--- + +## Prerequisites + +- Docker Engine ≥ 20.10 +- Docker Compose v2 (the `docker compose` plugin, not the legacy `docker-compose`) + +--- + +## Quick start + +### 1. Prepare the config directory + +```bash +mkdir config +cp /path/to/your/svxreflector.conf config/svxreflector.conf +``` + +The config file is mounted read-only into the container. Edit it on the host; +restart the container to apply changes (or use the PTY interface for live +user/password reloads — see below). + +At minimum, add `LOCAL_PREFIX` and any `[TRUNK_x]` sections you need. See +[`docs/INSTALL.md`](INSTALL.md) for the config additions required for trunking. + +### 2. Build and start + +```bash +docker compose up -d +``` + +This builds the image on first run. To rebuild after a source change: + +```bash +docker compose up -d --build +``` + +### 3. Check logs + +```bash +docker compose logs -f +``` + +A healthy trunk connection appears as: + +``` +TRUNK_2: Connected to reflector-b.example.com:5302 +TRUNK_2: Trunk hello from peer 'TRUNK_1' local_prefix=2 priority=3847291042 +``` + +--- + +## Ports + +| Port | Protocol | Purpose | +|------------|----------|----------------------------------------------| +| 5300 | TCP | SvxLink client connections | +| 5300 | UDP | SvxLink client audio | +| 5302 | TCP | Server-to-server trunk links | +| 8080 | TCP | HTTP `/status` endpoint (optional) | + +If you do not set `HTTP_SRV_PORT` in the config, remove the `8080` port mapping +from `docker-compose.yml`. + +--- + +## Volumes + +| Mount point | Purpose | +|--------------------------------------|-------------------------------------------------| +| `/etc/svxlink/svxreflector.conf` | Configuration file (mount from host, read-only) | +| `/etc/svxlink/pki/` | TLS certificates and CA state (persistent) | + +The `pki` volume persists certificates across container restarts. If you manage +certificates externally, you can remove this volume and mount your cert directory +directly instead. + +--- + +## Live user/password reload (PTY interface) + +The command PTY is available inside the container at `/dev/shm/reflector_ctrl` +(set by `COMMAND_PTY` in the config). Use `docker exec` to write to it: + +```bash +docker compose exec svxreflector \ + sh -c 'echo "CFG PASSWORDS MyNodes newpassword" > /dev/shm/reflector_ctrl' +``` + +--- + +## Building the image manually (without Compose) + +```bash +docker build -t geureflector . + +docker run -d \ + --name svxreflector \ + --restart unless-stopped \ + -p 5300:5300/tcp \ + -p 5300:5300/udp \ + -p 5302:5302/tcp \ + -p 8080:8080/tcp \ + -v ./config/svxreflector.conf:/etc/svxlink/svxreflector.conf:ro \ + -v svxreflector_pki:/etc/svxlink/pki \ + geureflector +``` + +--- + +## Passing additional flags + +The container entrypoint is `svxreflector`. Append any supported flags after +the service definition or in the `command:` key of `docker-compose.yml`: + +```yaml +command: ["--config", "/etc/svxlink/svxreflector.conf", "--logfile", "/var/log/svxreflector.log"] +``` + +Available flags: + +| Flag | Description | +|------|-------------| +| `--config ` | Config file path (default used by the image: `/etc/svxlink/svxreflector.conf`) | +| `--logfile ` | Redirect stdout/stderr to a file instead of the container log | +| `--runasuser ` | Drop privileges to the given user after startup | +| `--pidfile ` | Write PID to file (not normally needed in containers) | + +Do **not** pass `--daemon` — Docker manages the process lifecycle. + +--- + +## Updating + +To update to a new version of the source: + +```bash +git pull +docker compose up -d --build +``` + +The old container is replaced; the `pki` volume is preserved. diff --git a/docs/INSTALL.md b/docs/INSTALL.md new file mode 100644 index 0000000..6248094 --- /dev/null +++ b/docs/INSTALL.md @@ -0,0 +1,206 @@ +# Installing GeuReflector as a replacement for SvxReflector + +This guide is for sysops who already have a working SvxReflector installation +and want to replace it with GeuReflector. The binary name, config format, and +systemd service are all compatible — the transition is a drop-in replacement +plus a few config additions. + +--- + +## Prerequisites + +- A working SvxReflector installation (any recent version) +- Build tools: `git`, `cmake` (≥ 3.7), `g++` with C++11 support +- Development libraries (same as SvxReflector itself): + ```bash + # Debian/Ubuntu + sudo apt install build-essential cmake libsigc++-2.0-dev libssl-dev \ + libjsoncpp-dev libpopt-dev + + # Optional codec support + sudo apt install libopus-dev libgsm1-dev libspeex-dev + ``` + +--- + +## 1. Get the source + +```bash +git clone https://github.com/IW1GEU/geureflector.git +cd geureflector +``` + +--- + +## 2. Build + +```bash +cmake -S src -B build -DLOCAL_STATE_DIR=/var +cmake --build build +``` + +The compiled binary is at `build/bin/svxreflector`. + +--- + +## 3. Stop the running service + +```bash +sudo systemctl stop svxreflector +``` + +--- + +## 4. Back up the original binary + +Find where your current binary lives: + +```bash +which svxreflector +# typically /usr/bin/svxreflector or /usr/local/bin/svxreflector +``` + +Back it up: + +```bash +sudo cp /usr/bin/svxreflector /usr/bin/svxreflector.orig +``` + +--- + +## 5. Install the new binary + +```bash +sudo cp build/bin/svxreflector /usr/bin/svxreflector +``` + +Use the same destination path you found in step 4. + +--- + +## 6. Update the configuration + +Your existing `/etc/svxlink/svxreflector.conf` works as-is — GeuReflector is +fully backwards compatible. The trunk features are opt-in: if you add nothing, +the reflector behaves exactly like the original. + +To enable trunking, add two things: + +### 6a. Declare this reflector's TG prefix + +In the `[GLOBAL]` section: + +```ini +[GLOBAL] +# ... existing settings ... +LOCAL_PREFIX=1 # this reflector owns TGs starting with "1" +``` + +A comma-separated list is accepted if this reflector covers multiple prefix +groups: + +```ini +LOCAL_PREFIX=11,12,13 +``` + +### 6b. Add a trunk section for each peer + +At the end of the config file, add one `[TRUNK_x]` section per peer reflector: + +```ini +[TRUNK_2] +HOST=reflector-b.example.com +PORT=5302 +SECRET=a_strong_shared_secret +REMOTE_PREFIX=2 +``` + +- `PORT` defaults to `5302` if omitted. +- Both sides of each trunk must use the same `SECRET`. +- `REMOTE_PREFIX` also accepts a comma-separated list. + +### 6c. Optional: enable the HTTP status endpoint + +```ini +[GLOBAL] +# ... existing settings ... +HTTP_SRV_PORT=8080 +``` + +The `/status` endpoint will include a `trunks` object showing connection state +and active talkers per link. + +--- + +## 7. Open the trunk port in the firewall + +The trunk uses TCP port `5302` (separate from the client port `5300`). + +```bash +# firewalld +sudo firewall-cmd --permanent --add-port=5302/tcp +sudo firewall-cmd --reload + +# ufw +sudo ufw allow 5302/tcp + +# iptables +sudo iptables -A INPUT -p tcp --dport 5302 -j ACCEPT +``` + +--- + +## 8. Start the service + +```bash +sudo systemctl start svxreflector +sudo systemctl status svxreflector +``` + +--- + +## 9. Verify the trunk is working + +Check the logs for the handshake message: + +```bash +journalctl -u svxreflector -f +``` + +A successful trunk connection looks like: + +``` +TRUNK_2: Connected to reflector-b.example.com:5302 +TRUNK_2: Trunk hello from peer 'TRUNK_1' local_prefix=2 priority=3847291042 +``` + +If `HTTP_SRV_PORT` is set, query the status endpoint: + +```bash +curl -s http://localhost:8080/status | python3 -m json.tool +``` + +The `trunks` object will show `"connected": true` for each active link. + +--- + +## Rolling back + +If anything goes wrong, restore the original binary and restart: + +```bash +sudo systemctl stop svxreflector +sudo cp /usr/bin/svxreflector.orig /usr/bin/svxreflector +sudo systemctl start svxreflector +``` + +No config changes are needed to roll back — the original binary simply ignores +the `LOCAL_PREFIX` and `[TRUNK_x]` entries. + +--- + +## Further reading + +- [`docs/TRUNK_PROTOCOL.md`](TRUNK_PROTOCOL.md) — wire protocol specification +- [`docs/DEPLOYMENT_ITALY.md`](DEPLOYMENT_ITALY.md) — full national deployment + example with configuration for all regions diff --git a/docs/TRUNK_PROTOCOL.md b/docs/TRUNK_PROTOCOL.md new file mode 100644 index 0000000..97562fa --- /dev/null +++ b/docs/TRUNK_PROTOCOL.md @@ -0,0 +1,328 @@ +# GeuReflector — Server-to-Server Trunk Protocol + +## Overview + +GeuReflector extends SvxReflector with a server-to-server trunk protocol that +allows two or more independent reflector instances to share talk groups (TGs) +as simultaneous, independent voice channels — analogous to telephone trunk lines. + +TGs are routed by **numeric prefix**: each reflector owns all TGs whose decimal +string representation starts with a configured digit string. For example, a +reflector configured with `LOCAL_PREFIX=1` is authoritative for TG 1, 10, 100, +1000, 12345, 19999999, etc. This is analogous to E.164 telephone numbering +(country codes, area codes). + +Multiple reflectors can be trunked together in a full mesh. Each pair of +reflectors has one `[TRUNK_x]` section per side. Each TG has exactly one +authoritative home reflector (the one whose prefix matches); all other +reflectors are transparent proxies for that TG. + +Clients (SvxLink nodes) connect to their local reflector as normal and are +completely unaware of the trunk. When a remote talker is active on a TG whose +prefix belongs to another reflector, local clients receive a standard +`MsgTalkerStart` with the remote node's callsign, indistinguishable from a +local talker. + +--- + +## Architecture + +``` + SvxLink nodes SvxLink nodes SvxLink nodes + │ │ │ + ▼ ▼ ▼ + Reflector 1 ◄──trunk──► Reflector 2 ◄──trunk──► Reflector 3 + prefix "1" prefix "2" prefix "3" + └──────────────────────────────────────────────►┘ + (full mesh) +``` + +Each reflector runs one `TrunkLink` instance per configured peer. The trunk +is a persistent outbound TCP connection from each reflector to its peer on a +dedicated port (default 5302, separate from the client port 5300). + +--- + +## TG Ownership and Routing + +A TG number belongs to the reflector whose `LOCAL_PREFIX` is a string prefix +of the TG's decimal representation: + +| TG number | Decimal string | Owned by | +|-----------|---------------|----------| +| 1 | "1" | prefix "1" | +| 10 | "10" | prefix "1" | +| 100 | "100" | prefix "1" | +| 1234567 | "1234567" | prefix "1" | +| 2 | "2" | prefix "2" | +| 25 | "25" | prefix "2" | +| 30 | "30" | prefix "3" | + +Ownership check: `std::to_string(tg).substr(0, prefix.size()) == prefix` + +In a full mesh of N reflectors, each `[TRUNK_x]` section on reflector A points +at reflector B and carries only TGs whose decimal string starts with B's prefix +(`REMOTE_PREFIX`). No multi-hop routing is needed because every pair has a +direct trunk. + +--- + +## Connection and Handshake + +On TCP connect, both sides immediately send `MsgTrunkHello`. No challenge/response +is used; authentication relies on network-level access control (firewall rules) +and the shared secret validated at the application level in future versions. + +``` +Reflector A (prefix "1") Reflector B (prefix "2") + │──── MsgTrunkHello(id, local_prefix="1", priority) ──►│ + │◀─── MsgTrunkHello(id, local_prefix="2", priority) ───│ + │ (both sides ready) │ +``` + +`MsgTrunkHello` carries: +- `id` — the config section name (e.g. `TRUNK_2`), used for logging +- `local_prefix` — the sender's authoritative TG prefix (e.g. `"1"`) +- `priority` — random 32-bit nonce generated at startup, used for tie-breaking + +--- + +## Normal Talker Flow + +When a local client starts transmitting on a TG owned by the peer: + +``` +Reflector A (owns prefix "1") Reflector B (owns prefix "2") + │ SM0ABC starts on TG 25 │ + │ setTalkerForTG(25, SM0ABC) │ + │──── MsgTrunkTalkerStart(25, "SM0ABC") ──►│ + │ setTrunkTalkerForTG(25, "SM0ABC") + │ broadcast MsgTalkerStart(25, "SM0ABC") to B clients + │──── MsgTrunkAudio(25, ) ─►│ + │ broadcast MsgUdpAudio to B TG25 clients + │ (repeats for each audio frame) │ + │──── MsgTrunkFlush(25) ──────────►│ + │ setTalkerForTG(25, null) │ + │──── MsgTrunkTalkerStop(25) ─────►│ + │ clearTrunkTalkerForTG(25) + │ broadcast MsgTalkerStop(25, "SM0ABC") to B clients + │ broadcast MsgUdpFlushSamples to B TG25 clients +``` + +The reverse direction (B client talking on a TG owned by A) is symmetric. + +--- + +## Talker Arbitration and Tie-Breaking + +Each reflector arbitrates talkers independently for its own clients. The trunk +adds a second layer of arbitration for shared TGs. + +**Normal case (one side talking at a time):** the first `MsgTrunkTalkerStart` +to arrive locks the TG on the receiving side. Local clients on that side cannot +grab the talker slot while the trunk lock is held — their audio packets are +silently dropped by the existing `talker != client` check in the reflector's +UDP handler. + +**Simultaneous claim (both sides start at the same instant):** resolved using +the `priority` nonce exchanged in `MsgTrunkHello`: + +- The side with the **lower** priority value wins +- The losing side calls `setTalkerForTG(tg, nullptr)` to clear its local talker, + then calls `setTrunkTalkerForTG(tg, peer_callsign)` to accept the remote claim +- The losing side suppresses the spurious `MsgTrunkTalkerStop` that would + otherwise be sent when the local talker is cleared (tracked via `m_yielded_tgs`) + +--- + +## Local Client Arbitration Guard + +In `Reflector::udpDatagramReceived()`, before accepting a new local talker on a +TG that is shared with any trunk, the reflector checks whether the trunk already +holds the TG: + +```cpp +if ((talker == 0) && !TGHandler::instance()->hasTrunkTalker(tg)) +{ + TGHandler::instance()->setTalkerForTG(tg, client); + ... +} +``` + +This single guard ensures that remote conversations are protected from +interruption by local clients. + +--- + +## Keepalive / Heartbeat + +Both sides send `MsgTrunkHeartbeat` periodically to keep the TCP connection +alive and detect peer disconnection: + +- TX: sent when no other message has been sent for 10 timer ticks (10 s) +- RX timeout: connection torn down if no message received for 15 ticks (15 s) + +On disconnect, all trunk talker locks are immediately released so local clients +are not left blocked. The `TcpPrioClient` infrastructure handles automatic +reconnection. + +--- + +## Protocol Messages + +All messages use the existing SvxReflector TCP framing: a serialized +`ReflectorMsg` header (containing the type field) followed by the message +payload, packed using the `ASYNC_MSG_MEMBERS` macro. + +| Type | Name | Fields | +|------|-----------------------|--------------------------------------------------| +| 115 | `MsgTrunkHello` | `string id`, `string local_prefix`, `uint32 priority` | +| 116 | `MsgTrunkTalkerStart` | `uint32 tg`, `string callsign` | +| 117 | `MsgTrunkTalkerStop` | `uint32 tg` | +| 118 | `MsgTrunkAudio` | `uint32 tg`, `uint8[] audio` | +| 119 | `MsgTrunkFlush` | `uint32 tg` | +| 120 | `MsgTrunkHeartbeat` | *(no fields)* | + +Type numbers 115–120 are chosen to follow the last existing SvxReflector TCP +message type (114 = `MsgStartUdpEncryption`) without collision. + +Audio is transported over TCP (not UDP) — the trunk is a reliable server-to-server +link, not a lossy radio gateway path, so TCP framing is appropriate and avoids +the complexity of a separate UDP cipher channel. + +--- + +## TGHandler Changes + +Trunk talker state is stored separately from the `ReflectorClient*` talker +pointer in `TGHandler`, in a parallel `std::map` keyed +by TG number. This avoids any refactoring of the existing `ReflectorClient` +type hierarchy. + +New methods: +- `setTrunkTalkerForTG(tg, callsign)` — lock TG, emit `trunkTalkerUpdated` signal +- `clearTrunkTalkerForTG(tg)` — release lock on one TG +- `clearAllTrunkTalkers()` — release all locks (used on disconnect) +- `trunkTalkerForTG(tg)` — returns callsign or `""` if none +- `hasTrunkTalker(tg)` — returns bool, used in arbitration guard +- `trunkTalkersSnapshot()` — returns const ref to the map (for HTTP status) + +New signal: +- `trunkTalkerUpdated(tg, old_callsign, new_callsign)` — connected to + `Reflector::onTrunkTalkerUpdated()` which broadcasts `MsgTalkerStart`/`Stop` + to local clients with the correct remote callsign + +--- + +## HTTP Status + +The `/status` JSON endpoint includes a `trunks` object alongside `nodes`: + +```json +{ + "nodes": { ... }, + "trunks": { + "TRUNK_2": { + "host": "reflector-b.example.com", + "port": 5302, + "connected": true, + "local_prefix": "1", + "remote_prefix": "2", + "active_talkers": { + "25": "SM0ABC", + "200": "LA8PV" + } + } + } +} +``` + +`active_talkers` contains only TGs with an active remote talker at the moment +of the request. It is empty when no trunk conversations are in progress. + +--- + +## Configuration + +### Global setting (both reflectors) + +Add `LOCAL_PREFIX` to the `[GLOBAL]` section. This declares which TG prefix +this reflector is authoritative for: + +```ini +[GLOBAL] +... +LOCAL_PREFIX=1 # this reflector owns TGs 1, 10, 100, 1000, ... +``` + +### Per-trunk section + +Add one `[TRUNK_x]` section per peer. `REMOTE_PREFIX` declares which TG prefix +the peer owns: + +```ini +[TRUNK_2] +HOST=reflector-b.example.com # peer hostname or IP +PORT=5302 # peer trunk port (default 5302) +SECRET=change_this_secret # shared secret +REMOTE_PREFIX=2 # peer owns TGs 2, 20, 200, 2000, ... +``` + +### Full-mesh example (three reflectors) + +**Reflector 1** (`LOCAL_PREFIX=1`): +```ini +[TRUNK_2] +HOST=reflector-b.example.com +SECRET=secret_ab +REMOTE_PREFIX=2 + +[TRUNK_3] +HOST=reflector-c.example.com +SECRET=secret_ac +REMOTE_PREFIX=3 +``` + +**Reflector 2** (`LOCAL_PREFIX=2`): +```ini +[TRUNK_1] +HOST=reflector-a.example.com +SECRET=secret_ab +REMOTE_PREFIX=1 + +[TRUNK_3] +HOST=reflector-c.example.com +SECRET=secret_bc +REMOTE_PREFIX=3 +``` + +**Reflector 3** (`LOCAL_PREFIX=3`): +```ini +[TRUNK_1] +HOST=reflector-a.example.com +SECRET=secret_ac +REMOTE_PREFIX=1 + +[TRUNK_2] +HOST=reflector-b.example.com +SECRET=secret_bc +REMOTE_PREFIX=2 +``` + +--- + +## Files Changed vs Upstream SvxLink + +| File | Change | +|------|--------| +| `src/svxlink/reflector/ReflectorMsg.h` | Added `MsgTrunkHello`–`MsgTrunkHeartbeat` (types 115–120); `MsgTrunkHello` carries `local_prefix` string instead of TG list | +| `src/svxlink/reflector/TGHandler.h` | Added trunk talker map, 5 methods + snapshot accessor, `trunkTalkerUpdated` signal | +| `src/svxlink/reflector/TGHandler.cpp` | Implemented trunk talker methods including `clearAllTrunkTalkers` | +| `src/svxlink/reflector/Reflector.h` | Added `m_trunk_links`, `initTrunkLinks()`, `onTrunkTalkerUpdated()` | +| `src/svxlink/reflector/Reflector.cpp` | Wired trunk init, signals, audio relay, arbitration guard, HTTP status | +| `src/svxlink/reflector/TrunkLink.h` | **New** — `TrunkLink` class declaration | +| `src/svxlink/reflector/TrunkLink.cpp` | **New** — full trunk link implementation | +| `src/svxlink/reflector/CMakeLists.txt` | Added `TrunkLink.cpp` to sources | +| `src/svxlink/reflector/svxreflector.conf.in` | Added prefix-based trunk example config | +| `src/versions` | Version bumped to `1.3.99.12+trunk1` | +| `src/svxlink/reflector/svxreflector.cpp` | Updated startup banner | diff --git a/src/CMakeLists.txt b/src/CMakeLists.txt new file mode 100644 index 0000000..ef09e83 --- /dev/null +++ b/src/CMakeLists.txt @@ -0,0 +1,121 @@ +############################################################################## +# GeuReflector — SvxReflector with server-to-server trunk support +# +# Based on SvxLink by Tobias Blomberg / SM0SVX +# Trunk extension by IW1GEU +# +# This program is free software; you can redistribute it and/or modify +# it under the terms of the GNU General Public License as published by +# the Free Software Foundation; either version 2 of the License, or +# (at your option) any later version. +############################################################################## + +cmake_minimum_required(VERSION 3.7...3.30) +project(geureflector C CXX) + +set(PROJECT_INCLUDE_DIR ${PROJECT_BINARY_DIR}/include) +set(CMAKE_LIBRARY_OUTPUT_DIRECTORY ${PROJECT_BINARY_DIR}/lib) +set(CMAKE_BUILD_RPATH "${CMAKE_LIBRARY_OUTPUT_DIRECTORY}") +set(RUNTIME_OUTPUT_DIRECTORY ${PROJECT_BINARY_DIR}/bin) + +list(APPEND CMAKE_MODULE_PATH "${CMAKE_SOURCE_DIR}/cmake/Modules/") + +option(BUILD_STATIC_LIBS "Build static libraries in addition to dynamic" OFF) + +if(NOT DEFINED INTERNAL_SAMPLE_RATE) + set(INTERNAL_SAMPLE_RATE 16000) +endif() +add_definitions(-DINTERNAL_SAMPLE_RATE=${INTERNAL_SAMPLE_RATE}) + +include_directories(${PROJECT_INCLUDE_DIR} ${CMAKE_BINARY_DIR}) + +if(CMAKE_COMPILER_IS_GNUCC OR CMAKE_COMPILER_IS_GNUCXX) + set(CMAKE_C_FLAGS "${CMAKE_C_FLAGS} -Wall -Wstrict-prototypes -Wpointer-arith") + set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -Wall -Wpointer-arith") + set(CMAKE_C_FLAGS_RELEASE "-O3") + set(CMAKE_CXX_FLAGS_RELEASE "-O3") +endif() + +if(NOT CMAKE_BUILD_TYPE) + set(CMAKE_BUILD_TYPE "Release" CACHE STRING "Build type" FORCE) +endif() + +############################################################################## +# Install dirs +############################################################################## +include(GNUInstallDirs) + +set(BIN_INSTALL_DIR ${CMAKE_INSTALL_FULL_BINDIR}) +set(LIB_INSTALL_DIR ${CMAKE_INSTALL_FULL_LIBDIR}) +set(SYSCONF_INSTALL_DIR ${CMAKE_INSTALL_FULL_SYSCONFDIR}) +set(SHARE_INSTALL_PREFIX ${CMAKE_INSTALL_FULL_DATADIR}) +set(SVX_SYSCONF_INSTALL_DIR ${SYSCONF_INSTALL_DIR}/svxlink) +set(SVX_SHARE_INSTALL_DIR ${SHARE_INSTALL_PREFIX}/svxlink) +set(SVX_INCLUDE_INSTALL_DIR ${CMAKE_INSTALL_FULL_INCLUDEDIR}/svxlink) +set(SVX_MODULE_INSTALL_DIR ${LIB_INSTALL_DIR}/svxlink) +set(SVX_LOGIC_CORE_INSTALL_DIR ${LIB_INSTALL_DIR}/svxlink) +set(INCLUDE_INSTALL_DIR ${CMAKE_INSTALL_FULL_INCLUDEDIR}) +set(MAN_INSTALL_DIR ${CMAKE_INSTALL_FULL_MANDIR}) +set(DOC_INSTALL_DIR ${CMAKE_INSTALL_FULL_DOCDIR}) +set(SVX_SPOOL_INSTALL_DIR ${LOCAL_STATE_DIR}/spool/svxlink) + +if(NOT DEFINED LOCAL_STATE_DIR) + set(LOCAL_STATE_DIR ${CMAKE_INSTALL_FULL_LOCALSTATEDIR}) +endif() +set(SVX_LOCAL_STATE_DIR ${LOCAL_STATE_DIR}/lib/svxlink) + +set(CONFIG_FILES "" CACHE INTERNAL "CONFIG_FILES") +set(FILE_OWNER "" CACHE INTERNAL "FILE_OWNER") + +function(expinc filename) + configure_file(${CMAKE_CURRENT_SOURCE_DIR}/${filename} + ${PROJECT_INCLUDE_DIR}/${filename} COPYONLY) +endfunction() + +function(install_if_not_exists src dest) + if(NOT IS_ABSOLUTE "${src}") + set(src "${CMAKE_CURRENT_SOURCE_DIR}/${src}") + endif() + get_filename_component(src_name "${src}" NAME) + if(NOT IS_ABSOLUTE "${dest}") + set(dest "${CMAKE_INSTALL_PREFIX}/${dest}") + endif() + install(CODE " + if(NOT EXISTS \"\$ENV{DESTDIR}${dest}/${src_name}\") + message(STATUS \"Installing: \$ENV{DESTDIR}${dest}/${src_name}\") + execute_process(COMMAND \${CMAKE_COMMAND} -E copy \"${src}\" + \"\$ENV{DESTDIR}${dest}/${src_name}\" + RESULT_VARIABLE copy_result ERROR_VARIABLE error_output) + if(copy_result) + message(FATAL_ERROR \${error_output}) + endif() + else() + message(STATUS \"Skipping : \$ENV{DESTDIR}${dest}/${src_name}\") + endif() + ") +endfunction() + +############################################################################## +# Dependencies +############################################################################## +add_subdirectory(include) + +configure_file(${CMAKE_SOURCE_DIR}/config.h.in ${CMAKE_BINARY_DIR}/config.h @ONLY) + +find_package(SIGC REQUIRED) +include_directories(${SIGC_INCLUDE_DIRS}) +add_definitions(${SIGC_DEFINITIONS}) +set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} ${SIGC_CXX_FLAGS}") +set(LIBS ${LIBS} ${SIGC_LIBRARIES}) + +include(FindCHOWN) + +set(SVXLINK_USER "svxlink" CACHE STRING "SvxLink system user") +set(SVXLINK_GROUP "svxlink" CACHE STRING "SvxLink system group") + +############################################################################## +# Subdirectories +############################################################################## +add_subdirectory(async) +add_subdirectory(misc) +add_subdirectory(svxlink) diff --git a/src/async/.gitignore b/src/async/.gitignore new file mode 100644 index 0000000..b521f04 --- /dev/null +++ b/src/async/.gitignore @@ -0,0 +1,2 @@ +/makefile.root +/Makefile diff --git a/src/async/CMakeLists.txt b/src/async/CMakeLists.txt new file mode 100644 index 0000000..987a94e --- /dev/null +++ b/src/async/CMakeLists.txt @@ -0,0 +1,7 @@ +add_subdirectory(core) +add_subdirectory(cpp) +add_subdirectory(audio) +if(USE_QT) + add_subdirectory(qt) +endif(USE_QT) +add_subdirectory(demo) diff --git a/src/async/ChangeLog b/src/async/ChangeLog new file mode 100644 index 0000000..56fc889 --- /dev/null +++ b/src/async/ChangeLog @@ -0,0 +1,602 @@ + 1.9.0 -- ?? ??? ???? +---------------------- + +* Async::AudioStreamStateDetector facelift + +* Add support for sigc++3 + +* Add support for Qt6 contributed by DL1JBE + + + + 1.8.1 -- 01 Jul 2025 +---------------------- + +* Bugfix in Async::TcpConnection: Illegal cast of pointer to receive buffer. + +* Async::Config: Now specifically handling the "char" case for getValue. + + + + 1.8.0 -- 03 May 2025 +---------------------- + +* Code cleanup of Async::Pty, fixing a small memory leak. + +* TLS connection support added. Build require OpenSSL development files. + +* Async::Exec: Now possible to set up environment variables. + +* Async::Config now have a mechanism for subscribing to changes for specific + configuration variables. + +* Add connection rate throttling, using token buckets, to Async::TcpServer. + +* Async::Config bugfix: Add parameter missing_ok to the string specific + getValue function. + +* Async::AudioDevice: Reopen the audio device on failure + +* Async::DnsLookup: The addresses method will now return the list of hosts in + random order for each call. + + + + 1.7.0 -- 25 Feb 2024 +---------------------- + +* ASYNC_AUDIO_ALSA_ZEROFILL is now enabled by default. + +* Config::getValue() for vectors now tokenize on comma in addition to + whitespace. + +* Improved implementation for SvxLink::SepPair to work better with streaming. + +* Config: Add support for calling getValue on keyed containers, like std::set + and std::multiset. + +* Msg: Add support for std::set variable in messages. + +* New member function AudioSelector::selectedSource() used to find out which + source that are currently selected. + +* AudioSelector::autoSelectEnabled() can now be called with a constant source. + +* Add a signal to the Config class so that one can subscribe to changes in the + configuration. + +* Chunked mode added to the Async::HttpServerConnection. + +* Fix small memory deallocation bug in Async::AudioEncoderOpus. + +* New template class Async::Factory for creating object factories. + +* Added an AudioContainer framework. + +* The Config::valueUpdated signal is now only emitted if the value is changed. + +* The Pty::setLineBuffered method can now be used to enable line buffered mode + where incoming chars are buffered until a or is received. + +* Templated function Async::Config::setValue() so that generic types can be + used without converting them to string first. + +* The Async::Serial class now support all extended baudrates. + +* Rewritten the DNS resolver with support for more resource record types. + +* AudioDeviceUDP can now write zeros to the UDP connection on underflow. That + is, when the "audio device" is open but there is no audio to write zeros + will be written instead. Enable this behavior by setting the environment + variable ASYNC_AUDIO_UDP_ZEROFILL=1. + +* The Config class now also accept associative containers for getValue. This + for example make it possible to use a std::map to read a + configuration variable with a list of values on the form + VARNAME=88.5:-1,136.5:1. It is also possible to use other key/value + separators. + +* New class Async::StateMachine used to build Hierarchical Finite State + Machines. + +* New class Async::TcpPrioClient for handling pools of servers to connect to. + The pool members can be specified using DNS SRV records or locally using the + HOSTS configuration variable. + +* Slightly changed semantics of the TcpClient::connect functions. It's now + not allowed to call the connect function if already connected. + +* Async::Plugin: A new class for loading code as plugins. + +* Bugfix Async::HttpServerConnection: EOL handling failed with newer + compilers. + +* New class Async::AudioLADSPAPlugin which enable the use of LADSPA plugins to + process audio. + + + + 1.6.0 -- 01 Sep 2019 +---------------------- + +* New class Async::AudioFsf, an implementation of a frequency sampling filter. + +* Add method Async::Config::listSections() making it possible to list all + configuration sections. + +* Small optimization in the Async::CppApplication main loop. + +* AudioDeviceAlsa can now write zeros to the audio device on underflow. That + is, when the audio device is open but there is no audio to write zeros will + be written instead. Enable this behavior by setting the environment variable + ASYNC_AUDIO_ALSA_ZEROFILL=1. + + + + 1.5.0 -- 30 Dec 2017 +---------------------- + +* Support for Qt5 added. Patch contributed by Richard Neese. + +* Bugfix in AsyncCppDnsLookupWorker: Lookup could hang due to mutex + locking issues. Patch by Steve/DH1DM. + +* Bugfix in AsyncTcpClient: An assertion error could occur if the connect + method was called twice. + +* Bugfix in AsyncTcpServer: Fix memory leak + +* UdpSocket: The dataReceived signal now also provide the remote UDP port + number + +* Bugfix in AsyncAudioDeviceAlsa/AsyncAudioIO: Assertion for some sound + boards in Async::Timer::setEnable due to the + AudioDeviceAlsa::samplesToWrite function returning a negative value. + +* Now possible to ask the AudioEncoder and AudioDecoder if a codec is + supported or not. + +* AudioSelector bugfix: The output was not flushed when disconnecting + the active source. This hung the audio stream. In the process of fixing + that bug the AudioSelector was to a large part rewritten. The new + implementation also made it possible to implement seamless switches (no + flush when switching between two active streams). + +* AudioProcessor bugfix: State could get corrupted due to double call of + sample output function. + +* TcpClient: DNS lookup is now done on every reconnect. + +* AudioDeviceAlsa: Relaxed assertion for snd_pcm_readi so that fewer frames + than requested may be returned. + + + + 1.4.0 -- 22 Nov 2015 +---------------------- + +* New class Async::Pty used to communicate over UNIX 98 PTY:s. + +* Bugfix in Async::CppApplication: The max_desc variable used in the call to + pselect was not correctly set up in some cases after a removal of a file + descriptor watch. + +* Fixed issues reported by the Coverity scan static analysis tool. + +* The AudioSplitter now also is a source itself so that it can be used to + pipe audio through to the next stage in the pipe instead of having to + use addSink to register all sinks. This make the splitter easier to + handle in an audio pipe where there is a main path and you just want + to split off a branch. + +* The NULL audio codec now encode and decode a sample count which is used + by the decoder to write the correct number of samples to its audio sink. + All written samples will be zero-samples. + +* Bugfix in Async::AudioRecorder: If a write error occurred, the audio pipe + could be blocked indefinitely. Error handling was improved all over. There + now is a "errorOccurred" signal and a "errorMsg" function. + +* Bugfix in Async::AudioEncoder{Opus,Speex,Gsm}: Applications using the Opus, + Speex or Gsm audio encoders could crash due to some dangerous coding. + +* Async::Config: Removed error message printed on open failure. Printing an + error message must be done by the caller. + +* Async::Timer: Now possible to create a timer object that is disabled from + the beginning. + +* Async::Application: New function runTask which can be used to delay + execution of a function until control is returned to the Async main loop. + +* New class Async::AudioGenerator that can generate sine and square waves. + +* Now possible to create an Async::FdWatch without an associated file + descriptor and then set it later using the new setFd function. + + + + 1.3.1 -- 02 Apr 2015 +---------------------- + +* New functions Async::TcpClient::idIdle and Async::TcpConnection::idle + + + + 1.3.0 -- 02 Aug 2014 +---------------------- + +* Fixed Async::AudioDeviceUDP so that audio output is paced instead of writing + as fast as possible. + +* Added a NULL audio encoder and decoder that can be used when one does not + want audio to be sent at all. + +* Added the ability to bind TCP client and server sockets to a specific + IP-address. + +* Serial port settings are now not restored unless they have been explicitly + changed using the Serial::setParams function. + +* The serial port TX/RX buffers are now only flushed if explicitly specified + in the open call. + +* The IpAddress class now have an input stream operator. + +* Bugfix in Async::{AudioSelector,DnsLookup,AudioSplitter}: Important code had + been placed within assert statements. By default CMake set compiler options + that remove all assert statements when compiling in release mode. This + caused some things to stop working when compiling for release. + +* Now possible to change the buffer size of a TCP connection using + TcpConnection::setRecvBufLen. + + + + 1.2.1 -- 30 Jun 2014 +---------------------- + +* Some sound hardware cause an Alsa failure which SvxLink previously handled + as a fatal error (assert) and aborted the application. A warning is now + printed instead and the sound device is closed and reopened. + +* Support added for Opus version < 1.0. + +* Bugfix for Async::Config::getValue. The std::ws operator set the fail bit on + FreeBSD at end of string. This caused some config options to not be read + correctly. + + + + 1.2.0 -- 01 Dec 2013 +---------------------- + +* The Async::AudioRecorder class now have some added features. In addition to + the hard filesize limit there now is a soft limit at which the file will be + closed when the audio source call flushSamples. A signal will be emitted + when either of the limits are hit. Also, begin and end timestamps now are + available. + +* The Async::AudioFilter setOutputGain method now take a dB value as argument. + +* Added support for the Opus audio codec. + +* New class Async::Exec for executing external programs. + + + + 1.1.1 -- 29 Jul 2013 +---------------------- + +* Fixed some include directives for Async::CppApplication. + +* The Async::TcpClient class now always do a name lookup before trying to + connect. Previously, when the old lookup was cached, IP addresses that + changed over time was not handled. + + + + 1.1.0 -- 06 Jun 2013 +---------------------- + +* New class AtTimer to set a timer that expires at the specified time of day. + +* Now using clock_gettime instead of gettimeofday to get the time for timers + since the former provides monotonically increasing time which the + latter don't. + +* Async::DnsLookup: New method resultsAreReady with which it is possible + to check if the DNS lookup is done or if it's still pending. + +* Fixed a buffer overflow bug in the AudioInterpolator. + +* Async::TcpClient: Added two new constructors, one which take an IpAddress + object instead of a host name which is good if you already have the IP + address. The other only take an optional buffer size. The hostname or ip + address is given later to the new connect method. + +* There now is an overloaded Config::getValue function which can take a + container, like an std::vector, to read in a number of space separated values + from a configuration variable. + + + + 1.0.0 -- 08 Mar 2013 +----------------------- + +* Moved from sigc++ version 1.2 to version 2. + Patch contributed by Felix/WU8K. + +* Now possible to bind a UDP socket to a given interface. + +* New "audio device" which read and write audio from a UDP socket. + This can for example be used to stream audio to/from GNU Radio. + +* Now possible to set config variables in memory using the Config::setValue + function. It is not possible to write the change back to the config + file though. + +* New class FileReader to read a file in non blocking mode. Contributed + by Steve / DH1DM. + + + + 0.18.2 -- 27 Nov 2011 +----------------------- + +* Bugfix in Async::AudioDeviceAlsa: There were problems when reopening + the audio device in another mode, like when going from WR to RDWR. + In some cases the audio output would hang. + + + + 0.18.1 -- 05 Nov 2011 +----------------------- + +* Bugfix in Async::AudioDelayLine: Calling "clear" while a "mute" was in + effect would cancel the mute. + +* New methods IpAddress::isEmpty() and IpAddress::clear(). + +* Bugfix in the OSS code: The audio device would not open when not using stdout + as the logging output due to an obscure bug in the OSS code. When not using + stdout, the audio device would open with fd 0, which was interpreted as and + error. + + + + 0.18.0 -- 14 May 2011 +----------------------- + +* The Async::AudioRecorder can now auto detect the file format from the + filename extension. + +* Improved audio device handling which makes it easier to add support for + different kinds of audio subsystems. + +* Added support for the ALSA audio subsystem. + +* Bugfix in AudioFifo: The clear method did not work properly. Under some + circumstances, allSamplesFlushed was not called which caused problems + upstream. + +* Bugfix in AudioDelayLine: Mute and clear did not do the right thing + with the buffer pointer so it didn't behave properly. + +* The AudioDelayLine now fade in and out when muting and clearing to + avoid creating pops in the audio stream. + + + + 0.17.0 -- 26 Apr 2009 +----------------------- + +* The Async internal sample rate is now configurable by a compile time + define (INTERNAL_SAMPLE_RATE in makefile.cfg). + +* Added the Async::AudioStreamStateDetector contributed by Steve / DH1DM. + +* Implemented an audio codec framework that is meant to be used when + streaming audio over a network. + +* Bugfixes in Async::AudioPacer: Using a prebuf size of zero millisecods + did not work. DH1DM: Fixed a buffer calculation error. Gaps could be + introduced in some situations. + +* Moved the audio recorder class from the SvxLink server application + to Async (Async::AudioRecorder). + +* The AudioRecorder can now write WAV files. + +* Bugfix in AudioFilter: Filters was not properly created under som locales, + like german (de_DE). + + + + 0.16.1 -- 30 Jul 2008 +----------------------- + +* Bugfix and rewrite of the sound card code (AudioIO and AudioDevice classes). + +* Bugfix and rewrite of the Async::AudioPacer class. + +* Bugfix in Async::Timer: At destruction of a timer, a check was not made + to see if it really existed. + +* Bugfix in Async::CppDnsLookupWorker: The DNS code had a bug which showed + itself under Ubuntu. A local variable was called "h_addr" which is defined + in netdb.h as "h_addr_list[0]". + + + + 0.16.0 -- 18 May 2008 +----------------------- + +* Lots of changes to adapt everything to the audio pipe infrastructure. + +* Decreased buffers to improve audio latency. + +* New method: AudioSplitter::enableSink make it possible to enable/disable + branches in the splitter. + +* Cleanup: Removed obsolete files and fixed files with wrong names. + +* New method AudioFifo::enableBuffering: Use this method to enable/disable + the buffering in the fifo. When disabled, this will effectively just make + it a passthrough. + +* Now possible to open left/right stereo channels as separate mono + channels. + +* Now possible to set sample rate, block size, channels and buffer size for + a sound card. + +* Made Async::CppDnsLookupWorker thread safe. + + + + 0.15.0 -- 02 Jan 2008 +----------------------- + +* Now compile under CentOS 5.0, Fedora 8, Ubuntu 7.10 and Gentoo. + +* Inserting repeated audio fragments if incoming audio pase is too slow. + Good when using two sound cards with slightly different sampling rate. + +* Fixes for 64 bit platforms. + + + + 0.14.0 -- 15 Apr 2007 +----------------------- + +* Async::Config: Now possible to use the "open" method more than once to + read multiple configuration files. + +* It is now possible to read the sampling rate the audio device is using + method Async::AudioIO::sampleRate. + +* It is now possible to set the gain to use on an audio stream. + +* New method: TcpConnection::isConnected. + +* Now using float instead of short int to represent samples. This make it + easier to apply audio processing on them. + +* Created a new audio-lib where all the audio related classes have been put. + +* A lot of new audio handling classes have been added. + +* Now using libsigc++ version 1.2 instead of the old and outdated 1.0. + +* ASYNC_AUDIO_NOTRIGGER=1 is now the default. + + + + 0.13.0 -- 02 Dec 2005 +----------------------- + +* Merged Serial::InPin and Serial::OutPin to a new typedef Serial::Pin. + Also added PIN_NONE. + +* Bugfix in Config: It was not possible to specify an empty configuration + variable using "". + +* Fixed the makefile problem where it was not possible to compile SvxLink + when a previous "make install" had been done. It tried to link against + the installed libraries instead of the ones just compiled. + + + + 0.12.1 -- 09 Oct 2005 +----------------------- + +* Bugfix in Async::TcpClient: In some situations sockets could be leaked + and functions could be called twice instead of once. This may have caused + crashes. + + + + 0.12.0 -- 14 Aug 2005 +----------------------- + +* Bugfixes for multi open of sound device. + +* New method in AudioIO to clear all samples in the buffer. + +* Bugfix: Handling flushing when already flushed correctly. + +* Bugfix: CPU could go up to 100% when writing audio to the + sound card. + +* DNS lookups are now asynchronous (well, threaded anyway) + so no more blocking DNS lookup calls. + +* Start and end of sound playback is now amplitude shaped + to not create noise at end of playback. + +* Bugfix: Forgot to clean up everything when the + Async::TcpClient::disconnect method was called. + +* Added an unequality operator to the Async::IpAddress class. + + + + 0.11.0 -- 25 Mar 2005 +----------------------- + +* Added some code to AsyncTcpServer to broadcast data to connected + clients. Contribution by SM0LCB / Ulf. + +* Now possible to list all tags in a config section. + +* The Serial class can now handle multiple users per port. + +* Bugfix: The AudioIO class did not handle mutiple users correctly. + + + + 0.10.0 -- 26 Sep 2004 +---------------------- + +* Now the AudioIO object is really checking for full duplex capablility. + +* Bugfix: The application would crash if the AudioIO object were deleted + and then recreated. + +* New class "Serial" for serial port usage. + +* Bugfix: The DnsLookup class did not delete its DnsLookupWorker object + and other memory handling was a mess as well. Thank you "valgrind" + for helping me find this! + + + + 0.9.0 -- 27 May 2004 +---------------------- + +* Separate reader and writer in the same application can now open the audio + device at the same time. The device is automatically set to full duplex + operation. Previously, only one AudioIO object could have the device opened + at a time. + +* Added an environment variable that make it possible to disable the use + of the trigger functionality when opening an audio device. This was necessary + to make Alsa OSS emulation work. Set ASYNC_AUDIO_NOTRIGGER=1 to disable the + use of the trigger functionality. + +* Decreased the audio buffers in the audio device to make audio playback + more responsive. + + + + 0.8.0 -- 04 Apr 2004 +---------------------- + +* Audio handling rewritten to handle a separate reader and writer within + the same application. + +* Split the async lib into core, cpp, qt and demo parts. + +* Earlier log entries for the async library can be found in ../qtel/ChangeLog. + diff --git a/src/async/audio/.gitignore b/src/async/audio/.gitignore new file mode 100644 index 0000000..13b166a --- /dev/null +++ b/src/async/audio/.gitignore @@ -0,0 +1,3 @@ +/depend +/makefile.root +/Makefile diff --git a/src/async/audio/AsyncAudioAmp.h b/src/async/audio/AsyncAudioAmp.h new file mode 100644 index 0000000..5db2b49 --- /dev/null +++ b/src/async/audio/AsyncAudioAmp.h @@ -0,0 +1,166 @@ +/** +@file AsyncAudioAmp.h +@brief Contains an audio pipe class for amplification/attenuation +@author Tobias Blomberg / SM0SVX +@date 2006-07-08 + +\verbatim +Async - A library for programming event driven applications +Copyright (C) 2004-2008 Tobias Blomberg / SM0SVX + +This program is free software; you can redistribute it and/or modify +it under the terms of the GNU General Public License as published by +the Free Software Foundation; either version 2 of the License, or +(at your option) any later version. + +This program is distributed in the hope that it will be useful, +but WITHOUT ANY WARRANTY; without even the implied warranty of +MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +GNU General Public License for more details. + +You should have received a copy of the GNU General Public License +along with this program; if not, write to the Free Software +Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA +\endverbatim +*/ + +#ifndef ASYNC_AUDIO_AMP_INCLUDED +#define ASYNC_AUDIO_AMP_INCLUDED + + +/**************************************************************************** + * + * System Includes + * + ****************************************************************************/ + +#include + + +/**************************************************************************** + * + * Project Includes + * + ****************************************************************************/ + +#include + + +/**************************************************************************** + * + * Local Includes + * + ****************************************************************************/ + + + +/**************************************************************************** + * + * Forward declarations + * + ****************************************************************************/ + + + +/**************************************************************************** + * + * Namespace + * + ****************************************************************************/ + +namespace Async +{ + + +/**************************************************************************** + * + * Forward declarations of classes inside of the declared namespace + * + ****************************************************************************/ + + + +/**************************************************************************** + * + * Defines & typedefs + * + ****************************************************************************/ + + + +/**************************************************************************** + * + * Exported Global Variables + * + ****************************************************************************/ + + + +/**************************************************************************** + * + * Class definitions + * + ****************************************************************************/ + +/** +@brief An audio pipe class for amplification/attenuation of an audio stream +@author Tobias Blomberg / SM0SVX +@date 2006-07-08 + +Use this class to amplify or attenuate an audio stream. +*/ +class AudioAmp : public Async::AudioProcessor +{ + public: + /** + * @brief Default constuctor + */ + AudioAmp(void) : m_gain(1.0) {} + + /** + * @brief Destructor + */ + ~AudioAmp(void) {} + + /** + * @brief Set the gain to use + * @param gain_db The gain given in dB + */ + void setGain(float gain_db) { m_gain = powf(10, gain_db / 20); } + + /** + * @brief Read the gain + * @return Return the gain in dB + */ + float gain(void) const { return 20 * log10(m_gain); } + + + protected: + void processSamples(float *dest, const float *src, int count) + { + for (int i=0; i + + + +/**************************************************************************** + * + * Local Includes + * + ****************************************************************************/ + + + +/**************************************************************************** + * + * Forward declarations + * + ****************************************************************************/ + + + +/**************************************************************************** + * + * Namespace + * + ****************************************************************************/ + +namespace Async +{ + + +/**************************************************************************** + * + * Forward declarations of classes inside of the declared namespace + * + ****************************************************************************/ + + + +/**************************************************************************** + * + * Defines & typedefs + * + ****************************************************************************/ + + + +/**************************************************************************** + * + * Exported Global Variables + * + ****************************************************************************/ + + + +/**************************************************************************** + * + * Class definitions + * + ****************************************************************************/ + +/** +@brief An audio pipe class to clip audio to a given maximum level +@author Tobias Blomberg / SM0SVX +@date 2005-08-01 + +This is an audio pipe class that is used to clip an audio stream to a +given maximum level. +*/ +class AudioClipper : public AudioProcessor +{ + public: + /** + * @brief Default constuctor + * @param clip_level The level to clip at (1.0 is default) + */ + explicit AudioClipper(float clip_level=1.0) : clip_level(clip_level) {} + + /** + * @brief Destructor + */ + ~AudioClipper(void) {} + + /** + * @brief Set the clip level + * @param level The level to set + */ + void setClipLevel(float level) { clip_level = level; } + + + protected: + virtual void processSamples(float *dest, const float *src, int count) + { + for (int i=0; i clip_level) + { + dest[i] = clip_level; + } + else if (src[i] < -clip_level) + { + dest[i] = -clip_level; + } + else + { + dest[i] = src[i]; + } + } + } + + + private: + float clip_level; + + AudioClipper(const AudioClipper&); + AudioClipper& operator=(const AudioClipper&); + +}; /* class AudioClipper */ + + +} /* namespace */ + +#endif /* ASYNC_AUDIO_CLIPPER_INCLUDED */ + + + +/* + * This file has not been truncated + */ + diff --git a/src/async/audio/AsyncAudioCompressor.cpp b/src/async/audio/AsyncAudioCompressor.cpp new file mode 100644 index 0000000..8939310 --- /dev/null +++ b/src/async/audio/AsyncAudioCompressor.cpp @@ -0,0 +1,269 @@ +/** +@file AsyncAudioCompressor.cpp +@brief Contains a class to do audio compression/limiting +@author Tobias Blomberg / SM0SVX +@date 2006-05-01 + +\verbatim +Async - A library for programming event driven applications +Copyright (C) 2004-2008 Tobias Blomberg / SM0SVX + +This program is free software; you can redistribute it and/or modify +it under the terms of the GNU General Public License as published by +the Free Software Foundation; either version 2 of the License, or +(at your option) any later version. + +This program is distributed in the hope that it will be useful, +but WITHOUT ANY WARRANTY; without even the implied warranty of +MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +GNU General Public License for more details. + +You should have received a copy of the GNU General Public License +along with this program; if not, write to the Free Software +Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA +\endverbatim +*/ + + + +/**************************************************************************** + * + * System Includes + * + ****************************************************************************/ + +#include + + +/**************************************************************************** + * + * Project Includes + * + ****************************************************************************/ + + + +/**************************************************************************** + * + * Local Includes + * + ****************************************************************************/ + +#include "AsyncAudioCompressor.h" + + + +/**************************************************************************** + * + * Namespaces to use + * + ****************************************************************************/ + +using namespace std; +using namespace Async; + + + +/**************************************************************************** + * + * Defines & typedefs + * + ****************************************************************************/ + +// DC offset to prevent denormal +static const double DC_OFFSET = 1.0E-25; + + + + + +/**************************************************************************** + * + * Local class definitions + * + ****************************************************************************/ + + + +/**************************************************************************** + * + * Prototypes + * + ****************************************************************************/ + +// linear -> dB conversion +static inline double lin2dB( double lin ) +{ + // 20 / ln( 10 ) + static const double LOG_2_DB = 8.6858896380650365530225783783321; + return log( lin ) * LOG_2_DB; +} + +// dB -> linear conversion +static inline double dB2lin( double dB ) +{ + // ln( 10 ) / 20 + static const double DB_2_LOG = 0.11512925464970228420089957273422; + return exp( dB * DB_2_LOG ); +} + + + +/**************************************************************************** + * + * Exported Global Variables + * + ****************************************************************************/ + + + + +/**************************************************************************** + * + * Local Global Variables + * + ****************************************************************************/ + + + +/**************************************************************************** + * + * Public member functions + * + ****************************************************************************/ + +AudioCompressor::AudioCompressor(void) + : threshdB_(0.0), ratio_(1.0), output_gain(1.0),att_(10.0), rel_(100.0), + envdB_(DC_OFFSET) +{ +} /* AudioCompressor::AudioCompressor */ + + +AudioCompressor::~AudioCompressor(void) +{ +} /* AudioCompressor::~AudioCompressor */ + + +#if 0 +void AudioCompressor::setRatio(float ratio) +{ + comp_ratio = ratio; + attack_step = comp_ratio / (attack * sampling_rate / 1000); + decay_step = comp_ratio / (decay * sampling_rate / 1000); +} /* AudioCompressor::setRatio */ + + +void AudioCompressor::setAttack(unsigned attack_ms) +{ + attack = attack_ms; + attack_step = comp_ratio / (attack * sampling_rate / 1000); +} /* AudioCompressor::setAttack */ + + +void AudioCompressor::setDecay(unsigned decay_ms) +{ + decay = decay_ms; + decay_step = comp_ratio / (decay * sampling_rate / 1000); +} /* AudioCompressor::setDecay */ +#endif + + +void AudioCompressor::setOutputGain(float gain) +{ + if (gain == 0) + { + output_gain = dB2lin(threshdB_ * ratio_ - threshdB_); + } + else + { + output_gain = gain; + } + //cout << "output_gain=" << output_gain << endl; +} /* AudioCompressor::setOutputGain */ + + +void AudioCompressor::reset(void) +{ + envdB_ = DC_OFFSET; +} /* AudioCompressor::reset */ + + +/**************************************************************************** + * + * Protected member functions + * + ****************************************************************************/ + + +void AudioCompressor::processSamples(float *dest, const float *src, int count) +{ + //double max_sample = 0.0; + for (int i=0; i dB + + // threshold + double overdB = keydB - threshdB_; // delta over threshold + if ( overdB < 0.0 ) + overdB = 0.0; + + // attack/release + + overdB += DC_OFFSET; // add DC offset to avoid denormal + + if ( overdB > envdB_ ) + { + att_.run( overdB, envdB_ ); // attack + } + else + { + rel_.run( overdB, envdB_ ); // release + } + + overdB = envdB_ - DC_OFFSET; // subtract DC offset + //cout << overdB << endl; + + /* Regarding the DC offset: In this case, since the offset is added before + * the attack/release processes, the envelope will never fall below the offset, + * thereby avoiding denormals. However, to prevent the offset from causing + * constant gain reduction, we must subtract it from the envelope, yielding + * a minimum value of 0dB. + */ + + // transfer function + double gr = overdB * ( ratio_ - 1.0 ); // gain reduction (dB) + gr = dB2lin( gr ); // convert dB -> linear + + // output gain + dest[i] = output_gain * src[i] * gr; // apply gain reduction to input + + //if (fabs(dest[i]) > max_sample) + // max_sample = dest[i]; + } + + //cout << "max_sample=" << max_sample << endl; + +} /* AudioCompressor::writeSamples */ + + + +/**************************************************************************** + * + * Private member functions + * + ****************************************************************************/ + + + + +/* + * This file has not been truncated + */ + diff --git a/src/async/audio/AsyncAudioCompressor.h b/src/async/audio/AsyncAudioCompressor.h new file mode 100644 index 0000000..9a1790c --- /dev/null +++ b/src/async/audio/AsyncAudioCompressor.h @@ -0,0 +1,257 @@ +/** +@file AsyncAudioCompressor.h +@brief Contains a class to do audio compression/limiting +@author Tobias Blomberg / SM0SVX +@date 2006-05-01 + +\verbatim +Async - A library for programming event driven applications +Copyright (C) 2004-2008 Tobias Blomberg / SM0SVX + +This program is free software; you can redistribute it and/or modify +it under the terms of the GNU General Public License as published by +the Free Software Foundation; either version 2 of the License, or +(at your option) any later version. + +This program is distributed in the hope that it will be useful, +but WITHOUT ANY WARRANTY; without even the implied warranty of +MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +GNU General Public License for more details. + +You should have received a copy of the GNU General Public License +along with this program; if not, write to the Free Software +Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA +\endverbatim +*/ + +#ifndef ASYNC_AUDIO_COMPRESSOR_INCLUDED +#define ASYNC_AUDIO_COMPRESSOR_INCLUDED + + +/**************************************************************************** + * + * System Includes + * + ****************************************************************************/ + +#include + + +/**************************************************************************** + * + * Project Includes + * + ****************************************************************************/ + +#include + + + +/**************************************************************************** + * + * Local Includes + * + ****************************************************************************/ + + + +/**************************************************************************** + * + * Forward declarations + * + ****************************************************************************/ + + + +/**************************************************************************** + * + * Namespace + * + ****************************************************************************/ + +namespace Async +{ + + +/**************************************************************************** + * + * Forward declarations of classes inside of the declared namespace + * + ****************************************************************************/ + + + +/**************************************************************************** + * + * Defines & typedefs + * + ****************************************************************************/ + + + +/**************************************************************************** + * + * Exported Global Variables + * + ****************************************************************************/ + + + +/**************************************************************************** + * + * Class definitions + * + ****************************************************************************/ + +class EnvelopeDetector +{ + public: + EnvelopeDetector( double ms = 1.0, double sampleRate = INTERNAL_SAMPLE_RATE ) + : sampleRate_( sampleRate ), ms_( ms ), coef_( 0.0 ) + { + setCoef(); + } + + virtual ~EnvelopeDetector() {} + + // time constant + virtual void setTc( double ms ) + { + ms_ = ms; + setCoef(); + } + + virtual double getTc( void ) { return ms_; } + + // sample rate + virtual void setSampleRate( double sampleRate ) + { + sampleRate_ = sampleRate; + setCoef(); + } + + virtual double getSampleRate( void ) { return sampleRate_; } + + // runtime function + inline void run( double in, double &state ) + { + state = in + coef_ * ( state - in ); + } + + private: + double sampleRate_; // sample rate + double ms_; // time constant in ms + double coef_; // runtime coefficient + + void setCoef( void ) // coef algorithm + { + coef_ = exp( -1.0 / ( 0.001 * ms_ * sampleRate_ ) ); + } + +}; // end EnvelopeDetector class + + + + + +/** +@brief A class to do audio compression/limiting +@author Tobias Blomberg / SM0SVX +@date 2006-05-01 + +Use this audio pipe class to do compression on an audio stream. Compression +is a method to reduce the dynamic range of an audio signal. After it has been +compressed it can be amplified to get a more audible end result. + +This audio pipe component is mostly untested and is based on some ripped off +code which I really have not checked how it performs or if it works at all... +*/ +class AudioCompressor : public AudioProcessor +{ + public: + /** + * @brief Default constuctor + */ + AudioCompressor(void); + + /** + * @brief Destructor + */ + ~AudioCompressor(void); + + /** + * @brief Set the compression threshold + * @param thresh_db The compression threshold in dB + * + * The threshold is the level, in dB, the signal must rise to before + * the compressor kicks in. + */ + void setThreshold(double thresh_db) { threshdB_ = thresh_db; } + + /** + * @brief Set the compression ratio + * @param ratio The compression ratio (ex 0.1 == 10:1) + */ + void setRatio(double ratio) { ratio_ = ratio; } + + /** + * @brief Set the compressor attack time + * @param attack_ms The attack time in milliseconds + */ + void setAttack(double attack_ms) { att_.setTc(attack_ms);} + + /** + * @brief Set the compressor decay time + * @param decay_ms The decay time in milliseconds + */ + void setDecay(double decay_ms) { rel_.setTc(decay_ms); } + + /** + * @brief Set the output gain + * @param gain The gain to set. + * + * The output gain is the amplification applied to the audio signal + * before it leaves the compressor. If gain > 1 the signal is amplified. + * If gain < 1 the signal is attenuated. + */ + void setOutputGain(float gain); + + /** + * @brief Reset the compressor + */ + void reset(void); + + + protected: + virtual void processSamples(float *dest, const float *src, int count); + + + private: + // transfer function + double threshdB_; // threshold (dB) + double ratio_; // ratio (compression: < 1 ; expansion: > 1) + double output_gain; + + // attack/release + EnvelopeDetector att_; // attack + EnvelopeDetector rel_; // release + + // runtime variables + double envdB_; // over-threshold envelope (dB) + + AudioCompressor(const AudioCompressor&); + AudioCompressor& operator=(const AudioCompressor&); + +}; /* class AudioCompressor */ + + +} /* namespace */ + +#endif /* ASYNC_AUDIO_COMPRESSOR_INCLUDED */ + + + +/* + * This file has not been truncated + */ + diff --git a/src/async/audio/AsyncAudioContainer.cpp b/src/async/audio/AsyncAudioContainer.cpp new file mode 100644 index 0000000..5de6c25 --- /dev/null +++ b/src/async/audio/AsyncAudioContainer.cpp @@ -0,0 +1,140 @@ +/** +@file AsyncAudioContainer.cpp +@brief Base class for audio container handlers +@author Tobias Blomberg / SM0SVX +@date 2020-02-29 + +\verbatim +Async - A library for programming event driven applications +Copyright (C) 2003-2020 Tobias Blomberg / SM0SVX + +This program is free software; you can redistribute it and/or modify +it under the terms of the GNU General Public License as published by +the Free Software Foundation; either version 2 of the License, or +(at your option) any later version. + +This program is distributed in the hope that it will be useful, +but WITHOUT ANY WARRANTY; without even the implied warranty of +MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +GNU General Public License for more details. + +You should have received a copy of the GNU General Public License +along with this program; if not, write to the Free Software +Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA +\endverbatim +*/ + + +/**************************************************************************** + * + * System Includes + * + ****************************************************************************/ + + + +/**************************************************************************** + * + * Project Includes + * + ****************************************************************************/ + + + +/**************************************************************************** + * + * Local Includes + * + ****************************************************************************/ + +#include "AsyncAudioContainer.h" +#include "AsyncAudioContainerWav.h" +#ifdef OGG_MAJOR +#include "AsyncAudioContainerOpus.h" +#endif +#include "AsyncAudioContainerPcm.h" + + +/**************************************************************************** + * + * Namespaces to use + * + ****************************************************************************/ + +using namespace Async; + + +/**************************************************************************** + * + * Defines & typedefs + * + ****************************************************************************/ + + + +/**************************************************************************** + * + * Local class definitions + * + ****************************************************************************/ + + + +/**************************************************************************** + * + * Prototypes + * + ****************************************************************************/ + + + +/**************************************************************************** + * + * Exported Global Variables + * + ****************************************************************************/ + + + +/**************************************************************************** + * + * Local Global Variables + * + ****************************************************************************/ + + + +/**************************************************************************** + * + * Public member functions + * + ****************************************************************************/ + +AudioContainer* Async::createAudioContainer(const std::string& name) +{ + static AudioContainerSpecificFactory wav; +#ifdef OGG_MAJOR + static AudioContainerSpecificFactory opus; +#endif + static AudioContainerSpecificFactory pcm; + return AudioContainerFactory::createNamedObject(name); +} /* Async::createAudioContainer */ + + +/**************************************************************************** + * + * Protected member functions + * + ****************************************************************************/ + + + +/**************************************************************************** + * + * Private member functions + * + ****************************************************************************/ + +/* + * This file has not been truncated + */ diff --git a/src/async/audio/AsyncAudioContainer.h b/src/async/audio/AsyncAudioContainer.h new file mode 100644 index 0000000..4823946 --- /dev/null +++ b/src/async/audio/AsyncAudioContainer.h @@ -0,0 +1,265 @@ +/** +@file AsyncAudioContainer.h +@brief Base class for audio container handlers +@author Tobias Blomberg / SM0SVX +@date 2020-02-29 + +\verbatim +Async - A library for programming event driven applications +Copyright (C) 2003-2020 Tobias Blomberg / SM0SVX + +This program is free software; you can redistribute it and/or modify +it under the terms of the GNU General Public License as published by +the Free Software Foundation; either version 2 of the License, or +(at your option) any later version. + +This program is distributed in the hope that it will be useful, +but WITHOUT ANY WARRANTY; without even the implied warranty of +MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +GNU General Public License for more details. + +You should have received a copy of the GNU General Public License +along with this program; if not, write to the Free Software +Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA +\endverbatim +*/ + +/** @example AsyncAudioContainer_demo.cpp +An example of how to use the Async::AudioContainer class +*/ + +#ifndef ASYNC_AUDIO_CONTAINER_INCLUDED +#define ASYNC_AUDIO_CONTAINER_INCLUDED + + +/**************************************************************************** + * + * System Includes + * + ****************************************************************************/ + +#include + + +/**************************************************************************** + * + * Project Includes + * + ****************************************************************************/ + +#include +#include + + +/**************************************************************************** + * + * Local Includes + * + ****************************************************************************/ + + + +/**************************************************************************** + * + * Forward declarations + * + ****************************************************************************/ + + + +/**************************************************************************** + * + * Namespace + * + ****************************************************************************/ + +namespace Async +{ + + +/**************************************************************************** + * + * Forward declarations of classes inside of the declared namespace + * + ****************************************************************************/ + + + +/**************************************************************************** + * + * Defines & typedefs + * + ****************************************************************************/ + + + +/**************************************************************************** + * + * Exported Global Variables + * + ****************************************************************************/ + + + +/**************************************************************************** + * + * Class definitions + * + ****************************************************************************/ + +/** +@brief Base class for audio container handlers +@author Tobias Blomberg / SM0SVX +@date 2020-02-29 + +This is the base class for an audio container. An audio container is a format +for storing audio to file or stream it over a network. Audio containers are +normally created using a factory, via the createAudioContainer function, but it +is possible to create an audio container directly as well. + +After creating a new container it should be fed with audio by connecing the +object to an audio source. If writing a file, the first this to do after +opening the file is to reserve space for the file header, which is written at +the end just before closing the file. This is done so since most formats +include metadata about the size of the file in the header. + +When the AudioContainer class want to write a block of data it emits the +writeBlock signal so you need to connect to this signal to handle the blocks. + +It is up to the application to decide when to close a file. The endStream +function should then be called and then the file header, retrieved using the +header function, can be written at the beginning of the file before closing it. + +@example AsyncAudioContainer_demo.cpp +*/ +class AudioContainer : public Async::AudioSink +{ + public: + /** + * @brief Default constructor + */ + AudioContainer(void) {} + + /** + * @brief Disable copy constructor + */ + AudioContainer(const AudioContainer&) = delete; + + /** + * @brief Disable assignment operator + */ + AudioContainer& operator=(const AudioContainer&) = delete; + + /** + * @brief Destructor + */ + virtual ~AudioContainer(void) {} + + /** + * @brief Retrieve the media type for the audio container + * @return Returns a string representing the media type, e.g. audio/wav + */ + virtual const char* mediaType(void) const = 0; + + /** + * @brief Get the standard filename extension for the audio container + * @return Returns a string containing the filename extention, e.g. wav + */ + virtual const char* filenameExtension(void) const = 0; + + /** + * @brief Indicate to the container that realtime operation is desired + * + * This function can be called to inidicate to the container that we are + * writing the audio to a realtime stream rather than to a file. The + * container can then choose to indicate this in the audio stream if it + * have that capability. + */ + virtual void setRealtime(void) {} + + /** + * @brief Indicate to the container that the stream has ended + * + * This function should be called when the stream has ended. The container + * can then flush the last samples in any buffers and maybe write an end + * record if applicable. All this will happen through the writeBlock + * signal. + */ + virtual void endStream(void) { flushSamples(); } + + /** + * @brief Get the size of the header for this container + * @return Returns the size of the header + * + * This function is normally called directly after opening a file to + * reserve space for writing the header later, usually as the last thing + * before closing the file. + */ + virtual size_t headerSize(void) const { return 0; } + + /** + * @brief Get the header data + * @return Return a pointer to the header data + * + * This function is normally called as the last thing before closing a file + * in order to write the header to the beginning of the file. This of + * course requires that space have been reserved as described in the + * documentation for the headerSize function. + */ + virtual const char* header(void) { return nullptr; } + + /** + * @brief A signal that is emitted when a block is ready to be written + * @param buf Pointer to a buffer containing the data block + * @param len The size of the data block + * + * This signal is emitted each time that the container want to write data. + * The application should connect to this signal i order to write the data + * to file or whatever the destination is. + */ + sigc::signal writeBlock; +}; /* class AudioContainer */ + + +/** + * @brief Convenience typedef for easier access to the factory members + * + * This typedef make it easier to access the members in the Async::Factory + * class e.g. AudioContainerFactory::validFactories(). + */ +typedef Async::Factory AudioContainerFactory; + + +/** + * @brief Convenience struct to make specific factory instantiation easier + * + * This struct make it easier to create a specific factory for a AudioContainer + * class. The constant OBJNAME must be declared in the class that the specific + * factory is for. To instantiate a specific factory is then as easy as: + * + * static AudioContainerSpecificFactory wav; + */ +template +struct AudioContainerSpecificFactory + : public Async::SpecificFactory +{ + AudioContainerSpecificFactory(void) + : Async::SpecificFactory(T::OBJNAME) {} +}; + + +/** + * @brief Create a named AudioContainer-derived object + * @param name The OBJNAME of the class + * @return Returns a new AudioContainer or nullptr on failure + */ +AudioContainer* createAudioContainer(const std::string& name); + + +} /* namespace */ + +#endif /* ASYNC_AUDIO_CONTAINER_INCLUDED */ + +/* + * This file has not been truncated + */ diff --git a/src/async/audio/AsyncAudioContainerOpus.cpp b/src/async/audio/AsyncAudioContainerOpus.cpp new file mode 100644 index 0000000..d030250 --- /dev/null +++ b/src/async/audio/AsyncAudioContainerOpus.cpp @@ -0,0 +1,387 @@ +/** +@file AsyncAudioContainerOpus.cpp +@brief Handle Ogg/Opus type audio container +@author Tobias Blomberg / SM0SVX +@date 2020-02-29 + +\verbatim +Async - A library for programming event driven applications +Copyright (C) 2003-2020 Tobias Blomberg / SM0SVX + +This program is free software; you can redistribute it and/or modify +it under the terms of the GNU General Public License as published by +the Free Software Foundation; either version 2 of the License, or +(at your option) any later version. + +This program is distributed in the hope that it will be useful, +but WITHOUT ANY WARRANTY; without even the implied warranty of +MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +GNU General Public License for more details. + +You should have received a copy of the GNU General Public License +along with this program; if not, write to the Free Software +Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA +\endverbatim +*/ + +/**************************************************************************** + * + * System Includes + * + ****************************************************************************/ + +#include +#include + + +/**************************************************************************** + * + * Project Includes + * + ****************************************************************************/ + + + +/**************************************************************************** + * + * Local Includes + * + ****************************************************************************/ + +#include "AsyncAudioContainerOpus.h" +#include "AsyncAudioEncoder.h" + + +/**************************************************************************** + * + * Namespaces to use + * + ****************************************************************************/ + +using namespace Async; + + +/**************************************************************************** + * + * Defines & typedefs + * + ****************************************************************************/ + + + +/**************************************************************************** + * + * Local class definitions + * + ****************************************************************************/ + + + +/**************************************************************************** + * + * Prototypes + * + ****************************************************************************/ + + + +/**************************************************************************** + * + * Exported Global Variables + * + ****************************************************************************/ + + + +/**************************************************************************** + * + * Local Global Variables + * + ****************************************************************************/ + + + +/**************************************************************************** + * + * Public member functions + * + ****************************************************************************/ + +AudioContainerOpus::AudioContainerOpus(void) +{ + m_enc = AudioEncoder::create("OPUS"); + assert(m_enc != 0); + std::ostringstream ss; + ss << FRAME_SIZE; + m_enc->setOption("FRAME_SIZE", ss.str()); + setHandler(m_enc); + m_enc->writeEncodedSamples.connect( + sigc::mem_fun(*this, &AudioContainerOpus::onWriteEncodedSamples)); + + //int ret = ogg_stream_init(&m_ogg_stream, m_ogg_serial++); + int ret = ogg_stream_init(&m_ogg_stream, 1); + assert(ret == 0); + ret = writeOggOpusHeader(); +} /* AudioContainerOpus::AudioContainerOpus */ + + +AudioContainerOpus::~AudioContainerOpus(void) +{ + clearHandler(); + delete m_enc; + m_enc = 0; + int ret = ogg_stream_clear(&m_ogg_stream); + assert(ret == 0); +} /* AudioContainerOpus::~AudioContainerOpus */ + + +void AudioContainerOpus::endStream(void) +{ + m_enc->flushSamples(); + + // Assemble nil Ogg page + oggpack_buffer oggbuf; + oggpack_writeinit(&oggbuf); + oggpackWriteString(&oggbuf, "OggS", 0); // Magic Signature + oggpack_write(&oggbuf, 0x00, 8); // Stream structure rev + oggpack_write(&oggbuf, 0x04, 8); // EOS flag + oggpack_write(&oggbuf, 0xffffffff, 32); // Absolute granule pos + oggpack_write(&oggbuf, 0xffffffff, 32); // Absolute granule pos + oggpack_write(&oggbuf, m_ogg_stream.serialno, 32); // Stream serial number + oggpack_write(&oggbuf, m_ogg_stream.pageno, 32); // Page sequence no + oggpack_write(&oggbuf, 0, 32); // Page checksum + oggpack_write(&oggbuf, 0, 8); // Page segments + + ogg_page page; + page.header = oggpack_get_buffer(&oggbuf); + page.header_len = oggpack_bytes(&oggbuf); + page.body = 0; + page.body_len = 0; + ogg_page_checksum_set(&page); + + m_block.clear(); + writePage(page, m_block); + writeBlock(m_block.data(), m_block.size()); + + oggpack_writeclear(&oggbuf); +} /* AudioContainerOpus::endStream */ + + +/**************************************************************************** + * + * Protected member functions + * + ****************************************************************************/ + + + +/**************************************************************************** + * + * Private member functions + * + ****************************************************************************/ + +void AudioContainerOpus::onWriteEncodedSamples(const void *data, int len) +{ + //std::cout << "### AudioContainerOpus::onWriteEncodedSamples: len=" + // << len << std::endl; + + //if (m_packet.packet == nullptr) + //{ + // int ret = ogg_stream_init(&m_ogg_stream, m_ogg_serial++); + // assert(ret == 0); + // writeOggOpusHeader(); + + // m_packet.packet = reinterpret_cast(malloc(len)); + // assert(m_packet.packet != nullptr); + // std::memcpy(m_packet.packet, data, len); + // m_packet.bytes = len; + // m_packet.b_o_s = 0; + // m_packet.e_o_s = 0; + // m_packet.granulepos = 48000 * FRAME_SIZE / 1000; + // m_packet.packetno = 2; + // return; + //} + + m_packet.packet = const_cast( + reinterpret_cast(data)); + m_packet.bytes = len; + //m_packet.b_o_s = 0; + //m_packet.e_o_s = 0; + if (len > 0) + { + m_packet.granulepos += 48000 * FRAME_SIZE / 1000; + } + m_packet.packetno += 1; + + if (ogg_stream_packetin(&m_ogg_stream, &m_packet) != 0) + { + std::cerr << "*** ERROR: Could not add Ogg packet to stream" << std::endl; + return; + } + m_pending_packets += 1; + + //free(m_packet.packet); + //m_packet.packet = reinterpret_cast(malloc(len)); + //assert(m_packet.packet != nullptr); + //std::memcpy(m_packet.packet, data, len); + //m_packet.bytes = len; + ////m_packet.b_o_s = 0; + ////m_packet.e_o_s = 0; + //m_packet.granulepos += 48000 * FRAME_SIZE / 1000; + //m_packet.packetno += 1; + + ogg_page page; + while ((ogg_stream_pageout(&m_ogg_stream, &page) != 0) || + (((m_pending_packets >= 5) || m_packet.e_o_s) && + (ogg_stream_flush(&m_ogg_stream, &page) != 0))) + { + m_block.clear(); + writePage(page, m_block); + writeBlock(m_block.data(), m_block.size()); + assert(ogg_page_packets(&page) <= m_pending_packets); + m_pending_packets -= ogg_page_packets(&page); + } + + if (ogg_stream_check(&m_ogg_stream) != 0) + { + printf("### Ogg stream error\n"); + } +} /* AudioContainerOpus::onWriteEncodedSamples */ + + +void AudioContainerOpus::oggpackWriteString(oggpack_buffer* oggbuf, + const char *str, int lenbits) +{ + size_t len = std::strlen(str); + if (lenbits > 0) + { + oggpack_write(oggbuf, len, lenbits); + } + char ch; + while ((ch = *str++)) + { + oggpack_write(oggbuf, ch, 8); + } +} /* AudioContainerOpus::oggpackWriteString */ + + +void AudioContainerOpus::oggpackWriteCommentList(oggpack_buffer* oggbuf, + const std::vector &comments) +{ + oggpack_write(oggbuf, comments.size(), 32); // User Comment List Length + for (auto& comment : comments) + { + oggpackWriteString(oggbuf, comment); + } +} /* AudioContainerOpus::oggpackWriteCommentList */ + + +bool AudioContainerOpus::writePage(const ogg_page& page, std::vector& buf) +{ + //std::cout << "### Writing Ogg/Opus page:" + // << " serialno=" << ogg_page_serialno(&page) + // << " pageno=" << ogg_page_pageno(&page) + // << " gpos=" << ogg_page_granulepos(&page) + // << " cont=" << ogg_page_continued(&page) + // << " pkts=" << ogg_page_packets(&page) + // << " bos=" << ogg_page_bos(&page) + // << " eos=" << ogg_page_eos(&page) + // << " ver=" << ogg_page_version(&page) + // << std::endl; + buf.insert(buf.end(), page.header, page.header + page.header_len); + if (page.body_len > 0) + { + buf.insert(buf.end(), page.body, page.body + page.body_len); + } + return true; +} /* AudioContainerOpus::writePage */ + + +bool AudioContainerOpus::writeOggOpusHeader(void) +{ + m_header.clear(); + + // Assemble Opus ID Header buffer + oggpack_buffer oggbuf; + oggpack_writeinit(&oggbuf); + oggpackWriteString(&oggbuf, "OpusHead", 0); // Magic Signature + oggpack_write(&oggbuf, 1, 8); // Version + oggpack_write(&oggbuf, 1, 8); // Channel Count + oggpack_write(&oggbuf, 48000 * FRAME_SIZE / 1000, 16); // Pre-skip + oggpack_write(&oggbuf, INTERNAL_SAMPLE_RATE, 32); // Sample Rate + oggpack_write(&oggbuf, 0, 16); // Output Gain + oggpack_write(&oggbuf, 0, 8); // Mapping Family + + // Put Opus ID Header in an Ogg packet and add it to the stream + ogg_packet oggpkt; + oggpkt.packet = oggpack_get_buffer(&oggbuf); + oggpkt.bytes = oggpack_bytes(&oggbuf); + oggpkt.b_o_s = 1; + oggpkt.e_o_s = 0; + oggpkt.granulepos = 0; + oggpkt.packetno = 0; + if (ogg_stream_packetin(&m_ogg_stream, &oggpkt) != 0) + { + std::cerr << "*** ERROR: Could not add Ogg packet to stream" << std::endl; + return false; + } + oggpack_writeclear(&oggbuf); + + // Flush the stream, get the Opus ID Header Ogg page and send it + ogg_page page; + if (ogg_stream_flush(&m_ogg_stream, &page) == 0) + { + std::cerr << "*** ERROR: Added Ogg packet produced no page" << std::endl; + return false; + } + if (!writePage(page, m_header)) + { + std::cout << "### Ogg Header: ogg_stream_flush" << std::endl; + return false; + } + + // Assemble Opus Comment Header buffer + oggpack_writeinit(&oggbuf); + oggpackWriteString(&oggbuf, "OpusTags", 0); // Magic Signature + oggpackWriteString(&oggbuf, "SvxLink"); // Vendor String + oggpackWriteCommentList(&oggbuf, { // User Comment List + //"R128_TRACK_GAIN=1536", + //"R128_ALBUM_GAIN=1536", + "TITLE=SvxLink Audio Stream", + "DESCRIPTION=A SvxLink audio stream", + //"DATE=2020-02-07", + "GENRE=Ham Radio" + }); + + // Put Opus Comment Header in an Ogg packet and add it to the stream + oggpkt.packet = oggpack_get_buffer(&oggbuf); + oggpkt.bytes = oggpack_bytes(&oggbuf); + oggpkt.b_o_s = 0; + oggpkt.e_o_s = 0; + oggpkt.granulepos = 0; + oggpkt.packetno = 1; + if (ogg_stream_packetin(&m_ogg_stream, &oggpkt) != 0) + { + std::cerr << "*** ERROR: Could not add Ogg packet to stream" << std::endl; + return false; + } + oggpack_writeclear(&oggbuf); + + // Flush the stream, get the Opus Comment Header Ogg pages and send them + while (ogg_stream_flush(&m_ogg_stream, &page) != 0) + { + //std::cout << "### Comment Header: ogg_stream_flush" << std::endl; + if (!writePage(page, m_header)) + { + return false; + } + } + + return true; +} /* AudioContainerOpus::writeOggOpusHeader */ + + +/* + * This file has not been truncated + */ diff --git a/src/async/audio/AsyncAudioContainerOpus.h b/src/async/audio/AsyncAudioContainerOpus.h new file mode 100644 index 0000000..5b9f68c --- /dev/null +++ b/src/async/audio/AsyncAudioContainerOpus.h @@ -0,0 +1,218 @@ +/** +@file AsyncAudioContainerOpus.h +@brief Handle Ogg/Opus type audio container +@author Tobias Blomberg / SM0SVX +@date 2020-02-29 + +\verbatim +Async - A library for programming event driven applications +Copyright (C) 2003-2020 Tobias Blomberg / SM0SVX + +This program is free software; you can redistribute it and/or modify +it under the terms of the GNU General Public License as published by +the Free Software Foundation; either version 2 of the License, or +(at your option) any later version. + +This program is distributed in the hope that it will be useful, +but WITHOUT ANY WARRANTY; without even the implied warranty of +MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +GNU General Public License for more details. + +You should have received a copy of the GNU General Public License +along with this program; if not, write to the Free Software +Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA +\endverbatim +*/ + +/** @example AsyncAudioContainer_demo.cpp +An example of how to use the Async::AudioContainer class +*/ + +#ifndef ASYNC_AUDIO_CONTAINER_OPUS_INCLUDED +#define ASYNC_AUDIO_CONTAINER_OPUS_INCLUDED + + +/**************************************************************************** + * + * System Includes + * + ****************************************************************************/ + +#include +#include +#include + + +/**************************************************************************** + * + * Project Includes + * + ****************************************************************************/ + +#include + + +/**************************************************************************** + * + * Local Includes + * + ****************************************************************************/ + + + +/**************************************************************************** + * + * Forward declarations + * + ****************************************************************************/ + + + +/**************************************************************************** + * + * Namespace + * + ****************************************************************************/ + +namespace Async +{ + + +/**************************************************************************** + * + * Forward declarations of classes inside of the declared namespace + * + ****************************************************************************/ + +class AudioEncoder; + + +/**************************************************************************** + * + * Defines & typedefs + * + ****************************************************************************/ + + + +/**************************************************************************** + * + * Exported Global Variables + * + ****************************************************************************/ + + + +/**************************************************************************** + * + * Class definitions + * + ****************************************************************************/ + +/** +@brief Handle a Ogg/Opus type audio container +@author Tobias Blomberg / SM0SVX +@date 2020-02-29 + +This is an implementation of an Ogg/Opus audio container. An audio container is +a format for storing audio to file or stream it over a network. Audio +containers are normally created using a factory, via the +Async::createAudioContainer function, but it is possible to create an audio +container directly as well. + +\example AsyncAudioContainer_demo.cpp +*/ +class AudioContainerOpus : public AudioContainer +{ + public: + /// The name of this class when used by the object factory + static constexpr const char *OBJNAME = "opus"; + + /** + * @brief Default constructor + */ + AudioContainerOpus(void); + + /** + * @brief Destructor + */ + virtual ~AudioContainerOpus(void); + + /** + * @brief Retrieve the media type for the audio container + * @return Returns a string representing the media type, e.g. audio/wav + */ + virtual const char* mediaType(void) const { return "audio/opus"; } + + /** + * @brief Get the standard filename extension for the audio container + * @return Returns a string containing the filename extention, e.g. wav + */ + virtual const char* filenameExtension(void) const { return "opus"; } + + /** + * @brief Indicate to the container that the stream has ended + * + * This function should be called when the stream has ended. The container + * can then flush the last samples in any buffers and maybe write an end + * record if applicable. All this will happen through the writeBlock + * signal. + */ + virtual void endStream(void); + + /** + * @brief Get the size of the header for this container + * @return Returns the size of the header + * + * This function is normally called directly after opening a file to + * reserve space for writing the header later, usually as the last thing + * before closing the file. + */ + virtual size_t headerSize(void) const { return m_header.size(); } + + /** + * @brief Get the header data + * @return Return a pointer to the header data + * + * This function is normally called as the last thing before closing a file + * in order to write the header to the beginning of the file. This of + * course requires that space have been reserved as described in the + * documentation for the headerSize function. + */ + virtual const char* header(void) { return m_header.data(); } + + protected: + + private: + static constexpr const size_t FRAME_SIZE = 20; + + Async::AudioEncoder* m_enc; + ogg_stream_state m_ogg_stream; + ogg_packet m_packet{0}; + //int m_ogg_serial = 1; + int m_pending_packets = 0; + AudioContainer* m_container = nullptr; + std::vector m_header; + std::vector m_block; + + AudioContainerOpus(const AudioContainerOpus&); + AudioContainerOpus& operator=(const AudioContainerOpus&); + void onWriteBlock(const char *buf, size_t len); + void onWriteEncodedSamples(const void *data, int len); + void oggpackWriteString(oggpack_buffer* oggbuf, + const char *str, int lenbits=32); + void oggpackWriteCommentList(oggpack_buffer* oggbuf, + const std::vector &comments); + bool writePage(const ogg_page& page, std::vector& buf); + bool writeOggOpusHeader(void); + +}; /* class AudioContainerOpus */ + + +} /* namespace */ + +#endif /* ASYNC_AUDIO_CONTAINER_OPUS_INCLUDED */ + +/* + * This file has not been truncated + */ diff --git a/src/async/audio/AsyncAudioContainerPcm.cpp b/src/async/audio/AsyncAudioContainerPcm.cpp new file mode 100644 index 0000000..ec371b3 --- /dev/null +++ b/src/async/audio/AsyncAudioContainerPcm.cpp @@ -0,0 +1,190 @@ +/** +@file AsyncAudioContainerPcm.cpp +@brief Handle PCM type audio container +@author Tobias Blomberg / SM0SVX +@date 2020-02-29 + +\verbatim +Async - A library for programming event driven applications +Copyright (C) 2003-2020 Tobias Blomberg / SM0SVX + +This program is free software; you can redistribute it and/or modify +it under the terms of the GNU General Public License as published by +the Free Software Foundation; either version 2 of the License, or +(at your option) any later version. + +This program is distributed in the hope that it will be useful, +but WITHOUT ANY WARRANTY; without even the implied warranty of +MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +GNU General Public License for more details. + +You should have received a copy of the GNU General Public License +along with this program; if not, write to the Free Software +Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA +\endverbatim +*/ + +/**************************************************************************** + * + * System Includes + * + ****************************************************************************/ + +#include + + +/**************************************************************************** + * + * Project Includes + * + ****************************************************************************/ + + + +/**************************************************************************** + * + * Local Includes + * + ****************************************************************************/ + +#include "AsyncAudioContainerPcm.h" + + +/**************************************************************************** + * + * Namespaces to use + * + ****************************************************************************/ + +using namespace Async; + + +/**************************************************************************** + * + * Defines & typedefs + * + ****************************************************************************/ + + + +/**************************************************************************** + * + * Local class definitions + * + ****************************************************************************/ + + + +/**************************************************************************** + * + * Prototypes + * + ****************************************************************************/ + + + +/**************************************************************************** + * + * Exported Global Variables + * + ****************************************************************************/ + + + +/**************************************************************************** + * + * Local Global Variables + * + ****************************************************************************/ + + + +/**************************************************************************** + * + * Public member functions + * + ****************************************************************************/ + +AudioContainerPcm::AudioContainerPcm(void) +{ + m_block.reserve(m_block_size); +} /* AudioContainerPcm::AudioContainerPcm */ + + +AudioContainerPcm::~AudioContainerPcm(void) +{ +} /* AudioContainerPcm::~AudioContainerPcm */ + + +int AudioContainerPcm::writeSamples(const float *samples, int count) +{ + //std::cout << "### AudioContainerPcm::writeSamples: count=" << count + // << std::endl; + if (count <= 0) + { + return -1; + } + + for (int i=0; i 1.0f) + { + sample = 32767; + } + else if (samples[i] < -1.0f) + { + sample = -32767; + } + else + { + sample = samples[i] * 32767.0f; + } + m_block.push_back(sample); + + if (m_block.size() >= m_block_size) + { + writeBlock(reinterpret_cast(m_block.data()), + sizeof(int16_t) * m_block.size()); + m_block.clear(); + } + } + return count; +} /* AudioContainerPcm::writeSamples */ + + +void AudioContainerPcm::flushSamples(void) +{ + //std::cout << "### AudioContainerPcm::flushSamples" << std::endl; + if (m_block.size() > 0) + { + writeBlock(reinterpret_cast(m_block.data()), + sizeof(int16_t) * m_block.size()); + m_block.clear(); + } +} /* AudioContainerPcm::flushSamples */ + + +//void AudioContainerOpus::endStream(void) +//{ +// flushSamples(); +//} /* AudioContainerOpus::endStream */ + + +/**************************************************************************** + * + * Protected member functions + * + ****************************************************************************/ + + + +/**************************************************************************** + * + * Private member functions + * + ****************************************************************************/ + +/* + * This file has not been truncated + */ diff --git a/src/async/audio/AsyncAudioContainerPcm.h b/src/async/audio/AsyncAudioContainerPcm.h new file mode 100644 index 0000000..4fe5668 --- /dev/null +++ b/src/async/audio/AsyncAudioContainerPcm.h @@ -0,0 +1,194 @@ +/** +@file AsyncAudioContainerPcm.h +@brief Handle PCM type audio container +@author Tobias Blomberg / SM0SVX +@date 2020-02-29 + +\verbatim +Async - A library for programming event driven applications +Copyright (C) 2003-2020 Tobias Blomberg / SM0SVX + +This program is free software; you can redistribute it and/or modify +it under the terms of the GNU General Public License as published by +the Free Software Foundation; either version 2 of the License, or +(at your option) any later version. + +This program is distributed in the hope that it will be useful, +but WITHOUT ANY WARRANTY; without even the implied warranty of +MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +GNU General Public License for more details. + +You should have received a copy of the GNU General Public License +along with this program; if not, write to the Free Software +Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA +\endverbatim +*/ + +/** @example AsyncAudioContainer_demo.cpp +An example of how to use the Async::AudioContainer class +*/ + +#ifndef ASYNC_AUDIO_CONTAINER_PCM_INCLUDED +#define ASYNC_AUDIO_CONTAINER_PCM_INCLUDED + + +/**************************************************************************** + * + * System Includes + * + ****************************************************************************/ + +#include +#include + + +/**************************************************************************** + * + * Project Includes + * + ****************************************************************************/ + +#include + + +/**************************************************************************** + * + * Local Includes + * + ****************************************************************************/ + + + +/**************************************************************************** + * + * Forward declarations + * + ****************************************************************************/ + + + +/**************************************************************************** + * + * Namespace + * + ****************************************************************************/ + +namespace Async +{ + + +/**************************************************************************** + * + * Forward declarations of classes inside of the declared namespace + * + ****************************************************************************/ + + + +/**************************************************************************** + * + * Defines & typedefs + * + ****************************************************************************/ + + + +/**************************************************************************** + * + * Exported Global Variables + * + ****************************************************************************/ + + + +/**************************************************************************** + * + * Class definitions + * + ****************************************************************************/ + +/** +@brief Handle a PCM type audio container +@author Tobias Blomberg / SM0SVX +@date 2020-02-29 + +This is an implementation of a PCM audio container. An audio container is +a format for storing audio to file or stream it over a network. Audio +containers are normally created using a factory, via the +Async::createAudioContainer function, but it is possible to create an audio +container directly as well. + +The PCM audio container really is not a container but a raw format where the +samples are just stored as is, without any header or other metadata. + +\example AsyncAudioContainer_demo.cpp +*/ +class AudioContainerPcm : public AudioContainer +{ + public: + /// The name of this class when used by the object factory + static constexpr const char *OBJNAME = "vnd.svxlink.pcm"; + + /** + * @brief Default constructor + */ + AudioContainerPcm(void); + + /** + * @brief Destructor + */ + virtual ~AudioContainerPcm(void); + + /** + * @brief Retrieve the media type for the audio container + * @return Returns a string representing the media type, e.g. audio/wav + */ + virtual const char* mediaType(void) const + { + return "audio/vnd.svxlink.pcm"; + } + + /** + * @brief Get the standard filename extension for the audio container + * @return Returns a string containing the filename extention, e.g. wav + */ + virtual const char* filenameExtension(void) const { return "raw"; } + + /** + * @brief Write samples into this audio sink + * @param samples The buffer containing the samples + * @param count The number of samples in the buffer + * @return Returns the number of samples that has been taken care of + * + * This function is used to write audio into this audio sink. If it + * returns 0, no more samples should be written until the resumeOutput + * function in the source have been called. + */ + virtual int writeSamples(const float *samples, int count); + + /** + * @brief Tell the sink to flush the previously written samples + * + * This function is used to tell the sink to flush previously written + * samples. When done flushing, the sink should call the + * sourceAllSamplesFlushed function. + */ + virtual void flushSamples(void); + + private: + size_t m_block_size = INTERNAL_SAMPLE_RATE / 10; + std::vector m_block; + + AudioContainerPcm(const AudioContainerPcm&); + AudioContainerPcm& operator=(const AudioContainerPcm&); + +}; /* class AudioContainerPcm */ + + +} /* namespace */ + +#endif /* ASYNC_AUDIO_CONTAINER_PCM_INCLUDED */ + +/* + * This file has not been truncated + */ diff --git a/src/async/audio/AsyncAudioContainerWav.cpp b/src/async/audio/AsyncAudioContainerWav.cpp new file mode 100644 index 0000000..fbd54d0 --- /dev/null +++ b/src/async/audio/AsyncAudioContainerWav.cpp @@ -0,0 +1,253 @@ +/** +@file AsyncAudioContainerWav.cpp +@brief Handle WAV type audio container +@author Tobias Blomberg / SM0SVX +@date 2020-02-29 + +\verbatim +Async - A library for programming event driven applications +Copyright (C) 2003-2020 Tobias Blomberg / SM0SVX + +This program is free software; you can redistribute it and/or modify +it under the terms of the GNU General Public License as published by +the Free Software Foundation; either version 2 of the License, or +(at your option) any later version. + +This program is distributed in the hope that it will be useful, +but WITHOUT ANY WARRANTY; without even the implied warranty of +MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +GNU General Public License for more details. + +You should have received a copy of the GNU General Public License +along with this program; if not, write to the Free Software +Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA +\endverbatim +*/ + +/**************************************************************************** + * + * System Includes + * + ****************************************************************************/ + +#include + + +/**************************************************************************** + * + * Project Includes + * + ****************************************************************************/ + +#include + + +/**************************************************************************** + * + * Local Includes + * + ****************************************************************************/ + +#include "AsyncAudioContainerWav.h" + + +/**************************************************************************** + * + * Namespaces to use + * + ****************************************************************************/ + +using namespace Async; + + +/**************************************************************************** + * + * Defines & typedefs + * + ****************************************************************************/ + + + +/**************************************************************************** + * + * Local class definitions + * + ****************************************************************************/ + + + +/**************************************************************************** + * + * Prototypes + * + ****************************************************************************/ + + + +/**************************************************************************** + * + * Exported Global Variables + * + ****************************************************************************/ + + + +/**************************************************************************** + * + * Local Global Variables + * + ****************************************************************************/ + + + +/**************************************************************************** + * + * Public member functions + * + ****************************************************************************/ + +AudioContainerWav::AudioContainerWav(void) +{ + m_block = new char[m_block_size]; + m_block_ptr = m_block; +} /* AudioContainerWav::AudioContainerWav */ + + +AudioContainerWav::~AudioContainerWav(void) +{ + delete [] m_block; + m_block = nullptr; + m_block_ptr = nullptr; +} /* AudioContainerWav::~AudioContainerWav */ + + +int AudioContainerWav::writeSamples(const float *samples, int count) +{ + //std::cout << "### AudioContainerWav::writeSamples: count=" << count + // << std::endl; + if (count <= 0) + { + return -1; + } + + for (int i=0; i 1.0f) + { + sample = 32767; + } + else if (samples[i] < -1.0f) + { + sample = -32767; + } + else + { + sample = samples[i] * 32767.0f; + } + m_block_ptr += store16bitValue(m_block_ptr, sample); + + if (m_block_ptr >= m_block+m_block_size) + { + writeBlock(reinterpret_cast(m_block), m_block_size); + m_samples_written += m_block_size; + m_block_ptr = m_block; + } + } + return count; +} /* AudioContainerWav::writeSamples */ + + +void AudioContainerWav::flushSamples(void) +{ + if (m_block_ptr > m_block) + { + writeBlock(reinterpret_cast(m_block), m_block_ptr - m_block); + m_samples_written += m_block_ptr - m_block; + m_block_ptr = m_block; + } +} /* AudioContainerWav::flushSamples */ + + +const char* AudioContainerWav::header(void) +{ + char *ptr = m_wave_header; + + // ChunkID + ptr += storeBuf(ptr, "RIFF", 4); + + // ChunkSize + if (m_realtime) + { + ptr += store32bitValue(ptr, 0xffffffff); + } + else + { + ptr += store32bitValue(ptr, + WAVE_HEADER_SIZE - 8 + m_samples_written * NUM_CHANNELS * sizeof(short)); + } + + // Format + ptr += storeBuf(ptr, "WAVE", 4); + + // Subchunk1ID + ptr += storeBuf(ptr, "fmt ", 4); + + // Subchunk1Size + ptr += store32bitValue(ptr, 16); + + // AudioFormat (PCM) + ptr += store16bitValue(ptr, 1); + + // NumChannels + ptr += store16bitValue(ptr, NUM_CHANNELS); + + // SampleRate + ptr += store32bitValue(ptr, m_sample_rate); + + // ByteRate (sample rate * num channels * bytes per sample) + ptr += store32bitValue(ptr, m_sample_rate * NUM_CHANNELS * sizeof(short)); + + // BlockAlign (num channels * bytes per sample) + ptr += store16bitValue(ptr, NUM_CHANNELS * sizeof(short)); + + // BitsPerSample + ptr += store16bitValue(ptr, 16); + + // Subchunk2ID + ptr += storeBuf(ptr, "data", 4); + + // Subchunk2Size (num samples * num channels * bytes per sample) + if (m_realtime) + { + ptr += store32bitValue(ptr, 0xffffffff - WAVE_HEADER_SIZE + 8); + } + else + { + ptr += store32bitValue(ptr, + m_samples_written * NUM_CHANNELS * sizeof(short)); + } + + assert(ptr - m_wave_header == WAVE_HEADER_SIZE); + + return m_wave_header; +} /* AudioContainerWav::header */ + + +/**************************************************************************** + * + * Protected member functions + * + ****************************************************************************/ + + + +/**************************************************************************** + * + * Private member functions + * + ****************************************************************************/ + +/* + * This file has not been truncated + */ diff --git a/src/async/audio/AsyncAudioContainerWav.h b/src/async/audio/AsyncAudioContainerWav.h new file mode 100644 index 0000000..0761136 --- /dev/null +++ b/src/async/audio/AsyncAudioContainerWav.h @@ -0,0 +1,258 @@ +/** +@file AsyncAudioContainerWav.h +@brief Handle WAV type audio container +@author Tobias Blomberg / SM0SVX +@date 2020-02-29 + +\verbatim +Async - A library for programming event driven applications +Copyright (C) 2003-2020 Tobias Blomberg / SM0SVX + +This program is free software; you can redistribute it and/or modify +it under the terms of the GNU General Public License as published by +the Free Software Foundation; either version 2 of the License, or +(at your option) any later version. + +This program is distributed in the hope that it will be useful, +but WITHOUT ANY WARRANTY; without even the implied warranty of +MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +GNU General Public License for more details. + +You should have received a copy of the GNU General Public License +along with this program; if not, write to the Free Software +Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA +\endverbatim +*/ + +/** @example AsyncAudioContainer_demo.cpp +An example of how to use the Async::AudioContainer class +*/ + +#ifndef ASYNC_AUDIO_CONTAINER_WAV_INCLUDED +#define ASYNC_AUDIO_CONTAINER_WAV_INCLUDED + + +/**************************************************************************** + * + * System Includes + * + ****************************************************************************/ + +#include +#include +#include + + +/**************************************************************************** + * + * Project Includes + * + ****************************************************************************/ + +#include + + +/**************************************************************************** + * + * Local Includes + * + ****************************************************************************/ + + + +/**************************************************************************** + * + * Forward declarations + * + ****************************************************************************/ + + + +/**************************************************************************** + * + * Namespace + * + ****************************************************************************/ + +namespace Async +{ + + +/**************************************************************************** + * + * Forward declarations of classes inside of the declared namespace + * + ****************************************************************************/ + + + +/**************************************************************************** + * + * Defines & typedefs + * + ****************************************************************************/ + + + +/**************************************************************************** + * + * Exported Global Variables + * + ****************************************************************************/ + + + +/**************************************************************************** + * + * Class definitions + * + ****************************************************************************/ + +/** +@brief Handle a WAV type audio container +@author Tobias Blomberg / SM0SVX +@date 2020-02-29 + +This is an implementation of a WAV audio container. An audio container is +a format for storing audio to file or stream it over a network. Audio +containers are normally created using a factory, via the +Async::createAudioContainer function, but it is possible to create an audio +container directly as well. + +\example AsyncAudioContainer_demo.cpp +*/ +class AudioContainerWav : public AudioContainer +{ + public: + /// The name of this class when used by the object factory + static constexpr const char *OBJNAME = "wav"; + + //struct Factory : public AudioContainerSpecificFactory + //{ + // Factory(void) : AudioContainerSpecificFactory("wav") {} + //}; + + /** + * @brief Default constructor + */ + AudioContainerWav(void); + + /** + * @brief Destructor + */ + virtual ~AudioContainerWav(void); + + /** + * @brief Retrieve the media type for the audio container + * @return Returns a string representing the media type, e.g. audio/wav + */ + virtual const char* mediaType(void) const { return "audio/wav"; } + + /** + * @brief Get the standard filename extension for the audio container + * @return Returns a string containing the filename extention, e.g. wav + */ + virtual const char* filenameExtension(void) const { return "wav"; } + + /** + * @brief Write samples into this audio sink + * @param samples The buffer containing the samples + * @param count The number of samples in the buffer + * @return Returns the number of samples that has been taken care of + * + * This function is used to write audio into this audio sink. If it + * returns 0, no more samples should be written until the resumeOutput + * function in the source have been called. + */ + virtual int writeSamples(const float *samples, int count); + + /** + * @brief Tell the sink to flush the previously written samples + * + * This function is used to tell the sink to flush previously written + * samples. When done flushing, the sink should call the + * sourceAllSamplesFlushed function. + */ + virtual void flushSamples(void); + + /** + * @brief Indicate to the container that realtime operation is desired + * + * This function can be called to inidicate to the container that we are + * writing the audio to a realtime stream rather than to a file. The + * container can then choose to indicate this in the audio stream if it + * have that capability. + */ + virtual void setRealtime(void) { m_realtime = true; } + + /** + * @brief Get the size of the header for this container + * @return Returns the size of the header + * + * This function is normally called directly after opening a file to + * reserve space for writing the header later, usually as the last thing + * before closing the file. + */ + virtual size_t headerSize(void) const { return WAVE_HEADER_SIZE; } + + /** + * @brief Get the header data + * @return Return a pointer to the header data + * + * This function is normally called as the last thing before closing a file + * in order to write the header to the beginning of the file. This of + * course requires that space have been reserved as described in the + * documentation for the headerSize function. + */ + virtual const char* header(void); + + private: + static const size_t WAVE_HEADER_SIZE = 44; + static const size_t NUM_CHANNELS = 1; + + char m_wave_header[WAVE_HEADER_SIZE]; + int m_sample_rate = INTERNAL_SAMPLE_RATE; + size_t m_samples_written = 0; + size_t m_block_size = sizeof(int16_t)*INTERNAL_SAMPLE_RATE / 10; + char* m_block = nullptr; + char* m_block_ptr = nullptr; + bool m_realtime = false; + + AudioContainerWav(const AudioContainerWav&); + AudioContainerWav& operator=(const AudioContainerWav&); + + size_t storeBuf(char *ptr, const char* buf, size_t len) + { + std::memcpy(ptr, buf, len); + return len; + } + + size_t store32bitValue(char *ptr, uint32_t val) + { + *ptr++ = val & 0xff; + val >>= 8; + *ptr++ = val & 0xff; + val >>= 8; + *ptr++ = val & 0xff; + val >>= 8; + *ptr++ = val & 0xff; + return 4; + } + + size_t store16bitValue(char *ptr, uint16_t val) + { + *ptr++ = val & 0xff; + val >>= 8; + *ptr++ = val & 0xff; + return 2; + } +}; /* class AudioContainerWav */ + + +} /* namespace */ + +#endif /* ASYNC_AUDIO_CONTAINER_WAV_INCLUDED */ + +/* + * This file has not been truncated + */ diff --git a/src/async/audio/AsyncAudioDebugger.h b/src/async/audio/AsyncAudioDebugger.h new file mode 100644 index 0000000..00f05cc --- /dev/null +++ b/src/async/audio/AsyncAudioDebugger.h @@ -0,0 +1,265 @@ +/** +@file AsyncAudioDebugger.h +@brief This file contains a class that can be used for debugging +@author Tobias Blomberg / SM0SVX +@date 2007-10-14 + +\verbatim +Async - A library for programming event driven applications +Copyright (C) 2004-2005 Tobias Blomberg / SM0SVX + +This program is free software; you can redistribute it and/or modify +it under the terms of the GNU General Public License as published by +the Free Software Foundation; either version 2 of the License, or +(at your option) any later version. + +This program is distributed in the hope that it will be useful, +but WITHOUT ANY WARRANTY; without even the implied warranty of +MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +GNU General Public License for more details. + +You should have received a copy of the GNU General Public License +along with this program; if not, write to the Free Software +Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA +\endverbatim +*/ + + +#ifndef AUDIO_DEBUGGER_INCLUDED +#define AUDIO_DEBUGGER_INCLUDED + + +/**************************************************************************** + * + * System Includes + * + ****************************************************************************/ + +#include +#include +#include +#include + + +/**************************************************************************** + * + * Project Includes + * + ****************************************************************************/ + +#include +#include + + +/**************************************************************************** + * + * Local Includes + * + ****************************************************************************/ + + + +/**************************************************************************** + * + * Forward declarations + * + ****************************************************************************/ + + + +/**************************************************************************** + * + * Namespace + * + ****************************************************************************/ + +namespace Async +{ + + +/**************************************************************************** + * + * Forward declarations of classes inside of the declared namespace + * + ****************************************************************************/ + + + +/**************************************************************************** + * + * Defines & typedefs + * + ****************************************************************************/ + + + +/**************************************************************************** + * + * Exported Global Variables + * + ****************************************************************************/ + + + +/**************************************************************************** + * + * Class definitions + * + ****************************************************************************/ + +/** +@brief This class is used to debug an audio stream +@author Tobias Blomberg / SM0SVX +@date 2007-10-14 + +This class is used to debug an audio stream. It can be inserted in the flow +and it will print out a debug message for each function call between the sink +and the source. +*/ +class AudioDebugger : public AudioSink, public AudioSource +{ + public: + /** + * @brief Default constuctor + */ + AudioDebugger(Async::AudioSource *src=0, + const std::string& name="AudioDebugger") + : name(name), sample_count(0) + { + gettimeofday(&start_time, 0); + if (src != 0) + { + Async::AudioSink *sink = src->sink(); + if (sink != 0) + { + src->unregisterSink(); + registerSink(sink); + } + registerSource(src); + } + } + + /** + * @brief Destructor + */ + virtual ~AudioDebugger(void) {} + + /** + * @brief Set the name that is displayed before debug messages + * @param debug_name The name to set + */ + void setName(std::string debug_name) { name = debug_name; } + + /** + * @brief Write samples into this audio sink + * @param samples The buffer containing the samples + * @param count The number of samples in the buffer + * @return Returns the number of samples that has been taken care of + * + * This function is used to write audio into this audio sink. If it + * returns 0, no more samples should be written until the resumeOutput + * function in the source have been called. + * This function is normally only called from a connected source object. + */ + virtual int writeSamples(const float *samples, int count) + { + int ret = sinkWriteSamples(samples, count); + sample_count += ret; + + float max_samp = 0.0f; + for (int i=0; i max_samp) + { + max_samp = samples[i]; + } + if (-samples[i] > max_samp) + { + max_samp = -samples[i]; + } + } + + struct timeval time, diff; + gettimeofday(&time, 0); + + timersub(&time, &start_time, &diff); + uint64_t diff_ms = diff.tv_sec * 1000 + diff.tv_usec / 1000; + + std::cout << name << "::writeSamples: count=" << count + << " ret=" << ret << " sample_rate="; + if (diff_ms > 0) + { + std::cout << sample_count * 1000 / diff_ms; + } + else + { + std::cout << "inf"; + } + std::cout << " max=" << max_samp; + std::cout << std::endl; + return ret; + } + + /** + * @brief Tell the sink to flush the previously written samples + * + * This function is used to tell the sink to flush previously written + * samples. When done flushing, the sink should call the + * sourceAllSamplesFlushed function. + * This function is normally only called from a connected source object. + */ + virtual void flushSamples(void) + { + std::cout << name << "::flushSamples\n"; + sinkFlushSamples(); + } + + /** + * @brief Resume audio output to the sink + * + * This function will be called when the registered audio sink is ready + * to accept more samples. + * This function is normally only called from a connected sink object. + */ + virtual void resumeOutput(void) + { + std::cout << name << "::resumeOutput\n"; + sourceResumeOutput(); + } + + /** + * @brief The registered sink has flushed all samples + * + * This function will be called when all samples have been flushed in the + * registered sink. + * This function is normally only called from a connected sink object. + */ + virtual void allSamplesFlushed(void) + { + std::cout << name << "::allSamplesFlushed\n"; + sourceAllSamplesFlushed(); + } + + protected: + + private: + std::string name; + struct timeval start_time; + uint64_t sample_count; + + AudioDebugger(const AudioDebugger&); + AudioDebugger& operator=(const AudioDebugger&); + +}; /* AudioDebugger */ + + +} /* namespace */ + +#endif /* AUDIO_DEBUGGER_INCLUDED */ + + + +/* + * This file has not been truncated + */ + diff --git a/src/async/audio/AsyncAudioDecimator.cpp b/src/async/audio/AsyncAudioDecimator.cpp new file mode 100644 index 0000000..8b4e8f7 --- /dev/null +++ b/src/async/audio/AsyncAudioDecimator.cpp @@ -0,0 +1,183 @@ +/** +@file AsyncAudioDecimator.cpp +@brief Decimates a higher sample rate to a lower one +@author Tobias Blomberg / SM0SVX +@date 2008-04-06 + +\verbatim +Original code by by Grant R. Griffin modified by Tobias Blomberg / SM0SVX. +Provided by Iowegian's "dspGuru" service (http://www.dspguru.com). +Copyright 2001, Iowegian International Corporation (http://www.iowegian.com) + + The Wide Open License (WOL) + +Permission to use, copy, modify, distribute and sell this software and its +documentation for any purpose is hereby granted without fee, provided that +the above copyright notice and this license appear in all source copies. +THIS SOFTWARE IS PROVIDED "AS IS" WITHOUT EXPRESS OR IMPLIED WARRANTY OF +ANY KIND. See http://www.dspguru.com/wol.htm for more information. +\endverbatim +*/ + + + +/**************************************************************************** + * + * System Includes + * + ****************************************************************************/ + +#include + + +/**************************************************************************** + * + * Project Includes + * + ****************************************************************************/ + + + +/**************************************************************************** + * + * Local Includes + * + ****************************************************************************/ + +#include "AsyncAudioDecimator.h" + + + +/**************************************************************************** + * + * Namespaces to use + * + ****************************************************************************/ + +using namespace std; +using namespace Async; + + + +/**************************************************************************** + * + * Defines & typedefs + * + ****************************************************************************/ + + + +/**************************************************************************** + * + * Local class definitions + * + ****************************************************************************/ + + + +/**************************************************************************** + * + * Prototypes + * + ****************************************************************************/ + + + +/**************************************************************************** + * + * Exported Global Variables + * + ****************************************************************************/ + + + + +/**************************************************************************** + * + * Local Global Variables + * + ****************************************************************************/ + + + +/**************************************************************************** + * + * Public member functions + * + ****************************************************************************/ + +AudioDecimator::AudioDecimator(int decimation_factor, + const float *filter_coeff, int taps) + : factor_M(decimation_factor), H_size(taps), p_H(filter_coeff) +{ + setInputOutputSampleRate(factor_M, 1); + p_Z = new float[H_size]; + memset(p_Z, 0, H_size * sizeof(*p_Z)); +} /* AudioDecimator::AudioDecimator */ + + +AudioDecimator::~AudioDecimator(void) +{ + delete [] p_Z; +} /* AudioDecimator::~AudioDecimator */ + + + + +/**************************************************************************** + * + * Protected member functions + * + ****************************************************************************/ + +void AudioDecimator::processSamples(float *dest, const float *src, int count) +{ + int orig_count = count; + + //printf("count=%d\n", count); + + // this implementation assumes num_inp is a multiple of factor_M + assert(count % factor_M == 0); + + int num_out = 0; + while (count >= factor_M) + { + // shift Z delay line up to make room for next samples + memmove(p_Z + factor_M, p_Z, (H_size - factor_M) * sizeof(float)); + + // copy next samples from input buffer to bottom of Z delay line + for (int tap = factor_M - 1; tap >= 0; tap--) + { + p_Z[tap] = *src++; + } + count -= factor_M; + + // calculate FIR sum + float sum = 0.0; + for (int tap = 0; tap < H_size; tap++) + { + sum += p_H[tap] * p_Z[tap]; + } + *dest++ = sum; /* store sum and point to next output */ + num_out++; + } + + //printf("num_out=%d count=%d factor_M=%d\n", num_out, count, factor_M); + assert(num_out == orig_count / factor_M); + +} /* AudioDecimator::processSamples */ + + + +/**************************************************************************** + * + * Private member functions + * + ****************************************************************************/ + + + +/* + * This file has not been truncated + */ + diff --git a/src/async/audio/AsyncAudioDecimator.h b/src/async/audio/AsyncAudioDecimator.h new file mode 100644 index 0000000..74fc739 --- /dev/null +++ b/src/async/audio/AsyncAudioDecimator.h @@ -0,0 +1,170 @@ +/** +@file AsyncAudioDecimator.h +@brief Decimates a higher sample rate to a lower one +@author Tobias Blomberg / SM0SVX +@date 2008-04-06 + +\verbatim +Original code by by Grant R. Griffin modified by Tobias Blomberg / SM0SVX. +Provided by Iowegian's "dspGuru" service (http://www.dspguru.com). +Copyright 2001, Iowegian International Corporation (http://www.iowegian.com) + + The Wide Open License (WOL) + +Permission to use, copy, modify, distribute and sell this software and its +documentation for any purpose is hereby granted without fee, provided that +the above copyright notice and this license appear in all source copies. +THIS SOFTWARE IS PROVIDED "AS IS" WITHOUT EXPRESS OR IMPLIED WARRANTY OF +ANY KIND. See http://www.dspguru.com/wol.htm for more information. +\endverbatim +*/ + +#ifndef ASYNC_AUDIO_DECIMATOR_INCLUDED +#define ASYNC_AUDIO_DECIMATOR_INCLUDED + + +/**************************************************************************** + * + * System Includes + * + ****************************************************************************/ + + + +/**************************************************************************** + * + * Project Includes + * + ****************************************************************************/ + +#include + + +/**************************************************************************** + * + * Local Includes + * + ****************************************************************************/ + + + +/**************************************************************************** + * + * Forward declarations + * + ****************************************************************************/ + + + +/**************************************************************************** + * + * Namespace + * + ****************************************************************************/ + +namespace Async +{ + + +/**************************************************************************** + * + * Forward declarations of classes inside of the declared namespace + * + ****************************************************************************/ + + + +/**************************************************************************** + * + * Defines & typedefs + * + ****************************************************************************/ + + + +/**************************************************************************** + * + * Exported Global Variables + * + ****************************************************************************/ + + + +/**************************************************************************** + * + * Class definitions + * + ****************************************************************************/ + +/** +@brief Decimates a higher sample rate into a lower one +@author Tobias Blomberg / SM0SVX +@date 2008-04-06 + +This audio pipe class will decimate an audio stream down to a lower sampling +rate. Decimation is a process where the sampling rate is reduced by an +integer factor. To reduce the rate a lowpass filter must first be applied. +This filter is built into this component. However, the filter coefficients +(FIR) must be calculated manually. + +Use this web page to calculate the coefficients: +http://www.dsptutor.freeuk.com/remez/RemezFIRFilterDesign.html + +This implementation is based on the multirate FAQ at dspguru.com: +http://dspguru.com/info/faqs/mrfaq.htm +*/ +class AudioDecimator : public AudioProcessor +{ + public: + /** + * @brief Constructor + * @param decimation_factor The decimation factor + * @param filter_coeff An array holding the filter coefficients + * @param taps The numer of taps in the filter + */ + AudioDecimator(int decimation_factor, const float *filter_coeff, + int taps); + + /** + * @brief Destructor + */ + ~AudioDecimator(void); + + + protected: + /** + * @brief Process incoming samples and put them into the output buffer + * @param dest Destination buffer + * @param src Source buffer + * @param count Number of samples in the source buffer + * + * This function should be reimplemented by the inheriting class to + * do the actual processing of the incoming samples. All samples must + * be processed, otherwise they are lost and the output buffer will + * contain garbage. + */ + virtual void processSamples(float *dest, const float *src, int count); + + + private: + const int factor_M; + float *p_Z; + int H_size; + const float *p_H; + + AudioDecimator(const AudioDecimator&); + AudioDecimator& operator=(const AudioDecimator&); + +}; /* class AudioDecimator */ + + +} /* namespace */ + +#endif /* ASYNC_AUDIO_DECIMATOR_INCLUDED */ + + + +/* + * This file has not been truncated + */ + diff --git a/src/async/audio/AsyncAudioDecoder.cpp b/src/async/audio/AsyncAudioDecoder.cpp new file mode 100644 index 0000000..8f90523 --- /dev/null +++ b/src/async/audio/AsyncAudioDecoder.cpp @@ -0,0 +1,197 @@ +/** +@file AsyncAudioDecoder.cpp +@brief Base class of an audio decoder +@author Tobias Blomberg / SM0SVX +@date 2008-10-06 + +\verbatim +Async - A library for programming event driven applications +Copyright (C) 2003-2008 Tobias Blomberg / SM0SVX + +This program is free software; you can redistribute it and/or modify +it under the terms of the GNU General Public License as published by +the Free Software Foundation; either version 2 of the License, or +(at your option) any later version. + +This program is distributed in the hope that it will be useful, +but WITHOUT ANY WARRANTY; without even the implied warranty of +MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +GNU General Public License for more details. + +You should have received a copy of the GNU General Public License +along with this program; if not, write to the Free Software +Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA +\endverbatim +*/ + + + +/**************************************************************************** + * + * System Includes + * + ****************************************************************************/ + + + +/**************************************************************************** + * + * Project Includes + * + ****************************************************************************/ + + + +/**************************************************************************** + * + * Local Includes + * + ****************************************************************************/ + +#include "AsyncAudioDecoder.h" +#include "AsyncAudioDecoderDummy.h" +#include "AsyncAudioDecoderNull.h" +#include "AsyncAudioDecoderRaw.h" +#include "AsyncAudioDecoderS16.h" +#include "AsyncAudioDecoderGsm.h" +#ifdef SPEEX_MAJOR +#include "AsyncAudioDecoderSpeex.h" +#endif +#ifdef OPUS_MAJOR +#include "AsyncAudioDecoderOpus.h" +#endif + + +/**************************************************************************** + * + * Namespaces to use + * + ****************************************************************************/ + +using namespace std; +using namespace Async; + + + +/**************************************************************************** + * + * Defines & typedefs + * + ****************************************************************************/ + + + +/**************************************************************************** + * + * Local class definitions + * + ****************************************************************************/ + + + +/**************************************************************************** + * + * Prototypes + * + ****************************************************************************/ + + + +/**************************************************************************** + * + * Exported Global Variables + * + ****************************************************************************/ + + + + +/**************************************************************************** + * + * Local Global Variables + * + ****************************************************************************/ + + + +/**************************************************************************** + * + * Public member functions + * + ****************************************************************************/ + +bool AudioDecoder::isAvailable(const std::string &name) +{ + return (name == "NULL") || (name == "RAW") || (name == "S16") || + (name == "GSM") || +#ifdef SPEEX_MAJOR + (name == "SPEEX") || +#endif +#ifdef OPUS_MAJOR + (name == "OPUS") || +#endif + (name == "DUMMY"); +} /* AudioDecoder::isAvailable */ + + +AudioDecoder *AudioDecoder::create(const std::string &name) +{ + if (name == "NULL") + { + return new AudioDecoderNull; + } + else if (name == "DUMMY") + { + return new AudioDecoderDummy; + } + else if (name == "RAW") + { + return new AudioDecoderRaw; + } + else if (name == "S16") + { + return new AudioDecoderS16; + } + else if (name == "GSM") + { + return new AudioDecoderGsm; + } +#ifdef SPEEX_MAJOR + else if (name == "SPEEX") + { + return new AudioDecoderSpeex; + } +#endif +#ifdef OPUS_MAJOR + else if (name == "OPUS") + { + return new AudioDecoderOpus; + } +#endif + else + { + return 0; + } +} + + +/**************************************************************************** + * + * Protected member functions + * + ****************************************************************************/ + + + +/**************************************************************************** + * + * Private member functions + * + ****************************************************************************/ + + + +/* + * This file has not been truncated + */ + diff --git a/src/async/audio/AsyncAudioDecoder.h b/src/async/audio/AsyncAudioDecoder.h new file mode 100644 index 0000000..c929380 --- /dev/null +++ b/src/async/audio/AsyncAudioDecoder.h @@ -0,0 +1,210 @@ +/** +@file AsyncAudioDecoder.h +@brief Base class of an audio decoder +@author Tobias Blomberg / SM0SVX +@date 2008-10-06 + +\verbatim +Async - A library for programming event driven applications +Copyright (C) 2003-2008 Tobias Blomberg / SM0SVX + +This program is free software; you can redistribute it and/or modify +it under the terms of the GNU General Public License as published by +the Free Software Foundation; either version 2 of the License, or +(at your option) any later version. + +This program is distributed in the hope that it will be useful, +but WITHOUT ANY WARRANTY; without even the implied warranty of +MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +GNU General Public License for more details. + +You should have received a copy of the GNU General Public License +along with this program; if not, write to the Free Software +Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA +\endverbatim +*/ + +#ifndef ASYNC_AUDIO_DECODER_INCLUDED +#define ASYNC_AUDIO_DECODER_INCLUDED + + +/**************************************************************************** + * + * System Includes + * + ****************************************************************************/ + +#include +#include + + +/**************************************************************************** + * + * Project Includes + * + ****************************************************************************/ + +#include + + +/**************************************************************************** + * + * Local Includes + * + ****************************************************************************/ + + + +/**************************************************************************** + * + * Forward declarations + * + ****************************************************************************/ + + + +/**************************************************************************** + * + * Namespace + * + ****************************************************************************/ + +namespace Async +{ + + +/**************************************************************************** + * + * Forward declarations of classes inside of the declared namespace + * + ****************************************************************************/ + + + +/**************************************************************************** + * + * Defines & typedefs + * + ****************************************************************************/ + + + +/**************************************************************************** + * + * Exported Global Variables + * + ****************************************************************************/ + + + +/**************************************************************************** + * + * Class definitions + * + ****************************************************************************/ + +/** +@brief Base class for an audio decoder +@author Tobias Blomberg / SM0SVX +@date 2008-10-06 + +This is the base class for an audio decoder. +*/ +class AudioDecoder : public AudioSource, public sigc::trackable +{ + public: + /** + * @brief Check if a specific decoder is available + * @param name The name of the decoder to look for + */ + static bool isAvailable(const std::string &name); + + /** + * @brief Create a new decoder of the specified type + * @param name The name of the decoder to create + */ + static AudioDecoder *create(const std::string &name); + + /** + * @brief Default constuctor + */ + AudioDecoder(void) {} + + /** + * @brief Destructor + */ + virtual ~AudioDecoder(void) {} + + /** + * @brief Get the name of the codec + * @returns Return the name of the codec + */ + virtual const char *name(void) const = 0; + + /** + * @brief Set an option for the decoder + * @param name The name of the option + * @param value The value of the option + */ + virtual void setOption(const std::string &name, const std::string &value) {} + + /** + * @brief Print codec parameter settings + */ + virtual void printCodecParams(void) const {} + + /** + * @brief Write encoded samples into the decoder + * @param buf Buffer containing encoded samples + * @param size The size of the buffer + */ + virtual void writeEncodedSamples(void *buf, int size) = 0; + + /** + * @brief Call this function when all encoded samples have been received + */ + virtual void flushEncodedSamples(void) { sinkFlushSamples(); } + + /** + * @brief Resume audio output to the sink + * + * This function will be called when the registered audio sink is ready to + * accept more samples. + * This function is normally only called from a connected sink object. + */ + virtual void resumeOutput(void) {} + + /** + * @brief This signal is emitted when all encoded samples have been flushed + */ + sigc::signal allEncodedSamplesFlushed; + + + protected: + /** + * @brief The registered sink has flushed all samples + * + * This function will be called when all samples have been flushed in the + * registered sink. + * This function is normally only called from a connected sink object. + */ + virtual void allSamplesFlushed(void) { allEncodedSamplesFlushed(); } + + + private: + AudioDecoder(const AudioDecoder&); + AudioDecoder& operator=(const AudioDecoder&); + +}; /* class AudioDecoder */ + + +} /* namespace */ + +#endif /* ASYNC_AUDIO_DECODER_INCLUDED */ + + + +/* + * This file has not been truncated + */ + diff --git a/src/async/audio/AsyncAudioDecoderDummy.h b/src/async/audio/AsyncAudioDecoderDummy.h new file mode 100644 index 0000000..e4562f7 --- /dev/null +++ b/src/async/audio/AsyncAudioDecoderDummy.h @@ -0,0 +1,162 @@ +/** +@file AsyncAudioDecoderDummy.h +@brief An audio "decoder" used when audio should just be thrown away +@author Tobias Blomberg / SM0SVX +@date 2017-10-28 + +\verbatim +Async - A library for programming event driven applications +Copyright (C) 2003-2017 Tobias Blomberg / SM0SVX + +This program is free software; you can redistribute it and/or modify +it under the terms of the GNU General Public License as published by +the Free Software Foundation; either version 2 of the License, or +(at your option) any later version. + +This program is distributed in the hope that it will be useful, +but WITHOUT ANY WARRANTY; without even the implied warranty of +MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +GNU General Public License for more details. + +You should have received a copy of the GNU General Public License +along with this program; if not, write to the Free Software +Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA +\endverbatim +*/ + +#ifndef ASYNC_AUDIO_DECODER_DUMMY_INCLUDED +#define ASYNC_AUDIO_DECODER_DUMMY_INCLUDED + + +/**************************************************************************** + * + * System Includes + * + ****************************************************************************/ + + + +/**************************************************************************** + * + * Project Includes + * + ****************************************************************************/ + +#include + + +/**************************************************************************** + * + * Local Includes + * + ****************************************************************************/ + + + +/**************************************************************************** + * + * Forward declarations + * + ****************************************************************************/ + + + +/**************************************************************************** + * + * Namespace + * + ****************************************************************************/ + +namespace Async +{ + + +/**************************************************************************** + * + * Forward declarations of classes inside of the declared namespace + * + ****************************************************************************/ + + + +/**************************************************************************** + * + * Defines & typedefs + * + ****************************************************************************/ + + + +/**************************************************************************** + * + * Exported Global Variables + * + ****************************************************************************/ + + + +/**************************************************************************** + * + * Class definitions + * + ****************************************************************************/ + +/** +@brief An audio "decoder" used when audio should just be thrown away +@author Tobias Blomberg / SM0SVX +@date 2017-10-28 + +This class implements an audio "decoder" that will just throw away incoming +encoded audio. It may be good to use as a placeholder before a real audio +codec has been chosen. +*/ +class AudioDecoderDummy : public AudioDecoder +{ + public: + /** + * @brief Default constuctor + */ + AudioDecoderDummy(void) {} + + /** + * @brief Destructor + */ + virtual ~AudioDecoderDummy(void) {} + + /** + * @brief Get the name of the codec + * @returns Return the name of the codec + */ + virtual const char *name(void) const { return "DUMMY"; } + + /** + * @brief Write encoded samples into the decoder + * @param buf Buffer containing encoded samples + * @param size The size of the buffer + * + * This DUMMY decoder will just throw away incoming encoded samples. + */ + virtual void writeEncodedSamples(void*, int) {} + + /** + * @brief Call this function when all encoded samples have been received + * + * This DUMMY decoder will just ignore the flush request. + */ + virtual void flushEncodedSamples(void) {} + + private: + AudioDecoderDummy(const AudioDecoderDummy&); + AudioDecoderDummy& operator=(const AudioDecoderDummy&); + +}; /* class AudioDecoderDummy */ + + +} /* namespace */ + +#endif /* ASYNC_AUDIO_DECODER_DUMMY_INCLUDED */ + + +/* + * This file has not been truncated + */ diff --git a/src/async/audio/AsyncAudioDecoderGsm.cpp b/src/async/audio/AsyncAudioDecoderGsm.cpp new file mode 100644 index 0000000..3ba46d6 --- /dev/null +++ b/src/async/audio/AsyncAudioDecoderGsm.cpp @@ -0,0 +1,171 @@ +/** +@file AudioDecoderGsm.cpp +@brief An audio decoder for GSM +@author Tobias Blomberg / SM0SVX +@date 2008-10-15 + +\verbatim +Async - A library for programming event driven applications +Copyright (C) 2003-2008 Tobias Blomberg / SM0SVX + +This program is free software; you can redistribute it and/or modify +it under the terms of the GNU General Public License as published by +the Free Software Foundation; either version 2 of the License, or +(at your option) any later version. + +This program is distributed in the hope that it will be useful, +but WITHOUT ANY WARRANTY; without even the implied warranty of +MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +GNU General Public License for more details. + +You should have received a copy of the GNU General Public License +along with this program; if not, write to the Free Software +Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA +\endverbatim +*/ + + + +/**************************************************************************** + * + * System Includes + * + ****************************************************************************/ + + + +/**************************************************************************** + * + * Project Includes + * + ****************************************************************************/ + + + +/**************************************************************************** + * + * Local Includes + * + ****************************************************************************/ + +#include "AsyncAudioDecoderGsm.h" + + + +/**************************************************************************** + * + * Namespaces to use + * + ****************************************************************************/ + +using namespace std; +using namespace Async; + + + +/**************************************************************************** + * + * Defines & typedefs + * + ****************************************************************************/ + + + +/**************************************************************************** + * + * Local class definitions + * + ****************************************************************************/ + + + +/**************************************************************************** + * + * Prototypes + * + ****************************************************************************/ + + + +/**************************************************************************** + * + * Exported Global Variables + * + ****************************************************************************/ + + + + +/**************************************************************************** + * + * Local Global Variables + * + ****************************************************************************/ + + + +/**************************************************************************** + * + * Public member functions + * + ****************************************************************************/ + +AudioDecoderGsm::AudioDecoderGsm(void) + : gsmh(0), frame_len(0) +{ + gsmh = gsm_create(); +} /* AudioDecoderGsm::AudioDecoderGsm */ + + +AudioDecoderGsm::~AudioDecoderGsm(void) +{ + gsm_destroy(gsmh); + gsmh = 0; +} /* AudioDecoderGsm::~AudioDecoderGsm */ + + +void AudioDecoderGsm::writeEncodedSamples(void *buf, int size) +{ + unsigned char *ptr = (unsigned char *)buf; + + for (int i=0; i(s16_samples[j]) / 32768.0; + } + sinkWriteSamples(samples, FRAME_SAMPLE_CNT); + frame_len = 0; + } + } +} /* AudioDecoderGsm::writeEncodedSamples */ + + + +/**************************************************************************** + * + * Protected member functions + * + ****************************************************************************/ + + + +/**************************************************************************** + * + * Private member functions + * + ****************************************************************************/ + + + +/* + * This file has not been truncated + */ + diff --git a/src/async/audio/AsyncAudioDecoderGsm.h b/src/async/audio/AsyncAudioDecoderGsm.h new file mode 100644 index 0000000..e11b520 --- /dev/null +++ b/src/async/audio/AsyncAudioDecoderGsm.h @@ -0,0 +1,173 @@ +/** +@file AsyncAudioDecoderGsm.h +@brief An audio decoder for GSM +@author Tobias Blomberg / SM0SVX +@date 2008-10-15 + +\verbatim +Async - A library for programming event driven applications +Copyright (C) 2003-2008 Tobias Blomberg / SM0SVX + +This program is free software; you can redistribute it and/or modify +it under the terms of the GNU General Public License as published by +the Free Software Foundation; either version 2 of the License, or +(at your option) any later version. + +This program is distributed in the hope that it will be useful, +but WITHOUT ANY WARRANTY; without even the implied warranty of +MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +GNU General Public License for more details. + +You should have received a copy of the GNU General Public License +along with this program; if not, write to the Free Software +Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA +\endverbatim +*/ + +#ifndef ASYNC_AUDIO_DECODER_GSM_INCLUDED +#define ASYNC_AUDIO_DECODER_GSM_INCLUDED + + +/**************************************************************************** + * + * System Includes + * + ****************************************************************************/ + +extern "C" { +#include +} + + + +/**************************************************************************** + * + * Project Includes + * + ****************************************************************************/ + +#include + + +/**************************************************************************** + * + * Local Includes + * + ****************************************************************************/ + + + +/**************************************************************************** + * + * Forward declarations + * + ****************************************************************************/ + + + +/**************************************************************************** + * + * Namespace + * + ****************************************************************************/ + +namespace Async +{ + + +/**************************************************************************** + * + * Forward declarations of classes inside of the declared namespace + * + ****************************************************************************/ + + + +/**************************************************************************** + * + * Defines & typedefs + * + ****************************************************************************/ + + + +/**************************************************************************** + * + * Exported Global Variables + * + ****************************************************************************/ + + + +/**************************************************************************** + * + * Class definitions + * + ****************************************************************************/ + +/** +@brief An audio decoder GSM +@author Tobias Blomberg / SM0SVX +@date 2008-10-15 + +This class implements an audio decoder that converts GSM frames into +the native sample format. +*/ +class AudioDecoderGsm : public AudioDecoder +{ + public: + /** + * @brief Default constuctor + */ + AudioDecoderGsm(void); + + /** + * @brief Destructor + */ + virtual ~AudioDecoderGsm(void); + + /** + * @brief Get the name of the codec + * @returns Return the name of the codec + */ + virtual const char *name(void) const { return "GSM"; } + + /** + * @brief A_brief_member_function_description + * @param param1 Description_of_param1 + * @return Return_value_of_this_member_function + */ + + /** + * @brief Write encoded samples into the decoder + * @param buf Buffer containing encoded samples + * @param size The size of the buffer + */ + virtual void writeEncodedSamples(void *buf, int size); + + + protected: + + private: + static const int FRAME_SAMPLE_CNT = 160; + + gsm gsmh; + gsm_frame frame; + int frame_len; + + AudioDecoderGsm(const AudioDecoderGsm&); + AudioDecoderGsm& operator=(const AudioDecoderGsm&); + +}; /* class AudioDecoderGsm */ + + +} /* namespace */ + +#endif /* ASYNC_AUDIO_DECODER_GSM_INCLUDED */ + + + +/* + * This file has not been truncated + */ + diff --git a/src/async/audio/AsyncAudioDecoderNull.h b/src/async/audio/AsyncAudioDecoderNull.h new file mode 100644 index 0000000..84abb41 --- /dev/null +++ b/src/async/audio/AsyncAudioDecoderNull.h @@ -0,0 +1,180 @@ +/** +@file AsyncAudioDecoderNull.h +@brief An audio "decoder" used when no real audio need to be communicated +@author Tobias Blomberg / SM0SVX +@date 2014-05-05 + +\verbatim +Async - A library for programming event driven applications +Copyright (C) 2003-2015 Tobias Blomberg / SM0SVX + +This program is free software; you can redistribute it and/or modify +it under the terms of the GNU General Public License as published by +the Free Software Foundation; either version 2 of the License, or +(at your option) any later version. + +This program is distributed in the hope that it will be useful, +but WITHOUT ANY WARRANTY; without even the implied warranty of +MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +GNU General Public License for more details. + +You should have received a copy of the GNU General Public License +along with this program; if not, write to the Free Software +Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA +\endverbatim +*/ + +#ifndef ASYNC_AUDIO_DECODER_NULL_INCLUDED +#define ASYNC_AUDIO_DECODER_NULL_INCLUDED + + +/**************************************************************************** + * + * System Includes + * + ****************************************************************************/ + +#include +#include + + +/**************************************************************************** + * + * Project Includes + * + ****************************************************************************/ + +#include + + +/**************************************************************************** + * + * Local Includes + * + ****************************************************************************/ + + + +/**************************************************************************** + * + * Forward declarations + * + ****************************************************************************/ + + + +/**************************************************************************** + * + * Namespace + * + ****************************************************************************/ + +namespace Async +{ + + +/**************************************************************************** + * + * Forward declarations of classes inside of the declared namespace + * + ****************************************************************************/ + + + +/**************************************************************************** + * + * Defines & typedefs + * + ****************************************************************************/ + + + +/**************************************************************************** + * + * Exported Global Variables + * + ****************************************************************************/ + + + +/**************************************************************************** + * + * Class definitions + * + ****************************************************************************/ + +/** +@brief An audio "decoder" used when no real audio need to be communicated +@author Tobias Blomberg / SM0SVX +@date 2014-05-05 + +This class implements an audio "decoder" that will just produce zero samples. +The only thing transmitted by the encoder is the number of samples in the +block but no real samples are encoded. The NULL codec may be of use when the +real audio is communicated through another path. +*/ +class AudioDecoderNull : public AudioDecoder +{ + public: + /** + * @brief Default constuctor + */ + AudioDecoderNull(void) {} + + /** + * @brief Destructor + */ + virtual ~AudioDecoderNull(void) {} + + /** + * @brief Get the name of the codec + * @returns Return the name of the codec + */ + virtual const char *name(void) const { return "NULL"; } + + /** + * @brief Write encoded samples into the decoder + * @param buf Buffer containing encoded samples + * @param size The size of the buffer + * + * This NULL decoder will just write zero samples. The incoming buffer + * will contain a 16 bit unsigned integer that indicate how many zero + * samples that should be written to the audio sink. + */ + virtual void writeEncodedSamples(void *buf, int size) + { + // Sanity check + if (size != sizeof(uint16_t)) + { + return; + } + + // Decode incoming buffer + uint8_t *ptr = reinterpret_cast(buf); + uint16_t cnt = static_cast(ptr[0]); + cnt |= static_cast(ptr[1]) << 8; + + // Allocate a zeroed out buffer and write it to the sink + float samples[cnt]; + std::memset(samples, 0, cnt * sizeof(*samples)); + sinkWriteSamples(samples, cnt); + } + + protected: + + private: + AudioDecoderNull(const AudioDecoderNull&); + AudioDecoderNull& operator=(const AudioDecoderNull&); + +}; /* class AudioDecoderNull */ + + +} /* namespace */ + +#endif /* ASYNC_AUDIO_DECODER_NULL_INCLUDED */ + + + +/* + * This file has not been truncated + */ diff --git a/src/async/audio/AsyncAudioDecoderOpus.cpp b/src/async/audio/AsyncAudioDecoderOpus.cpp new file mode 100644 index 0000000..5235bb0 --- /dev/null +++ b/src/async/audio/AsyncAudioDecoderOpus.cpp @@ -0,0 +1,264 @@ +/** +@file AudioDecoderOpus.cpp +@brief An audio decoder that use the Opus audio codec +@author Tobias Blomberg / SM0SVX +@date 2013-10-12 + +\verbatim +Async - A library for programming event driven applications +Copyright (C) 2003-2013 Tobias Blomberg / SM0SVX + +This program is free software; you can redistribute it and/or modify +it under the terms of the GNU General Public License as published by +the Free Software Foundation; either version 2 of the License, or +(at your option) any later version. + +This program is distributed in the hope that it will be useful, +but WITHOUT ANY WARRANTY; without even the implied warranty of +MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +GNU General Public License for more details. + +You should have received a copy of the GNU General Public License +along with this program; if not, write to the Free Software +Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA +\endverbatim +*/ + + + +/**************************************************************************** + * + * System Includes + * + ****************************************************************************/ + +#include +#include +#include +#include + + +/**************************************************************************** + * + * Project Includes + * + ****************************************************************************/ + + + +/**************************************************************************** + * + * Local Includes + * + ****************************************************************************/ + +#include "AsyncAudioDecoderOpus.h" + + + +/**************************************************************************** + * + * Namespaces to use + * + ****************************************************************************/ + +using namespace std; +using namespace Async; + + + +/**************************************************************************** + * + * Defines & typedefs + * + ****************************************************************************/ + + + +/**************************************************************************** + * + * Local class definitions + * + ****************************************************************************/ + + + +/**************************************************************************** + * + * Prototypes + * + ****************************************************************************/ + + + +/**************************************************************************** + * + * Exported Global Variables + * + ****************************************************************************/ + + + + +/**************************************************************************** + * + * Local Global Variables + * + ****************************************************************************/ + + + +/**************************************************************************** + * + * Public member functions + * + ****************************************************************************/ + +AudioDecoderOpus::AudioDecoderOpus(void) + : frame_size(0) +{ + int error; + dec = opus_decoder_create(INTERNAL_SAMPLE_RATE, 1, &error); + if (error != OPUS_OK) + { + cerr << "*** ERROR: Could not initialize Opus decoder\n"; + exit(1); + } +} /* AudioDecoderOpus::AudioDecoderOpus */ + + +AudioDecoderOpus::~AudioDecoderOpus(void) +{ + opus_decoder_destroy(dec); +} /* AudioDecoderOpus::~AudioDecoderOpus */ + + +void AudioDecoderOpus::setOption(const std::string &name, + const std::string &value) +{ +#if OPUS_MAJOR > 0 + if (name == "GAIN") + { + setGain(atof(value.c_str())); + } + else +#endif + { + cerr << "*** WARNING AudioDecoderOpus: Unknown option \"" + << name << "\". Ignoring it.\n"; + } +} /* AudioDecoderOpus::setOption */ + + +void AudioDecoderOpus::printCodecParams(void) const +{ +#if OPUS_MAJOR > 0 + cout << "------ Opus decoder parameters ------\n"; + cout << "Gain = " << gain() << "dB\n"; + cout << "--------------------------------------\n"; +#endif +} /* AudioDecoderOpus::printCodecParams */ + + +#if OPUS_MAJOR > 0 +float AudioDecoderOpus::setGain(float new_gain) +{ + opus_int32 gaini = static_cast(256.0f * new_gain); + opus_decoder_ctl(dec, OPUS_SET_GAIN(gaini)); + return gain(); +} /* AudioDecoderOpus::setGain */ + + +float AudioDecoderOpus::gain(void) const +{ + opus_int32 gaini; + // coverity[ptr_arith] + opus_decoder_ctl(dec, OPUS_GET_GAIN(&gaini)); + return gaini / 256.0f; +} /* AudioDecoderOpus::gain */ +#endif + + +void AudioDecoderOpus::reset(void) +{ + opus_decoder_ctl(dec, OPUS_RESET_STATE); +} /* AudioDecoderOpus::reset */ + + +void AudioDecoderOpus::writeEncodedSamples(void *buf, int size) +{ + unsigned char *packet = reinterpret_cast(buf); + + int frame_cnt = opus_packet_get_nb_frames(packet, size); + if (frame_cnt == 0) + { + return; + } + else if (frame_cnt < 0) + { + cerr << "*** ERROR: Opus decoder error: " << opus_strerror(frame_size) + << endl; + return; + } + frame_size = opus_packet_get_samples_per_frame(packet, INTERNAL_SAMPLE_RATE); + if (frame_size == 0) + { + return; + } + else if (frame_size < 0) + { + cerr << "*** ERROR: Opus decoder error: " << opus_strerror(frame_size) + << endl; + return; + } + int channels = opus_packet_get_nb_channels(packet); + if (channels <= 0) + { + cerr << "*** ERROR: Opus decoder error: " << opus_strerror(channels) + << endl; + return; + } + else if (channels != 1) + { + cerr << "*** ERROR: Multi channel Opus packet received but only one " + "channel can be handled\n"; + return; + } + //cout << "### frame_cnt=" << frame_cnt << " frame_size=" << frame_size; + float samples[frame_cnt*frame_size]; + frame_size = opus_decode_float(dec, packet, size, samples, + frame_cnt*frame_size, 0); + //cout << " " << frame_size << endl; + if (frame_size > 0) + { + sinkWriteSamples(samples, frame_size); + } + else if (frame_size < 0) + { + cerr << "**** ERROR: Opus decoder error: " << opus_strerror(frame_size) + << endl; + } +} /* AudioDecoderOpus::writeEncodedSamples */ + + + +/**************************************************************************** + * + * Protected member functions + * + ****************************************************************************/ + + + +/**************************************************************************** + * + * Private member functions + * + ****************************************************************************/ + + + +/* + * This file has not been truncated + */ + diff --git a/src/async/audio/AsyncAudioDecoderOpus.h b/src/async/audio/AsyncAudioDecoderOpus.h new file mode 100644 index 0000000..fa8286a --- /dev/null +++ b/src/async/audio/AsyncAudioDecoderOpus.h @@ -0,0 +1,192 @@ +/** +@file AsyncAudioDecoderOpus.h +@brief An audio decoder that use the Opus audio codec +@author Tobias Blomberg / SM0SVX +@date 2013-10-12 + +\verbatim +Async - A library for programming event driven applications +Copyright (C) 2003-2013 Tobias Blomberg / SM0SVX + +This program is free software; you can redistribute it and/or modify +it under the terms of the GNU General Public License as published by +the Free Software Foundation; either version 2 of the License, or +(at your option) any later version. + +This program is distributed in the hope that it will be useful, +but WITHOUT ANY WARRANTY; without even the implied warranty of +MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +GNU General Public License for more details. + +You should have received a copy of the GNU General Public License +along with this program; if not, write to the Free Software +Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA +\endverbatim +*/ + +#ifndef ASYNC_AUDIO_DECODER_OPUS_INCLUDED +#define ASYNC_AUDIO_DECODER_OPUS_INCLUDED + + +/**************************************************************************** + * + * System Includes + * + ****************************************************************************/ + +#include + + +/**************************************************************************** + * + * Project Includes + * + ****************************************************************************/ + +#include + + +/**************************************************************************** + * + * Local Includes + * + ****************************************************************************/ + + + +/**************************************************************************** + * + * Forward declarations + * + ****************************************************************************/ + + + +/**************************************************************************** + * + * Namespace + * + ****************************************************************************/ + +namespace Async +{ + + +/**************************************************************************** + * + * Forward declarations of classes inside of the declared namespace + * + ****************************************************************************/ + + + +/**************************************************************************** + * + * Defines & typedefs + * + ****************************************************************************/ + + + +/**************************************************************************** + * + * Exported Global Variables + * + ****************************************************************************/ + + + +/**************************************************************************** + * + * Class definitions + * + ****************************************************************************/ + +/** +@brief An audio decoder that use the Opus audio codec +@author Tobias Blomberg / SM0SVX +@date 2013-10-12 + +This class implements an audio decoder that use the Opus audio codec. +*/ +class AudioDecoderOpus : public AudioDecoder +{ + public: + /** + * @brief Default constuctor + */ + AudioDecoderOpus(void); + + /** + * @brief Destructor + */ + virtual ~AudioDecoderOpus(void); + + /** + * @brief Get the name of the codec + * @returns Return the name of the codec + */ + virtual const char *name(void) const { return "OPUS"; } + + /** + * @brief Set an option for the decoder + * @param name The name of the option + * @param value The value of the option + */ + virtual void setOption(const std::string &name, const std::string &value); + + /** + * @brief Print codec parameter settings + */ + virtual void printCodecParams(void) const; + +#if OPUS_MAJOR + /** + * @brief Configures decoder gain adjustment + * @param new_gain The new gain to set [dB] + * @returns Retunrs the newly set gain + */ + float setGain(float new_gain); + + /** + * @brief Get the currently set gain + * @returns Returns the currently set gain in dB + */ + float gain(void) const; +#endif + + /** + * @brief Resets encoder to be equivalent to a freshly initialized one + */ + void reset(void); + + /** + * @brief Write encoded samples into the decoder + * @param buf Buffer containing encoded samples + * @param size The size of the buffer + */ + virtual void writeEncodedSamples(void *buf, int size); + + + protected: + + private: + OpusDecoder *dec; + int frame_size; + + AudioDecoderOpus(const AudioDecoderOpus&); + AudioDecoderOpus& operator=(const AudioDecoderOpus&); + +}; /* class AudioDecoderOpus */ + + +} /* namespace */ + +#endif /* ASYNC_AUDIO_DECODER_OPUS_INCLUDED */ + + + +/* + * This file has not been truncated + */ + diff --git a/src/async/audio/AsyncAudioDecoderRaw.h b/src/async/audio/AsyncAudioDecoderRaw.h new file mode 100644 index 0000000..7d1212e --- /dev/null +++ b/src/async/audio/AsyncAudioDecoderRaw.h @@ -0,0 +1,162 @@ +/** +@file AsyncAudioDecoderRaw.h +@brief A pass through (unencoded) audio "decoder" +@author Tobias Blomberg / SM0SVX +@date 2008-10-06 + +\verbatim +Async - A library for programming event driven applications +Copyright (C) 2003-2008 Tobias Blomberg / SM0SVX + +This program is free software; you can redistribute it and/or modify +it under the terms of the GNU General Public License as published by +the Free Software Foundation; either version 2 of the License, or +(at your option) any later version. + +This program is distributed in the hope that it will be useful, +but WITHOUT ANY WARRANTY; without even the implied warranty of +MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +GNU General Public License for more details. + +You should have received a copy of the GNU General Public License +along with this program; if not, write to the Free Software +Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA +\endverbatim +*/ + +#ifndef ASYNC_AUDIO_DECODER_RAW_INCLUDED +#define ASYNC_AUDIO_DECODER_RAW_INCLUDED + + +/**************************************************************************** + * + * System Includes + * + ****************************************************************************/ + + + +/**************************************************************************** + * + * Project Includes + * + ****************************************************************************/ + +#include + + +/**************************************************************************** + * + * Local Includes + * + ****************************************************************************/ + + + +/**************************************************************************** + * + * Forward declarations + * + ****************************************************************************/ + + + +/**************************************************************************** + * + * Namespace + * + ****************************************************************************/ + +namespace Async +{ + + +/**************************************************************************** + * + * Forward declarations of classes inside of the declared namespace + * + ****************************************************************************/ + + + +/**************************************************************************** + * + * Defines & typedefs + * + ****************************************************************************/ + + + +/**************************************************************************** + * + * Exported Global Variables + * + ****************************************************************************/ + + + +/**************************************************************************** + * + * Class definitions + * + ****************************************************************************/ + +/** +@brief A pass through (unencoded) audio "decoder" +@author Tobias Blomberg / SM0SVX +@date 2008-10-06 + +This class implements an audio "decoder" that just passes the native sample +format through. +*/ +class AudioDecoderRaw : public AudioDecoder +{ + public: + /** + * @brief Default constuctor + */ + AudioDecoderRaw(void) {} + + /** + * @brief Destructor + */ + virtual ~AudioDecoderRaw(void) {} + + /** + * @brief Get the name of the codec + * @return Return the name of the codec + */ + virtual const char *name(void) const { return "RAW"; } + + /** + * @brief Write encoded samples into the decoder + * @param buf Buffer containing encoded samples + * @param size The size of the buffer + */ + virtual void writeEncodedSamples(void *buf, int size) + { + const float *samples = reinterpret_cast(buf); + int count = size / sizeof(*samples); + sinkWriteSamples(samples, count); + } + + + protected: + + private: + AudioDecoderRaw(const AudioDecoderRaw&); + AudioDecoderRaw& operator=(const AudioDecoderRaw&); + +}; /* class AudioDecoderRaw */ + + +} /* namespace */ + +#endif /* ASYNC_AUDIO_DECODER_RAW_INCLUDED */ + + + +/* + * This file has not been truncated + */ + diff --git a/src/async/audio/AsyncAudioDecoderS16.cpp b/src/async/audio/AsyncAudioDecoderS16.cpp new file mode 100644 index 0000000..77687fc --- /dev/null +++ b/src/async/audio/AsyncAudioDecoderS16.cpp @@ -0,0 +1,159 @@ +/** +@file AudioDecoderS16.cpp +@brief An audio decoder for signed 16 bit samples +@author Tobias Blomberg / SM0SVX +@date 2008-10-06 + +\verbatim +Async - A library for programming event driven applications +Copyright (C) 2003-2008 Tobias Blomberg / SM0SVX + +This program is free software; you can redistribute it and/or modify +it under the terms of the GNU General Public License as published by +the Free Software Foundation; either version 2 of the License, or +(at your option) any later version. + +This program is distributed in the hope that it will be useful, +but WITHOUT ANY WARRANTY; without even the implied warranty of +MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +GNU General Public License for more details. + +You should have received a copy of the GNU General Public License +along with this program; if not, write to the Free Software +Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA +\endverbatim +*/ + + + +/**************************************************************************** + * + * System Includes + * + ****************************************************************************/ + +#include + + +/**************************************************************************** + * + * Project Includes + * + ****************************************************************************/ + + + +/**************************************************************************** + * + * Local Includes + * + ****************************************************************************/ + +#include "AsyncAudioDecoderS16.h" + + + +/**************************************************************************** + * + * Namespaces to use + * + ****************************************************************************/ + +using namespace std; +using namespace Async; + + + +/**************************************************************************** + * + * Defines & typedefs + * + ****************************************************************************/ + + + +/**************************************************************************** + * + * Local class definitions + * + ****************************************************************************/ + + + +/**************************************************************************** + * + * Prototypes + * + ****************************************************************************/ + + + +/**************************************************************************** + * + * Exported Global Variables + * + ****************************************************************************/ + + + + +/**************************************************************************** + * + * Local Global Variables + * + ****************************************************************************/ + + + +/**************************************************************************** + * + * Public member functions + * + ****************************************************************************/ + +AudioDecoderS16::AudioDecoderS16(void) +{ + +} /* AudioDecoderS16::AudioDecoderS16 */ + + +AudioDecoderS16::~AudioDecoderS16(void) +{ + +} /* AudioDecoderS16::~AudioDecoderS16 */ + + +void AudioDecoderS16::writeEncodedSamples(void *buf, int size) +{ + int16_t *s16_samples = reinterpret_cast(buf); + int count = size / sizeof(int16_t); + float samples[count]; + for (int i=0; i(s16_samples[i]) / 32768.0; + } + sinkWriteSamples(samples, count); +} /* AudioDecoderS16::writeEncodedSamples */ + + + +/**************************************************************************** + * + * Protected member functions + * + ****************************************************************************/ + + + +/**************************************************************************** + * + * Private member functions + * + ****************************************************************************/ + + + +/* + * This file has not been truncated + */ + diff --git a/src/async/audio/AsyncAudioDecoderS16.h b/src/async/audio/AsyncAudioDecoderS16.h new file mode 100644 index 0000000..3f3cb0a --- /dev/null +++ b/src/async/audio/AsyncAudioDecoderS16.h @@ -0,0 +1,157 @@ +/** +@file AsyncAudioDecoderS16.h +@brief An audio decoder for signed 16 bit samples +@author Tobias Blomberg / SM0SVX +@date 2008-10-06 + +\verbatim +Async - A library for programming event driven applications +Copyright (C) 2003-2008 Tobias Blomberg / SM0SVX + +This program is free software; you can redistribute it and/or modify +it under the terms of the GNU General Public License as published by +the Free Software Foundation; either version 2 of the License, or +(at your option) any later version. + +This program is distributed in the hope that it will be useful, +but WITHOUT ANY WARRANTY; without even the implied warranty of +MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +GNU General Public License for more details. + +You should have received a copy of the GNU General Public License +along with this program; if not, write to the Free Software +Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA +\endverbatim +*/ + +#ifndef ASYNC_AUDIO_DECODER_S16_INCLUDED +#define ASYNC_AUDIO_DECODER_S16_INCLUDED + + +/**************************************************************************** + * + * System Includes + * + ****************************************************************************/ + + + +/**************************************************************************** + * + * Project Includes + * + ****************************************************************************/ + +#include + + +/**************************************************************************** + * + * Local Includes + * + ****************************************************************************/ + + + +/**************************************************************************** + * + * Forward declarations + * + ****************************************************************************/ + + + +/**************************************************************************** + * + * Namespace + * + ****************************************************************************/ + +namespace Async +{ + + +/**************************************************************************** + * + * Forward declarations of classes inside of the declared namespace + * + ****************************************************************************/ + + + +/**************************************************************************** + * + * Defines & typedefs + * + ****************************************************************************/ + + + +/**************************************************************************** + * + * Exported Global Variables + * + ****************************************************************************/ + + + +/**************************************************************************** + * + * Class definitions + * + ****************************************************************************/ + +/** +@brief An audio decoder for signed 16 bit samples +@author Tobias Blomberg / SM0SVX +@date 2008-10-06 + +This class implements an audio "decoder" that converts signed 16 bit fixed +precision samples to the native sample format. +*/ +class AudioDecoderS16 : public AudioDecoder +{ + public: + /** + * @brief Default constuctor + */ + AudioDecoderS16(void); + + /** + * @brief Destructor + */ + virtual ~AudioDecoderS16(void); + + /** + * @brief Get the name of the codec + * @returns Return the name of the codec + */ + virtual const char *name(void) const { return "S16"; } + + /** + * @brief Write encoded samples into the decoder + * @param buf Buffer containing encoded samples + * @param size The size of the buffer + */ + virtual void writeEncodedSamples(void *buf, int size); + + + protected: + + private: + AudioDecoderS16(const AudioDecoderS16&); + AudioDecoderS16& operator=(const AudioDecoderS16&); + +}; /* class AudioDecoderS16 */ + + +} /* namespace */ + +#endif /* ASYNC_AUDIO_DECODER_S16_INCLUDED */ + + + +/* + * This file has not been truncated + */ + diff --git a/src/async/audio/AsyncAudioDecoderSpeex.cpp b/src/async/audio/AsyncAudioDecoderSpeex.cpp new file mode 100644 index 0000000..e71abfc --- /dev/null +++ b/src/async/audio/AsyncAudioDecoderSpeex.cpp @@ -0,0 +1,220 @@ +/** +@file AudioDecoderSpeex.cpp +@brief An audio decoder that use the Speex audio codec +@author Tobias Blomberg / SM0SVX +@date 2008-10-15 + +\verbatim +Async - A library for programming event driven applications +Copyright (C) 2003-2008 Tobias Blomberg / SM0SVX + +This program is free software; you can redistribute it and/or modify +it under the terms of the GNU General Public License as published by +the Free Software Foundation; either version 2 of the License, or +(at your option) any later version. + +This program is distributed in the hope that it will be useful, +but WITHOUT ANY WARRANTY; without even the implied warranty of +MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +GNU General Public License for more details. + +You should have received a copy of the GNU General Public License +along with this program; if not, write to the Free Software +Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA +\endverbatim +*/ + + + +/**************************************************************************** + * + * System Includes + * + ****************************************************************************/ + +#include +#include + + +/**************************************************************************** + * + * Project Includes + * + ****************************************************************************/ + + + +/**************************************************************************** + * + * Local Includes + * + ****************************************************************************/ + +#include "AsyncAudioDecoderSpeex.h" + + + +/**************************************************************************** + * + * Namespaces to use + * + ****************************************************************************/ + +using namespace std; +using namespace Async; + + + +/**************************************************************************** + * + * Defines & typedefs + * + ****************************************************************************/ + + + +/**************************************************************************** + * + * Local class definitions + * + ****************************************************************************/ + + + +/**************************************************************************** + * + * Prototypes + * + ****************************************************************************/ + + + +/**************************************************************************** + * + * Exported Global Variables + * + ****************************************************************************/ + + + + +/**************************************************************************** + * + * Local Global Variables + * + ****************************************************************************/ + + + +/**************************************************************************** + * + * Public member functions + * + ****************************************************************************/ + +AudioDecoderSpeex::AudioDecoderSpeex(void) +{ + speex_bits_init(&bits); +#if INTERNAL_SAMPLE_RATE == 16000 + dec_state = speex_decoder_init(&speex_wb_mode); +#else + dec_state = speex_decoder_init(&speex_nb_mode); +#endif + speex_decoder_ctl(dec_state, SPEEX_GET_FRAME_SIZE, &frame_size); + + //enableEnhancer(false); +} /* AudioDecoderSpeex::AudioDecoderSpeex */ + + +AudioDecoderSpeex::~AudioDecoderSpeex(void) +{ + speex_bits_destroy(&bits); + speex_decoder_destroy(dec_state); +} /* AudioDecoderSpeex::~AudioDecoderSpeex */ + + +void AudioDecoderSpeex::setOption(const std::string &name, + const std::string &value) +{ + if (name == "ENHANCER") + { + enableEnhancer(atoi(value.c_str()) != 0); + } + else + { + cerr << "*** WARNING AudioDecoderSpeex: Unknown option \"" + << name << "\". Ignoring it.\n"; + } +} /* AudioDecoderSpeex::setOption */ + + +void AudioDecoderSpeex::printCodecParams(void) const +{ + cout << "------ Speex decoder parameters ------\n"; + cout << "Frame size = " << frameSize() << endl; + cout << "Enhancer = " << (enhancerEnabled() ? "EN" : "DIS") << "ABLED\n"; + cout << "--------------------------------------\n"; +} /* AudioDecoderSpeex::printCodecParams */ + + +bool AudioDecoderSpeex::enableEnhancer(bool enable) +{ + int enh = enable ? 1 : 0; + speex_decoder_ctl(dec_state, SPEEX_SET_ENH, &enh); + return enhancerEnabled(); +} /* AudioDecoderSpeex::enableEnhancer */ + + +bool AudioDecoderSpeex::enhancerEnabled(void) const +{ + int enh; + speex_decoder_ctl(dec_state, SPEEX_GET_ENH, &enh); + return (enh != 0); +} /* AudioDecoderSpeex::enhancerEnabled */ + + + + +void AudioDecoderSpeex::writeEncodedSamples(void *buf, int size) +{ + char *ptr = (char *)buf; + + speex_bits_read_from(&bits, ptr, size); + float samples[frame_size]; +#if SPEEX_MAJOR > 1 || (SPEEX_MAJOR == 1 && SPEEX_MINOR >= 1) + while (speex_decode(dec_state, &bits, samples) == 0) +#else + while ((speex_decode(dec_state, &bits, samples) == 0) && + (speex_bits_remaining(&bits) > 0)) +#endif + { + for (int i=0; i + + +/**************************************************************************** + * + * Project Includes + * + ****************************************************************************/ + +#include + + +/**************************************************************************** + * + * Local Includes + * + ****************************************************************************/ + + + +/**************************************************************************** + * + * Forward declarations + * + ****************************************************************************/ + + + +/**************************************************************************** + * + * Namespace + * + ****************************************************************************/ + +namespace Async +{ + + +/**************************************************************************** + * + * Forward declarations of classes inside of the declared namespace + * + ****************************************************************************/ + + + +/**************************************************************************** + * + * Defines & typedefs + * + ****************************************************************************/ + + + +/**************************************************************************** + * + * Exported Global Variables + * + ****************************************************************************/ + + + +/**************************************************************************** + * + * Class definitions + * + ****************************************************************************/ + +/** +@brief An audio decoder that use the Speex audio codec +@author Tobias Blomberg / SM0SVX +@date 2008-10-15 + +This class implements an audio decoder that use the Speex audio codec. +*/ +class AudioDecoderSpeex : public AudioDecoder +{ + public: + /** + * @brief Default constuctor + */ + AudioDecoderSpeex(void); + + /** + * @brief Destructor + */ + virtual ~AudioDecoderSpeex(void); + + /** + * @brief Get the name of the codec + * @returns Return the name of the codec + */ + virtual const char *name(void) const { return "SPEEX"; } + + /** + * @brief Set an option for the decoder + * @param name The name of the option + * @param value The value of the option + */ + virtual void setOption(const std::string &name, const std::string &value); + + /** + * @brief Print codec parameter settings + */ + virtual void printCodecParams(void) const; + + /** + * @brief Get the frame size for the decoder + * @return Returns the decoder frame size + */ + int frameSize(void) const { return frame_size; } + + /** + * @brief Enable or disable the perceptual enhancer + * @param enable Set to \em true to enable or \em false to disable + * @return Returns the new setting + */ + bool enableEnhancer(bool enable); + + /** + * @brief Get the perceptual enhance enable/disable state + * @return Returns \em true if the enhancer is enabled or \em false if + * it's not + */ + bool enhancerEnabled(void) const; + + /** + * @brief Write encoded samples into the decoder + * @param buf Buffer containing encoded samples + * @param size The size of the buffer + */ + virtual void writeEncodedSamples(void *buf, int size); + + + protected: + + private: + SpeexBits bits; + void *dec_state; + int frame_size; + + AudioDecoderSpeex(const AudioDecoderSpeex&); + AudioDecoderSpeex& operator=(const AudioDecoderSpeex&); + +}; /* class AudioDecoderSpeex */ + + +} /* namespace */ + +#endif /* ASYNC_AUDIO_DECODER_SPEEX_INCLUDED */ + + + +/* + * This file has not been truncated + */ + diff --git a/src/async/audio/AsyncAudioDelayLine.cpp b/src/async/audio/AsyncAudioDelayLine.cpp new file mode 100644 index 0000000..3ddb241 --- /dev/null +++ b/src/async/audio/AsyncAudioDelayLine.cpp @@ -0,0 +1,345 @@ +/** +@file AsyncAudioDelayLine.cpp +@brief An audio pipe component to create a delay used for muting +@author Tobias Blomberg / SM0SVX +@date 2006-07-08 + +\verbatim +Async - A library for programming event driven applications +Copyright (C) 2003-2010 Tobias Blomberg / SM0SVX + +This program is free software; you can redistribute it and/or modify +it under the terms of the GNU General Public License as published by +the Free Software Foundation; either version 2 of the License, or +(at your option) any later version. + +This program is distributed in the hope that it will be useful, +but WITHOUT ANY WARRANTY; without even the implied warranty of +MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +GNU General Public License for more details. + +You should have received a copy of the GNU General Public License +along with this program; if not, write to the Free Software +Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA +\endverbatim +*/ + + + +/**************************************************************************** + * + * System Includes + * + ****************************************************************************/ + +#include +#include +#include + + +/**************************************************************************** + * + * Project Includes + * + ****************************************************************************/ + + + +/**************************************************************************** + * + * Local Includes + * + ****************************************************************************/ + +#include "AsyncAudioDelayLine.h" + + + +/**************************************************************************** + * + * Namespaces to use + * + ****************************************************************************/ + +using namespace std; +using namespace Async; + + + +/**************************************************************************** + * + * Defines & typedefs + * + ****************************************************************************/ + + + +/**************************************************************************** + * + * Local class definitions + * + ****************************************************************************/ + + + +/**************************************************************************** + * + * Prototypes + * + ****************************************************************************/ + + + +/**************************************************************************** + * + * Exported Global Variables + * + ****************************************************************************/ + + + + +/**************************************************************************** + * + * Local Global Variables + * + ****************************************************************************/ + + + +/**************************************************************************** + * + * Public member functions + * + ****************************************************************************/ + +AudioDelayLine::AudioDelayLine(int length_ms) + : size(length_ms * INTERNAL_SAMPLE_RATE / 1000), ptr(0), flush_cnt(0), + is_muted(false), mute_cnt(0), last_clear(0), fade_gain(0), fade_len(0), + fade_pos(0), fade_dir(0) +{ + buf = new float[size]; + memset(buf, 0, size * sizeof(*buf)); + clear(); + setFadeTime(DEFAULT_FADE_TIME); +} /* AudioDelayLine::AudioDelayLine */ + + +AudioDelayLine::~AudioDelayLine(void) +{ + delete [] fade_gain; + delete [] buf; +} /* AudioDelayLine::~AudioDelayLine */ + + +void AudioDelayLine::setFadeTime(int time_ms) +{ + delete [] fade_gain; + fade_gain = 0; + + if (time_ms <= 0) + { + fade_len = 0; + fade_pos = 0; + fade_dir = 0; + return; + } + + fade_len = time_ms * INTERNAL_SAMPLE_RATE / 1000; + fade_pos = min(fade_pos, fade_len-1); + fade_gain = new float[fade_len]; + for (int i=0; i(i) / fade_len)); + } + fade_gain[fade_len-1] = 0; +} /* AudioDelayLine::setFadeTime */ + + +void AudioDelayLine::mute(bool do_mute, int time_ms) +{ + int mute_ext = 0; + if (time_ms > 0) + { + mute_ext = min(size, time_ms * INTERNAL_SAMPLE_RATE / 1000); + } + + if (do_mute) + { + fade_pos = 0; // Reset fade gain + fade_dir = 1; // Fade out + ptr = (ptr + size - mute_ext) % size; + for (int i=0; i 0) && (--mute_cnt == 0)) + { + fade_dir = -1; // Fade in + is_muted = false; + } + ptr = (ptr < size-1) ? ptr+1 : 0; + } + + return written; + +} /* AudioDelayLine::writeSamples */ + + +void AudioDelayLine::flushSamples(void) +{ + flush_cnt = size - last_clear; + + if (flush_cnt > 0) + { + writeRemainingSamples(); + } + else + { + sinkFlushSamples(); + } +} /* AudioDelayLine::flushSamples */ + + +void AudioDelayLine::resumeOutput(void) +{ + if (flush_cnt > 0) + { + writeRemainingSamples(); + } + else + { + sourceResumeOutput(); + } +} /* AudioDelayLine::resumeOutput */ + + +void AudioDelayLine::allSamplesFlushed(void) +{ + sourceAllSamplesFlushed(); +} /* AudioDelayLine::allSamplesFlushed */ + + + +/**************************************************************************** + * + * Protected member functions + * + ****************************************************************************/ + + + +/**************************************************************************** + * + * Private member functions + * + ****************************************************************************/ + +void AudioDelayLine::writeRemainingSamples(void) +{ + float output[512]; + int written = 1; // Set to 1 so that we enter the loop the first time around + + while ((written > 0) && (flush_cnt > 0)) + { + int count = min(512, flush_cnt); + + int out_ptr = ptr; + for (int i=0; i +#include + + +/**************************************************************************** + * + * Local Includes + * + ****************************************************************************/ + + + +/**************************************************************************** + * + * Forward declarations + * + ****************************************************************************/ + + + +/**************************************************************************** + * + * Namespace + * + ****************************************************************************/ + +namespace Async +{ + + +/**************************************************************************** + * + * Forward declarations of classes inside of the declared namespace + * + ****************************************************************************/ + + + +/**************************************************************************** + * + * Defines & typedefs + * + ****************************************************************************/ + + + +/**************************************************************************** + * + * Exported Global Variables + * + ****************************************************************************/ + + + +/**************************************************************************** + * + * Class definitions + * + ****************************************************************************/ + +/** +@brief This class implements an audio delay line +@author Tobias Blomberg / SM0SVX +@date 2006-07-08 + +This class implements an audio delay line. It simply delays the audio with +the specified amount of time. This can be useful if you want to mute audio +based on a slow detector. With a delay line you have the possibility to +mute audio that have passed the detector but have not yet passed through the +delay line. +*/ +class AudioDelayLine : public Async::AudioSink, public Async::AudioSource +{ + public: + /** + * @brief Constuctor + * @param length_ms The length in milliseconds of the delay line + */ + explicit AudioDelayLine(int length_ms); + + /** + * @brief Destructor + */ + ~AudioDelayLine(void); + + /** + * @brief Set the fade in/out time when muting and clearing + * @param time_ms The time in milliseconds for the fade in/out + * + * When a mute or clear is issued the audio stream will not abruptly + * go to zero. Instead it will fade in and out smoothly to avoid + * popping sounds due to discontinueties in the sample stream. + * The default is 10 milliseconds. Set to 0 to turn off. + */ + void setFadeTime(int time_ms); + + /** + * @brief Mute audio + * @param do_mute If \em true mute else unmute + * @param time_ms How much more time in milliseconds to mute (see below) + * + * This function is used to mute audio in the delay line. With time_ms + * equal to zero its function is trivial. Mute incoming audio until + * mute is called with do_mute = false. The time_ms paramter specify how + * much time before (do_mute = true) or time after (do_mute = false) + * should be muted. Negative values are not allowed. + */ + void mute(bool do_mute, int time_ms=0); + + /** + * @brief Clear samples in the delay line + * @param time_ms How much time in milliseconds to clear + * + * Will clear the specified amount of samples in the delay line. If a + * clear is issued right before the delay line is flushed, the cleared + * samples will not be flushed. They will be thrown away. + */ + void clear(int time_ms=-1); + + /** + * @brief Write samples into the delay line + * @param samples The buffer containing the samples + * @param count The number of samples in the buffer + * @return Returns the number of samples that has been taken care of + * + * This function will write samples into the delay line. It's normally + * only called from a connected source object. + */ + int writeSamples(const float *samples, int count); + + /** + * @brief Tell the sink to flush the previously written samples + * + * This function is used to tell the sink to flush previously written + * samples. When done flushing, the sink should call the + * sourceAllSamplesFlushed function. + * This function is normally only called from a connected source object. + */ + void flushSamples(void); + + /** + * @brief Resume audio output to the sink + * + * This function must be reimplemented by the inheriting class. It + * will be called when the registered audio sink is ready to + * accept more samples. + * This function is normally only called from a connected sink object. + */ + void resumeOutput(void); + + /** + * @brief The registered sink has flushed all samples + * + * This function must be implemented by the inheriting class. It + * will be called when all samples have been flushed in + * the registered sink. + * This function is normally only called from a connected sink object. + */ + void allSamplesFlushed(void); + + + protected: + + private: + static const int DEFAULT_FADE_TIME = 10; // 10ms default fade time + + float *buf; + int size; + int ptr; + int flush_cnt; + bool is_muted; + int mute_cnt; + int last_clear; + float *fade_gain; + int fade_len; + int fade_pos; + int fade_dir; + + AudioDelayLine(const AudioDelayLine&); + AudioDelayLine& operator=(const AudioDelayLine&); + void writeRemainingSamples(void); + + inline float currentFadeGain(void) + { + if (fade_gain == 0) + { + return 1.0f; + } + + float gain = fade_gain[fade_pos]; + fade_pos += fade_dir; + + if ((fade_dir > 0) && (fade_pos >= fade_len-1)) + { + fade_dir = 0; + fade_pos = fade_len-1; + } + else if ((fade_dir < 0) && (fade_pos <= 0)) + { + fade_dir = 0; + fade_pos = 0; + } + + return gain; + + } /* AudioDelayLine::currentFadeGain */ + +}; /* class AudioDelayLine */ + + +} /* namespace */ + +#endif /* ASYNC_AUDIO_DELAY_LINE_INCLUDED */ + + + +/* + * This file has not been truncated + */ + diff --git a/src/async/audio/AsyncAudioDevice.cpp b/src/async/audio/AsyncAudioDevice.cpp new file mode 100644 index 0000000..e4d1e1b --- /dev/null +++ b/src/async/audio/AsyncAudioDevice.cpp @@ -0,0 +1,424 @@ +/** +@file AsyncAudioDevice.cpp +@brief Handle audio devices +@author Tobias Blomberg +@date 2004-03-20 + +This file contains an implementation of a class that handles +hardware audio devices. + +\verbatim +Async - A library for programming event driven applications +Copyright (C) 2003-2025 Tobias Blomberg / SM0SVX + +This program is free software; you can redistribute it and/or modify +it under the terms of the GNU General Public License as published by +the Free Software Foundation; either version 2 of the License, or +(at your option) any later version. + +This program is distributed in the hope that it will be useful, +but WITHOUT ANY WARRANTY; without even the implied warranty of +MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +GNU General Public License for more details. + +You should have received a copy of the GNU General Public License +along with this program; if not, write to the Free Software +Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA +\endverbatim +*/ + + + +/**************************************************************************** + * + * System Includes + * + ****************************************************************************/ + +#include +#include +#include +#include + + +/**************************************************************************** + * + * Project Includes + * + ****************************************************************************/ + + + +/**************************************************************************** + * + * Local Includes + * + ****************************************************************************/ + +#include "AsyncFdWatch.h" +#include "AsyncAudioIO.h" +#include "AsyncAudioDevice.h" +#include "AsyncAudioDeviceFactory.h" + + +/**************************************************************************** + * + * Namespaces to use + * + ****************************************************************************/ + +using namespace std; +using namespace Async; + + +/**************************************************************************** + * + * Defines & typedefs + * + ****************************************************************************/ + + + +/**************************************************************************** + * + * Local class definitions + * + ****************************************************************************/ + + + +/**************************************************************************** + * + * Prototypes + * + ****************************************************************************/ + + + +/**************************************************************************** + * + * Exported Global Variables + * + ****************************************************************************/ + + + + +/**************************************************************************** + * + * Local Global Variables + * + ****************************************************************************/ + +map AudioDevice::devices; +int AudioDevice::sample_rate = DEFAULT_SAMPLE_RATE; +size_t AudioDevice::block_size_hint = DEFAULT_BLOCK_SIZE_HINT; +size_t AudioDevice::block_count_hint = DEFAULT_BLOCK_COUNT_HINT; +size_t AudioDevice::channels = DEFAULT_CHANNELS; + + + +/**************************************************************************** + * + * Public member functions + * + ****************************************************************************/ + +AudioDevice *AudioDevice::registerAudioIO(const string& dev_designator, + AudioIO *audio_io) +{ + string::size_type colon_pos = dev_designator.find_first_of(':'); + if (colon_pos == string::npos) + { + cerr << "*** ERROR: The audio device name must be given on the form " + "\"devtype:devname\".\n"; + return 0; + } + + string dev_type(dev_designator.substr(0, colon_pos)); + string dev_name(dev_designator.substr(colon_pos+1, string::npos)); + + AudioDevice *dev = 0; + if (devices.count(dev_designator) == 0) + { + dev = AudioDeviceFactory::instance().create(dev_type, dev_name); + if (dev == 0) + { + cerr << "*** ERROR: Unknown audio device type \"" << dev_type << "\" " + "given. Valid device types: " + << AudioDeviceFactory::instance().validDevTypes() << endl; + return 0; + } + + devices[dev_designator] = dev; + } + dev = devices[dev_designator]; + ++dev->use_count; + dev->aios.push_back(audio_io); + return dev; + +} /* AudioDevice::registerAudioIO */ + + +void AudioDevice::unregisterAudioIO(AudioIO *audio_io) +{ + AudioDevice *dev = audio_io->device(); + if (dev == 0) + { + return; + } + + assert(dev->use_count > 0); + + list::iterator it = + find(dev->aios.begin(), dev->aios.end(), audio_io); + assert(it != dev->aios.end()); + dev->aios.erase(it); + + if (--dev->use_count == 0) + { + // The device designator isn't available here, so we have + // to iterate through the map to find the device instance. + map::iterator it; + for (it = devices.begin(); it != devices.end(); ++it) + { + if (it->second == dev) + { + devices.erase(it); + break; + } + } + delete dev; + } +} /* AudioDevice::unregisterAudioIO */ + + +bool AudioDevice::open(Mode mode) +{ + if (mode == current_mode) // Same mode => do nothing + { + return true; + } + + if (mode == MODE_NONE) // Same as calling close + { + close(); + } + + if (current_mode == MODE_RDWR) // Already RDWR => don't have to do anything + { + return true; + } + + // Is not closed and is either read or write and we want the other + if ((current_mode != MODE_NONE) && (mode != current_mode)) + { + mode = MODE_RDWR; + } + + if (openDevice(mode)) + { + current_mode = mode; + return true; + } + + return false; + +} /* AudioDevice::open */ + + +void AudioDevice::close(void) +{ + list::iterator it; + for (it=aios.begin(); it!=aios.end(); ++it) + { + if ((*it)->mode() != AudioIO::MODE_NONE) + { + return; + } + } + + closeDevice(); + current_mode = MODE_NONE; + +} /* AudioDevice::close */ + + + +/**************************************************************************** + * + * Protected member functions + * + ****************************************************************************/ + + +AudioDevice::AudioDevice(const string& dev_name) + : dev_name(dev_name), current_mode(MODE_NONE), use_count(0) +{ + reopen_timer.setEnable(false); + reopen_timer.expired.connect( + sigc::hide(sigc::mem_fun(*this, &AudioDevice::reopenDevice))); +} /* AudioDevice::AudioDevice */ + + +AudioDevice::~AudioDevice(void) +{ +} /* AudioDevice::~AudioDevice */ + + +void AudioDevice::putBlocks(int16_t *buf, size_t frame_cnt) +{ + //printf("putBlocks: frame_cnt=%zu\n", frame_cnt); + float samples[frame_cnt]; + for (size_t ch=0; ch(buf[i * channels + ch]) / 32768.0; + } + list::iterator it; + for (it=aios.begin(); it!=aios.end(); ++it) + { + if ((*it)->channel() == ch) + { + (*it)->audioRead(samples, frame_cnt); + } + } + } +} /* AudioDevice::putBlocks */ + + +size_t AudioDevice::getBlocks(int16_t *buf, size_t block_cnt) +{ + size_t block_size = writeBlocksize(); + size_t frames_to_write = block_cnt * block_size; + memset(buf, 0, channels * frames_to_write * sizeof(*buf)); + + // Loop through all AudioIO objects and find out if they have any + // samples to write and how many. The non-flushing AudioIO object with + // the least number of samples will decide how many samples can be + // written in total. If all AudioIO objects are flushing, the AudioIO + // object with the most number of samples will decide how many samples + // get written. + list::iterator it; + bool do_flush = true; + unsigned int max_samples_in_fifo = 0; + for (it=aios.begin(); it!=aios.end(); ++it) + { + if (!(*it)->isIdle()) + { + unsigned samples_avail = (*it)->samplesAvailable(); + if (!(*it)->doFlush()) + { + do_flush = false; + if (samples_avail < frames_to_write) + { + frames_to_write = samples_avail; + } + } + + if (samples_avail > max_samples_in_fifo) + { + max_samples_in_fifo = samples_avail; + } + } + } + do_flush &= (max_samples_in_fifo <= frames_to_write); + if (max_samples_in_fifo < frames_to_write) + { + frames_to_write = max_samples_in_fifo; + } + + //printf("### frames_to_write=%d do_flush=%s fragsize=%u\n", + // frames_to_write, do_flush ? "TRUE" : "FALSE", fragsize); + + // If not flushing, make sure the number of frames to write is an even + // multiple of the frag size. + if (!do_flush) + { + frames_to_write /= block_size; + frames_to_write *= block_size; + } + + // If there are no frames to write, bail out and wait for an AudioIO + // object to provide us with some. + if (frames_to_write == 0) + { + return 0; + } + + // Fill the sample buffer with samples from the non-idle AudioIO objects. + for (it=aios.begin(); it!=aios.end(); ++it) + { + if (!(*it)->isIdle()) + { + size_t channel = (*it)->channel(); + float tmp[frames_to_write]; + int samples_read = (*it)->readSamples(tmp, frames_to_write); + assert(samples_read >= 0); + for (size_t i=0; i(samples_read); ++i) + { + int buf_pos = i * channels + channel; + float sample = 32767.0 * tmp[i] + buf[buf_pos]; + if (sample > 32767) + { + buf[buf_pos] = 32767; + } + else if (sample < -32767) + { + buf[buf_pos] = -32767; + } + else + { + buf[buf_pos] = static_cast(sample); + } + } + } + } + + // If flushing and the number of frames to write is not an even + // multiple of the frag size, round the number of frags to write + // up. The end of the buffer is already zeroed out. + if (do_flush && (frames_to_write % block_size > 0)) + { + frames_to_write /= block_size; + frames_to_write = (frames_to_write + 1) * block_size; + } + + return frames_to_write / block_size; + +} /* AudioDevice::getBlocks */ + + +void AudioDevice::setDeviceError(void) +{ + if (!reopen_timer.isEnabled()) + { + std::cerr << "*** ERROR: Audio device failed. Trying to reopen..." + << std::endl; + reopen_timer.setEnable(true); + } +} /* AudioDevice::setDeviceError */ + + +/**************************************************************************** + * + * Private member functions + * + ****************************************************************************/ + +void AudioDevice::reopenDevice(void) +{ + //std::cout << "### AudioDevice::reopenDevice" << std::endl; + closeDevice(); + if ((current_mode == MODE_NONE) || openDevice(current_mode)) + { + reopen_timer.setEnable(false); + } +} /* AudioDevice::reopenDevice */ + +/* + * This file has not been truncated + */ + diff --git a/src/async/audio/AsyncAudioDevice.h b/src/async/audio/AsyncAudioDevice.h new file mode 100644 index 0000000..3763fdf --- /dev/null +++ b/src/async/audio/AsyncAudioDevice.h @@ -0,0 +1,390 @@ +/** +@file AsyncAudioDevice.h +@brief Base class for handling audio devices +@author Tobias Blomberg / SM0SVX +@date 2009-07-18 + +\verbatim +Async - A library for programming event driven applications +Copyright (C) 2004-2025 Tobias Blomberg / SM0SVX + +This program is free software; you can redistribute it and/or modify +it under the terms of the GNU General Public License as published by +the Free Software Foundation; either version 2 of the License, or +(at your option) any later version. + +This program is distributed in the hope that it will be useful, +but WITHOUT ANY WARRANTY; without even the implied warranty of +MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +GNU General Public License for more details. + +You should have received a copy of the GNU General Public License +along with this program; if not, write to the Free Software +Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA +\endverbatim +*/ + + +#ifndef ASYNC_AUDIO_DEVICE_INCLUDED +#define ASYNC_AUDIO_DEVICE_INCLUDED + + +/**************************************************************************** + * + * System Includes + * + ****************************************************************************/ + +#include +#include + +#include +#include +#include + + +/**************************************************************************** + * + * Project Includes + * + ****************************************************************************/ + +#include + + +/**************************************************************************** + * + * Local Includes + * + ****************************************************************************/ + + + +/**************************************************************************** + * + * Forward declarations + * + ****************************************************************************/ + + + +/**************************************************************************** + * + * Namespace + * + ****************************************************************************/ + +namespace Async +{ + + +/**************************************************************************** + * + * Forward declarations of classes inside of the declared namespace + * + ****************************************************************************/ + +class AudioIO; +class FdWatch; + + +/**************************************************************************** + * + * Defines & typedefs + * + ****************************************************************************/ + + + +/**************************************************************************** + * + * Exported Global Variables + * + ****************************************************************************/ + + + +/**************************************************************************** + * + * Class definitions + * + ****************************************************************************/ + +/** +@brief Base class for handling audio devices +@author Tobias Blomberg / SM0SVX +@date 2004-03-20 + +This class implements a base class for supporting different audio devices. This +class is not intended to be used by the end user of the Async library. It is +used by the Async::AudioIO class, which is the Async API frontend for using +audio in an application. +*/ +class AudioDevice : public sigc::trackable +{ + public: + /** + * @brief The different modes to open a device in + */ + typedef enum + { + MODE_NONE, ///< No mode. The same as close + MODE_RD, ///< Read + MODE_WR, ///< Write + MODE_RDWR ///< Both read and write + } Mode; + + /** + * @brief Register an AudioIO object with the given device name + * @param dev_designator The name of the audio device + * @param audio_io A previously created AudioIO object + * @return Returns an AudioDevice object associated with the given device + * + * This function is used to register an AudioIO object with the given + * audio device. If an AudioDevice object already exist for the given + * device, it is returned. If there is no AudioDevice object for the + * given device, a new one is created. + */ + static AudioDevice *registerAudioIO(const std::string& dev_designator, + AudioIO *audio_io); + + /** + * @brief Unregister a previously registered AudioIO object + * @param audio_io A previously registered AudioIO object + */ + static void unregisterAudioIO(AudioIO *audio_io); + + /** + * @brief Set the sample rate used when doing future opens + * @param rate The sampling rate to use + * + * Use this function to set the sample rate used when opening audio + * devices. + * This is a global setting so all sound cards will be affected. Already + * opened sound cards will not be affected. + */ + static void setSampleRate(int rate) { sample_rate = rate; } + + /** + * @brief Set the blocksize used when opening audio devices + * @param size The blocksize, in samples per channel, to use + * @return Returns the blocksize actually set + * + * Use this function to set the block size used when opening audio + * devices. The block size is the size of the blocks used when reading + * and writing audio to/from the sound card. Smaller blocks give less + * delay but could cause choppy audio if the computer is too slow. + * The blocksize is set as samples per channel. For example, a blocksize + * of 256 samples at 8kHz sample rate will give a delay of 256/8000 = 32ms. + * This is a global setting so all sound cards will be affected. Already + * opened sound cards will not be affected. + */ + static void setBlocksize(size_t size) + { + block_size_hint = size; + } + + /** + * @brief Find out what the read (recording) blocksize is set to + * @return Returns the currently set blocksize in samples per channel + */ + virtual size_t readBlocksize(void) = 0; + + /** + * @brief Find out what the write (playback) blocksize is set to + * @return Returns the currently set blocksize in samples per channel + */ + virtual size_t writeBlocksize(void) = 0; + + /** + * @brief Set the buffer count used when opening audio devices + * @param count The buffer count to use + * @return Returns the buffer count actually set + * + * Use this function to set the buffer count used when opening audio + * devices. The buffer count is the maximum number of blocks the driver + * will buffer when reading and writing audio to/from the sound card. + * Lower numbers give less delay but could cause choppy audio if the + * computer is too slow. + * This is a global setting so all sound cards will be affected. Already + * opened sound cards will not be affected. + */ + static void setBlockCount(size_t count) + { + block_count_hint = (count <= 0) ? 0 : count; + } + + /** + * @brief Set the number of channels used when doing future opens + * @param channels The number of channels to use + * + * Use this function to set the number of channels used when opening audio + * devices. + * This is a global setting so all sound cards will be affected. Already + * opened sound cards will not be affected. + */ + static void setChannels(size_t channels) + { + AudioDevice::channels = channels; + } + + /** + * @brief Get the number of channels used for future opens + * @return Returns the number of channels used for future opens + */ + static size_t getChannels(void) { return channels; } + + /** + * @brief Check if the audio device has full duplex capability + * @return Returns \em true if the device has full duplex capability + * or else \em false + */ + virtual bool isFullDuplexCapable(void) = 0; + + /** + * @brief Open the audio device + * @param mode The mode to open the audio device in (See AudioIO::Mode) + * @return Returns \em true on success or else \em false + */ + bool open(Mode mode); + + /** + * @brief Close the audio device + */ + void close(void); + + /** + * @brief Get the current operating mode of this audio device + * @return Returns the current mode (See AudioIO::Mode) + */ + Mode mode(void) const { return current_mode; } + + /** + * @brief Tell the audio device handler that there are audio to be + * written in the buffer + */ + virtual void audioToWriteAvailable(void) = 0; + + /* + * @brief Tell the audio device to flush its buffers + */ + virtual void flushSamples(void) = 0; + + /** + * @brief Find out how many samples there are in the output buffer + * @return Returns the number of samples in the output buffer on + * success or -1 on failure. + * + * This function can be used to find out how many samples there are + * in the output buffer at the moment. This can for example be used + * to find out how long it will take before the output buffer has + * been flushed. + */ + virtual int samplesToWrite(void) const = 0; + + /** + * @brief Return the sample rate + * @return Returns the sample rate + */ + int sampleRate(void) const { return sample_rate; } + + /** + * @brief Return the device name + * @return Returns the device name + */ + const std::string& devName(void) const { return dev_name; } + + + protected: + static int sample_rate; + static size_t block_size_hint; + static size_t block_count_hint; + static size_t channels; + + std::string dev_name; + + /** + * @brief Constuctor + * @param dev_name The name of the device to associate this object with + */ + explicit AudioDevice(const std::string& dev_name); + + /** + * @brief Destructor + */ + virtual ~AudioDevice(void); + + /** + * @brief Open the audio device + * @param mode The mode to open the audio device in (See AudioIO::Mode) + * @return Returns \em true on success or else \em false + */ + virtual bool openDevice(Mode mode) = 0; + + /** + * @brief Close the audio device + */ + virtual void closeDevice(void) = 0; + + /** + * @brief Write samples read from audio device to upper layers + * @param buf Buffer containing frames of samples to write + * @param frame_cnt The number of frames of samples in the buffer + * + * This function is used by an audio device implementation to write audio + * samples recorded from the actual audio device to the upper software + * layers, for further processing. The number of samples is given as + * frames. A frame contains one sample per channel starting with channel 0. + * Frames are put in the buffer one after the other. Thus, the sample + * buffer should contain frame_cnt * channels samples. + */ + void putBlocks(int16_t *buf, size_t frame_cnt); + + /** + * @brief Read samples from upper layers to write to audio device + * @brief buf Buffer which will be filled with frames of samples + * @brief block_cnt The size of the buffer counted in blocks + * @return The number of blocks actually stored in the buffer + * + * This function is used by an audio device implementation to get samples + * from upper layers to write to the actual audio device. The count is + * given in blocks. The buffer must be able to store the number given in + * block_cnt. Fewer blocks may be returned if the requested block count is + * not available. The number of samples a block contain is + * ret_blocks * writeBlocksize() * channels. + */ + size_t getBlocks(int16_t *buf, size_t block_cnt); + + /** + * @brief Called by the device object to indicate an error condition + */ + void setDeviceError(void); + + private: + static const int DEFAULT_SAMPLE_RATE = INTERNAL_SAMPLE_RATE; + static const size_t DEFAULT_CHANNELS = 2; + static const size_t DEFAULT_BLOCK_COUNT_HINT = 4; + static const size_t DEFAULT_BLOCK_SIZE_HINT = 256; // Samples/channel/block + + static std::map devices; + + Mode current_mode; + size_t use_count; + std::list aios; + Async::Timer reopen_timer {1000, Async::Timer::TYPE_PERIODIC}; + + void reopenDevice(void); + +}; /* class AudioDevice */ + + +} /* namespace */ + +#endif /* ASYNC_AUDIO_DEVICE_INCLUDED */ + + + +/* + * This file has not been truncated + */ + diff --git a/src/async/audio/AsyncAudioDeviceAlsa.cpp b/src/async/audio/AsyncAudioDeviceAlsa.cpp new file mode 100644 index 0000000..52b9d45 --- /dev/null +++ b/src/async/audio/AsyncAudioDeviceAlsa.cpp @@ -0,0 +1,825 @@ +/** +@file AsyncAudioDeviceAlsa.cpp +@brief Handle Alsa audio devices +@author Steve / DH1DM, Tobias Blomberg / SM0SVX +@date 2009-07-21 + +Implements the low level interface to an Alsa audio device. + +\verbatim +Async - A library for programming event driven applications +Copyright (C) 2003-2025 Tobias Blomberg / SM0SVX + +This program is free software; you can redistribute it and/or modify +it under the terms of the GNU General Public License as published by +the Free Software Foundation; either version 2 of the License, or +(at your option) any later version. + +This program is distributed in the hope that it will be useful, +but WITHOUT ANY WARRANTY; without even the implied warranty of +MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +GNU General Public License for more details. + +You should have received a copy of the GNU General Public License +along with this program; if not, write to the Free Software +Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA +\endverbatim +*/ + + + +/**************************************************************************** + * + * System Includes + * + ****************************************************************************/ + +#include +#include +#include +#include +#include +#include + + +/**************************************************************************** + * + * Project Includes + * + ****************************************************************************/ + +#include + + +/**************************************************************************** + * + * Local Includes + * + ****************************************************************************/ + +#include "AsyncAudioDeviceAlsa.h" +#include "AsyncAudioDeviceFactory.h" + + + +/**************************************************************************** + * + * Namespaces to use + * + ****************************************************************************/ + +using namespace std; +using namespace Async; +using namespace sigc; + + + +/**************************************************************************** + * + * Defines & typedefs + * + ****************************************************************************/ + + + +/**************************************************************************** + * + * Local class definitions + * + ****************************************************************************/ + +class AudioDeviceAlsa::AlsaWatch : public sigc::trackable +{ + public: + AlsaWatch(snd_pcm_t *pcm_handle) : pcm_handle(pcm_handle) + { + int nfds = snd_pcm_poll_descriptors_count(pcm_handle); + pollfd pfds[nfds]; + snd_pcm_poll_descriptors(pcm_handle, pfds, nfds); + for (int i = 0; i < nfds; i++) + { + if (pfds[i].events & POLLOUT) + { + FdWatch *watch = new FdWatch(pfds[i].fd, FdWatch::FD_WATCH_WR); + watch->activity.connect(mem_fun(*this, &AlsaWatch::writeEvent)); + watch_list.push_back(watch); + } + if (pfds[i].events & POLLIN) + { + FdWatch *watch = new FdWatch(pfds[i].fd, FdWatch::FD_WATCH_RD); + watch->activity.connect(mem_fun(*this, &AlsaWatch::readEvent)); + watch_list.push_back(watch); + } + pfd_map[pfds[i].fd] = pfds[i]; + } + } + + ~AlsaWatch() + { + //activity.clear(); + std::list::const_iterator cii; + for(cii = watch_list.begin(); cii != watch_list.end(); ++cii) + { + delete *cii; + } + } + + void setEnabled(bool enable) + { + std::list::const_iterator cii; + for(cii = watch_list.begin(); cii != watch_list.end(); ++cii) + { + (*cii)->setEnabled(enable); + } + } + + sigc::signal activity; + + private: + std::map pfd_map; + std::list watch_list; + snd_pcm_t *pcm_handle; + + void writeEvent(FdWatch *watch) + { + pollfd pfd = pfd_map[watch->fd()]; + pfd.revents = POLLOUT; + unsigned short revents; + snd_pcm_poll_descriptors_revents(pcm_handle, &pfd, 1, &revents); + activity(watch, revents); + } + void readEvent(FdWatch *watch) + { + pollfd pfd = pfd_map[watch->fd()]; + pfd.revents = POLLIN; + unsigned short revents; + snd_pcm_poll_descriptors_revents(pcm_handle, &pfd, 1, &revents); + activity(watch, revents); + } +}; + + +/**************************************************************************** + * + * Prototypes + * + ****************************************************************************/ + + + +/**************************************************************************** + * + * Exported Global Variables + * + ****************************************************************************/ + + + + +/**************************************************************************** + * + * Local Global Variables + * + ****************************************************************************/ + +REGISTER_AUDIO_DEVICE_TYPE("alsa", AudioDeviceAlsa); + + +/**************************************************************************** + * + * Public member functions + * + ****************************************************************************/ + +AudioDeviceAlsa::AudioDeviceAlsa(const std::string& dev_name) + : AudioDevice(dev_name), play_block_size(0), play_block_count(0), + rec_block_size(0), rec_block_count(0), play_handle(0), + rec_handle(0), play_watch(0), rec_watch(0), duplex(false), + zerofill_on_underflow(true) +{ + assert(AudioDeviceAlsa_creator_registered); + + char *zerofill_str = getenv("ASYNC_AUDIO_ALSA_ZEROFILL"); + if (zerofill_str != 0) + { + istringstream(zerofill_str) >> zerofill_on_underflow; + } + + snd_pcm_t *play, *capture; + + // Open the device to check its duplex capability + if (snd_pcm_open(&play, dev_name.c_str(), SND_PCM_STREAM_PLAYBACK, 0) == 0) + { + // Further initialization is not required here, since the AudioDevice + // creator function will open the audio device again to check the assigned + // I/O parameters. + if (snd_pcm_open(&capture, dev_name.c_str(), + SND_PCM_STREAM_CAPTURE, 0) == 0) + { + snd_pcm_close(capture); + duplex = true; + } + snd_pcm_close(play); + } +} /* AudioDeviceAlsa::AudioDeviceAlsa */ + + +AudioDeviceAlsa::~AudioDeviceAlsa(void) +{ + closeDevice(); + + // FIXME: What if we have multiple Alsa devices open? + snd_config_update_free_global(); +} /* AudioDeviceAlsa::~AudioDeviceAlsa */ + + +size_t AudioDeviceAlsa::readBlocksize(void) +{ + return rec_block_size; +} /* AudioDeviceAlsa::readBlocksize */ + + +size_t AudioDeviceAlsa::writeBlocksize(void) +{ + return play_block_size; +} /* AudioDeviceAlsa::writeBlocksize */ + + +bool AudioDeviceAlsa::isFullDuplexCapable(void) +{ + return duplex; +} /* AudioDeviceAlsa::isFullDuplexCapable */ + + +void AudioDeviceAlsa::audioToWriteAvailable(void) +{ + //printf("AudioDeviceAlsa::audioToWriteAvailable\n"); + if (play_watch) + { + play_watch->setEnabled(true); + } +} /* AudioDeviceAlsa::audioToWriteAvailable */ + + +void AudioDeviceAlsa::flushSamples(void) +{ + if (play_watch) + { + play_watch->setEnabled(true); + } +} /* AudioDeviceAlsa::flushSamples */ + + +int AudioDeviceAlsa::samplesToWrite(void) const +{ + if ((mode() != MODE_WR) && (mode() != MODE_RDWR)) + { + return 0; + } + + int space_avail = snd_pcm_avail_update(play_handle); + if (space_avail < 0) + { + return 0; + } + int samples_to_write = (play_block_count * play_block_size) - space_avail; + if (samples_to_write < 0) + { + return 0; + } + return samples_to_write; + +} /* AudioDeviceAlsa::samplesToWrite */ + + + +/**************************************************************************** + * + * Protected member functions + * + ****************************************************************************/ + +bool AudioDeviceAlsa::openDevice(Mode mode) +{ + closeDevice(); + + if ((mode == MODE_WR) || (mode == MODE_RDWR)) + { + int err = snd_pcm_open(&play_handle, dev_name.c_str(), + SND_PCM_STREAM_PLAYBACK, 0); + if (err < 0) + { + cerr << "*** ERROR: Open playback audio device failed: " + << snd_strerror(err) + << endl; + return false; + } + + if (!initParams(play_handle)) + { + closeDevice(); + return false; + } + + if (!getBlockAttributes(play_handle, play_block_size, play_block_count)) + { + closeDevice(); + return false; + } + + play_watch = new AlsaWatch(play_handle); + play_watch->activity.connect( + mem_fun(*this, &AudioDeviceAlsa::writeSpaceAvailable)); + play_watch->setEnabled(true); + + if (!startPlayback(play_handle)) + { + cerr << "*** ERROR: Start playback failed" << endl; + closeDevice(); + return false; + } + } + + if ((mode == MODE_RD) || (mode == MODE_RDWR)) + { + int err = snd_pcm_open (&rec_handle, dev_name.c_str(), + SND_PCM_STREAM_CAPTURE, 0); + if (err < 0) + { + cerr << "*** ERROR: Open capture audio device failed: " + << snd_strerror(err) + << endl; + return false; + } + + if (!initParams(rec_handle)) + { + closeDevice(); + return false; + } + + if (!getBlockAttributes(rec_handle, rec_block_size, rec_block_count)) + { + closeDevice(); + return false; + } + + rec_watch = new AlsaWatch(rec_handle); + rec_watch->activity.connect( + mem_fun(*this, &AudioDeviceAlsa::audioReadHandler)); + + if (!startCapture(rec_handle)) + { + cerr << "*** ERROR: Start capture failed" << endl; + closeDevice(); + return false; + } + } + + return true; + +} /* AudioDeviceAlsa::openDevice */ + + +void AudioDeviceAlsa::closeDevice(void) +{ + if (play_handle != 0) + { + snd_pcm_close(play_handle); + play_handle = 0; + delete play_watch; + play_watch = 0; + } + + if (rec_handle != 0) + { + snd_pcm_close(rec_handle); + rec_handle = 0; + delete rec_watch; + rec_watch = 0; + } +} /* AudioDeviceAlsa::closeDevice */ + + + +/**************************************************************************** + * + * Private member functions + * + ****************************************************************************/ + + +void AudioDeviceAlsa::audioReadHandler(FdWatch *watch, unsigned short revents) +{ + //std::cout << "### AudioDeviceAlsa::audioReadHandler" << std::endl; + + assert(rec_handle != 0); + assert((mode() == MODE_RD) || (mode() == MODE_RDWR)); + + if (!(revents & POLLIN)) + { + return; + } + + const auto pcm_state = snd_pcm_state(rec_handle); + //std::cout << "### AudioDeviceAlsa::writeSpaceAvailable: pcm_state=" + // << pcm_state << std::endl; + if ((pcm_state < 0) || (pcm_state == SND_PCM_STATE_DISCONNECTED)) + { + setDeviceError(); + return; + } + + snd_pcm_sframes_t frames_avail = snd_pcm_avail_update(rec_handle); + if (frames_avail < 0) + { + if (!startCapture(rec_handle)) + { + watch->setEnabled(false); + } + return; + } + + //printf("### frames_avail=%d\n", frames_avail); + + if (static_cast(frames_avail) >= rec_block_size) + { + frames_avail /= rec_block_size; + frames_avail *= rec_block_size; + + int16_t buf[frames_avail * channels]; + memset(buf, 0, sizeof(buf)); + + const auto frames_read = snd_pcm_readi(rec_handle, buf, frames_avail); + if (frames_read < 0) + { + if (!startCapture(rec_handle)) + { + setDeviceError(); + watch->setEnabled(false); + } + return; + } + assert(frames_read <= frames_avail); + + putBlocks(buf, frames_read); + } +} /* AudioDeviceAlsa::audioReadHandler */ + + +void AudioDeviceAlsa::writeSpaceAvailable(FdWatch *watch, unsigned short revents) +{ + //printf("### AudioDeviceAlsa::writeSpaceAvailable\n"); + + assert(play_handle != 0); + assert((mode() == MODE_WR) || (mode() == MODE_RDWR)); + + if (!(revents & POLLOUT)) + { + return; + } + + const auto pcm_state = snd_pcm_state(play_handle); + //std::cout << "### AudioDeviceAlsa::writeSpaceAvailable: pcm_state=" + // << pcm_state << std::endl; + if ((pcm_state < 0) || (pcm_state == SND_PCM_STATE_DISCONNECTED)) + { + setDeviceError(); + return; + } + + while (1) + { + snd_pcm_sframes_t space_avail = snd_pcm_avail_update(play_handle); + + // Bail out if there's an error + if (space_avail < 0) + { + if (!startPlayback(play_handle)) + { + setDeviceError(); + watch->setEnabled(false); + return; + } + continue; + } + + size_t blocks_to_read = static_cast(space_avail) / play_block_size; + if (blocks_to_read == 0) + { + //printf("No free blocks available in sound card buffer\n"); + return; + } + + int16_t buf[space_avail * channels]; + + int blocks_avail = getBlocks(buf, blocks_to_read); + if (blocks_avail == 0) + { + if (zerofill_on_underflow) + { + blocks_avail = 1; + memset(buf, 0, blocks_avail * play_block_size); + } + else + { + watch->setEnabled(false); + return; + } + } + + int frames_to_write = blocks_avail * play_block_size; + int frames_written = snd_pcm_writei(play_handle, buf, frames_to_write); + //printf("frames_avail=%d blocks_avail=%d blocks_gotten=%d " + // "frames_written=%d\n", (int)frames_avail, blocks_avail, + // blocks_gotten, (int)frames_written); + if (frames_written < 0) + { + if (!startPlayback(play_handle)) + { + setDeviceError(); + watch->setEnabled(false); + return; + } + continue; + } + + if (frames_written != frames_to_write) + { + cerr << "*** WARNING: Number of frames written to sound device " + << devName() + << " (" << frames_written << ") differs from what was expected (" + << frames_to_write << "). Audio was probably lost.\n"; + return; + } + + if (frames_to_write != space_avail) + { + return; + } + } +} /* AudioDeviceAlsa::writeSpaceAvailable */ + + +bool AudioDeviceAlsa::initParams(snd_pcm_t *pcm_handle) +{ + snd_pcm_hw_params_t* hw_params = nullptr; + + int err = snd_pcm_hw_params_malloc (&hw_params); + if (err < 0) + { + cerr << "*** ERROR: Allocate hardware parameter structure failed: " + << snd_strerror(err) + << endl; + return false; + } + + err = snd_pcm_hw_params_any (pcm_handle, hw_params); + if (err < 0) + { + cerr << "*** ERROR: Initialize hardware parameter structure failed: " + << snd_strerror(err) + << endl; + snd_pcm_hw_params_free (hw_params); + return false; + } + + err = snd_pcm_hw_params_set_access(pcm_handle, hw_params, + SND_PCM_ACCESS_RW_INTERLEAVED); + if (err < 0) + { + cerr << "*** ERROR: Set access type failed: " + << snd_strerror(err) + << endl; + snd_pcm_hw_params_free (hw_params); + return false; + } + + err = snd_pcm_hw_params_set_format(pcm_handle, hw_params, + SND_PCM_FORMAT_S16_LE); + if (err < 0) + { + cerr << "*** ERROR: Set sample format failed: " + << snd_strerror(err) + << endl; + snd_pcm_hw_params_free (hw_params); + return false; + } + + unsigned int real_rate = sample_rate; + err = snd_pcm_hw_params_set_rate_near(pcm_handle, hw_params, &real_rate, 0); + if (err < 0) + { + cerr << "*** ERROR: Set sample rate failed: " + << snd_strerror(err) + << endl; + snd_pcm_hw_params_free (hw_params); + return false; + } + + if (::abs(static_cast(real_rate) - sample_rate) > 100) + { + cerr << "*** ERROR: The sample rate could not be set to " + << sample_rate << "Hz for ALSA device \"" << dev_name << "\". " + << "The closest rate returned by the driver was " + << real_rate << "Hz." + << endl; + snd_pcm_hw_params_free(hw_params); + return false; + } + + err = snd_pcm_hw_params_set_channels(pcm_handle, hw_params, channels); + if (err < 0) + { + cerr << "*** ERROR: Set channel count failed: " + << snd_strerror(err) + << endl; + snd_pcm_hw_params_free (hw_params); + return false; + } + + snd_pcm_uframes_t period_size = block_size_hint; + err = snd_pcm_hw_params_set_period_size_near(pcm_handle, hw_params, + &period_size, 0); + if (err < 0) + { + cerr << "*** ERROR: Set period size failed: " + << snd_strerror(err) + << endl; + snd_pcm_hw_params_free (hw_params); + return false; + } + + snd_pcm_uframes_t buffer_size = block_count_hint * block_size_hint; + err = snd_pcm_hw_params_set_buffer_size_near(pcm_handle, hw_params, + &buffer_size); + if (err < 0) + { + cerr << "*** ERROR: Set buffer size failed: " + << snd_strerror(err) + << endl; + snd_pcm_hw_params_free (hw_params); + return false; + } + + err = snd_pcm_hw_params(pcm_handle, hw_params); + if (err < 0) + { + cerr << "*** ERROR: Set hardware parameters failed: " + << snd_strerror(err) + << endl; + snd_pcm_hw_params_free (hw_params); + return false; + } + + snd_pcm_uframes_t ret_period_size, ret_buffer_size; + snd_pcm_hw_params_get_period_size(hw_params, &ret_period_size, 0); + snd_pcm_hw_params_get_buffer_size(hw_params, &ret_buffer_size); + + snd_pcm_hw_params_free(hw_params); + + + snd_pcm_sw_params_t *sw_params; + + err = snd_pcm_sw_params_malloc(&sw_params); + if (err < 0) + { + cerr << "*** ERROR: Allocate software parameter structure failed: " + << snd_strerror(err) + << endl; + return false; + } + + err = snd_pcm_sw_params_current(pcm_handle, sw_params); + if (err < 0) + { + cerr << "*** ERROR: Initialize software parameter structure failed: " + << snd_strerror(err) + << endl; + snd_pcm_sw_params_free (sw_params); + return false; + } + + err = snd_pcm_sw_params_set_start_threshold(pcm_handle, sw_params, + (ret_buffer_size / ret_period_size - 1) * ret_period_size); + if (err < 0) + { + cerr << "*** ERROR: Set start threshold failed: " + << snd_strerror(err) + << endl; + snd_pcm_sw_params_free (sw_params); + return false; + } + + err = snd_pcm_sw_params_set_avail_min(pcm_handle, sw_params, ret_period_size); + if (err < 0) + { + cerr << "*** ERROR: Set min_avail threshold failed: " + << snd_strerror(err) + << endl; + snd_pcm_sw_params_free(sw_params); + return false; + } + + err = snd_pcm_sw_params(pcm_handle, sw_params); + if (err < 0) + { + cerr << "*** ERROR: Set software parameters failed: " + << snd_strerror(err) + << endl; + snd_pcm_sw_params_free (sw_params); + return false; + } + + snd_pcm_sw_params_free(sw_params); + + return true; +} /* AudioDeviceAlsa::initParams */ + + +bool AudioDeviceAlsa::getBlockAttributes(snd_pcm_t *pcm_handle, + size_t &block_size, + size_t &block_count) +{ + snd_pcm_hw_params_t *hw_params; + int err = snd_pcm_hw_params_malloc (&hw_params); + if (err < 0) + { + cerr << "*** ERROR: Allocate hardware parameter structure failed: " + << snd_strerror(err) + << endl; + return false; + } + err = snd_pcm_hw_params_current(pcm_handle, hw_params); + if (err < 0) + { + cerr << "*** ERROR: Failed to read current hardware params: " + << snd_strerror(err) + << endl; + return false; + } + snd_pcm_uframes_t ret_period_size, ret_buffer_size; + err = snd_pcm_hw_params_get_period_size(hw_params, &ret_period_size, 0); + if (err < 0) + { + cerr << "*** ERROR: Failed to get period size: " + << snd_strerror(err) + << endl; + return false; + } + err = snd_pcm_hw_params_get_buffer_size(hw_params, &ret_buffer_size); + if (err < 0) + { + cerr << "*** ERROR: Failed to get buffer size: " + << snd_strerror(err) + << endl; + return false; + } + snd_pcm_hw_params_free(hw_params); + block_size = ret_period_size; + block_count = ret_buffer_size / ret_period_size; + return true; +} /* AudioDeviceAlsa::getBlockAttributes */ + + +bool AudioDeviceAlsa::startPlayback(snd_pcm_t *pcm_handle) +{ + //std::cout << "### AudioDeviceAlsa::startPlayback" << std::endl; + int err = snd_pcm_prepare(pcm_handle); + if (err < 0) + { + cerr << "*** ERROR: snd_pcm_prepare failed (unrecoverable error): " + << snd_strerror(err) + << endl; + return false; + } + return true; +} /* AudioDeviceAlsa::startPlayback */ + + +bool AudioDeviceAlsa::startCapture(snd_pcm_t *pcm_handle) +{ + //std::cout << "### AudioDeviceAlsa::startCapture" << std::endl; + int err = snd_pcm_prepare(pcm_handle); + if (err < 0) + { + cerr << "*** ERROR: snd_pcm_prepare failed (unrecoverable error): " + << snd_strerror(err) + << endl; + return false; + } + err = snd_pcm_start(pcm_handle); + if (err < 0) + { + cerr << "*** ERROR: snd_pcm_start failed (unrecoverable error): " + << snd_strerror(err) + << endl; + return false; + } + return true; +} /* AudioDeviceAlsa::startCapture */ + + +/* + * This file has not been truncated + */ diff --git a/src/async/audio/AsyncAudioDeviceAlsa.h b/src/async/audio/AsyncAudioDeviceAlsa.h new file mode 100644 index 0000000..fc539f0 --- /dev/null +++ b/src/async/audio/AsyncAudioDeviceAlsa.h @@ -0,0 +1,223 @@ +/** +@file AsyncAudioDeviceAlsa.h +@brief Handle Alsa audio devices +@author Tobias Blomberg / SM0SVX +@date 2009-07-21 + +Implements the low level interface to an Alsa audio device. + +\verbatim +Async - A library for programming event driven applications +Copyright (C) 2003-2019 Tobias Blomberg / SM0SVX + +This program is free software; you can redistribute it and/or modify +it under the terms of the GNU General Public License as published by +the Free Software Foundation; either version 2 of the License, or +(at your option) any later version. + +This program is distributed in the hope that it will be useful, +but WITHOUT ANY WARRANTY; without even the implied warranty of +MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +GNU General Public License for more details. + +You should have received a copy of the GNU General Public License +along with this program; if not, write to the Free Software +Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA +\endverbatim +*/ + +#ifndef ASYNC_AUDIO_DEVICE_ALSA_INCLUDED +#define ASYNC_AUDIO_DEVICE_ALSA_INCLUDED + + +/**************************************************************************** + * + * System Includes + * + ****************************************************************************/ + +#include + + +/**************************************************************************** + * + * Project Includes + * + ****************************************************************************/ + + + +/**************************************************************************** + * + * Local Includes + * + ****************************************************************************/ + +#include "AsyncAudioDevice.h" + + +/**************************************************************************** + * + * Forward declarations + * + ****************************************************************************/ + + + +/**************************************************************************** + * + * Namespace + * + ****************************************************************************/ + +namespace Async +{ + + +/**************************************************************************** + * + * Forward declarations of classes inside of the declared namespace + * + ****************************************************************************/ + + + +/**************************************************************************** + * + * Defines & typedefs + * + ****************************************************************************/ + + + +/**************************************************************************** + * + * Exported Global Variables + * + ****************************************************************************/ + + + +/**************************************************************************** + * + * Class definitions + * + ****************************************************************************/ + +/** +@brief Implements the low level interface to an Alsa audio device +@author Tobias Blomberg / SM0SVX +@date 2009-07-21 + +This class implements the low level interface to an Alsa audio device. This +class is not intended to be used by the end user of the Async library. It is +used by the Async::AudioIO class, which is the Async API frontend for using +audio in an application. +*/ +class AudioDeviceAlsa : public AudioDevice +{ + public: + /** + * @brief Constuctor + * @param dev_name The name of the Alsa PCM to associate this object with + */ + explicit AudioDeviceAlsa(const std::string& dev_name); + + /** + * @brief Destructor + */ + ~AudioDeviceAlsa(void); + + /** + * @brief Find out what the read (recording) blocksize is set to + * @return Returns the currently set blocksize in samples per channel + */ + virtual size_t readBlocksize(void); + + /** + * @brief Find out what the write (playback) blocksize is set to + * @return Returns the currently set blocksize in samples per channel + */ + virtual size_t writeBlocksize(void); + + /** + * @brief Check if the audio device has full duplex capability + * @return Returns \em true if the device has full duplex capability + * or else \em false + */ + virtual bool isFullDuplexCapable(void); + + /** + * @brief Tell the audio device handler that there are audio to be + * written in the buffer + */ + virtual void audioToWriteAvailable(void); + + /** + * @brief Tell the audio device to flush its buffers + */ + virtual void flushSamples(void); + + /** + * @brief Find out how many samples there are in the output buffer + * @return Returns the number of samples in the output buffer on + * success or -1 on failure. + * + * This function can be used to find out how many samples there are + * in the output buffer at the moment. This can for example be used + * to find out how long it will take before the output buffer has + * been flushed. + */ + virtual int samplesToWrite(void) const; + + + protected: + /** + * @brief Open the audio device + * @param mode The mode to open the audio device in (See AudioIO::Mode) + * @return Returns \em true on success or else \em false + */ + virtual bool openDevice(Mode mode); + + /** + * @brief Close the audio device + */ + virtual void closeDevice(void); + + + private: + class AlsaWatch; + size_t play_block_size; + size_t play_block_count; + size_t rec_block_size; + size_t rec_block_count; + snd_pcm_t *play_handle; + snd_pcm_t *rec_handle; + AlsaWatch *play_watch; + AlsaWatch *rec_watch; + bool duplex; + bool zerofill_on_underflow; + + AudioDeviceAlsa(const AudioDeviceAlsa&); + AudioDeviceAlsa& operator=(const AudioDeviceAlsa&); + void audioReadHandler(FdWatch *watch, unsigned short revents); + void writeSpaceAvailable(FdWatch *watch, unsigned short revents); + bool initParams(snd_pcm_t *pcm_handle); + bool getBlockAttributes(snd_pcm_t *pcm_handle, size_t &block_size, + size_t &period_size); + bool startPlayback(snd_pcm_t *pcm_handle); + bool startCapture(snd_pcm_t *pcm_handle); + +}; /* class AudioDeviceAlsa */ + + +} /* namespace */ + +#endif /* ASYNC_AUDIO_DEVICE_ALSA_INCLUDED */ + + + +/* + * This file has not been truncated + */ + diff --git a/src/async/audio/AsyncAudioDeviceFactory.cpp b/src/async/audio/AsyncAudioDeviceFactory.cpp new file mode 100644 index 0000000..afe7ebe --- /dev/null +++ b/src/async/audio/AsyncAudioDeviceFactory.cpp @@ -0,0 +1,187 @@ +/** +@file AsyncAudioDeviceFactory.cpp +@brief A class for handling audio device types +@author Tobias Blomberg / SM0SVX +@date 2009-12-26 + +\verbatim +Async - A library for programming event driven applications +Copyright (C) 2003-2013 Tobias Blomberg / SM0SVX + +This program is free software; you can redistribute it and/or modify +it under the terms of the GNU General Public License as published by +the Free Software Foundation; either version 2 of the License, or +(at your option) any later version. + +This program is distributed in the hope that it will be useful, +but WITHOUT ANY WARRANTY; without even the implied warranty of +MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +GNU General Public License for more details. + +You should have received a copy of the GNU General Public License +along with this program; if not, write to the Free Software +Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA +\endverbatim +*/ + + + +/**************************************************************************** + * + * System Includes + * + ****************************************************************************/ + +#include + + +/**************************************************************************** + * + * Project Includes + * + ****************************************************************************/ + + + +/**************************************************************************** + * + * Local Includes + * + ****************************************************************************/ + +#include "AsyncAudioDeviceFactory.h" + + + +/**************************************************************************** + * + * Namespaces to use + * + ****************************************************************************/ + +using namespace std; +using namespace Async; + + + +/**************************************************************************** + * + * Defines & typedefs + * + ****************************************************************************/ + + + +/**************************************************************************** + * + * Local class definitions + * + ****************************************************************************/ + + + +/**************************************************************************** + * + * Prototypes + * + ****************************************************************************/ + + + +/**************************************************************************** + * + * Exported Global Variables + * + ****************************************************************************/ + + + +/**************************************************************************** + * + * Local Global Variables + * + ****************************************************************************/ + + + +/**************************************************************************** + * + * Public static functions + * + ****************************************************************************/ + + + +/**************************************************************************** + * + * Public member functions + * + ****************************************************************************/ + +AudioDeviceFactory::~AudioDeviceFactory(void) +{ +} /* AudioDeviceFactory::~AudioDeviceFactory */ + + +bool AudioDeviceFactory::registerCreator(const std::string &name, + CreatorFunc creator) +{ + creator_map[name] = creator; + return true; +} /* AudioDeviceFactory::registerCreator */ + + +AudioDevice *AudioDeviceFactory::create(const std::string &name, + const std::string &dev_name) +{ + CreatorMap::iterator it = creator_map.find(name); + if (it == creator_map.end()) + { + return 0; + } + return it->second(dev_name); +} /* AudioDeviceFactory::create */ + + +std::string AudioDeviceFactory::validDevTypes(void) const +{ + string type_list; + CreatorMap::const_iterator it; + for (it = creator_map.begin(); it != creator_map.end(); ++it) + { + if (!type_list.empty()) + { + type_list += " "; + } + type_list += it->first; + } + return type_list; +} /* AudioDeviceFactory::validDevTypes */ + + + +/**************************************************************************** + * + * Protected member functions + * + ****************************************************************************/ + +AudioDeviceFactory::AudioDeviceFactory(void) +{ +} /* AudioDeviceFactory::AudioDeviceFactory */ + + + + +/**************************************************************************** + * + * Private member functions + * + ****************************************************************************/ + + + +/* + * This file has not been truncated + */ + diff --git a/src/async/audio/AsyncAudioDeviceFactory.h b/src/async/audio/AsyncAudioDeviceFactory.h new file mode 100644 index 0000000..f580c69 --- /dev/null +++ b/src/async/audio/AsyncAudioDeviceFactory.h @@ -0,0 +1,197 @@ +/** +@file AsyncAudioDeviceFactory.h +@brief A class for handling audio device types +@author Tobias Blomberg / SM0SVX +@date 2009-12-26 + +\verbatim +Async - A library for programming event driven applications +Copyright (C) 2003-2013 Tobias Blomberg / SM0SVX + +This program is free software; you can redistribute it and/or modify +it under the terms of the GNU General Public License as published by +the Free Software Foundation; either version 2 of the License, or +(at your option) any later version. + +This program is distributed in the hope that it will be useful, +but WITHOUT ANY WARRANTY; without even the implied warranty of +MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +GNU General Public License for more details. + +You should have received a copy of the GNU General Public License +along with this program; if not, write to the Free Software +Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA +\endverbatim +*/ + + +#ifndef ASYNC_AUDIO_DEVICE_FACTORY_INCLUDED +#define ASYNC_AUDIO_DEVICE_FACTORY_INCLUDED + + +/**************************************************************************** + * + * System Includes + * + ****************************************************************************/ + +#include +#include + + +/**************************************************************************** + * + * Project Includes + * + ****************************************************************************/ + + + +/**************************************************************************** + * + * Local Includes + * + ****************************************************************************/ + + + +/**************************************************************************** + * + * Forward declarations + * + ****************************************************************************/ + + + +/**************************************************************************** + * + * Namespace + * + ****************************************************************************/ + +namespace Async +{ + + +/**************************************************************************** + * + * Forward declarations of classes inside of the declared namespace + * + ****************************************************************************/ + +class AudioDevice; + + +/**************************************************************************** + * + * Defines & typedefs + * + ****************************************************************************/ + +/** + * @brief Register a new audio device type + * @param _name The name of the audio device type + * @param _class The name of the class that handle the new audio device type + */ +#define REGISTER_AUDIO_DEVICE_TYPE(_name, _class) \ + AudioDevice *create_ ## _class(const std::string& dev_name) \ + { return new _class(dev_name); } \ + static bool _class ## _creator_registered = \ + AudioDeviceFactory::instance().registerCreator(_name, \ + create_ ## _class) + + + +/**************************************************************************** + * + * Exported Global Variables + * + ****************************************************************************/ + + + +/**************************************************************************** + * + * Class definitions + * + ****************************************************************************/ + +/** +@brief A factory class for audio devices +@author Tobias Blomberg / SM0SVX +@date 2009-12-26 + +This class is a factory class for creating audio devices. Use the +REGISTER_AUDIO_DEVICE_TYPE macro to register a new audio device type. +New audio device instances are created using the create method. +*/ +class AudioDeviceFactory +{ + public: + typedef AudioDevice* (*CreatorFunc)(const std::string &dev_designator); + + /** + * @brief Get the factory singleton instance + * @return Returns the factory instance + */ + static AudioDeviceFactory &instance(void) + { + static AudioDeviceFactory the_factory; + return the_factory; + } + + /** + * @brief Destructor + */ + ~AudioDeviceFactory(void); + + /** + * @brief Register a new audio device type + * @param name The name of the audio device type (e.g. alsa, oss etc) + * @param creator A function that create the AudioDevice object + * @return Return \em true on success or else \em false + */ + bool registerCreator(const std::string &name, CreatorFunc creator); + + /** + * @brief Create a new instance of the specified audio device type + * @param name The audio device type (e.g. alsa, oss etc) + * @param dev_name The audio device name (e.g. plughw:0, /dev/dsp etc) + * @return Returns an AudioDevice object + */ + AudioDevice *create(const std::string &name, const std::string &dev_name); + + /** + * @brief List valid device types + * @return Returns a space separated list of valid device type names + */ + std::string validDevTypes(void) const; + + protected: + /** + * @brief Default constuctor + */ + AudioDeviceFactory(void); + + + private: + typedef std::map CreatorMap; + + CreatorMap creator_map; + + AudioDeviceFactory(const AudioDeviceFactory&); + AudioDeviceFactory& operator=(const AudioDeviceFactory&); + +}; /* class AudioDeviceFactory */ + + +} /* namespace */ + +#endif /* ASYNC_AUDIO_DEVICE_FACTORY_INCLUDED */ + + + +/* + * This file has not been truncated + */ + diff --git a/src/async/audio/AsyncAudioDeviceOSS.cpp b/src/async/audio/AsyncAudioDeviceOSS.cpp new file mode 100644 index 0000000..8f56168 --- /dev/null +++ b/src/async/audio/AsyncAudioDeviceOSS.cpp @@ -0,0 +1,502 @@ +/** +@file AsyncAudioDeviceOSS.cpp +@brief Handle OSS audio devices +@author Tobias Blomberg +@date 2004-03-20 + +This file contains an implementation of a class that handles OSS audio +devices. + +\verbatim +Async - A library for programming event driven applications +Copyright (C) 2003-2009 Tobias Blomberg / SM0SVX + +This program is free software; you can redistribute it and/or modify +it under the terms of the GNU General Public License as published by +the Free Software Foundation; either version 2 of the License, or +(at your option) any later version. + +This program is distributed in the hope that it will be useful, +but WITHOUT ANY WARRANTY; without even the implied warranty of +MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +GNU General Public License for more details. + +You should have received a copy of the GNU General Public License +along with this program; if not, write to the Free Software +Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA +\endverbatim +*/ + + + +/**************************************************************************** + * + * System Includes + * + ****************************************************************************/ + +#include +#include +#include +#include +#include +#include +#include + +#include +#include +#include +#include +#include + + + +/**************************************************************************** + * + * Project Includes + * + ****************************************************************************/ + +#include + + +/**************************************************************************** + * + * Local Includes + * + ****************************************************************************/ + +#include "AsyncAudioDeviceOSS.h" +#include "AsyncAudioDeviceFactory.h" + + + +/**************************************************************************** + * + * Namespaces to use + * + ****************************************************************************/ + +using namespace std; +using namespace Async; + + +/**************************************************************************** + * + * Defines & typedefs + * + ****************************************************************************/ + + + +/**************************************************************************** + * + * Local class definitions + * + ****************************************************************************/ + + + +/**************************************************************************** + * + * Prototypes + * + ****************************************************************************/ + + + +/**************************************************************************** + * + * Exported Global Variables + * + ****************************************************************************/ + + + + +/**************************************************************************** + * + * Local Global Variables + * + ****************************************************************************/ + +REGISTER_AUDIO_DEVICE_TYPE("oss", AudioDeviceOSS); + + + +/**************************************************************************** + * + * Public member functions + * + ****************************************************************************/ + +size_t AudioDeviceOSS::readBlocksize(void) +{ + assert(fd != -1); + return frag_size / (channels * sizeof(int16_t)); +} /* AudioDeviceOSS::readBlocksize */ + + +size_t AudioDeviceOSS::writeBlocksize(void) +{ + assert(fd != -1); + return frag_size / (channels * sizeof(int16_t)); +} /* AudioDeviceOSS::writeBlocksize */ + + +bool AudioDeviceOSS::isFullDuplexCapable(void) +{ + return (device_caps & DSP_CAP_DUPLEX); +} /* AudioDeviceOSS::isFullDuplexCapable */ + + +void AudioDeviceOSS::audioToWriteAvailable(void) +{ + write_watch->setEnabled(true); +} /* AudioDeviceOSS::audioToWriteAvailable */ + + +void AudioDeviceOSS::flushSamples(void) +{ + if (write_watch != 0) + { + write_watch->setEnabled(true); + } +} /* AudioDeviceOSS::flushSamples */ + + +int AudioDeviceOSS::samplesToWrite(void) const +{ + if ((mode() != MODE_WR) && (mode() != MODE_RDWR)) + { + return 0; + } + + audio_buf_info info; + if (ioctl(fd, SNDCTL_DSP_GETOSPACE, &info) == -1) + { + perror("SNDCTL_DSP_GETOSPACE ioctl failed"); + return -1; + } + + return (info.fragsize * (info.fragstotal - info.fragments)) / + (sizeof(int16_t) * channels); + +} /* AudioDeviceOSS::samplesToWrite */ + + + +/**************************************************************************** + * + * Protected member functions + * + ****************************************************************************/ + + +AudioDeviceOSS::AudioDeviceOSS(const string& dev_name) + : AudioDevice(dev_name), fd(-1), read_watch(0), write_watch(0), + device_caps(0), use_trigger(false), frag_size(0) +{ + assert(AudioDeviceOSS_creator_registered); + + char *use_trigger_str = getenv("ASYNC_AUDIO_NOTRIGGER"); + use_trigger = (use_trigger_str != 0) && (atoi(use_trigger_str) == 0); + + // Open the device to check its capabilities + int f = ::open(dev_name.c_str(), O_RDWR); + if (f >= 0) + { + if (ioctl(fd, SNDCTL_DSP_SETDUPLEX, 0) == -1) + { + perror("SNDCTL_DSP_SETDUPLEX ioctl failed"); + } + if (ioctl(fd, SNDCTL_DSP_GETCAPS, &device_caps) == -1) + { + perror("SNDCTL_DSP_GETCAPS ioctl failed"); + } + ::close(f); + } +} /* AudioDeviceOSS::AudioDeviceOSS */ + + +AudioDeviceOSS::~AudioDeviceOSS(void) +{ +} /* AudioDeviceOSS::~AudioDeviceOSS */ + + +bool AudioDeviceOSS::openDevice(Mode mode) +{ + int arg; + + if (fd != -1) + { + closeDevice(); + } + + int flags = 0; // = O_NONBLOCK; // Not supported according to the OSS docs + switch (mode) + { + case MODE_RD: + flags |= O_RDONLY; + break; + case MODE_WR: + flags |= O_WRONLY; + break; + case MODE_RDWR: + flags |= O_RDWR; + break; + case MODE_NONE: + return true; + } + + fd = ::open(dev_name.c_str(), flags); + if(fd < 0) + { + perror("open audio device failed"); + return false; + } + + if (mode == MODE_RDWR) + { + if (ioctl(fd, SNDCTL_DSP_SETDUPLEX, 0) == -1) + { + perror("SNDCTL_DSP_SETDUPLEX ioctl failed"); + close(); + return false; + } + } + + if (ioctl(fd, SNDCTL_DSP_GETCAPS, &device_caps) == -1) + { + perror("SNDCTL_DSP_GETCAPS ioctl failed"); + close(); + return false; + } + + if (use_trigger && (device_caps & DSP_CAP_TRIGGER)) + { + arg = ~(PCM_ENABLE_OUTPUT | PCM_ENABLE_INPUT); + if(ioctl(fd, SNDCTL_DSP_SETTRIGGER, &arg) == -1) + { + perror("SNDCTL_DSP_SETTRIGGER ioctl failed"); + close(); + return false; + } + } + + size_t size = (block_size_hint == 0) ? 1 : + block_size_hint * channels * sizeof(int16_t); + int frag_size_log2 = static_cast(log2(size)); + arg = static_cast((block_count_hint << 16) | frag_size_log2); + if (ioctl(fd, SNDCTL_DSP_SETFRAGMENT, &arg) == -1) + { + perror("SNDCTL_DSP_SETFRAGMENT ioctl failed"); + close(); + return false; + } + + arg = AFMT_S16_NE; + if(ioctl(fd, SNDCTL_DSP_SETFMT, &arg) == -1) + { + perror("SNDCTL_DSP_SETFMT ioctl failed"); + close(); + return false; + } + if (arg != AFMT_S16_NE) + { + fprintf(stderr, "*** error: The sound device does not support 16 bit " + "signed samples\n"); + close(); + return false; + } + + arg = static_cast(channels); + if(ioctl(fd, SNDCTL_DSP_CHANNELS, &arg) == -1) + { + perror("SNDCTL_DSP_CHANNELS ioctl failed"); + close(); + return false; + } + if(arg != static_cast(channels)) + { + fprintf(stderr, "*** error: Unable to set number of channels to %zu. The " + "driver suggested %d channels\n", + channels, arg); + close(); + return false; + } + + arg = sample_rate; + if(ioctl(fd, SNDCTL_DSP_SPEED, &arg) == -1) + { + perror("SNDCTL_DSP_SPEED ioctl failed"); + close(); + return false; + } + if (abs(arg - sample_rate) > 100) + { + fprintf(stderr, "*** error: The sampling rate could not be set to %dHz " + "for OSS device %s. The closest rate returned by the " + "driver was %dHz\n", + sample_rate, dev_name.c_str(), arg); + close(); + return false; + } + + arg = 0; + if ((mode == MODE_RD) || (mode == MODE_RDWR)) + { + read_watch = new FdWatch(fd, FdWatch::FD_WATCH_RD); + assert(read_watch != 0); + read_watch->activity.connect( + mem_fun(*this, &AudioDeviceOSS::audioReadHandler)); + arg |= PCM_ENABLE_INPUT; + } + + if ((mode == MODE_WR) || (mode == MODE_RDWR)) + { + write_watch = new FdWatch(fd, FdWatch::FD_WATCH_WR); + assert(write_watch != 0); + write_watch->activity.connect( + mem_fun(*this, &AudioDeviceOSS::writeSpaceAvailable)); + arg |= PCM_ENABLE_OUTPUT; + } + + if (use_trigger && (device_caps & DSP_CAP_TRIGGER)) + { + if(ioctl(fd, SNDCTL_DSP_SETTRIGGER, &arg) == -1) + { + perror("SNDCTL_DSP_SETTRIGGER ioctl failed"); + close(); + return false; + } + } + + arg = 0; + if (ioctl(fd, SNDCTL_DSP_GETBLKSIZE, &arg) == -1) + { + perror("SNDCTL_DSP_GETBLKSIZE ioctl failed"); + close(); + return false; + } + assert(arg >= 0); + frag_size = static_cast(arg); + + return true; + +} /* AudioDeviceOSS::openDevice */ + + +void AudioDeviceOSS::closeDevice(void) +{ + frag_size = 0; + + delete write_watch; + write_watch = 0; + + delete read_watch; + read_watch = 0; + + if (fd != -1) + { + ::close(fd); + fd = -1; + } +} /* AudioDeviceOSS::closeDevice */ + + + +/**************************************************************************** + * + * Private member functions + * + ****************************************************************************/ + + +void AudioDeviceOSS::audioReadHandler(FdWatch *watch) +{ + audio_buf_info info; + if (ioctl(fd, SNDCTL_DSP_GETISPACE, &info) == -1) + { + perror("SNDCTL_DSP_GETISPACE ioctl failed"); + return; + } + + if (info.fragments > 0) + { + int frags_to_read = info.fragments; + int bytes_to_read = frags_to_read * info.fragsize; + int16_t buf[bytes_to_read / sizeof(int16_t)]; + int cnt = read(fd, buf, bytes_to_read); + if (cnt == -1) + { + perror("read in AudioDeviceOSS::audioReadHandler"); + return; + } + assert(cnt == bytes_to_read); + + int frame_cnt = bytes_to_read / (channels * sizeof(int16_t)); + //printf("frags_to_read=%d bytes_to_read=%d frame_cnt=%d\n", frags_to_read, bytes_to_read, frame_cnt); + putBlocks(buf, frame_cnt); + } +} /* AudioDeviceOSS::audioReadHandler */ + + +void AudioDeviceOSS::writeSpaceAvailable(FdWatch *watch) +{ + assert(fd >= 0); + assert((mode() == MODE_WR) || (mode() == MODE_RDWR)); + + audio_buf_info info; + //unsigned fragsize; // The frag (block) size in frames + unsigned fragments; + size_t frags_read; + do + { + // Find out how many frags we can write to the sound card + if (ioctl(fd, SNDCTL_DSP_GETOSPACE, &info) == -1) + { + perror("SNDCTL_DSP_GETOSPACE ioctl failed"); + return; + } + fragments = info.fragments; + //fragsize = info.fragsize / (sizeof(int16_t) * channels); + + // Bail out if there's no free frags + if (fragments == 0) + { + break; + } + + int16_t buf[32768]; + frags_read = getBlocks(buf, fragments); + //printf("fragments=%u frags_read=%zu\n", fragments, frags_read); + if (frags_read == 0) + { + watch->setEnabled(false); + return; + } + + // Write the samples to the sound card + //printf("Writing %zu fragments\n", frags_read); + int written = ::write(fd, buf, frags_read * frag_size); + if (written < 0) + { + perror("write in AudioIO::write"); + return; + } + + assert(static_cast(written) == frags_read * frag_size); + + } while(frags_read == fragments); + + watch->setEnabled(true); + +} /* AudioDeviceOSS::writeSpaceAvailable */ + + + +/* + * This file has not been truncated + */ + diff --git a/src/async/audio/AsyncAudioDeviceOSS.h b/src/async/audio/AsyncAudioDeviceOSS.h new file mode 100644 index 0000000..76b5947 --- /dev/null +++ b/src/async/audio/AsyncAudioDeviceOSS.h @@ -0,0 +1,215 @@ +/** +@file AsyncAudioDevice.h +@brief Handle OSS audio devices +@author Tobias Blomberg / SM0SVX +@date 2004-03-20 + +Implements the low level interface to an OSS audio device. + +\verbatim +Async - A library for programming event driven applications +Copyright (C) 2004 Tobias Blomberg / SM0SVX + +This program is free software; you can redistribute it and/or modify +it under the terms of the GNU General Public License as published by +the Free Software Foundation; either version 2 of the License, or +(at your option) any later version. + +This program is distributed in the hope that it will be useful, +but WITHOUT ANY WARRANTY; without even the implied warranty of +MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +GNU General Public License for more details. + +You should have received a copy of the GNU General Public License +along with this program; if not, write to the Free Software +Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA +\endverbatim +*/ + + +#ifndef ASYNC_AUDIO_DEVICE_OSS_INCLUDED +#define ASYNC_AUDIO_DEVICE_OSS_INCLUDED + + +/**************************************************************************** + * + * System Includes + * + ****************************************************************************/ + +#include + + +/**************************************************************************** + * + * Project Includes + * + ****************************************************************************/ + + + +/**************************************************************************** + * + * Local Includes + * + ****************************************************************************/ + +#include "AsyncAudioDevice.h" + + +/**************************************************************************** + * + * Forward declarations + * + ****************************************************************************/ + + + +/**************************************************************************** + * + * Namespace + * + ****************************************************************************/ + +namespace Async +{ + + +/**************************************************************************** + * + * Forward declarations of classes inside of the declared namespace + * + ****************************************************************************/ + +class FdWatch; + + +/**************************************************************************** + * + * Defines & typedefs + * + ****************************************************************************/ + + + +/**************************************************************************** + * + * Exported Global Variables + * + ****************************************************************************/ + + + +/**************************************************************************** + * + * Class definitions + * + ****************************************************************************/ + +/** +@brief A class that implements the low level interface to an OSS audio device +@author Tobias Blomberg / SM0SVX +@date 2004-03-20 + +This class implements the low level interface to an OSS audio device. This +class is not intended to be used by the end user of the Async library. It is +used by the Async::AudioIO class, which is the Async API frontend for using +audio in an application. +*/ +class AudioDeviceOSS : public Async::AudioDevice +{ + public: + /** + * @brief Constuctor + * @param dev_name The name of the device to associate this object with + */ + explicit AudioDeviceOSS(const std::string& dev_name); + + /** + * @brief Destructor + */ + ~AudioDeviceOSS(void); + + /** + * @brief Find out what the read (recording) blocksize is set to + * @return Returns the currently set blocksize in samples per channel + */ + virtual size_t readBlocksize(void); + + /** + * @brief Find out what the write (playback) blocksize is set to + * @return Returns the currently set blocksize in samples per channel + */ + virtual size_t writeBlocksize(void); + + /** + * @brief Check if the audio device has full duplex capability + * @return Returns \em true if the device has full duplex capability + * or else \em false + */ + virtual bool isFullDuplexCapable(void); + + /** + * @brief Tell the audio device handler that there are audio to be + * written in the buffer + */ + virtual void audioToWriteAvailable(void); + + /** + * @brief Tell the audio device to flush its buffers + */ + virtual void flushSamples(void); + + /** + * @brief Find out how many samples there are in the output buffer + * @return Returns the number of samples in the output buffer on + * success or -1 on failure. + * + * This function can be used to find out how many samples there are + * in the output buffer at the moment. This can for example be used + * to find out how long it will take before the output buffer has + * been flushed. + */ + virtual int samplesToWrite(void) const; + + + protected: + /** + * @brief Open the audio device + * @param mode The mode to open the audio device in (See AudioIO::Mode) + * @return Returns \em true on success or else \em false + */ + virtual bool openDevice(Mode mode); + + /** + * @brief Close the audio device + */ + virtual void closeDevice(void); + + + private: + //static const int BUF_FRAG_COUNT = 4; + + int fd; + FdWatch *read_watch; + FdWatch *write_watch; + int device_caps; + bool use_trigger; + size_t frag_size; + + void audioReadHandler(FdWatch *watch); + void writeSpaceAvailable(FdWatch *watch); + +}; /* class AudioDevice */ + + +} /* namespace */ + +#endif /* ASYNC_AUDIO_DEVICE_OSS_INCLUDED */ + + + +/* + * This file has not been truncated + */ + diff --git a/src/async/audio/AsyncAudioDeviceUDP.cpp b/src/async/audio/AsyncAudioDeviceUDP.cpp new file mode 100644 index 0000000..14a1f8c --- /dev/null +++ b/src/async/audio/AsyncAudioDeviceUDP.cpp @@ -0,0 +1,387 @@ +/** +@file AsyncAudioDeviceUDP.cpp +@brief Handle simple streaming of audio samples via UDP +@author Tobias Blomberg / SM0SVX +@date 2012-06-25 + +Implements a simple "audio interface" that stream samples via +UDP. This can for example be used to stream audio to/from +GNU Radio. + +\verbatim +Async - A library for programming event driven applications +Copyright (C) 2003-2012 Tobias Blomberg / SM0SVX + +This program is free software; you can redistribute it and/or modify +it under the terms of the GNU General Public License as published by +the Free Software Foundation; either version 2 of the License, or +(at your option) any later version. + +This program is distributed in the hope that it will be useful, +but WITHOUT ANY WARRANTY; without even the implied warranty of +MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +GNU General Public License for more details. + +You should have received a copy of the GNU General Public License +along with this program; if not, write to the Free Software +Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA +\endverbatim +*/ + + + +/**************************************************************************** + * + * System Includes + * + ****************************************************************************/ + +#include + +#include +#include +#include +#include +#include +#include + + + +/**************************************************************************** + * + * Project Includes + * + ****************************************************************************/ + +#include +#include + + +/**************************************************************************** + * + * Local Includes + * + ****************************************************************************/ + +#include "AsyncAudioDeviceUDP.h" +#include "AsyncAudioDeviceFactory.h" + + + +/**************************************************************************** + * + * Namespaces to use + * + ****************************************************************************/ + +using namespace std; +using namespace Async; + + +/**************************************************************************** + * + * Defines & typedefs + * + ****************************************************************************/ + + + +/**************************************************************************** + * + * Local class definitions + * + ****************************************************************************/ + + + +/**************************************************************************** + * + * Prototypes + * + ****************************************************************************/ + + + +/**************************************************************************** + * + * Exported Global Variables + * + ****************************************************************************/ + + + + +/**************************************************************************** + * + * Local Global Variables + * + ****************************************************************************/ + +REGISTER_AUDIO_DEVICE_TYPE("udp", AudioDeviceUDP); + + + +/**************************************************************************** + * + * Public member functions + * + ****************************************************************************/ + +size_t AudioDeviceUDP::readBlocksize(void) +{ + return block_size; +} /* AudioDeviceUDP::readBlocksize */ + + +size_t AudioDeviceUDP::writeBlocksize(void) +{ + return block_size; +} /* AudioDeviceUDP::writeBlocksize */ + + +bool AudioDeviceUDP::isFullDuplexCapable(void) +{ + return true; +} /* AudioDeviceUDP::isFullDuplexCapable */ + + +void AudioDeviceUDP::audioToWriteAvailable(void) +{ + if (!pace_timer->isEnabled()) + { + audioWriteHandler(); + } +} /* AudioDeviceUDP::audioToWriteAvailable */ + + +void AudioDeviceUDP::flushSamples(void) +{ + if (!pace_timer->isEnabled()) + { + audioWriteHandler(); + } +} /* AudioDeviceUDP::flushSamples */ + + +int AudioDeviceUDP::samplesToWrite(void) const +{ + if ((mode() != MODE_WR) && (mode() != MODE_RDWR)) + { + return 0; + } + assert(sock != 0); + + int len = 0; + if (ioctl(sock->fd(), TIOCOUTQ, &len) == -1) + { + return 0; + } + + return len / sizeof(*read_buf); + +} /* AudioDeviceUDP::samplesToWrite */ + + + +/**************************************************************************** + * + * Protected member functions + * + ****************************************************************************/ + + +AudioDeviceUDP::AudioDeviceUDP(const string& dev_name) + : AudioDevice(dev_name), block_size(0), sock(0), read_buf(0), + read_buf_pos(0), port(0), zerofill_on_underflow(false) +{ + assert(AudioDeviceUDP_creator_registered); + assert(sampleRate() > 0); + size_t pace_interval = 1000 * block_size_hint / sampleRate(); + block_size = pace_interval * sampleRate() / 1000; + + read_buf = new int16_t[block_size * channels]; + pace_timer = new Timer(pace_interval, Timer::TYPE_PERIODIC); + pace_timer->setEnable(false); + pace_timer->expired.connect( + sigc::hide(mem_fun(*this, &AudioDeviceUDP::audioWriteHandler))); + + char *zerofill_str = std::getenv("ASYNC_AUDIO_UDP_ZEROFILL"); + if (zerofill_str != 0) + { + std::istringstream(zerofill_str) >> zerofill_on_underflow; + } +} /* AudioDeviceUDP::AudioDeviceUDP */ + + +AudioDeviceUDP::~AudioDeviceUDP(void) +{ + delete [] read_buf; + delete pace_timer; +} /* AudioDeviceUDP::~AudioDeviceUDP */ + + +bool AudioDeviceUDP::openDevice(Mode mode) +{ + if (sock != 0) + { + closeDevice(); + } + + const string &dev_name = devName(); + size_t colon = dev_name.find(':'); + if (colon == string::npos) + { + cerr << "*** ERROR: Illegal UDP audio device specification (" << devName() + << "). Should be udp:ip-addr:port\n"; + return false; + } + + string ip_addr_str = dev_name.substr(0, colon); + string port_str = dev_name.substr(colon+1); + if (ip_addr_str.empty() || port_str.empty()) + { + cerr << "*** ERROR: Illegal UDP audio device specification (" << devName() + << "). Should be udp:ip-addr:port\n"; + return false; + } + + ip_addr = IpAddress(ip_addr_str); + port = 0; + stringstream ss(port_str); + ss >> port; + + switch (mode) + { + case MODE_WR: + if (ip_addr.isEmpty() || (port == 0)) + { + cerr << "*** ERROR: Illegal UDP audio device specification (" + << devName() + << "). Should be udp:ip-addr:port\n"; + return false; + } + sock = new UdpSocket; + if (!sock->initOk()) + { + cerr << "*** ERROR: Could not create UDP socket for writing (" + << devName() << ")\n"; + return false; + } + break; + + case MODE_RDWR: + if (ip_addr.isEmpty()) + { + cerr << "*** ERROR: Illegal UDP audio device specification (" + << devName() + << "). Should be udp:ip-addr:port\n"; + return false; + } + // Fall through! + + case MODE_RD: + if (port == 0) + { + cerr << "*** ERROR: Illegal UDP audio device specification (" + << devName() + << "). Should be udp:ip-addr:port\n"; + return false; + } + sock = new UdpSocket(port, ip_addr); + if (!sock->initOk()) + { + cerr << "*** ERROR: Could not bind to UDP socket (" << devName() + << ")\n"; + return false; + } + sock->dataReceived.connect( + mem_fun(*this, &AudioDeviceUDP::audioReadHandler)); + break; + + case MODE_NONE: + break; + } + + return true; + +} /* AudioDeviceUDP::openDevice */ + + +void AudioDeviceUDP::closeDevice(void) +{ + pace_timer->setEnable(false); + delete sock; + sock = 0; + ip_addr = IpAddress(); + port = 0; +} /* AudioDeviceUDP::closeDevice */ + + + +/**************************************************************************** + * + * Private member functions + * + ****************************************************************************/ + + +void AudioDeviceUDP::audioReadHandler(const IpAddress &ip, uint16_t port, + void *buf, int count) +{ + for (unsigned i=0; i < count / (channels * sizeof(int16_t)); ++i) + { + for (size_t ch=0; ch < channels; ++ch) + { + read_buf[read_buf_pos * channels + ch] = + ((int16_t *)buf)[i * channels + ch]; + } + if (++read_buf_pos == block_size) + { + putBlocks(read_buf, block_size); + read_buf_pos = 0; + } + } +} /* AudioDeviceUDP::audioReadHandler */ + + +void AudioDeviceUDP::audioWriteHandler(void) +{ + assert(sock != 0); + assert((mode() == MODE_WR) || (mode() == MODE_RDWR)); + + unsigned frags_read; + const unsigned frag_size = block_size * sizeof(int16_t) * channels; + int16_t buf[block_size * channels]; + frags_read = getBlocks(buf, 1); + if (frags_read == 0) + { + if (zerofill_on_underflow) + { + frags_read = 1; + std::memset(buf, 0, block_size * channels); + } + else + { + pace_timer->setEnable(false); + return; + } + } + + // Write the samples to the socket + if (!sock->write(ip_addr, port, (void *)buf, frag_size)) + { + perror("write in AudioDeviceUDP::write"); + pace_timer->setEnable(false); + return; + } + + pace_timer->setEnable(true); + +} /* AudioDeviceUDP::audioWriteHandler */ + + + +/* + * This file has not been truncated + */ + diff --git a/src/async/audio/AsyncAudioDeviceUDP.h b/src/async/audio/AsyncAudioDeviceUDP.h new file mode 100644 index 0000000..2d58af1 --- /dev/null +++ b/src/async/audio/AsyncAudioDeviceUDP.h @@ -0,0 +1,220 @@ +/** +@file AsyncAudioDevice.h +@brief Handle simple streaming of audio samples via UDP +@author Tobias Blomberg / SM0SVX +@date 2012-06-25 + +Implements a simple "audio interface" that stream samples via +UDP. This can for example be used to stream audio to/from +GNU Radio. + +\verbatim +Async - A library for programming event driven applications +Copyright (C) 2003-2012 Tobias Blomberg / SM0SVX + +This program is free software; you can redistribute it and/or modify +it under the terms of the GNU General Public License as published by +the Free Software Foundation; either version 2 of the License, or +(at your option) any later version. + +This program is distributed in the hope that it will be useful, +but WITHOUT ANY WARRANTY; without even the implied warranty of +MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +GNU General Public License for more details. + +You should have received a copy of the GNU General Public License +along with this program; if not, write to the Free Software +Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA +\endverbatim +*/ + + +#ifndef ASYNC_AUDIO_DEVICE_UDP_INCLUDED +#define ASYNC_AUDIO_DEVICE_UDP_INCLUDED + + +/**************************************************************************** + * + * System Includes + * + ****************************************************************************/ + +#include + + +/**************************************************************************** + * + * Project Includes + * + ****************************************************************************/ + +#include +#include + + +/**************************************************************************** + * + * Local Includes + * + ****************************************************************************/ + +#include "AsyncAudioDevice.h" + + +/**************************************************************************** + * + * Forward declarations + * + ****************************************************************************/ + + + +/**************************************************************************** + * + * Namespace + * + ****************************************************************************/ + +namespace Async +{ + + +/**************************************************************************** + * + * Forward declarations of classes inside of the declared namespace + * + ****************************************************************************/ + +class UdpSocket; +class Timer; + + +/**************************************************************************** + * + * Defines & typedefs + * + ****************************************************************************/ + + + +/**************************************************************************** + * + * Exported Global Variables + * + ****************************************************************************/ + + + +/**************************************************************************** + * + * Class definitions + * + ****************************************************************************/ + +/** +@brief +@author Tobias Blomberg / SM0SVX +@date 2012-06-25 + +Implements a simple "audio interface" that stream samples via +UDP. This can for example be used to stream audio to/from +GNU Radio. +*/ +class AudioDeviceUDP : public Async::AudioDevice +{ + public: + /** + * @brief Constuctor + * @param dev_name The name of the device to associate this object with + */ + explicit AudioDeviceUDP(const std::string& dev_name); + + /** + * @brief Destructor + */ + ~AudioDeviceUDP(void); + + /** + * @brief Find out what the read (recording) blocksize is set to + * @return Returns the currently set blocksize in samples per channel + */ + virtual size_t readBlocksize(void); + + /** + * @brief Find out what the write (playback) blocksize is set to + * @return Returns the currently set blocksize in samples per channel + */ + virtual size_t writeBlocksize(void); + + /** + * @brief Check if the audio device has full duplex capability + * @return Returns \em true if the device has full duplex capability + * or else \em false + */ + virtual bool isFullDuplexCapable(void); + + /** + * @brief Tell the audio device handler that there are audio to be + * written in the buffer + */ + virtual void audioToWriteAvailable(void); + + /** + * @brief Tell the audio device to flush its buffers + */ + virtual void flushSamples(void); + + /** + * @brief Find out how many samples there are in the output buffer + * @return Returns the number of samples in the output buffer on + * success or -1 on failure. + * + * This function can be used to find out how many samples there are + * in the output buffer at the moment. This can for example be used + * to find out how long it will take before the output buffer has + * been flushed. + */ + virtual int samplesToWrite(void) const; + + + protected: + /** + * @brief Open the audio device + * @param mode The mode to open the audio device in (See AudioIO::Mode) + * @return Returns \em true on success or else \em false + */ + virtual bool openDevice(Mode mode); + + /** + * @brief Close the audio device + */ + virtual void closeDevice(void); + + + private: + size_t block_size; + Async::UdpSocket *sock; + int16_t *read_buf; + size_t read_buf_pos; + IpAddress ip_addr; + uint16_t port; + Async::Timer *pace_timer; + bool zerofill_on_underflow; + + void audioReadHandler(const Async::IpAddress &ip, uint16_t port, + void *buf, int count); + void audioWriteHandler(void); + +}; /* class AudioDeviceUDP */ + + +} /* namespace */ + +#endif /* ASYNC_AUDIO_DEVICE_UDP_INCLUDED */ + + + +/* + * This file has not been truncated + */ + diff --git a/src/async/audio/AsyncAudioEncoder.cpp b/src/async/audio/AsyncAudioEncoder.cpp new file mode 100644 index 0000000..1b2761c --- /dev/null +++ b/src/async/audio/AsyncAudioEncoder.cpp @@ -0,0 +1,197 @@ +/** +@file AudioEncoder.cpp +@brief Base class for an audio decoder +@author Tobias Blomberg / SM0SVX +@date 2008-10-06 + +\verbatim +Async - A library for programming event driven applications +Copyright (C) 2003-2014 Tobias Blomberg / SM0SVX + +This program is free software; you can redistribute it and/or modify +it under the terms of the GNU General Public License as published by +the Free Software Foundation; either version 2 of the License, or +(at your option) any later version. + +This program is distributed in the hope that it will be useful, +but WITHOUT ANY WARRANTY; without even the implied warranty of +MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +GNU General Public License for more details. + +You should have received a copy of the GNU General Public License +along with this program; if not, write to the Free Software +Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA +\endverbatim +*/ + + + +/**************************************************************************** + * + * System Includes + * + ****************************************************************************/ + + + +/**************************************************************************** + * + * Project Includes + * + ****************************************************************************/ + + + +/**************************************************************************** + * + * Local Includes + * + ****************************************************************************/ + +#include "AsyncAudioEncoder.h" +#include "AsyncAudioEncoderDummy.h" +#include "AsyncAudioEncoderNull.h" +#include "AsyncAudioEncoderRaw.h" +#include "AsyncAudioEncoderS16.h" +#include "AsyncAudioEncoderGsm.h" +#ifdef SPEEX_MAJOR +#include "AsyncAudioEncoderSpeex.h" +#endif +#ifdef OPUS_MAJOR +#include "AsyncAudioEncoderOpus.h" +#endif + + +/**************************************************************************** + * + * Namespaces to use + * + ****************************************************************************/ + +using namespace std; +using namespace Async; + + + +/**************************************************************************** + * + * Defines & typedefs + * + ****************************************************************************/ + + + +/**************************************************************************** + * + * Local class definitions + * + ****************************************************************************/ + + + +/**************************************************************************** + * + * Prototypes + * + ****************************************************************************/ + + + +/**************************************************************************** + * + * Exported Global Variables + * + ****************************************************************************/ + + + + +/**************************************************************************** + * + * Local Global Variables + * + ****************************************************************************/ + + + +/**************************************************************************** + * + * Public member functions + * + ****************************************************************************/ + +bool AudioEncoder::isAvailable(const std::string &name) +{ + return (name == "NULL") || (name == "RAW") || (name == "S16") || + (name == "GSM") || +#ifdef SPEEX_MAJOR + (name == "SPEEX") || +#endif +#ifdef OPUS_MAJOR + (name == "OPUS") || +#endif + (name == "DUMMY"); +} /* AudioEncoder::isAvailable */ + + +AudioEncoder *AudioEncoder::create(const std::string &name) +{ + if (name == "NULL") + { + return new AudioEncoderNull; + } + else if (name == "DUMMY") + { + return new AudioEncoderDummy; + } + else if (name == "RAW") + { + return new AudioEncoderRaw; + } + else if (name == "S16") + { + return new AudioEncoderS16; + } + else if (name == "GSM") + { + return new AudioEncoderGsm; + } +#ifdef SPEEX_MAJOR + else if (name == "SPEEX") + { + return new AudioEncoderSpeex; + } +#endif +#ifdef OPUS_MAJOR + else if (name == "OPUS") + { + return new AudioEncoderOpus; + } +#endif + else + { + return 0; + } +} /* AudioEncoder::create */ + + +/**************************************************************************** + * + * Protected member functions + * + ****************************************************************************/ + + + +/**************************************************************************** + * + * Private member functions + * + ****************************************************************************/ + + + +/* + * This file has not been truncated + */ + diff --git a/src/async/audio/AsyncAudioEncoder.h b/src/async/audio/AsyncAudioEncoder.h new file mode 100644 index 0000000..66adb81 --- /dev/null +++ b/src/async/audio/AsyncAudioEncoder.h @@ -0,0 +1,202 @@ +/** +@file AsyncAudioEncoder.h +@brief Base class for an audio decoder +@author Tobias Blomberg / SM0SVX +@date 2008-10-06 + +\verbatim +Async - A library for programming event driven applications +Copyright (C) 2003-2017 Tobias Blomberg / SM0SVX + +This program is free software; you can redistribute it and/or modify +it under the terms of the GNU General Public License as published by +the Free Software Foundation; either version 2 of the License, or +(at your option) any later version. + +This program is distributed in the hope that it will be useful, +but WITHOUT ANY WARRANTY; without even the implied warranty of +MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +GNU General Public License for more details. + +You should have received a copy of the GNU General Public License +along with this program; if not, write to the Free Software +Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA +\endverbatim +*/ + +#ifndef ASYNC_AUDIO_ENCODER_INCLUDED +#define ASYNC_AUDIO_ENCODER_INCLUDED + + +/**************************************************************************** + * + * System Includes + * + ****************************************************************************/ + +#include +#include + + +/**************************************************************************** + * + * Project Includes + * + ****************************************************************************/ + +#include + + +/**************************************************************************** + * + * Local Includes + * + ****************************************************************************/ + + + +/**************************************************************************** + * + * Forward declarations + * + ****************************************************************************/ + + + +/**************************************************************************** + * + * Namespace + * + ****************************************************************************/ + +namespace Async +{ + + +/**************************************************************************** + * + * Forward declarations of classes inside of the declared namespace + * + ****************************************************************************/ + + + +/**************************************************************************** + * + * Defines & typedefs + * + ****************************************************************************/ + + + +/**************************************************************************** + * + * Exported Global Variables + * + ****************************************************************************/ + + + +/**************************************************************************** + * + * Class definitions + * + ****************************************************************************/ + +/** +@brief Base class for an audio encoder +@author Tobias Blomberg / SM0SVX +@date 2008-10-06 + +This is the base class for implementing an audio encoder. +*/ +class AudioEncoder : public AudioSink, public sigc::trackable +{ + public: + /** + * @brief Check if a specific encoder is available + * @param name The name of the encoder to look for + */ + static bool isAvailable(const std::string &name); + + /** + * @brief Create a new encoder of the specified type + * @param name The name of the encoder to create + */ + static AudioEncoder *create(const std::string &name); + + /** + * @brief Default constuctor + */ + AudioEncoder(void) {} + + /** + * @brief Destructor + */ + ~AudioEncoder(void) {} + + /** + * @brief Get the name of the codec + * @returns Return the name of the codec + */ + virtual const char *name(void) const = 0; + + /** + * @brief Set an option for the encoder + * @param name The name of the option + * @param value The value of the option + */ + virtual void setOption(const std::string &name, const std::string &value) {} + + /** + * @brief Print codec parameter settings + */ + virtual void printCodecParams(void) {} + + /** + * @brief Call this function when all encoded samples have been flushed + */ + void allEncodedSamplesFlushed(void) { sourceAllSamplesFlushed(); } + + /** + * @brief Tell the sink to flush the previously written samples + * + * This function is used to tell the sink to flush previously written + * samples. When done flushing, the sink should call the + * sourceAllSamplesFlushed function. + * This function is normally only called from a connected source object. + */ + virtual void flushSamples(void) { flushEncodedSamples(); } + + /** + * @brief A signal emitted when encoded samples are available + * @param buf Buffer containing encoded samples + * @param size The size of the buffer + */ + sigc::signal writeEncodedSamples; + + /** + * @brief This signal is emitted when the source calls flushSamples + */ + sigc::signal flushEncodedSamples; + + + protected: + + private: + AudioEncoder(const AudioEncoder&); + AudioEncoder& operator=(const AudioEncoder&); + +}; /* class AudioEncoder */ + + +} /* namespace */ + +#endif /* ASYNC_AUDIO_ENCODER_INCLUDED */ + + + +/* + * This file has not been truncated + */ + diff --git a/src/async/audio/AsyncAudioEncoderDummy.h b/src/async/audio/AsyncAudioEncoderDummy.h new file mode 100644 index 0000000..4af2179 --- /dev/null +++ b/src/async/audio/AsyncAudioEncoderDummy.h @@ -0,0 +1,177 @@ +/** +@file AsyncAudioEncoderNull.h +@brief A DUMMY audio "encoder" that just throw away audio +@author Tobias Blomberg / SM0SVX +@date 2017-10-28 + +\verbatim +Async - A library for programming event driven applications +Copyright (C) 2003-2017 Tobias Blomberg / SM0SVX + +This program is free software; you can redistribute it and/or modify +it under the terms of the GNU General Public License as published by +the Free Software Foundation; either version 2 of the License, or +(at your option) any later version. + +This program is distributed in the hope that it will be useful, +but WITHOUT ANY WARRANTY; without even the implied warranty of +MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +GNU General Public License for more details. + +You should have received a copy of the GNU General Public License +along with this program; if not, write to the Free Software +Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA +\endverbatim +*/ + +#ifndef ASYNC_AUDIO_ENCODER_DUMMY_INCLUDED +#define ASYNC_AUDIO_ENCODER_DUMMY_INCLUDED + + +/**************************************************************************** + * + * System Includes + * + ****************************************************************************/ + + + +/**************************************************************************** + * + * Project Includes + * + ****************************************************************************/ + +#include + + +/**************************************************************************** + * + * Local Includes + * + ****************************************************************************/ + + + +/**************************************************************************** + * + * Forward declarations + * + ****************************************************************************/ + + + +/**************************************************************************** + * + * Namespace + * + ****************************************************************************/ + +namespace Async +{ + + +/**************************************************************************** + * + * Forward declarations of classes inside of the declared namespace + * + ****************************************************************************/ + + + +/**************************************************************************** + * + * Defines & typedefs + * + ****************************************************************************/ + + + +/**************************************************************************** + * + * Exported Global Variables + * + ****************************************************************************/ + + + +/**************************************************************************** + * + * Class definitions + * + ****************************************************************************/ + +/** +@brief A DUMMY audio "encoder" that just throw away samples +@author Tobias Blomberg / SM0SVX +@date 2017-10-28 + +This class implements an audio "encoder" that will just throw away samples. +It may be good to use as a placeholder before a real audio encoder has been +selected. +*/ +class AudioEncoderDummy : public AudioEncoder +{ + public: + /** + * @brief Default constuctor + */ + AudioEncoderDummy(void) {} + + /** + * @brief Destructor + */ + virtual ~AudioEncoderDummy(void) {} + + /** + * @brief Get the name of the codec + * @return Return the name of the codec + */ + virtual const char *name(void) const { return "DUMMY"; } + + /** + * @brief Write samples into this audio sink + * @param samples The buffer containing the samples + * @param count The number of samples in the buffer + * @return Returns the number of samples that has been taken care of + * + * This function is used to write audio into this audio sink. If it + * returns 0, no more samples should be written until the resumeOutput + * function in the source have been called. + * This function is normally only called from a connected source object. + */ + virtual int writeSamples(const float *samples, int count) { return count; } + + /** + * @brief Call this function when all encoded samples have been flushed + * + * This DUMMY encoder will just ignore the allSamplesFlushed notification. + */ + void allEncodedSamplesFlushed(void) {} + + /** + * @brief Tell the sink to flush the previously written samples + * + * This function is used to tell the sink to flush previously written + * samples. When done flushing, the sink should call the + * sourceAllSamplesFlushed function. + * This function is normally only called from a connected source object. + */ + virtual void flushSamples(void) { sourceAllSamplesFlushed(); } + + private: + AudioEncoderDummy(const AudioEncoderDummy&); + AudioEncoderDummy& operator=(const AudioEncoderDummy&); + +}; /* class AudioEncoderDummy */ + + +} /* namespace */ + +#endif /* ASYNC_AUDIO_ENCODER_DUMMY_INCLUDED */ + + +/* + * This file has not been truncated + */ + diff --git a/src/async/audio/AsyncAudioEncoderGsm.cpp b/src/async/audio/AsyncAudioEncoderGsm.cpp new file mode 100644 index 0000000..3273528 --- /dev/null +++ b/src/async/audio/AsyncAudioEncoderGsm.cpp @@ -0,0 +1,187 @@ +/** +@file AsyncAudioEncoderGsm.cpp +@brief An audio encoder that encodes to GSM +@author Tobias Blomberg / SM0SVX +@date 2008-10-15 + +\verbatim +Async - A library for programming event driven applications +Copyright (C) 2003-2008 Tobias Blomberg / SM0SVX + +This program is free software; you can redistribute it and/or modify +it under the terms of the GNU General Public License as published by +the Free Software Foundation; either version 2 of the License, or +(at your option) any later version. + +This program is distributed in the hope that it will be useful, +but WITHOUT ANY WARRANTY; without even the implied warranty of +MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +GNU General Public License for more details. + +You should have received a copy of the GNU General Public License +along with this program; if not, write to the Free Software +Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA +\endverbatim +*/ + + + +/**************************************************************************** + * + * System Includes + * + ****************************************************************************/ + +#include + + +/**************************************************************************** + * + * Project Includes + * + ****************************************************************************/ + + + +/**************************************************************************** + * + * Local Includes + * + ****************************************************************************/ + +#include "AsyncAudioEncoderGsm.h" + + + +/**************************************************************************** + * + * Namespaces to use + * + ****************************************************************************/ + +using namespace std; +using namespace Async; + + + +/**************************************************************************** + * + * Defines & typedefs + * + ****************************************************************************/ + + + +/**************************************************************************** + * + * Local class definitions + * + ****************************************************************************/ + + + +/**************************************************************************** + * + * Prototypes + * + ****************************************************************************/ + + + +/**************************************************************************** + * + * Exported Global Variables + * + ****************************************************************************/ + + + + +/**************************************************************************** + * + * Local Global Variables + * + ****************************************************************************/ + + + +/**************************************************************************** + * + * Public member functions + * + ****************************************************************************/ + +AudioEncoderGsm::AudioEncoderGsm(void) + : gsmh(0), gsm_buf_len(0) +{ + gsmh = gsm_create(); +} /* AsyncAudioEncoderGsm::AsyncAudioEncoderGsm */ + + +AudioEncoderGsm::~AudioEncoderGsm(void) +{ + gsm_destroy(gsmh); + gsmh = 0; +} /* AsyncAudioEncoderGsm::~AsyncAudioEncoderGsm */ + + +int AudioEncoderGsm::writeSamples(const float *samples, int count) +{ + for (int i=0; i 1.0) + { + gsm_buf[gsm_buf_len++] = 32767; + } + else if (sample < -1.0) + { + gsm_buf[gsm_buf_len++] = -32767; + } + else + { + gsm_buf[gsm_buf_len++] = static_cast(sample * 32767.0); + } + + if (gsm_buf_len == GSM_BUF_SIZE) + { + gsm_buf_len = 0; + + gsm_frame frame[FRAME_COUNT]; + for (int frameno=0; frameno +} + + + +/**************************************************************************** + * + * Project Includes + * + ****************************************************************************/ + +#include + + +/**************************************************************************** + * + * Local Includes + * + ****************************************************************************/ + + + +/**************************************************************************** + * + * Forward declarations + * + ****************************************************************************/ + + + +/**************************************************************************** + * + * Namespace + * + ****************************************************************************/ + +namespace Async +{ + + +/**************************************************************************** + * + * Forward declarations of classes inside of the declared namespace + * + ****************************************************************************/ + + + +/**************************************************************************** + * + * Defines & typedefs + * + ****************************************************************************/ + + + +/**************************************************************************** + * + * Exported Global Variables + * + ****************************************************************************/ + + + +/**************************************************************************** + * + * Class definitions + * + ****************************************************************************/ + +/** +@brief An audio encoder that encodes to GSM +@author Tobias Blomberg / SM0SVX +@date 2008-10-15 + +This class implements an audio encoder that converts the native sample +format to GSM audio blocks. +*/ +class AudioEncoderGsm : public AudioEncoder +{ + public: + /** + * @brief Default constuctor + */ + AudioEncoderGsm(void); + + /** + * @brief Destructor + */ + virtual ~AudioEncoderGsm(void); + + /** + * @brief Get the name of the codec + * @returns Return the name of the codec + */ + virtual const char *name(void) const { return "GSM"; } + + /** + * @brief A_brief_member_function_description + * @param param1 Description_of_param1 + * @return Return_value_of_this_member_function + */ + + + /** + * @brief Write samples into this audio sink + * @param samples The buffer containing the samples + * @param count The number of samples in the buffer + * @return Returns the number of samples that has been taken care of + * + * This function is used to write audio into this audio sink. If it + * returns 0, no more samples should be written until the resumeOutput + * function in the source have been called. + * This function is normally only called from a connected source object. + */ + virtual int writeSamples(const float *samples, int count); + + + protected: + + private: + static const int FRAME_SAMPLE_CNT = 160; + static const int FRAME_COUNT = 4; + static const int GSM_BUF_SIZE = FRAME_COUNT * FRAME_SAMPLE_CNT; + + gsm gsmh; + gsm_signal gsm_buf[GSM_BUF_SIZE]; + int gsm_buf_len; + + AudioEncoderGsm(const AudioEncoderGsm&); + AudioEncoderGsm& operator=(const AudioEncoderGsm&); + +}; /* class AudioEncoderGsm */ + + +} /* namespace */ + +#endif /* ASYNC_AUDIO_ENCODER_GSM_INCLUDED */ + + + +/* + * This file has not been truncated + */ + diff --git a/src/async/audio/AsyncAudioEncoderNull.h b/src/async/audio/AsyncAudioEncoderNull.h new file mode 100644 index 0000000..45837e7 --- /dev/null +++ b/src/async/audio/AsyncAudioEncoderNull.h @@ -0,0 +1,181 @@ +/** +@file AsyncAudioEncoderNull.h +@brief A null audio "encoder" that just encode silence +@author Tobias Blomberg / SM0SVX +@date 2014-05-05 + +\verbatim +Async - A library for programming event driven applications +Copyright (C) 2003-2015 Tobias Blomberg / SM0SVX + +This program is free software; you can redistribute it and/or modify +it under the terms of the GNU General Public License as published by +the Free Software Foundation; either version 2 of the License, or +(at your option) any later version. + +This program is distributed in the hope that it will be useful, +but WITHOUT ANY WARRANTY; without even the implied warranty of +MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +GNU General Public License for more details. + +You should have received a copy of the GNU General Public License +along with this program; if not, write to the Free Software +Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA +\endverbatim +*/ + +#ifndef ASYNC_AUDIO_ENCODER_NULL_INCLUDED +#define ASYNC_AUDIO_ENCODER_NULL_INCLUDED + + +/**************************************************************************** + * + * System Includes + * + ****************************************************************************/ + +#include +#include + + +/**************************************************************************** + * + * Project Includes + * + ****************************************************************************/ + +#include + + +/**************************************************************************** + * + * Local Includes + * + ****************************************************************************/ + + + +/**************************************************************************** + * + * Forward declarations + * + ****************************************************************************/ + + + +/**************************************************************************** + * + * Namespace + * + ****************************************************************************/ + +namespace Async +{ + + +/**************************************************************************** + * + * Forward declarations of classes inside of the declared namespace + * + ****************************************************************************/ + + + +/**************************************************************************** + * + * Defines & typedefs + * + ****************************************************************************/ + + + +/**************************************************************************** + * + * Exported Global Variables + * + ****************************************************************************/ + + + +/**************************************************************************** + * + * Class definitions + * + ****************************************************************************/ + +/** +@brief A null audio "encoder" that just communicate zero samples +@author Tobias Blomberg / SM0SVX +@date 2014-05-05 + +This class implements an audio "encoder" that will just produce zero samples. +The only thing transmitted by the encoder is the number of samples in the +block but no real samples are encoded. The NULL codec may be of use when the +real audio is communicated through another path. +*/ +class AudioEncoderNull : public AudioEncoder +{ + public: + /** + * @brief Default constuctor + */ + AudioEncoderNull(void) {} + + /** + * @brief Destructor + */ + virtual ~AudioEncoderNull(void) {} + + /** + * @brief Get the name of the codec + * @return Return the name of the codec + */ + virtual const char *name(void) const { return "NULL"; } + + /** + * @brief Write samples into this audio sink + * @param samples The buffer containing the samples + * @param count The number of samples in the buffer + * @return Returns the number of samples that has been taken care of + * + * This function is used to write audio into this audio sink. If it + * returns 0, no more samples should be written until the resumeOutput + * function in the source have been called. + * This function is normally only called from a connected source object. + */ + virtual int writeSamples(const float *samples, int count) + { + if (count > std::numeric_limits::max()) + { + count = std::numeric_limits::max(); + } + else if (count < 0) + { + return -1; + } + uint8_t buf[2]; + buf[0] = static_cast(count & 0xff); + buf[1] = static_cast(count >> 8); + writeEncodedSamples(buf, sizeof(buf)); + return count; + } + + protected: + + private: + AudioEncoderNull(const AudioEncoderNull&); + AudioEncoderNull& operator=(const AudioEncoderNull&); + +}; /* class AudioEncoderNull */ + + +} /* namespace */ + +#endif /* ASYNC_AUDIO_ENCODER_NULL_INCLUDED */ + + + +/* + * This file has not been truncated + */ + diff --git a/src/async/audio/AsyncAudioEncoderOpus.cpp b/src/async/audio/AsyncAudioEncoderOpus.cpp new file mode 100644 index 0000000..73fc953 --- /dev/null +++ b/src/async/audio/AsyncAudioEncoderOpus.cpp @@ -0,0 +1,683 @@ +/** +@file AsyncAudioEncoderOpus.cpp +@brief An audio encoder that encodes samples using the Opus codec +@author Tobias Blomberg / SM0SVX +@date 2013-10-12 + +\verbatim +Async - A library for programming event driven applications +Copyright (C) 2003-2013 Tobias Blomberg / SM0SVX + +This program is free software; you can redistribute it and/or modify +it under the terms of the GNU General Public License as published by +the Free Software Foundation; either version 2 of the License, or +(at your option) any later version. + +This program is distributed in the hope that it will be useful, +but WITHOUT ANY WARRANTY; without even the implied warranty of +MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +GNU General Public License for more details. + +You should have received a copy of the GNU General Public License +along with this program; if not, write to the Free Software +Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA +\endverbatim +*/ + + + +/**************************************************************************** + * + * System Includes + * + ****************************************************************************/ + +#include +#include +#include +#include + + +/**************************************************************************** + * + * Project Includes + * + ****************************************************************************/ + + + +/**************************************************************************** + * + * Local Includes + * + ****************************************************************************/ + +#include "AsyncAudioEncoderOpus.h" + + + +/**************************************************************************** + * + * Namespaces to use + * + ****************************************************************************/ + +using namespace std; +using namespace Async; + + + +/**************************************************************************** + * + * Defines & typedefs + * + ****************************************************************************/ + + + +/**************************************************************************** + * + * Local class definitions + * + ****************************************************************************/ + + + +/**************************************************************************** + * + * Prototypes + * + ****************************************************************************/ + + + +/**************************************************************************** + * + * Exported Global Variables + * + ****************************************************************************/ + + + + +/**************************************************************************** + * + * Local Global Variables + * + ****************************************************************************/ + + + +/**************************************************************************** + * + * Public member functions + * + ****************************************************************************/ + +AudioEncoderOpus::AudioEncoderOpus(void) + : enc(0), frame_size(0), sample_buf(0), buf_len(0) +{ + int error; + enc = opus_encoder_create(INTERNAL_SAMPLE_RATE, 1, OPUS_APPLICATION_AUDIO, + &error); + if (error != OPUS_OK) + { + cerr << "*** ERROR: Opus encoder error: " << opus_strerror(error) << endl; + exit(1); + } + + setFrameSize(20); + setBitrate(20000); + enableVbr(true); + setMaxBandwidth(OPUS_BANDWIDTH_MEDIUMBAND); + setBandwidth(OPUS_AUTO); + setSignalType(OPUS_SIGNAL_VOICE); + enableDtx(false); +#if OPUS_MAJOR > 0 + setLsbDepth(16); +#endif + +} /* AsyncAudioEncoderOpus::AsyncAudioEncoderOpus */ + + +AudioEncoderOpus::~AudioEncoderOpus(void) +{ + delete [] sample_buf; + opus_encoder_destroy(enc); +} /* AsyncAudioEncoderOpus::~AsyncAudioEncoderOpus */ + + +void AudioEncoderOpus::setOption(const std::string &name, + const std::string &value) +{ +#if 0 + if (name == "FRAMES_PER_PACKET") + { + setFramesPerPacket(atoi(value.c_str())); + } +#endif + if (name == "FRAME_SIZE") + { + stringstream ss(value); + float frame_size; + if (ss >> frame_size) + { + setFrameSize(frame_size); + } + } + else if (name == "COMPLEXITY") + { + setComplexity(atoi(value.c_str())); + } + else if (name == "BITRATE") + { + setBitrate(atoi(value.c_str())); + } + else if (name == "VBR") + { + enableVbr(atoi(value.c_str()) != 0); + } + else if (name == "CVBR") + { + enableConstrainedVbr(atoi(value.c_str()) != 0); + } + else + { + cerr << "*** WARNING AudioEncoderOpus: Unknown option \"" + << name << "\". Ignoring it.\n"; + } +} /* AudioEncoderOpus::setOption */ + + +void AudioEncoderOpus::printCodecParams(void) +{ + cout << "------ Opus encoder parameters ------\n"; + cout << "Frame size = " << frameSize() << endl; + cout << "Complexity = " << complexity() << endl; + cout << "Bitrate = " << bitrate() << endl; + cout << "VBR = " + << (vbrEnabled() ? "YES" : "NO") << endl; + cout << "Constrained VBR = " + << (constrainedVbrEnabled() ? "YES" : "NO") << endl; + cout << "Maximum audio bw = " << bandwidthStr(maxBandwidth()) << endl; + cout << "Audio bw = " << bandwidthStr(bandwidth()) << endl; + cout << "Signal type = " << signalTypeStr(signalType()) << endl; + cout << "Application type = " + << applicationTypeStr(applicationType()) << endl; + cout << "Inband FEC = " + << (inbandFecEnabled() ? "YES" : "NO") << endl; + cout << "Expected Packet Loss = " << expectedPacketLoss() << "%\n"; + cout << "DTX = " << (dtxEnabled() ? "YES" : "NO") << endl; +#if OPUS_MAJOR > 0 + cout << "LSB depth = " << lsbDepth() << endl; +#endif + cout << "--------------------------------------\n"; +} /* AudioEncoderOpus::printCodecParams */ + + +float AudioEncoderOpus::setFrameSize(float new_frame_size_ms) +{ + // The frame size may be 2.5, 5, 10, 20, 40 or 60 ms + frame_size = + static_cast(new_frame_size_ms * INTERNAL_SAMPLE_RATE / 1000); + delete [] sample_buf; + sample_buf = new float[frame_size]; + return new_frame_size_ms; +} /* AudioEncoderOpus::setFrameSize */ + + +opus_int32 AudioEncoderOpus::setComplexity(opus_int32 new_comp) +{ + int err = opus_encoder_ctl(enc, OPUS_SET_COMPLEXITY(new_comp)); + if (err != OPUS_OK) + { + cerr << "*** ERROR: Could not set Opus encoder complexity: " + << opus_strerror(err) << endl; + } + return complexity(); +} /* AudioEncoderOpus::setBitrate */ + + +opus_int32 AudioEncoderOpus::complexity(void) +{ + opus_int32 comp; + // coverity[ptr_arith] + int err = opus_encoder_ctl(enc, OPUS_GET_COMPLEXITY(&comp)); + if (err != OPUS_OK) + { + cerr << "*** ERROR: Could not get Opus encoder complexity: " + << opus_strerror(err) << endl; + return -1; + } + return comp; +} /* AudioEncoderOpus::complexity */ + + +opus_int32 AudioEncoderOpus::setBitrate(opus_int32 new_bitrate) +{ + int err = opus_encoder_ctl(enc, OPUS_SET_BITRATE(new_bitrate)); + if (err != OPUS_OK) + { + cerr << "*** ERROR: Could not set Opus encoder bitrate: " + << opus_strerror(err) << endl; + } + return bitrate(); +} /* AudioEncoderOpus::setBitrate */ + + +opus_int32 AudioEncoderOpus::bitrate(void) +{ + opus_int32 br; + // coverity[ptr_arith] + int err = opus_encoder_ctl(enc, OPUS_GET_BITRATE(&br)); + if (err != OPUS_OK) + { + cerr << "*** ERROR: Could not get Opus encoder bitrate: " + << opus_strerror(err) << endl; + return -1; + } + return br; +} /* AudioEncoderOpus::bitrate */ + + +bool AudioEncoderOpus::enableVbr(bool enable) +{ + opus_int32 do_enable = enable ? 1 : 0; + int err = opus_encoder_ctl(enc, OPUS_SET_VBR(do_enable)); + if (err != OPUS_OK) + { + cerr << "*** ERROR: Could set Opus encoder VBR: " + << opus_strerror(err) << endl; + } + return vbrEnabled(); +} /* AudioEncoderOpus::enableVbr */ + + +bool AudioEncoderOpus::vbrEnabled(void) +{ + opus_int32 enabled; + // coverity[ptr_arith] + int err = opus_encoder_ctl(enc, OPUS_GET_VBR(&enabled)); + if (err != OPUS_OK) + { + cerr << "*** ERROR: Could not get Opus encoder VBR: " + << opus_strerror(err) << endl; + return false; + } + return (enabled != 0); +} /* AudioEncoderOpus::vbrEnabled */ + + +bool AudioEncoderOpus::enableConstrainedVbr(bool enable) +{ + opus_int32 do_enable = enable ? 1 : 0; + int err = opus_encoder_ctl(enc, OPUS_SET_VBR_CONSTRAINT(do_enable)); + if (err != OPUS_OK) + { + cerr << "*** ERROR: Could not set Opus encoder constrained VBR: " + << opus_strerror(err) << endl; + } + return constrainedVbrEnabled(); +} /* AudioEncoderOpus::enableConstrainedVbr */ + + +bool AudioEncoderOpus::constrainedVbrEnabled(void) +{ + opus_int32 enabled; + // coverity[ptr_arith] + int err = opus_encoder_ctl(enc, OPUS_GET_VBR_CONSTRAINT(&enabled)); + if (err != OPUS_OK) + { + cerr << "*** ERROR: Could not get Opus encoder constrained VBR: " + << opus_strerror(err) << endl; + return false; + } + return (enabled != 0); +} /* AudioEncoderOpus::constrainedVbrEnabled */ + + +opus_int32 AudioEncoderOpus::setMaxBandwidth(opus_int32 new_bw) +{ + int err = opus_encoder_ctl(enc, OPUS_SET_MAX_BANDWIDTH(new_bw)); + if (err != OPUS_OK) + { + cerr << "*** ERROR: Could not set Opus encoder max bandwidth: " + << opus_strerror(err) << endl; + } + return maxBandwidth(); +} /* AudioEncoderOpus::setMaxBandwidth */ + + +opus_int32 AudioEncoderOpus::maxBandwidth(void) +{ + opus_int32 bw; + // coverity[ptr_arith] + int err = opus_encoder_ctl(enc, OPUS_GET_MAX_BANDWIDTH(&bw)); + if (err != OPUS_OK) + { + cerr << "*** ERROR: Could not get Opus encoder max bandwidth: " + << opus_strerror(err) << endl; + return -1; + } + return bw; +} /* AudioEncoderOpus::maxBandwidth */ + + +opus_int32 AudioEncoderOpus::setBandwidth(opus_int32 new_bw) +{ + int err = opus_encoder_ctl(enc, OPUS_SET_BANDWIDTH(new_bw)); + if (err != OPUS_OK) + { + cerr << "*** ERROR: Could not set Opus encoder bandwidth: " + << opus_strerror(err) << endl; + } + return bandwidth(); +} /* AudioEncoderOpus::setBandwidth */ + + +opus_int32 AudioEncoderOpus::bandwidth(void) +{ + opus_int32 bw; + // coverity[ptr_arith] + int err = opus_encoder_ctl(enc, OPUS_GET_BANDWIDTH(&bw)); + if (err != OPUS_OK) + { + cerr << "*** ERROR: Could not get Opus encoder bandwidth: " + << opus_strerror(err) << endl; + return -1; + } + return bw; +} /* AudioEncoderOpus::bandwidth */ + + +opus_int32 AudioEncoderOpus::setSignalType(opus_int32 new_type) +{ + int err = opus_encoder_ctl(enc, OPUS_SET_SIGNAL(new_type)); + if (err != OPUS_OK) + { + cerr << "*** ERROR: Could not set Opus encoder signal type: " + << opus_strerror(err) << endl; + } + return signalType(); +} /* AudioEncoderOpus::setSignalType */ + + +opus_int32 AudioEncoderOpus::signalType(void) +{ + opus_int32 type; + // coverity[ptr_arith] + int err = opus_encoder_ctl(enc, OPUS_GET_SIGNAL(&type)); + if (err != OPUS_OK) + { + cerr << "*** ERROR: Could not get Opus encoder signal type: " + << opus_strerror(err) << endl; + return -1; + } + return type; +} /* AudioEncoderOpus::signalType */ + + +opus_int32 AudioEncoderOpus::setApplicationType(opus_int32 new_app) +{ + int err = opus_encoder_ctl(enc, OPUS_SET_APPLICATION(new_app)); + if (err != OPUS_OK) + { + cerr << "*** ERROR: Could not set Opus encoder application type: " + << opus_strerror(err) << endl; + } + return applicationType(); +} /* AudioEncoderOpus::setApplicationType */ + + +opus_int32 AudioEncoderOpus::applicationType(void) +{ + opus_int32 app; + // coverity[ptr_arith] + int err = opus_encoder_ctl(enc, OPUS_GET_APPLICATION(&app)); + if (err != OPUS_OK) + { + cerr << "*** ERROR: Could not get Opus encoder application type: " + << opus_strerror(err) << endl; + return -1; + } + return app; +} /* AudioEncoderOpus::applicationType */ + + +bool AudioEncoderOpus::enableInbandFec(bool enable) +{ + opus_int32 do_enable = enable ? 1 : 0; + int err = opus_encoder_ctl(enc, OPUS_SET_INBAND_FEC(do_enable)); + if (err != OPUS_OK) + { + cerr << "*** ERROR: Could not set Opus encoder inband FEC: " + << opus_strerror(err) << endl; + } + return inbandFecEnabled(); +} /* AudioEncoderOpus::enableInbandFec */ + + +bool AudioEncoderOpus::inbandFecEnabled(void) +{ + opus_int32 enabled; + // coverity[ptr_arith] + int err = opus_encoder_ctl(enc, OPUS_GET_INBAND_FEC(&enabled)); + if (err != OPUS_OK) + { + cerr << "*** ERROR: Could not get Opus encoder inband FEC: " + << opus_strerror(err) << endl; + return false; + } + return (enabled != 0); +} /* AudioEncoderOpus::inbandFecEnabled */ + + +opus_int32 AudioEncoderOpus::setExpectedPacketLoss(opus_int32 new_pl_perc) +{ + int err = opus_encoder_ctl(enc, OPUS_SET_PACKET_LOSS_PERC(new_pl_perc)); + if (err != OPUS_OK) + { + cerr << "*** ERROR: Could not set Opus encoder expected packet loss: " + << opus_strerror(err) << endl; + } + return expectedPacketLoss(); +} /* AudioEncoderOpus::setExpectedPacketLoss */ + + +opus_int32 AudioEncoderOpus::expectedPacketLoss(void) +{ + opus_int32 pl_perc; + // coverity[ptr_arith] + int err = opus_encoder_ctl(enc, OPUS_GET_PACKET_LOSS_PERC(&pl_perc)); + if (err != OPUS_OK) + { + cerr << "*** ERROR: Could not get Opus encoder estimated packet loss: " + << opus_strerror(err) << endl; + return -1; + } + return pl_perc; +} /* AudioEncoderOpus::expectedPacketLoss */ + + +bool AudioEncoderOpus::enableDtx(bool enable) +{ + opus_int32 do_enable = enable ? 1 : 0; + int err = opus_encoder_ctl(enc, OPUS_SET_DTX(do_enable)); + if (err != OPUS_OK) + { + cerr << "*** ERROR: Could not set Opus encoder DTX: " + << opus_strerror(err) << endl; + } + return dtxEnabled(); +} /* AudioEncoderOpus::enableDtx */ + + +bool AudioEncoderOpus::dtxEnabled(void) +{ + opus_int32 enabled; + // coverity[ptr_arith] + int err = opus_encoder_ctl(enc, OPUS_GET_DTX(&enabled)); + if (err != OPUS_OK) + { + cerr << "*** ERROR: Could not get Opus encoder DTX: " + << opus_strerror(err) << endl; + return false; + } + return (enabled != 0); +} /* AudioEncoderOpus::dtxEnabled */ + + +#if OPUS_MAJOR > 0 +opus_int32 AudioEncoderOpus::setLsbDepth(opus_int32 new_depth) +{ + int err = opus_encoder_ctl(enc, OPUS_SET_LSB_DEPTH(new_depth)); + if (err != OPUS_OK) + { + cerr << "*** ERROR: Could not set Opus encoder LSB depth: " + << opus_strerror(err) << endl; + } + return lsbDepth(); +} /* AudioEncoderOpus::setLsbDepth */ + + +opus_int32 AudioEncoderOpus::lsbDepth(void) +{ + opus_int32 depth; + // coverity[ptr_arith] + int err = opus_encoder_ctl(enc, OPUS_GET_LSB_DEPTH(&depth)); + if (err != OPUS_OK) + { + cerr << "*** ERROR: Could not get Opus encoder LSB depth: " + << opus_strerror(err) << endl; + return -1; + } + return depth; +} /* AudioEncoderOpus::lsbDepth */ +#endif + + +void AudioEncoderOpus::reset(void) +{ + int err = opus_encoder_ctl(enc, OPUS_RESET_STATE); + if (err != OPUS_OK) + { + cerr << "*** ERROR: Could not reset Opus encoder: " + << opus_strerror(err) << endl; + } +} /* AudioEncoderOpus::reset */ + + +#if 0 +void AudioEncoderOpus::setFramesPerPacket(unsigned fpp) +{ + frames_per_packet = fpp; +} /* AudioEncoderOpus::setFramesPerPacket */ +#endif + + +const char *AudioEncoderOpus::bandwidthStr(opus_int32 bw) +{ + switch (bw) + { + case OPUS_AUTO: + return "AUTO"; + case OPUS_BANDWIDTH_NARROWBAND: + return "NARROWBAND"; + case OPUS_BANDWIDTH_MEDIUMBAND: + return "MEDIUMBAND"; + case OPUS_BANDWIDTH_WIDEBAND: + return "WIDEBAND"; + case OPUS_BANDWIDTH_SUPERWIDEBAND: + return "SUPERWIDEBAND"; + case OPUS_BANDWIDTH_FULLBAND: + return "FULLBAND"; + default: + return "?"; + } +} /* AudioEncoderOpus::bandwidthStr */ + + +const char *AudioEncoderOpus::signalTypeStr(opus_int32 type) +{ + switch (type) + { + case OPUS_AUTO: + return "AUTO"; + case OPUS_SIGNAL_VOICE: + return "VOICE"; + case OPUS_SIGNAL_MUSIC: + return "MUSIC"; + default: + return "?"; + } +} /* AudioEncoderOpus::signalTypeStr */ + + +const char *AudioEncoderOpus::applicationTypeStr(opus_int32 type) +{ + switch (type) + { + case OPUS_APPLICATION_VOIP: + return "VOIP"; + case OPUS_APPLICATION_AUDIO: + return "AUDIO"; + case OPUS_APPLICATION_RESTRICTED_LOWDELAY: + return "RESTRICTED_LOWDELAY"; + default: + return "?"; + } +} /* AudioEncoderOpus::applicationTypeStr */ + + +int AudioEncoderOpus::writeSamples(const float *samples, int count) +{ + for (int i=0; i + + +/**************************************************************************** + * + * Project Includes + * + ****************************************************************************/ + +#include + + +/**************************************************************************** + * + * Local Includes + * + ****************************************************************************/ + + + +/**************************************************************************** + * + * Forward declarations + * + ****************************************************************************/ + + + +/**************************************************************************** + * + * Namespace + * + ****************************************************************************/ + +namespace Async +{ + + +/**************************************************************************** + * + * Forward declarations of classes inside of the declared namespace + * + ****************************************************************************/ + + + +/**************************************************************************** + * + * Defines & typedefs + * + ****************************************************************************/ + + + +/**************************************************************************** + * + * Exported Global Variables + * + ****************************************************************************/ + + + +/**************************************************************************** + * + * Class definitions + * + ****************************************************************************/ + +/** +@brief An audio encoder that encodes samples using the Opus codec +@author Tobias Blomberg / SM0SVX +@date 2013-10-12 + +This class implements an audio encoder that use the Opus audio codec. +*/ +class AudioEncoderOpus : public AudioEncoder +{ + public: + /** + * @brief Default constuctor + */ + AudioEncoderOpus(void); + + /** + * @brief Destructor + */ + virtual ~AudioEncoderOpus(void); + + /** + * @brief Get the name of the codec + * @returns Return the name of the codec + */ + virtual const char *name(void) const { return "OPUS"; } + + /** + * @brief Set the size of each encoded frame + * @param new_frame_size_ms The new frame size in milliseconds + * @returns Returns the new fram size + * + * Use this function to set the size of each encoded frame. + * Valid values for the frame size are 2.5, 5, 10, 20, 40 or 60 + * milliseconds. + */ + float setFrameSize(float new_frame_size_ms); + + /** + * @brief Get the current frame size + * @returns Returns the current frame size + */ + int frameSize(void) const { return frame_size; } + + /** + * @brief Set an option for the encoder + * @param name The name of the option + * @param value The value of the option + */ + virtual void setOption(const std::string &name, const std::string &value); + + /** + * @brief Print codec parameter settings + */ + virtual void printCodecParams(void); + + /** + * @brief Set the complexity to use + * @param new_comp The new complexity value to set (0-10) + * @returns The new complexity value + * + * Use this function to set the complexity used by the Opus encoder. + * Values between 0 to 10 are possible. Lower complexity values are + * lighter on the CPU but give poorer quality. + */ + opus_int32 setComplexity(opus_int32 new_comp); + + /** + * @brief Get the current complexity + * @returns Returns the current complexity + */ + opus_int32 complexity(void); + + /** + * @brief Set the bitrate to use + * @param new_bitrate The new bitrate to set (500 - 512000 bps) + * @returns The new bitrate + * + * Use this function to set the bitrate used by the Opus encoder. + * The chosen bitrate is returned. + */ + int setBitrate(int new_bitrate); + + /** + * @brief Get the current bitrate + * @returns Returns the current bitrate + */ + opus_int32 bitrate(void); + + /** + * @brief Enable or disable the variable bitrate mode + * @param enable Set to \em true to enable or \em false to disable VBR + * @returns Returns if VBR is enabled or not + * + * Use this function to enable or disable the variable bitrate (VBR) mode. + * Variable bitrate mode consume more bandwidth when needed to keep a + * certain quality level. VBR is enabled by default. + */ + bool enableVbr(bool enable); + + /** + * @brief Find out if variable bitrate is enabled or disabled + * @returns Returns the current frame size + */ + bool vbrEnabled(void); + + /** + * @brief Enable or disable constrained VBR + * @param enable Set to \em true to enable or \em false to disable + * @returns Returns if constrained VBR is enabled or not + * + * Constrained VBR is enabled by default. + */ + bool enableConstrainedVbr(bool enable); + + /** + * @brief Find out if variable bitrate is enabled or disabled + * @returns Returns the current frame size + */ + bool constrainedVbrEnabled(void); + + /** + * @brief Set the maximum audio bandwidth that the encoder will use + * @param new_bw The new bandwidth to set + * @returns Returns the current maximum bandwidth setting + * + * Use this function to set the maximum audio bandwidth for the encoder. + * The bandwidth is not given in Hz but as a number of constants. + * OPUS_BANDWIDTH_NARROWBAND (4 kHz), OPUS_BANDWIDTH_MEDIUMBAND (6 kHz), + * OPUS_BANDWIDTH_WIDEBAND (8 kHz), OPUS_BANDWIDTH_SUPERWIDEBAND (12 kHz), + * OPUS_BANDWIDTH_FULLBAND (20 kHz) (default) + */ + opus_int32 setMaxBandwidth(opus_int32 new_bw); + + /** + * @brief Get the current maximum audio bandwidth setting + * @returns Returns the current maximum audio bandwidth setting + */ + opus_int32 maxBandwidth(void); + + /** + * @brief Set the maximum audio bandwidth that the encoder will use + * @param new_bw The new bandwidth to set + * @returns Returns the current maximum bandwidth setting + * + * Use this function to set the audio bandwidth for the encoder. This will + * constrain the encoder to only use the specified bandwidth. Normally one + * should use the setMaxBandwidth function since that will let the encoder + * reduce the bandwith when appropriate. + * The bandwidth is not given in Hz but as a number of constants. + * OPUS_BANDWIDTH_NARROWBAND (4 kHz), OPUS_BANDWIDTH_MEDIUMBAND (6 kHz), + * OPUS_BANDWIDTH_WIDEBAND (8 kHz), OPUS_BANDWIDTH_SUPERWIDEBAND (12 kHz), + * OPUS_BANDWIDTH_FULLBAND (20 kHz) (default) + */ + opus_int32 setBandwidth(opus_int32 new_bw); + + /** + * @brief Get the current audio bandwidth setting + * @returns Returns the current audio bandwidth setting + */ + opus_int32 bandwidth(void); + + /** + * @brief Set which signal type to optimize the encoder for + * @param new_type The new signal type to set + * @returns Returns the new signal type + * + * Use this function to set the signal type that Opus should optimize the + * encoder for. Possible values are: + * OPUS_AUTO: Select automatically (default) + * OPUS_SIGNAL_VOICE: Bias thresholds towards choosing LPC or Hybrid modes. + * OPUS_SIGNAL_MUSIC: Bias thresholds towards choosing MDCT modes. + */ + opus_int32 setSignalType(opus_int32 new_type); + + /** + * @brief Get the currently configured signal type + * @returns Returns the currently configured signal type + */ + opus_int32 signalType(void); + + /** + * @brief Set which application type to optimize the encoder for + * @param new_type The new application type to set + * @returns Returns the new application type + * + * Use this function to set the application type that Opus should optimize + * the encoder for. Possible values are: + * OPUS_APPLICATION_VOIP: Process for improved speech intelligibility. + * OPUS_APPLICATION_AUDIO: Favor faithfulness to the original input. + * OPUS_APPLICATION_RESTRICTED_LOWDELAY: Configure the minimum possible + * coding delay by disabling certain modes of operation. + */ + opus_int32 setApplicationType(opus_int32 new_app); + + /** + * @brief Get the currently configured application type + * @returns Returns the currently configured application type + */ + opus_int32 applicationType(void); + + /** + * @brief Enable or disable inband FEC + * @param enable Set to \em true to enable or \em false to disable + * @returns Returns if inband FEC is enabled or not + * + * Inband FEC is disabled by default. + */ + bool enableInbandFec(bool enable); + + /** + * @brief Find out if variable bitrate is enabled or disabled + * @returns Returns the current frame size + */ + bool inbandFecEnabled(void); + + /** + * @brief Configures the encoder's expected packet loss percentage + * @param new_pl_perc Loss percentage in the range 0-100 (default: 0). + */ + opus_int32 setExpectedPacketLoss(opus_int32 new_pl_perc); + + /** + * @brief Get the currently configured expected packet loss percentage + * @returns Returns the currently configured packet loss percentage + */ + opus_int32 expectedPacketLoss(void); + + /** + * @brief Enable or disable discontinuous transmission (DTX) + * @param enable Set to \em true to enable or \em false to disable + * @returns Returns if DTX is enabled or not + * + * Discontinuous transmission (DTX) is disabled by default. + */ + bool enableDtx(bool enable); + + /** + * @brief Find out if variable bitrate is enabled or disabled + * @returns Returns the current frame size + */ + bool dtxEnabled(void); + +#if OPUS_MAJOR > 0 + /** + * @brief Configures the depth of signal being encoded + * @param new_depth The new LSB depth (8-24) + * @returns The new LSB depth + * + * This is a hint which helps the encoder identify silence and + * near-silence. + */ + opus_int32 setLsbDepth(opus_int32 new_depth); + + /** + * @brief Get the current bitrate + * @returns Returns the current bitrate + */ + opus_int32 lsbDepth(void); +#endif + + /** + * @brief Resets encoder to be equivalent to a freshly initialized one + */ + void reset(void); + +#if 0 + /** + * @brief Set the number of frames that are sent in each packet + * @param fpp Frames per packet + */ + void setFramesPerPacket(unsigned fpp); +#endif + + /** + * @brief Translate a bandwidth id to a string + * @param bw The bandwidth id + * @returns Returns the string corresponding to the given bandwidth id + */ + static const char *bandwidthStr(opus_int32 bw); + + /** + * @brief Translate a signal type id to a string + * @param bw The signal type id + * @returns Returns the string corresponding to the given signal type id + */ + static const char *signalTypeStr(opus_int32 type); + + /** + * @brief Translate a application type id to a string + * @param bw The application type id + * @returns Returns the string corresponding to the given application + * type id + */ + static const char *applicationTypeStr(opus_int32 type); + + /** + * @brief Write samples into this audio sink + * @param samples The buffer containing the samples + * @param count The number of samples in the buffer + * @return Returns the number of samples that has been taken care of + * + * This function is used to write audio into this audio sink. If it + * returns 0, no more samples should be written until the resumeOutput + * function in the source have been called. + * This function is normally only called from a connected source object. + */ + virtual int writeSamples(const float *samples, int count); + + + protected: + + private: + OpusEncoder *enc; + int frame_size; + float *sample_buf; + int buf_len; + //int frames_per_packet; + //int frame_cnt; + + AudioEncoderOpus(const AudioEncoderOpus&); + AudioEncoderOpus& operator=(const AudioEncoderOpus&); + +}; /* class AudioEncoderOpus */ + + +} /* namespace */ + +#endif /* ASYNC_AUDIO_ENCODER_OPUS_INCLUDED */ + + + +/* + * This file has not been truncated + */ + diff --git a/src/async/audio/AsyncAudioEncoderRaw.h b/src/async/audio/AsyncAudioEncoderRaw.h new file mode 100644 index 0000000..d39777f --- /dev/null +++ b/src/async/audio/AsyncAudioEncoderRaw.h @@ -0,0 +1,167 @@ +/** +@file AsyncAudioEncoderRaw.h +@brief A pass through audio "encoder" +@author Tobias Blomberg / SM0SVX +@date 2008-10-06 + +\verbatim +Async - A library for programming event driven applications +Copyright (C) 2003-2008 Tobias Blomberg / SM0SVX + +This program is free software; you can redistribute it and/or modify +it under the terms of the GNU General Public License as published by +the Free Software Foundation; either version 2 of the License, or +(at your option) any later version. + +This program is distributed in the hope that it will be useful, +but WITHOUT ANY WARRANTY; without even the implied warranty of +MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +GNU General Public License for more details. + +You should have received a copy of the GNU General Public License +along with this program; if not, write to the Free Software +Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA +\endverbatim +*/ + +#ifndef ASYNC_AUDIO_ENCODER_RAW_INCLUDED +#define ASYNC_AUDIO_ENCODER_RAW_INCLUDED + + +/**************************************************************************** + * + * System Includes + * + ****************************************************************************/ + + + +/**************************************************************************** + * + * Project Includes + * + ****************************************************************************/ + +#include + + +/**************************************************************************** + * + * Local Includes + * + ****************************************************************************/ + + + +/**************************************************************************** + * + * Forward declarations + * + ****************************************************************************/ + + + +/**************************************************************************** + * + * Namespace + * + ****************************************************************************/ + +namespace Async +{ + + +/**************************************************************************** + * + * Forward declarations of classes inside of the declared namespace + * + ****************************************************************************/ + + + +/**************************************************************************** + * + * Defines & typedefs + * + ****************************************************************************/ + + + +/**************************************************************************** + * + * Exported Global Variables + * + ****************************************************************************/ + + + +/**************************************************************************** + * + * Class definitions + * + ****************************************************************************/ + +/** +@brief A pass through audio "encoder" +@author Tobias Blomberg / SM0SVX +@date 2008-10-06 + +This class implements an audio "encoder" that just passes the native sample +format through. +*/ +class AudioEncoderRaw : public AudioEncoder +{ + public: + /** + * @brief Default constuctor + */ + AudioEncoderRaw(void) {} + + /** + * @brief Destructor + */ + virtual ~AudioEncoderRaw(void) {} + + /** + * @brief Get the name of the codec + * @return Return the name of the codec + */ + virtual const char *name(void) const { return "RAW"; } + + /** + * @brief Write samples into this audio sink + * @param samples The buffer containing the samples + * @param count The number of samples in the buffer + * @return Returns the number of samples that has been taken care of + * + * This function is used to write audio into this audio sink. If it + * returns 0, no more samples should be written until the resumeOutput + * function in the source have been called. + * This function is normally only called from a connected source object. + */ + virtual int writeSamples(const float *samples, int count) + { + writeEncodedSamples(samples, sizeof(*samples) * count); + return count; + } + + + protected: + + private: + AudioEncoderRaw(const AudioEncoderRaw&); + AudioEncoderRaw& operator=(const AudioEncoderRaw&); + +}; /* class AudioEncoderRaw */ + + +} /* namespace */ + +#endif /* ASYNC_AUDIO_ENCODER_S16_INCLUDED */ + + + +/* + * This file has not been truncated + */ + diff --git a/src/async/audio/AsyncAudioEncoderS16.cpp b/src/async/audio/AsyncAudioEncoderS16.cpp new file mode 100644 index 0000000..899894d --- /dev/null +++ b/src/async/audio/AsyncAudioEncoderS16.cpp @@ -0,0 +1,177 @@ +/** +@file AsyncAudioEncoderS16.cpp +@brief An audio encoder that encodes samples to signed 16 bit samples +@author Tobias Blomberg / SM0SVX +@date 2008-10-06 + +\verbatim +Async - A library for programming event driven applications +Copyright (C) 2003-2008 Tobias Blomberg / SM0SVX + +This program is free software; you can redistribute it and/or modify +it under the terms of the GNU General Public License as published by +the Free Software Foundation; either version 2 of the License, or +(at your option) any later version. + +This program is distributed in the hope that it will be useful, +but WITHOUT ANY WARRANTY; without even the implied warranty of +MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +GNU General Public License for more details. + +You should have received a copy of the GNU General Public License +along with this program; if not, write to the Free Software +Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA +\endverbatim +*/ + + + +/**************************************************************************** + * + * System Includes + * + ****************************************************************************/ + +#include + + +/**************************************************************************** + * + * Project Includes + * + ****************************************************************************/ + + + +/**************************************************************************** + * + * Local Includes + * + ****************************************************************************/ + +#include "AsyncAudioEncoderS16.h" + + + +/**************************************************************************** + * + * Namespaces to use + * + ****************************************************************************/ + +using namespace std; +using namespace Async; + + + +/**************************************************************************** + * + * Defines & typedefs + * + ****************************************************************************/ + + + +/**************************************************************************** + * + * Local class definitions + * + ****************************************************************************/ + + + +/**************************************************************************** + * + * Prototypes + * + ****************************************************************************/ + + + +/**************************************************************************** + * + * Exported Global Variables + * + ****************************************************************************/ + + + + +/**************************************************************************** + * + * Local Global Variables + * + ****************************************************************************/ + + + +/**************************************************************************** + * + * Public member functions + * + ****************************************************************************/ + +#if 0 +AudioEncoderS16::AudioEncoderS16(void) +{ + +} /* AsyncAudioEncoderS16::AsyncAudioEncoderS16 */ + + +AudioEncoderS16::~AudioEncoderS16(void) +{ + +} /* AsyncAudioEncoderS16::~AsyncAudioEncoderS16 */ +#endif + + +int AudioEncoderS16::writeSamples(const float *samples, int count) +{ + int16_t s16_samples[count]; + + for (int i=0; i 1.0) + { + s16_samples[i] = 32767; + } + else if (sample < -1.0) + { + s16_samples[i] = -32767; + } + else + { + s16_samples[i] = static_cast(sample * 32767.0); + } + } + + writeEncodedSamples(s16_samples, count * sizeof(*s16_samples)); + + return count; + +} /* AudioEncoderS16::writeSamples */ + + + + +/**************************************************************************** + * + * Protected member functions + * + ****************************************************************************/ + + + +/**************************************************************************** + * + * Private member functions + * + ****************************************************************************/ + + + +/* + * This file has not been truncated + */ + diff --git a/src/async/audio/AsyncAudioEncoderS16.h b/src/async/audio/AsyncAudioEncoderS16.h new file mode 100644 index 0000000..5d6a3d1 --- /dev/null +++ b/src/async/audio/AsyncAudioEncoderS16.h @@ -0,0 +1,163 @@ +/** +@file AsyncAudioEncoderS16.h +@brief An audio encoder that encodes samples to signed 16 bit samples +@author Tobias Blomberg / SM0SVX +@date 2008-10-06 + +\verbatim +Async - A library for programming event driven applications +Copyright (C) 2003-2008 Tobias Blomberg / SM0SVX + +This program is free software; you can redistribute it and/or modify +it under the terms of the GNU General Public License as published by +the Free Software Foundation; either version 2 of the License, or +(at your option) any later version. + +This program is distributed in the hope that it will be useful, +but WITHOUT ANY WARRANTY; without even the implied warranty of +MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +GNU General Public License for more details. + +You should have received a copy of the GNU General Public License +along with this program; if not, write to the Free Software +Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA +\endverbatim +*/ + +#ifndef ASYNC_AUDIO_ENCODER_S16_INCLUDED +#define ASYNC_AUDIO_ENCODER_S16_INCLUDED + + +/**************************************************************************** + * + * System Includes + * + ****************************************************************************/ + + + +/**************************************************************************** + * + * Project Includes + * + ****************************************************************************/ + +#include + + +/**************************************************************************** + * + * Local Includes + * + ****************************************************************************/ + + + +/**************************************************************************** + * + * Forward declarations + * + ****************************************************************************/ + + + +/**************************************************************************** + * + * Namespace + * + ****************************************************************************/ + +namespace Async +{ + + +/**************************************************************************** + * + * Forward declarations of classes inside of the declared namespace + * + ****************************************************************************/ + + + +/**************************************************************************** + * + * Defines & typedefs + * + ****************************************************************************/ + + + +/**************************************************************************** + * + * Exported Global Variables + * + ****************************************************************************/ + + + +/**************************************************************************** + * + * Class definitions + * + ****************************************************************************/ + +/** +@brief An audio encoder that encodes samples to signed 16 bit samples +@author Tobias Blomberg / SM0SVX +@date 2008-10-06 + +This class implements an audio "encoder" that converts the native sample +format to signed 16 bit fixed precision samples. +*/ +class AudioEncoderS16 : public AudioEncoder +{ + public: + /** + * @brief Default constuctor + */ + AudioEncoderS16(void) {} + + /** + * @brief Destructor + */ + virtual ~AudioEncoderS16(void) {} + + /** + * @brief Get the name of the codec + * @returns Return the name of the codec + */ + virtual const char *name(void) const { return "S16"; } + + /** + * @brief Write samples into this audio sink + * @param samples The buffer containing the samples + * @param count The number of samples in the buffer + * @return Returns the number of samples that has been taken care of + * + * This function is used to write audio into this audio sink. If it + * returns 0, no more samples should be written until the resumeOutput + * function in the source have been called. + * This function is normally only called from a connected source object. + */ + virtual int writeSamples(const float *samples, int count); + + + protected: + + private: + AudioEncoderS16(const AudioEncoderS16&); + AudioEncoderS16& operator=(const AudioEncoderS16&); + +}; /* class AudioEncoderS16 */ + + +} /* namespace */ + +#endif /* ASYNC_AUDIO_ENCODER_S16_INCLUDED */ + + + +/* + * This file has not been truncated + */ + diff --git a/src/async/audio/AsyncAudioEncoderSpeex.cpp b/src/async/audio/AsyncAudioEncoderSpeex.cpp new file mode 100644 index 0000000..e637993 --- /dev/null +++ b/src/async/audio/AsyncAudioEncoderSpeex.cpp @@ -0,0 +1,376 @@ +/** +@file AsyncAudioEncoderSpeex.cpp +@brief An audio encoder that encodes samples using the Speex codec +@author Tobias Blomberg / SM0SVX +@date 2008-10-16 + +\verbatim +Async - A library for programming event driven applications +Copyright (C) 2003-2008 Tobias Blomberg / SM0SVX + +This program is free software; you can redistribute it and/or modify +it under the terms of the GNU General Public License as published by +the Free Software Foundation; either version 2 of the License, or +(at your option) any later version. + +This program is distributed in the hope that it will be useful, +but WITHOUT ANY WARRANTY; without even the implied warranty of +MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +GNU General Public License for more details. + +You should have received a copy of the GNU General Public License +along with this program; if not, write to the Free Software +Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA +\endverbatim +*/ + + + +/**************************************************************************** + * + * System Includes + * + ****************************************************************************/ + +#include +#include +#include + + +/**************************************************************************** + * + * Project Includes + * + ****************************************************************************/ + + + +/**************************************************************************** + * + * Local Includes + * + ****************************************************************************/ + +#include "AsyncAudioEncoderSpeex.h" + + + +/**************************************************************************** + * + * Namespaces to use + * + ****************************************************************************/ + +using namespace std; +using namespace Async; + + + +/**************************************************************************** + * + * Defines & typedefs + * + ****************************************************************************/ + + + +/**************************************************************************** + * + * Local class definitions + * + ****************************************************************************/ + + + +/**************************************************************************** + * + * Prototypes + * + ****************************************************************************/ + + + +/**************************************************************************** + * + * Exported Global Variables + * + ****************************************************************************/ + + + + +/**************************************************************************** + * + * Local Global Variables + * + ****************************************************************************/ + + + +/**************************************************************************** + * + * Public member functions + * + ****************************************************************************/ + +AudioEncoderSpeex::AudioEncoderSpeex(void) + : buf_len(0), frames_per_packet(4), frame_cnt(0) +{ + speex_bits_init(&bits); +#if INTERNAL_SAMPLE_RATE == 16000 + enc_state = speex_encoder_init(&speex_wb_mode); +#else + enc_state = speex_encoder_init(&speex_nb_mode); +#endif + speex_encoder_ctl(enc_state, SPEEX_GET_FRAME_SIZE, &frame_size); + sample_buf = new float[frame_size]; + + //setQuality(10); + //setComplexity(10); + //setBitrate(8000); + //enableVbr(true); + //setVbrQuality(10); + //setAbr(8000); + //setVbrMaxBitrate(32000); + //enableHighpass(false); +} /* AsyncAudioEncoderSpeex::AsyncAudioEncoderSpeex */ + + +AudioEncoderSpeex::~AudioEncoderSpeex(void) +{ + delete [] sample_buf; + speex_bits_destroy(&bits); + speex_encoder_destroy(enc_state); +} /* AsyncAudioEncoderSpeex::~AsyncAudioEncoderSpeex */ + + +void AudioEncoderSpeex::setOption(const std::string &name, + const std::string &value) +{ + if (name == "FRAMES_PER_PACKET") + { + setFramesPerPacket(atoi(value.c_str())); + } + else if (name == "QUALITY") + { + setQuality(atoi(value.c_str())); + } + else if (name == "BITRATE") + { + setBitrate(atoi(value.c_str())); + } + else if (name == "COMPLEXITY") + { + setComplexity(atoi(value.c_str())); + } + else if (name == "VBR") + { + enableVbr(atoi(value.c_str()) != 0); + } + else if (name == "VBR_QUALITY") + { + setVbrQuality(atoi(value.c_str())); + } + else if (name == "ABR") + { + setAbr(atoi(value.c_str())); + } + else + { + cerr << "*** WARNING AudioEncoderSpeex: Unknown option \"" + << name << "\". Ignoring it.\n"; + } +} /* AudioEncoderSpeex::setOption */ + + +void AudioEncoderSpeex::printCodecParams(void) +{ + cout << "------ Speex encoder parameters ------\n"; + cout << "Frame size = " << frameSize() << endl; + cout << "Bitrate = " << bitrate() << endl; + cout << "Complexity = " << complexity() << endl; + //cout << "VBR quality = " << vbrQuality() << endl; + cout << "ABR = " << abr() << endl; + cout << "VBR enabled = " << (vbrEnabled() ? "EN" : "DIS") << "ABLED\n"; + //cout << "VBR max bitrate = " << vbrMaxBitrate() << endl; + //cout << "Highpass filter = " << (highpassEnabled() ? "EN" : "DIS") + // << "ABLED\n"; + cout << "--------------------------------------\n"; +} /* AudioEncoderSpeex::printCodecParams */ + + +void AudioEncoderSpeex::setFramesPerPacket(unsigned fpp) +{ + frames_per_packet = fpp; +} /* AudioEncoderSpeex::setFramesPerPacket */ + + +void AudioEncoderSpeex::setQuality(int quality) +{ + speex_encoder_ctl(enc_state, SPEEX_SET_QUALITY, &quality); +} /* AudioEncoderSpeex::setQuality */ + + +int AudioEncoderSpeex::setBitrate(int new_bitrate) +{ + speex_encoder_ctl(enc_state, SPEEX_SET_BITRATE, &new_bitrate); + return bitrate(); +} /* AudioEncoderSpeex::setBitrate */ + + +int AudioEncoderSpeex::bitrate(void) +{ + int br; + speex_encoder_ctl(enc_state, SPEEX_GET_BITRATE, &br); + return br; +} /* AudioEncoderSpeex::bitrate */ + + +int AudioEncoderSpeex::setComplexity(int new_comp) +{ + speex_encoder_ctl(enc_state, SPEEX_SET_COMPLEXITY, &new_comp); + return complexity(); +} /* AudioEncoderSpeex::setBitrate */ + + +int AudioEncoderSpeex::complexity(void) +{ + int comp; + speex_encoder_ctl(enc_state, SPEEX_GET_COMPLEXITY, &comp); + return comp; +} /* AudioEncoderSpeex::complexity */ + + +void AudioEncoderSpeex::enableVbr(bool enable) +{ + int do_enable = enable ? 1 : 0; + speex_encoder_ctl(enc_state, SPEEX_SET_VBR, &do_enable); +} /* AudioEncoderSpeex::enableVbr */ + + +bool AudioEncoderSpeex::vbrEnabled(void) +{ + int enabled; + speex_encoder_ctl(enc_state, SPEEX_GET_VBR, &enabled); + return (enabled != 0); +} /* AudioEncoderSpeex::vbrEnabled */ + + +int AudioEncoderSpeex::setVbrQuality(int quality) +{ + speex_encoder_ctl(enc_state, SPEEX_SET_VBR_QUALITY, &quality); + return vbrQuality(); +} /* AudioEncoderSpeex::setVbrQuality */ + + +int AudioEncoderSpeex::vbrQuality(void) +{ + int quality; + speex_encoder_ctl(enc_state, SPEEX_GET_VBR_QUALITY, &quality); + return quality; +} /* AudioEncoderSpeex::vbrQuality */ + + +#if 0 +int AudioEncoderSpeex::setVbrMaxBitrate(int bitrate) +{ + speex_encoder_ctl(enc_state, SPEEX_SET_VBR_MAX_BITRATE, &bitrate); + return vbrMaxBitrate(); +} /* AudioEncoderSpeex::setVbrMaxBitrate */ + + +int AudioEncoderSpeex::vbrMaxBitrate(void) +{ + int bitrate; + speex_encoder_ctl(enc_state, SPEEX_GET_VBR_MAX_BITRATE, &bitrate); + return bitrate; +} /* AudioEncoderSpeex::vbrMaxBitrate */ +#endif + + +int AudioEncoderSpeex::setAbr(int new_abr) +{ + speex_encoder_ctl(enc_state, SPEEX_SET_ABR, &new_abr); + return abr(); +} /* AudioEncoderSpeex::setAbr */ + + +int AudioEncoderSpeex::abr(void) +{ + int a; + speex_encoder_ctl(enc_state, SPEEX_GET_ABR, &a); + return a; +} /* AudioEncoderSpeex::abr */ + + +#if 0 +bool AudioEncoderSpeex::enableHighpass(bool enable) +{ + int hp = enable ? 1 : 0; + speex_encoder_ctl(enc_state, SPEEX_SET_HIGHPASS, &hp); + return highpassEnabled(); +} /* AudioEncoderSpeex::setAbr */ + + +bool AudioEncoderSpeex::highpassEnabled(void) +{ + int hp; + speex_encoder_ctl(enc_state, SPEEX_GET_HIGHPASS, &hp); + return (hp != 0); +} /* AudioEncoderSpeex::highpassEnabled */ +#endif + + + +int AudioEncoderSpeex::writeSamples(const float *samples, int count) +{ + for (int i=0; i + + +/**************************************************************************** + * + * Project Includes + * + ****************************************************************************/ + +#include + + +/**************************************************************************** + * + * Local Includes + * + ****************************************************************************/ + + + +/**************************************************************************** + * + * Forward declarations + * + ****************************************************************************/ + + + +/**************************************************************************** + * + * Namespace + * + ****************************************************************************/ + +namespace Async +{ + + +/**************************************************************************** + * + * Forward declarations of classes inside of the declared namespace + * + ****************************************************************************/ + + + +/**************************************************************************** + * + * Defines & typedefs + * + ****************************************************************************/ + + + +/**************************************************************************** + * + * Exported Global Variables + * + ****************************************************************************/ + + + +/**************************************************************************** + * + * Class definitions + * + ****************************************************************************/ + +/** +@brief An audio encoder that encodes samples using the Speex codec +@author Tobias Blomberg / SM0SVX +@date 2008-10-15 + +This class implements an audio encoder that use the Speex audio codec. +*/ +class AudioEncoderSpeex : public AudioEncoder +{ + public: + /** + * @brief Default constuctor + */ + AudioEncoderSpeex(void); + + /** + * @brief Destructor + */ + virtual ~AudioEncoderSpeex(void); + + /** + * @brief Get the name of the codec + * @returns Return the name of the codec + */ + virtual const char *name(void) const { return "SPEEX"; } + + /** + * @brief Set an option for the encoder + * @param name The name of the option + * @param value The value of the option + */ + virtual void setOption(const std::string &name, const std::string &value); + + /** + * @brief Print codec parameter settings + */ + virtual void printCodecParams(void); + + /** + * @brief Set the number of frames that are sent in each packet + * @param fpp Frames per packet + */ + void setFramesPerPacket(unsigned fpp); + + /** + * @brief Set the quality to use + * @param quality The new quality to set (0-10) + * @returns The new quality value + * + * Use this function to set the quality used by the Speex encoder. + * Values between 0 to 10 are possible. Low values give poorer quality + * and lower bitrate and higher values give better quality. + */ + void setQuality(int quality); + + /** + * @brief Set the bitrate to use + * @param new_bitrate The new bitrate to set + * @returns The new bitrate + * + * Use this function to set the bitrate used by the Speex encoder. + * Not all bitrates are possible. The chosen bitrate is returned. + */ + int setBitrate(int new_bitrate); + + /** + * @brief Get the current bitrate + * @returns Returns the current bitrate + */ + int bitrate(void); + + /** + * @brief Set the complexity to use + * @param new_comp The new complexity value to set (0-10) + * @returns The new complexity value + * + * Use this function to set the complexity used by the Speex encoder. + * Values between 0 to 10 are possible. Lower complexity values are + * lighter on the CPU but give poorer quality. The difference between + * lowest and highest value is about 2dB SNR. The highest complexity is + * about five times as heavy on the CPU as the lowest complexity. + */ + int setComplexity(int new_comp); + + /** + * @brief Get the current complexity + * @returns Returns the current complexity + */ + int complexity(void); + + /** + * @brief Get the current frame size + * @returns Returns the current frame size + */ + int frameSize(void) const { return frame_size; } + + /** + * @brief Enable or disable the variable bitrate mode + * @param enable Set to \em true to enable or \em false to disable VBR + * + * Use this function to enable or disable the variable bitrate (VBR) mode. + * Variable bitrate mode consume more bandwidth when needed to keep a + * certain quality level. + */ + void enableVbr(bool enable); + + /** + * @brief Find out if variable bitrate is enabled or disabled + * @returns Returns the current frame size + */ + bool vbrEnabled(void); + + /** + * @brief Set the VBR quality to use + * @param quality The new quality value to set (0-10) + * @returns The new quality value is returned + */ + int setVbrQuality(int quality); + + /** + * @brief Get the current VBR quality + * @returns Returns the current VBR quality + */ + int vbrQuality(void); + + //int setVbrMaxBitrate(int bitrate); + //int vbrMaxBitrate(void); + + /** + * @brief Set the average bitrate to use + * @param new_abr The new average bitrate to set + * @returns The new average bitrate value is returned + */ + int setAbr(int new_abr); + + /** + * @brief Get the currently set average bitrate + * @returns Returns the currently set average bitrate + */ + int abr(void); + + //bool enableHighpass(bool enable); + //bool highpassEnabled(void); + + + /** + * @brief Write samples into this audio sink + * @param samples The buffer containing the samples + * @param count The number of samples in the buffer + * @return Returns the number of samples that has been taken care of + * + * This function is used to write audio into this audio sink. If it + * returns 0, no more samples should be written until the resumeOutput + * function in the source have been called. + * This function is normally only called from a connected source object. + */ + virtual int writeSamples(const float *samples, int count); + + + protected: + + private: + SpeexBits bits; + void *enc_state; + int frame_size; + float *sample_buf; + int buf_len; + int frames_per_packet; + int frame_cnt; + + AudioEncoderSpeex(const AudioEncoderSpeex&); + AudioEncoderSpeex& operator=(const AudioEncoderSpeex&); + +}; /* class AudioEncoderSpeex */ + + +} /* namespace */ + +#endif /* ASYNC_AUDIO_ENCODER_SPEEX_INCLUDED */ + + + +/* + * This file has not been truncated + */ + diff --git a/src/async/audio/AsyncAudioFifo.cpp b/src/async/audio/AsyncAudioFifo.cpp new file mode 100644 index 0000000..e44874d --- /dev/null +++ b/src/async/audio/AsyncAudioFifo.cpp @@ -0,0 +1,414 @@ +/** +@file AsyncAudioFifo.cpp +@brief A FIFO for handling audio samples +@author Tobias Blomberg / SM0SVX +@date 2007-10-06 + +Implements a FIFO (with some extra functionality) for storing samples. + +\verbatim +Async - A library for programming event driven applications +Copyright (C) 2003-2009 Tobias Blomberg / SM0SVX + +This program is free software; you can redistribute it and/or modify +it under the terms of the GNU General Public License as published by +the Free Software Foundation; either version 2 of the License, or +(at your option) any later version. + +This program is distributed in the hope that it will be useful, +but WITHOUT ANY WARRANTY; without even the implied warranty of +MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +GNU General Public License for more details. + +You should have received a copy of the GNU General Public License +along with this program; if not, write to the Free Software +Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA +\endverbatim +*/ + + + +/**************************************************************************** + * + * System Includes + * + ****************************************************************************/ + +#include +#include +#include +#include + + +/**************************************************************************** + * + * Project Includes + * + ****************************************************************************/ + + + +/**************************************************************************** + * + * Local Includes + * + ****************************************************************************/ + +#include "AsyncAudioFifo.h" + + + +/**************************************************************************** + * + * Namespaces to use + * + ****************************************************************************/ + +using namespace std; +using namespace Async; + + +/**************************************************************************** + * + * Defines & typedefs + * + ****************************************************************************/ + + + +/**************************************************************************** + * + * Local class definitions + * + ****************************************************************************/ + + + +/**************************************************************************** + * + * Prototypes + * + ****************************************************************************/ + + + +/**************************************************************************** + * + * Exported Global Variables + * + ****************************************************************************/ + + + + +/**************************************************************************** + * + * Local Global Variables + * + ****************************************************************************/ + +static const unsigned MAX_WRITE_SIZE = 800; + + +/**************************************************************************** + * + * Public member functions + * + ****************************************************************************/ + + +AudioFifo::AudioFifo(unsigned fifo_size) + : fifo_size(fifo_size), head(0), tail(0), + do_overwrite(false), output_stopped(false), prebuf_samples(0), + prebuf(false), is_flushing(false), is_full(false), buffering_enabled(true), + disable_buffering_when_flushed(false), is_idle(true), input_stopped(false) +{ + assert(fifo_size > 0); + fifo = new float[fifo_size]; +} /* AudioFifo */ + + +AudioFifo::~AudioFifo(void) +{ + delete [] fifo; +} /* ~AudioFifo */ + + +void AudioFifo::setSize(unsigned new_size) +{ + assert(fifo_size > 0); + if (new_size != fifo_size) + { + delete [] fifo; + fifo_size = new_size; + fifo = new float[fifo_size]; + } + clear(); +} /* AudioFifo::setSize */ + + +unsigned AudioFifo::samplesInFifo(bool ignore_prebuf) const +{ + unsigned samples_in_buffer = + is_full ? fifo_size : (head - tail + fifo_size) % fifo_size; + + if (!ignore_prebuf && prebuf && !is_flushing) + { + if (samples_in_buffer < prebuf_samples) + { + return 0; + } + } + + return samples_in_buffer; + +} /* AudioFifo::samplesInFifo */ + + +void AudioFifo::clear(void) +{ + bool was_empty = empty(); + + is_full = false; + tail = head = 0; + prebuf = (prebuf_samples > 0); + output_stopped = false; + + if (is_flushing && !was_empty) + { + sinkFlushSamples(); + } +} /* AudioFifo::clear */ + + +void AudioFifo::setPrebufSamples(unsigned prebuf_samples) +{ + this->prebuf_samples = min(prebuf_samples, fifo_size-1); + if (empty()) + { + prebuf = (prebuf_samples > 0); + } +} /* AudioFifo::setPrebufSamples */ + + +void AudioFifo::enableBuffering(bool enable) +{ + if (enable) + { + disable_buffering_when_flushed = false; + if (!buffering_enabled) + { + buffering_enabled = true; + if (input_stopped) + { + sourceResumeOutput(); + } + } + } + else + { + if (buffering_enabled) + { + if (empty()) + { + buffering_enabled = false; + } + else + { + disable_buffering_when_flushed = true; + } + } + } +} /* AudioFifo::enableBuffering */ + + +int AudioFifo::writeSamples(const float *samples, int count) +{ + /* + printf("AudioFifo::writeSamples: count=%d empty=%s prebuf=%s\n", + count, empty() ? "true" : "false", prebuf ? "true" : "false"); + */ + + assert(count > 0); + + is_idle = false; + is_flushing = false; + + if (is_full) + { + input_stopped = true; + return 0; + } + + int samples_written = 0; + if (empty() && !prebuf) + { + samples_written = sinkWriteSamples(samples, count); + /* + printf("AudioFifo::writeSamples: count=%d " + "samples_written=%d\n", count, samples_written); + */ + } + + if (buffering_enabled) + { + while (!is_full && (samples_written < count)) + { + while (!is_full && (samples_written < count)) + { + fifo[head] = samples[samples_written++]; + head = (head < fifo_size-1) ? head + 1 : 0; + if (head == tail) + { + if (do_overwrite) + { + tail = (tail < fifo_size-1) ? tail + 1 : 0; + } + else + { + is_full = true; + } + } + } + + if (prebuf && (samplesInFifo() > 0)) + { + prebuf = false; + } + + writeSamplesFromFifo(); + } + } + else + { + output_stopped = (samples_written == 0); + } + + input_stopped = (samples_written == 0); + + return samples_written; + +} /* writeSamples */ + + +void AudioFifo::flushSamples(void) +{ + //printf("AudioFifo::flushSamples\n"); + is_flushing = true; + prebuf = (prebuf_samples > 0); + if (empty()) + { + sinkFlushSamples(); + } + else + { + writeSamplesFromFifo(); + } +} /* AudioFifo::flushSamples */ + + +void AudioFifo::resumeOutput(void) +{ + if (output_stopped) + { + output_stopped = false; + if (buffering_enabled) + { + writeSamplesFromFifo(); + } + else if (input_stopped) + { + sourceResumeOutput(); + } + } +} /* resumeOutput */ + + + +/**************************************************************************** + * + * Protected member functions + * + ****************************************************************************/ + + +void AudioFifo::allSamplesFlushed(void) +{ + if (empty()) + { + if (disable_buffering_when_flushed) + { + disable_buffering_when_flushed = false; + buffering_enabled = false; + } + + if (is_flushing) + { + is_flushing = false; + sourceAllSamplesFlushed(); + } + } +} /* AudioFifo::allSamplesFlushed */ + + + + + + +/**************************************************************************** + * + * Private member functions + * + ****************************************************************************/ + + +void AudioFifo::writeSamplesFromFifo(void) +{ + if (output_stopped || (samplesInFifo() == 0)) + { + return; + } + + bool was_full = full(); + + int samples_written; + do + { + int samples_to_write = min(MAX_WRITE_SIZE, samplesInFifo(true)); + int to_end_of_fifo = fifo_size - tail; + samples_to_write = min(samples_to_write, to_end_of_fifo); + samples_written = sinkWriteSamples(fifo+tail, samples_to_write); + //printf("AudioFifo::writeSamplesFromFifo(%s): samples_to_write=%d " + // "samples_written=%d\n", debug_name.c_str(), samples_to_write, + // samples_written); + if (was_full && (samples_written > 0)) + { + is_full = false; + was_full = false; + } + tail = (tail + samples_written) % fifo_size; + } while((samples_written > 0) && !empty()); + + if (samples_written == 0) + { + output_stopped = true; + } + + if (input_stopped && !full()) + { + input_stopped = false; + sourceResumeOutput(); + } + + if (is_flushing && empty()) + { + sinkFlushSamples(); + } + +} /* writeSamplesFromFifo */ + + + +/* + * This file has not been truncated + */ diff --git a/src/async/audio/AsyncAudioFifo.h b/src/async/audio/AsyncAudioFifo.h new file mode 100644 index 0000000..88951bc --- /dev/null +++ b/src/async/audio/AsyncAudioFifo.h @@ -0,0 +1,300 @@ +/** +@file AsyncAudioFifo.h +@brief A FIFO for handling audio samples +@author Tobias Blomberg / SM0SVX +@date 2007-10-06 + +Implements a FIFO (with some extra functionality) for storing samples. + +\verbatim +Async - A library for programming event driven applications +Copyright (C) 2003-2009 Tobias Blomberg / SM0SVX + +This program is free software; you can redistribute it and/or modify +it under the terms of the GNU General Public License as published by +the Free Software Foundation; either version 2 of the License, or +(at your option) any later version. + +This program is distributed in the hope that it will be useful, +but WITHOUT ANY WARRANTY; without even the implied warranty of +MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +GNU General Public License for more details. + +You should have received a copy of the GNU General Public License +along with this program; if not, write to the Free Software +Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA +\endverbatim +*/ + + +#ifndef ASYNC_AUDIO_FIFO_INCLUDED +#define ASYNC_AUDIO_FIFO_INCLUDED + + +/**************************************************************************** + * + * System Includes + * + ****************************************************************************/ + + + +/**************************************************************************** + * + * Project Includes + * + ****************************************************************************/ + +#include +#include + + +/**************************************************************************** + * + * Local Includes + * + ****************************************************************************/ + + + +/**************************************************************************** + * + * Forward declarations + * + ****************************************************************************/ + + + +/**************************************************************************** + * + * Namespace + * + ****************************************************************************/ + +namespace Async +{ + + +/**************************************************************************** + * + * Forward declarations of classes inside of the declared namespace + * + ****************************************************************************/ + + + +/**************************************************************************** + * + * Defines & typedefs + * + ****************************************************************************/ + + + +/**************************************************************************** + * + * Exported Global Variables + * + ****************************************************************************/ + + + +/**************************************************************************** + * + * Class definitions + * + ****************************************************************************/ + +/** +@brief A FIFO class for handling audio samples +@author Tobias Blomberg / SM0SVX +@date 2007-10-06 + +This class implements a FIFO for handling audio samples. The FIFO also have +some additional features. Output can be started or stopped and it can be +instructed to buffer some samples before starting to output audio. +Samples can be automatically output using the normal audio pipe infrastructure +or samples could be read on demand using the readSamples method. +*/ +class AudioFifo : public AudioSink, public AudioSource +{ + public: + /** + * @brief Constuctor + * @param fifo_size This is the size of the fifo expressed in number + * of samples. + */ + explicit AudioFifo(unsigned fifo_size); + + /** + * @brief Destructor + */ + virtual ~AudioFifo(void); + + /** + * @brief Set the size of the FIFO + * @param new_size This is the size of the fifo expressed in number + * of samples. + * + * Use this function to set the size of the FIFO. In doing this, the + * FIFO will also be cleared. + */ + void setSize(unsigned new_size); + + /** + * @brief Check if the FIFO is empty + * @return Returns \em true if the FIFO is empty or else \em false + */ + bool empty(void) const { return !is_full && (tail == head); } + + /** + * @brief Check if the FIFO is full + * @return Returns \em true if the FIFO is full or else \em false + * + * This function is used to check if the FIFO is full or not. The FIFO can + * only reach the buffer full condition if overwrite is false. The overwrite + * mode is set by the setOverwrite function. + */ + bool full(void) const { return is_full; } + + /** + * @brief Find out how many samples there are in the FIFO + * @param ignore_prebuf Set to \em true to not report pre-buffered + samples. + * @return Returns the number of samples in the FIFO + */ + unsigned samplesInFifo(bool ignore_prebuf=false) const; + + /** + * @brief Set the overwrite mode + * @param overwrite Set to \em true to overwrite or else \em false + * + * The FIFO can operate in overwrite or normal mode. When in normal mode, + * it will not be possible to add any more samples to the FIFO when it + * is full. Samples has to be removed first. When overwrite is set, newly + * added samples will overwrite the oldest samples in the buffer so the FIFO + * will never get full but instead samples are lost. + * Use this function to set the overwrite mode. + */ + void setOverwrite(bool overwrite) { do_overwrite = overwrite; } + + /** + * @brief Check the overwrite mode + * @return Returns \em true if overwrite is enabled or else \em false + * + * The FIFO can operate in overwrite or normal mode. When in normal mode, + * it will not be possible to add any more samples to the FIFO when it + * is full. Samples has to be removed first. When overwrite is set, newly + * added samples will overwrite the oldest samples in the buffer so the FIFO + * will never get full but instead samples are lost. + * Use this function the check the current overwrite mode. Use the + * setOverwrite function to set the overwrite mode. + */ + bool overwrite(void) const { return do_overwrite; } + + /** + * @brief Clear all samples from the FIFO + * + * This will immediately reset the FIFO and discard all samples. + * The source will be told that all samples have been flushed. + */ + void clear(void); + + /** + * @brief Set the number of samples that must be in the fifo before + * any samples are written out from it. + * @param prebuf_samples The number of samples + */ + void setPrebufSamples(unsigned prebuf_samples); + + /** + * @brief Enable/disable the fifo buffer + * @param enable Set to \em true to enable buffering or else \em false + * + * Use this method to turn buffering on and off. When buffering is off, + * no incoming samples will be stored in the fifo. If there are samples + * in the fifo at the time when buffering is disabled they will be sent + * out in the normal way. + * Don't disable buffering when pre-buffering is used. This will get + * you into trouble. + */ + void enableBuffering(bool enable); + + /** + * @brief Check if buffering is enabled or disabled + * @return Returns \em true if buffering is enabled or else \em false + */ + bool bufferingEnabled(void) const { return buffering_enabled; } + + /** + * @brief Write samples into the FIFO + * @param samples The buffer containing the samples + * @param count The number of samples in the buffer + * @return Returns the number of samples that has been taken care of + * + * This function is used to write audio into the FIFO. If it + * returns 0, no more samples should be written until the resumeOutput + * function in the source have been called. + * This function is normally only called from a connected source object. + */ + virtual int writeSamples(const float *samples, int count); + + /** + * @brief Tell the FIFO to flush the previously written samples + * + * This function is used to tell the FIFO to flush previously written + * samples. + * This function is normally only called from a connected source object. + */ + virtual void flushSamples(void); + + /** + * @brief Resume audio output to the connected sink + * + * This function will be called when the registered audio sink is ready + * to accept more samples. + * This function is normally only called from a connected sink object. + */ + virtual void resumeOutput(void); + + + protected: + /** + * @brief The registered sink has flushed all samples + * + * This function will be called when all samples have been flushed in the + * registered sink. + * This function is normally only called from a connected sink object. + */ + virtual void allSamplesFlushed(void); + + + private: + float *fifo; + unsigned fifo_size; + unsigned head, tail; + bool do_overwrite; + bool output_stopped; + unsigned prebuf_samples; + bool prebuf; + bool is_flushing; + bool is_full; + bool buffering_enabled; + bool disable_buffering_when_flushed; + bool is_idle; + bool input_stopped; + + void writeSamplesFromFifo(void); + +}; /* class AudioFifo */ + + +} /* namespace */ + +#endif /* ASYNC_AUDIO_FIFO_INCLUDED */ + + +/* + * This file has not been truncated + */ + diff --git a/src/async/audio/AsyncAudioFilter.cpp b/src/async/audio/AsyncAudioFilter.cpp new file mode 100644 index 0000000..8469bf6 --- /dev/null +++ b/src/async/audio/AsyncAudioFilter.cpp @@ -0,0 +1,245 @@ +/** +@file AsyncAudioFilter.cpp +@brief Contains a class for creating a wide range of audio filters +@author Tobias Blomberg / SM0SVX +@date 2006-04-23 + +\verbatim +Async - A library for programming event driven applications +Copyright (C) 2003-2008 Tobias Blomberg / SM0SVX + +This program is free software; you can redistribute it and/or modify +it under the terms of the GNU General Public License as published by +the Free Software Foundation; either version 2 of the License, or +(at your option) any later version. + +This program is distributed in the hope that it will be useful, +but WITHOUT ANY WARRANTY; without even the implied warranty of +MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +GNU General Public License for more details. + +You should have received a copy of the GNU General Public License +along with this program; if not, write to the Free Software +Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA +\endverbatim +*/ + + + +/**************************************************************************** + * + * System Includes + * + ****************************************************************************/ + +#include + +#include +#include +#include +#include + + +/**************************************************************************** + * + * Project Includes + * + ****************************************************************************/ + + + +/**************************************************************************** + * + * Local Includes + * + ****************************************************************************/ + +extern "C" { +#include "fidlib.h" +}; + +#include "AsyncAudioFilter.h" + + + +/**************************************************************************** + * + * Namespaces to use + * + ****************************************************************************/ + +using namespace std; +using namespace Async; + + + +/**************************************************************************** + * + * Defines & typedefs + * + ****************************************************************************/ + + + +/**************************************************************************** + * + * Local class definitions + * + ****************************************************************************/ + +namespace Async +{ + class FidVars + { + public: + FidFilter *ff; + FidRun *run; + FidFunc *func; + void *buf; + + FidVars(void) : ff(0), run(0), func(0), buf(0) {} + }; +}; + + +/**************************************************************************** + * + * Prototypes + * + ****************************************************************************/ + + + +/**************************************************************************** + * + * Exported Global Variables + * + ****************************************************************************/ + + + + +/**************************************************************************** + * + * Local Global Variables + * + ****************************************************************************/ + + + +/**************************************************************************** + * + * Public member functions + * + ****************************************************************************/ + +AudioFilter::AudioFilter(int sample_rate) + : sample_rate(sample_rate), fv(0), output_gain(1.0f) +{ + +} /* AudioFilter::AudioFilter */ + + +AudioFilter::AudioFilter(const string &filter_spec, int sample_rate) + : sample_rate(sample_rate), fv(0), output_gain(1.0f) +{ + if (!parseFilterSpec(filter_spec)) + { + cerr << "***ERROR: Filter creation error: " << error_str << endl; + exit(1); + } +} /* AudioFilter::AudioFilter */ + + +AudioFilter::~AudioFilter(void) +{ + deleteFilter(); +} /* AudioFilter::~AudioFilter */ + + +bool AudioFilter::parseFilterSpec(const std::string &filter_spec) +{ + deleteFilter(); + + fv = new FidVars; + + char spec_buf[256]; + strncpy(spec_buf, filter_spec.c_str(), sizeof(spec_buf)); + spec_buf[sizeof(spec_buf) - 1] = 0; + char *spec = spec_buf; + char *old_locale = setlocale(LC_ALL, "C"); + char *fferr = fid_parse(sample_rate, &spec, &fv->ff); + setlocale(LC_ALL, old_locale); + if (fferr != 0) + { + error_str = fferr; + free(fferr); + deleteFilter(); + return false; + } + fv->run = fid_run_new(fv->ff, &fv->func); + fv->buf = fid_run_newbuf(fv->run); + return true; +} /* AudioFilter::parseFilterSpec */ + + +void AudioFilter::setOutputGain(float gain_db) +{ + output_gain = powf(10.0f, gain_db / 20.0f); +} /* AudioFilter::setOutputGain */ + + +void AudioFilter::reset(void) +{ + fid_run_zapbuf(fv->buf); +} /* AudioFilter::reset */ + + + +/**************************************************************************** + * + * Protected member functions + * + ****************************************************************************/ + + +void AudioFilter::processSamples(float *dest, const float *src, int count) +{ + //cout << "AudioFilter::processSamples: len=" << len << endl; + + for (int i=0; ifunc(fv->buf, src[i]); + } +} /* AudioFilter::writeSamples */ + + + + +/**************************************************************************** + * + * Private member functions + * + ****************************************************************************/ + +void AudioFilter::deleteFilter(void) +{ + if (fv != 0) + { + if (fv->ff != 0) + { + fid_run_freebuf(fv->buf); + fid_run_free(fv->run); + free(fv->ff); + } + delete fv; + fv = 0; + } +} /* AudioFilter::deleteFilter */ + + + +/* + * This file has not been truncated + */ + diff --git a/src/async/audio/AsyncAudioFilter.h b/src/async/audio/AsyncAudioFilter.h new file mode 100644 index 0000000..bff4cc8 --- /dev/null +++ b/src/async/audio/AsyncAudioFilter.h @@ -0,0 +1,213 @@ +/** +@file AsyncAudioFilter.h +@brief Contains a class for creating a wide range of audio filters +@author Tobias Blomberg / SM0SVX +@date 2006-04-23 + +\verbatim +Async - A library for programming event driven applications +Copyright (C) 2003-2008 Tobias Blomberg / SM0SVX + +This program is free software; you can redistribute it and/or modify +it under the terms of the GNU General Public License as published by +the Free Software Foundation; either version 2 of the License, or +(at your option) any later version. + +This program is distributed in the hope that it will be useful, +but WITHOUT ANY WARRANTY; without even the implied warranty of +MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +GNU General Public License for more details. + +You should have received a copy of the GNU General Public License +along with this program; if not, write to the Free Software +Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA +\endverbatim +*/ + + +#ifndef ASYNC_AUDIO_FILTER_INCLUDED +#define ASYNC_AUDIO_FILTER_INCLUDED + + +/**************************************************************************** + * + * System Includes + * + ****************************************************************************/ + +#include + + +/**************************************************************************** + * + * Project Includes + * + ****************************************************************************/ + + + +/**************************************************************************** + * + * Local Includes + * + ****************************************************************************/ + +#include + + + +/**************************************************************************** + * + * Forward declarations + * + ****************************************************************************/ + + + +/**************************************************************************** + * + * Namespace + * + ****************************************************************************/ + +namespace Async +{ + + +/**************************************************************************** + * + * Forward declarations of classes inside of the declared namespace + * + ****************************************************************************/ + +class FidVars; + + +/**************************************************************************** + * + * Defines & typedefs + * + ****************************************************************************/ + + + +/**************************************************************************** + * + * Exported Global Variables + * + ****************************************************************************/ + + + +/**************************************************************************** + * + * Class definitions + * + ****************************************************************************/ + +/** +@brief A class for creating a wide range of audio filters +@author Tobias Blomberg / SM0SVX +@date 2006-04-23 +*/ +class AudioFilter : public AudioProcessor +{ + public: + /** + * @brief Constuctor + * @param sample_rate The sampling rate + */ + explicit AudioFilter(int sample_rate = INTERNAL_SAMPLE_RATE); + + /** + * @brief Constuctor + * @param filter_spec The filter specification + * @param sample_rate The sampling rate + * + * Use this constructor to set up at filter and call parseFilterSpec on + * the given filter specification. If the filter creation fails, this + * function will do an "exit(1)". + */ + explicit AudioFilter(const std::string &filter_spec, + int sample_rate = INTERNAL_SAMPLE_RATE); + + /** + * @brief Destructor + */ + ~AudioFilter(void); + + /** + * @brief Create the filter from the given filter specification + * @param filter_spec The filter specification + * @return Returns \em true on success or else \em false + * + * If using the constructor where a filter specification string is not + * given, use this function to set up the filter. + * This function may be called multiple times to change the filter without + * creating a new filter object. + */ + bool parseFilterSpec(const std::string &filter_spec); + + /** + * @brief Get the latest filter creation error + * @return Returns an error string if an error has occured previously + * + * If the parseFilterSpec function return \em false, this function can be + * used to retrieve an error text describing the error. + */ + std::string errorString(void) const { return error_str; } + + /** + * @brief Set the output gain of the filter + * @param gain_db The gain to set in dB + * + * Use this function to apply a gain (positive) or attenuation (negative) + * after the filter output. A gain of 6dB will amplify the signal with + * a factor of two. + */ + void setOutputGain(float gain_db); + + /** + * @brief Reset the filter state + */ + void reset(void); + + + protected: + /** + * @brief Process incoming samples and put them into the output buffer + * @param dest Destination buffer + * @param src Source buffer + * @param count Number of samples in the source buffer + * + * This function is called from the base class to do the actual + * processing of the incoming samples. All samples must + * be processed, otherwise they are lost and the output buffer will + * contain garbage. + */ + void processSamples(float *dest, const float *src, int count); + + + private: + int sample_rate; + FidVars *fv; + float output_gain; + std::string error_str; + + AudioFilter(const AudioFilter&); + AudioFilter& operator=(const AudioFilter&); + void deleteFilter(void); + +}; /* class AudioFilter */ + + +} /* namespace */ + +#endif /* ASYNC_AUDIO_FILTER_INCLUDED */ + + + +/* + * This file has not been truncated + */ + diff --git a/src/async/audio/AsyncAudioFsf.cpp b/src/async/audio/AsyncAudioFsf.cpp new file mode 100644 index 0000000..63ab6dd --- /dev/null +++ b/src/async/audio/AsyncAudioFsf.cpp @@ -0,0 +1,258 @@ +/** +@file AsyncAudioFsf.cpp +@brief Frequency Sampling Filter +@author Tobias Blomberg / SM0SVX +@date 2018-01-03 + +\verbatim +Async - A library for programming event driven applications +Copyright (C) 2003-2018 Tobias Blomberg / SM0SVX + +This program is free software; you can redistribute it and/or modify +it under the terms of the GNU General Public License as published by +the Free Software Foundation; either version 2 of the License, or +(at your option) any later version. + +This program is distributed in the hope that it will be useful, +but WITHOUT ANY WARRANTY; without even the implied warranty of +MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +GNU General Public License for more details. + +You should have received a copy of the GNU General Public License +along with this program; if not, write to the Free Software +Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA +\endverbatim +*/ + +/**************************************************************************** + * + * System Includes + * + ****************************************************************************/ + +#include +#include +#include +#include + + +/**************************************************************************** + * + * Project Includes + * + ****************************************************************************/ + + + +/**************************************************************************** + * + * Local Includes + * + ****************************************************************************/ + +#include "AsyncAudioFsf.h" + + + +/**************************************************************************** + * + * Namespaces to use + * + ****************************************************************************/ + +using namespace Async; + + + +/**************************************************************************** + * + * Defines & typedefs + * + ****************************************************************************/ + + + +/**************************************************************************** + * + * Local class definitions + * + ****************************************************************************/ + +namespace Async +{ + class AudioFsf::CombFilter + { + public: + CombFilter(size_t N, float r) + : N(N), r_fact(-std::pow(r, N)), pos(0) + { + delay = new float[N]; + std::memset(delay, 0, sizeof(*delay)*N); + //std::cout << "### CombFilter: N=" << N << " r_fact=" << r_fact + // << std::endl; + } + + ~CombFilter(void) + { + delete [] delay; + delay = 0; + } + + inline float processSample(const float& src) + { + float dest = src + delay[pos] * r_fact; + delay[pos] = src; + pos = (pos == N-1) ? 0 : pos + 1; + return dest; + } + + private: + const size_t N; + const float r_fact; + float * delay; + size_t pos; + + CombFilter(const CombFilter&); + CombFilter& operator=(const CombFilter&); + + }; /* AudioFsf::CombFilter */ + + + class AudioFsf::Resonator + { + public: + Resonator(const size_t N, const size_t k, const float r, const float H) + : gain(H), coeff1(2.0*r*cos(2.0*M_PI*k/N)), coeff2(-r*r), + z1(0.0), z2(0.0) + { + gain /= N; + if ((k == 0) || (k == N/2)) + { + gain /= 2.0; + } + if (k % 2 == 1) + { + gain = -gain; + } + //std::cout << "### Resonator: N=" << N << " k=" << k << " r=" << r + // << " H=" << H << " gain=" << gain + // << " coeff1=" << coeff1 << " coeff2=" << coeff2 << std::endl; + } + + inline float processSample(const float& src) + { + float dest = src + z1*coeff1 + z2*coeff2; + z2 = z1; + z1 = dest; + return dest * gain; + } + + private: + float gain; + const float coeff1; + const float coeff2; + float z1; + float z2; + }; +}; + + +/**************************************************************************** + * + * Prototypes + * + ****************************************************************************/ + + + +/**************************************************************************** + * + * Exported Global Variables + * + ****************************************************************************/ + + + +/**************************************************************************** + * + * Local Global Variables + * + ****************************************************************************/ + + + +/**************************************************************************** + * + * Public member functions + * + ****************************************************************************/ + +AudioFsf::AudioFsf(const size_t N, const float *coeff, const float r) +{ + assert(N % 2 == 0); + assert((r >= 0.0) && (r <= 1.0)); + m_combN = new CombFilter(N, r); + m_comb2 = new CombFilter(2, r); + for (size_t k=0; k<=N/2; ++k) + { + float H = coeff[k]; + if (H > 0.0f) + { + Resonator *res = new Resonator(N, k, r, H); + m_resonators.push_back(res); + } + } +} /* AudioFsf::AudioFsf */ + + +AudioFsf::~AudioFsf(void) +{ + for (std::vector::iterator it=m_resonators.begin(); + it!=m_resonators.end(); + ++it) + { + delete *it; + } + m_resonators.clear(); + delete m_comb2; + m_comb2 = 0; + delete m_combN; + m_combN = 0; +} /* AudioFsf::~AudioFsf */ + + +/**************************************************************************** + * + * Protected member functions + * + ****************************************************************************/ + +void AudioFsf::processSamples(float *dest, const float *src, int count) +{ + for (int i=0; iprocessSample(src[i]); + float dest2 = m_comb2->processSample(destN); + dest[i] = 0.0f; + for (std::vector::iterator it=m_resonators.begin(); + it!=m_resonators.end(); + ++it) + { + dest[i] += (*it)->processSample(dest2); + } + } +} /* AudioFsf::processSamples */ + + +/**************************************************************************** + * + * Private member functions + * + ****************************************************************************/ + + + +/* + * This file has not been truncated + */ + diff --git a/src/async/audio/AsyncAudioFsf.h b/src/async/audio/AsyncAudioFsf.h new file mode 100644 index 0000000..adb7579 --- /dev/null +++ b/src/async/audio/AsyncAudioFsf.h @@ -0,0 +1,225 @@ +/** +@file AsyncAudioFsf.h +@brief A Frequency Sampling Filter implementation +@author Tobias Blomberg / SM0SVX +@date 2018-01-03 + +\verbatim +Async - A library for programming event driven applications +Copyright (C) 2003-2018 Tobias Blomberg / SM0SVX + +This program is free software; you can redistribute it and/or modify +it under the terms of the GNU General Public License as published by +the Free Software Foundation; either version 2 of the License, or +(at your option) any later version. + +This program is distributed in the hope that it will be useful, +but WITHOUT ANY WARRANTY; without even the implied warranty of +MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +GNU General Public License for more details. + +You should have received a copy of the GNU General Public License +along with this program; if not, write to the Free Software +Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA +\endverbatim +*/ + +/** @example AsyncAudioFsf_demo.cpp +An example of how to use the Async::AudioFsf class +*/ + +#ifndef ASYNC_AUDIO_FSF_INCLUDED +#define ASYNC_AUDIO_FSF_INCLUDED + + +/**************************************************************************** + * + * System Includes + * + ****************************************************************************/ + +#include + + +/**************************************************************************** + * + * Project Includes + * + ****************************************************************************/ + +#include + + +/**************************************************************************** + * + * Local Includes + * + ****************************************************************************/ + + + +/**************************************************************************** + * + * Forward declarations + * + ****************************************************************************/ + + + +/**************************************************************************** + * + * Namespace + * + ****************************************************************************/ + +namespace Async +{ + + +/**************************************************************************** + * + * Forward declarations of classes inside of the declared namespace + * + ****************************************************************************/ + + + +/**************************************************************************** + * + * Defines & typedefs + * + ****************************************************************************/ + + + +/**************************************************************************** + * + * Exported Global Variables + * + ****************************************************************************/ + + + +/**************************************************************************** + * + * Class definitions + * + ****************************************************************************/ + +/** +@brief A Frequency Sampling Filter implementation +@author Tobias Blomberg / SM0SVX +@date 2018-01-03 + +This class implements a frequency sampling filter, FSF, as described in +"Understanding Digital Signal Processing" by "Richard G. Lyons" third edition +chapter 7.5. The variant that is implemented is the even 'N' real only variant +with a modified type-IV resonator supporting the case I constellation. A +frequency sampling filter is a FIR filter that consists of a cascade of comb +filters and a number of recursive resonators. Even though this filter contains +both non-recursive and recursive filters it has finate impulse response and +linear phase characteristics so it can replace a standard FIR filter in some +applications. + +Even though an optimally designed Parks-McClellan FIR filter often have better +performace in terms of stop band attenuation vs. transition bandwidth, the +frequency sampling filter may be much more computationally efficient. As a rule +of thumb, the frequency sampling filter is more computationally efficient than +a PM-designed FIR filter when the passband is less than fs/5 and the transition +bandwidth is less than fs/8, where fs is the sampling frequency. + +The base of the FSF (frequency sampling filter) is formed by two cascaded comb +filters. To define the frequency response of the filter, one or more resonators +are added in parallel after the comb filters. Each resonator will pass +frequencies falling within that "bin". The number of available bins are defined +using the 'N' design parameter. The binwidth is fs/N. In the real-only case the +bins 0 to N/2 can be used. The 'coeff' array is used to specify the stopband(s) +and passbands(s). Simply put, a 0 designates the bin as a stopband and a 1 +defines a passband. In reality values in between zero and one is often used to +improve filter performance with transition band coefficients. These are not +easily calculated so design tables are often used, like appendix H in the book +mentioned initially. + +Since the numeric precision in computers are finite a little tweak of the +theory is required. A dampening factor is added to make the filter stable even +though rounding errors are present. Whithout this dampening factor the filter +poles may end up outside of the unit circle which would make the filter +unstable. The drawback is that the filter will not have exactly linear phase +but the approximation should be enough for most applications since it's very +close to linear. + +As a design example, let us assume we want a bandpass filter centered around +5500Hz with a 3dB bandwidth of about 400Hz. The sampling frequency is 16000Hz. +The higher 'N' we choose, the more exact the passband will be in frequency and +the narrowser the transition bandwidth will be. However, when choosing a high +'N' we also increase the number of resonators needed to cover the passband +which increase the computational load. We thus like to keep 'N' as low as +possible. In the example above we need a quite high 'N' to get a narrow +passband that still have a flat frequency response so we choose N=128. To get +the index of the center frequency we calculate 5500/(fs/N)=44. We thus set +coeff[44]=1 and also do the same for coeff[43] and coeff[45] to get +approximately the desired bandwidth. To improve the stopband attenutation (from +like 15 to 40dB) we also need to add a couple of coefficients for the +transition regions of the filter so we set coeff[42] and coeff[46]=0.39811024. +That value has been looked up in the table mentioned above. All other positions +must be set to 0 to form the stop band. The dampening factor 'r' should be left +at its default unless there is a good reason to change it. + +\image html AsyncAudioFsfExample.png "Example filter frequency response (blue) and phase response (red)" + +\include AsyncAudioFsf_demo.cpp +*/ +class AudioFsf : public Async::AudioProcessor +{ + public: + /** + * @brief Default constructor + * @param N The number of filter "bins" + * @param coeff The N/2+1 coefficients defining the filter + * @param r The dampening factor + * + * See class description for how to choose the argument values. The coeff + * array must have the length N/2+1 where coeff[0] is the filter section + * representing DC and the coeff[N/2] filter section represents fs/2. + * coeff[0] must be set to implement a lowpass filter and coeff[N/2] must + * be set to implement a highpass filter. + */ + AudioFsf(size_t N, const float *coeff, float r=0.99999); + + /** + * @brief Destructor + */ + ~AudioFsf(void); + + protected: + /** + * @brief Process incoming samples and put them into the output buffer + * @param dest Destination buffer + * @param src Source buffer + * @param count Number of samples in the source buffer + * + * This function do the actual processing of the incoming samples. + */ + virtual void processSamples(float *dest, const float *src, int count); + + private: + class CombFilter; + class Resonator; + + CombFilter * m_combN; + CombFilter * m_comb2; + std::vector m_resonators; + + AudioFsf(const AudioFsf&); + AudioFsf& operator=(const AudioFsf&); + +}; /* class AudioFsf */ + + +} /* namespace */ + +#endif /* ASYNC_AUDIO_FSF_INCLUDED */ + +/* + * This file has not been truncated + */ diff --git a/src/async/audio/AsyncAudioGenerator.h b/src/async/audio/AsyncAudioGenerator.h new file mode 100644 index 0000000..b6f27ed --- /dev/null +++ b/src/async/audio/AsyncAudioGenerator.h @@ -0,0 +1,329 @@ +/** +@file AsyncAudioGenerator.h +@brief An audio generator +@author Tobias Blomberg / SM0SVX +@date 2015-09-28 + +\verbatim +Async - A library for programming event driven applications +Copyright (C) 2003-2019 Tobias Blomberg / SM0SVX + +This program is free software; you can redistribute it and/or modify +it under the terms of the GNU General Public License as published by +the Free Software Foundation; either version 2 of the License, or +(at your option) any later version. + +This program is distributed in the hope that it will be useful, +but WITHOUT ANY WARRANTY; without even the implied warranty of +MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +GNU General Public License for more details. + +You should have received a copy of the GNU General Public License +along with this program; if not, write to the Free Software +Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA +\endverbatim +*/ + +#ifndef ASYNC_AUDIO_GENERATOR_INCLUDED +#define ASYNC_AUDIO_GENERATOR_INCLUDED + + +/**************************************************************************** + * + * System Includes + * + ****************************************************************************/ + +#include +#include +#include + + +/**************************************************************************** + * + * Project Includes + * + ****************************************************************************/ + +#include + + +/**************************************************************************** + * + * Local Includes + * + ****************************************************************************/ + + + +/**************************************************************************** + * + * Forward declarations + * + ****************************************************************************/ + + + +/**************************************************************************** + * + * Namespace + * + ****************************************************************************/ + +namespace Async +{ + + +/**************************************************************************** + * + * Forward declarations of classes inside of the declared namespace + * + ****************************************************************************/ + + + +/**************************************************************************** + * + * Defines & typedefs + * + ****************************************************************************/ + + + +/**************************************************************************** + * + * Exported Global Variables + * + ****************************************************************************/ + + + +/**************************************************************************** + * + * Class definitions + * + ****************************************************************************/ + +/** +@brief A class for generating periodic audio signals +@author Tobias Blomberg / SM0SVX +@date 2015-09-28 + +This class is used to generate periodic audio signals. Note that audio samples +will be produced in an endless loop until the connected sink stop the flow. +This means that a sink have to be connected before enabling the generator or +the application will get stuck. There also must be some form of flow control +downstream in the audio pipe. One way to get flow control, if there are none +naturally, is to use an Async::AudioPacer. +*/ +class AudioGenerator : public Async::AudioSource +{ + public: + /** + * @brief The type of waveform to generate + */ + typedef enum { + SIN, ///< Sine wave + SQUARE, ///< Square wave + TRIANGLE ///< Triangular wave + } Waveform; + + /** + * @brief Contructor + * @param wf The waveform to use (@see Waveform) + */ + explicit AudioGenerator(Waveform wf=SIN) + : m_arg(0.0f), m_arginc(0.0f), m_peak(0.0f), + m_sample_rate(INTERNAL_SAMPLE_RATE), m_waveform(wf), m_power(0.0f), + m_enabled(false) + { + } + + /** + * @brief Destructor + */ + ~AudioGenerator(void) + { + enable(false); + } + + /** + * @brief Set which waveform to use + * @param wf The waveform to use (@see Waveform) + */ + void setWaveform(Waveform wf) + { + m_waveform = wf; + calcLevel(); + } + + /** + * @brief Set the audio frequency + * @param tone_fq The frequency in Hz + */ + void setFq(float tone_fq) + { + m_arginc = 2.0f * M_PI * tone_fq / m_sample_rate; + assert(m_arginc <= M_PI); + } + + /** + * @brief Set the power of the generated signal + * @param pwr_db The power of the signal in dBFS + * + * Use this function to set the power of the generated signal. 0dB power is + * defined as a full-scale sine wave. + */ + void setPower(float pwr_db) + { + m_power = powf(10.0f, pwr_db / 10.0f) / 2.0f; + calcLevel(); + } + + /** + * @brief Enable or disable the generator + * @param enable Set to \em true to enable the generator or \em false + * to disable it + */ + void enable(bool enable) + { + m_enabled = enable; + if (enable) + { + m_arg = 0.0f; + writeSamples(); + } + else + { + sinkFlushSamples(); + } + } + + /** + * @brief Resume audio output to the sink + * + * This function is normally only called from a connected sink object. + */ + void resumeOutput(void) + { + if (m_enabled) + { + writeSamples(); + } + } + + /** + * @brief The registered sink has flushed all samples + * + * This function is normally only called from a connected sink object. + */ + void allSamplesFlushed(void) + { + } + + private: + static const int BLOCK_SIZE = 128; + + float m_arg; + float m_arginc; + float m_peak; + int m_sample_rate; + Waveform m_waveform; + float m_power; + bool m_enabled; + + AudioGenerator(const AudioGenerator&); + AudioGenerator& operator=(const AudioGenerator&); + + /** + * @brief Calculate the peak level corresponding to the set power for + * the set waveform + */ + void calcLevel(void) + { + switch (m_waveform) + { + case SIN: + m_peak = sqrt(2.0f * m_power); + break; + case SQUARE: + m_peak = sqrt(m_power); + break; + case TRIANGLE: + m_peak = sqrt(3.0f * m_power); + break; + default: + m_peak = 0.0f; + break; + } + } + + /** + * @brief Write samples to the connected sink + */ + void writeSamples(void) + { + int written = 0; + do + { + float buf[BLOCK_SIZE]; + float arg = m_arg; + for (int i=0; i= 2.0f * M_PI) + { + arg -= 2.0f * M_PI; + } + } + written = sinkWriteSamples(buf, BLOCK_SIZE); + if (written > 0) + { + m_arg += written * m_arginc; + while (m_arg >= 2.0f * M_PI) + { + m_arg -= 2.0f * M_PI; + } + } + } while (m_enabled && (written > 0)); + } +}; /* class AudioGenerator */ + + +} /* namespace */ + +#endif /* ASYNC_AUDIO_GENERATOR_INCLUDED */ + +/* + * This file has not been truncated + */ diff --git a/src/async/audio/AsyncAudioIO.cpp b/src/async/audio/AsyncAudioIO.cpp new file mode 100644 index 0000000..606107b --- /dev/null +++ b/src/async/audio/AsyncAudioIO.cpp @@ -0,0 +1,480 @@ +/** +@file AsyncAudioIO.cpp +@brief Contains a class for handling audio input/output to an audio device +@author Tobias Blomberg +@date 2003-03-23 + +This file contains a class for handling audio input and output to an audio +device. See usage instruction in the class documentation. + +\verbatim +Async - A library for programming event driven applications +Copyright (C) 2003 Tobias Blomberg + +This program is free software; you can redistribute it and/or modify +it under the terms of the GNU General Public License as published by +the Free Software Foundation; either version 2 of the License, or +(at your option) any later version. + +This program is distributed in the hope that it will be useful, +but WITHOUT ANY WARRANTY; without even the implied warranty of +MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +GNU General Public License for more details. + +You should have received a copy of the GNU General Public License +along with this program; if not, write to the Free Software +Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA +\endverbatim +*/ + + + + +/**************************************************************************** + * + * System Includes + * + ****************************************************************************/ + +#include +#include + +#include +#include +#include +#include + + +/**************************************************************************** + * + * Project Includes + * + ****************************************************************************/ + + + +/**************************************************************************** + * + * Local Includes + * + ****************************************************************************/ + +#include "AsyncAudioDevice.h" +#include "AsyncFdWatch.h" +#include "AsyncAudioReader.h" +#include "AsyncAudioFifo.h" +#include "AsyncAudioValve.h" +#include "AsyncAudioIO.h" +#include "AsyncAudioDebugger.h" + + + +/**************************************************************************** + * + * Namespaces to use + * + ****************************************************************************/ + +using namespace std; +using namespace Async; + + + +/**************************************************************************** + * + * Defines & typedefs + * + ****************************************************************************/ + + + +/**************************************************************************** + * + * Local class definitions + * + ****************************************************************************/ + +class Async::AudioIO::InputFifo : public AudioFifo +{ + public: + InputFifo(int size, AudioDevice *audio_dev) + : AudioFifo(size), audio_dev(audio_dev), do_flush(false) + { + } + + virtual int writeSamples(const float *samples, int count) + { + do_flush = false; + if ((audio_dev->mode() != AudioDevice::MODE_WR) && + (audio_dev->mode() != AudioDevice::MODE_RDWR)) + { + return count; + } + int ret = AudioFifo::writeSamples(samples, count); + audio_dev->audioToWriteAvailable(); + return ret; + } + + virtual void flushSamples(void) + { + if ((audio_dev->mode() != AudioDevice::MODE_WR) && + (audio_dev->mode() != AudioDevice::MODE_RDWR)) + { + do_flush = false; + sourceAllSamplesFlushed(); + return; + } + do_flush = true; + if (!empty()) + { + audio_dev->audioToWriteAvailable(); + } + AudioFifo::flushSamples(); + } + + virtual void allSamplesFlushed(void) + { + do_flush = false; + AudioFifo::allSamplesFlushed(); + } + + bool doFlush(void) const { return do_flush; } + + private: + AudioDevice *audio_dev; + bool do_flush; + +}; /* Async::AudioIO::InputFifo */ + + +class Async::AudioIO::DelayedFlushAudioReader + : public AudioReader, public sigc::trackable +{ + public: + DelayedFlushAudioReader(AudioDevice *audio_dev) + : audio_dev(audio_dev), flush_timer(0, Timer::TYPE_ONESHOT, false), + is_idle(true) + { + flush_timer.expired.connect( + mem_fun(*this, &DelayedFlushAudioReader::flushDone)); + } + + ~DelayedFlushAudioReader(void) {} + + bool isIdle(void) const { return is_idle; } + + virtual int writeSamples(const float *samples, int count) + { + is_idle = false; + flush_timer.setEnable(false); + return AudioReader::writeSamples(samples, count); + } + + virtual void flushSamples(void) + { + is_idle = true; + audio_dev->flushSamples(); + long flushtime = + 1000 * audio_dev->samplesToWrite() / audio_dev->sampleRate(); + if (flushtime < 0) + { + flushtime = 0; + } + flush_timer.setEnable(false); + flush_timer.setTimeout(flushtime); + flush_timer.setEnable(true); + } + + private: + AudioDevice *audio_dev; + Timer flush_timer; + bool is_idle; + + void flushDone(Timer *timer) + { + flush_timer.setEnable(false); + AudioReader::flushSamples(); + } /* AudioIO::flushDone */ + +}; /* class Async::AudioIO::DelayedFlushAudioReader */ + + + +/**************************************************************************** + * + * Prototypes + * + ****************************************************************************/ + + + +/**************************************************************************** + * + * Exported Global Variables + * + ****************************************************************************/ + + + + +/**************************************************************************** + * + * Local Global Variables + * + ****************************************************************************/ + + + +/**************************************************************************** + * + * Public member functions + * + ****************************************************************************/ + + +void AudioIO::setSampleRate(int rate) +{ + AudioDevice::setSampleRate(rate); +} /* AudioIO::setSampleRate */ + + +void AudioIO::setBlocksize(size_t size) +{ + AudioDevice::setBlocksize(size); +} /* AudioIO::setBlocksize */ + + +size_t AudioIO::readBlocksize(void) +{ + return audio_dev->readBlocksize(); +} /* AudioIO::readBlocksize */ + + +size_t AudioIO::writeBlocksize(void) +{ + return audio_dev->writeBlocksize(); +} /* AudioIO::writeBlocksize */ + + +void AudioIO::setBlockCount(size_t count) +{ + AudioDevice::setBlockCount(count); +} /* AudioIO::setBlockCount */ + + +void AudioIO::setChannels(size_t channels) +{ + return AudioDevice::setChannels(channels); +} /* AudioIO::setBufferCount */ + + + +AudioIO::AudioIO(const string& dev_name, size_t channel) + : io_mode(MODE_NONE), audio_dev(0), + /* lead_in_pos(0), */ m_gain(1.0), sample_rate(-1), + m_channel(channel), input_valve(0), input_fifo(0), audio_reader(0) +{ + audio_dev = AudioDevice::registerAudioIO(dev_name, this); + if (audio_dev == 0) + { + return; + } + + sample_rate = audio_dev->sampleRate(); + + input_valve = new AudioValve; + input_valve->setOpen(false); + AudioSink::setHandler(input_valve); + AudioSource *prev_src = input_valve; + + input_fifo = new InputFifo(1, audio_dev); + input_fifo->setOverwrite(false); + prev_src->registerSink(input_fifo, true); + prev_src = input_fifo; + + audio_reader = new DelayedFlushAudioReader(audio_dev); + prev_src->registerSink(audio_reader, true); + prev_src = 0; + + //new AudioDebugger(input_fifo); + +} /* AudioIO::AudioIO */ + + +AudioIO::~AudioIO(void) +{ + close(); + AudioSink::clearHandler(); + delete input_valve; + AudioDevice::unregisterAudioIO(this); +} /* AudioIO::~AudioIO */ + + +bool AudioIO::isFullDuplexCapable(void) +{ + return audio_dev->isFullDuplexCapable(); +} /* AudioIO::isFullDuplexCapable */ + + +bool AudioIO::open(Mode mode) +{ + if (m_channel >= AudioDevice::getChannels()) + { + std::cerr << "*** ERROR: Audio channel out of range when opening audio " + "device \"" << audio_dev->devName() << ". " + << "The card have " << AudioDevice::getChannels() + << " channel(s) configured, but (zero based) channel number " + << m_channel << " was requested." << std::endl; + return false; + } + + if (audio_dev == 0) + { + return false; + } + + if (mode == io_mode) + { + return true; + } + + close(); + + if (mode == MODE_NONE) + { + return true; + } + + /* FIXME: Yes, I knwow... Ugly cast... */ + bool open_ok = audio_dev->open((AudioDevice::Mode)mode); + if (open_ok) + { + io_mode = mode; + input_fifo->setSize(audio_dev->writeBlocksize() * 2 + 1); + input_fifo->setPrebufSamples(audio_dev->writeBlocksize() * 2 + 1); + } + + input_valve->setOpen(true); + + return open_ok; + +} /* AudioIO::open */ + + +void AudioIO::close(void) +{ + if (io_mode == MODE_NONE) + { + return; + } + + /* + if ((io_mode == MODE_RD) || (io_mode == MODE_RDWR)) + { + read_con.disconnect(); + } + */ + + io_mode = MODE_NONE; + + input_valve->setOpen(false); + input_fifo->clear(); + + audio_dev->close(); + +} /* AudioIO::close */ + + + +/**************************************************************************** + * + * Protected member functions + * + ****************************************************************************/ + + +/* + *------------------------------------------------------------------------ + * Method: + * Purpose: + * Input: + * Output: + * Author: + * Created: + * Remarks: + * Bugs: + *------------------------------------------------------------------------ + */ + + + + +/**************************************************************************** + * + * Private member functions + * + ****************************************************************************/ + + + + +/**************************************************************************** + * + * Private member functions used by friend class AudioDevice + * + ****************************************************************************/ + +int AudioIO::readSamples(float *samples, int count) +{ + //printf("AudioIO[%s:%d]::readSamples\n", audio_dev->devName().c_str(), m_channel); + + int samples_read = audio_reader->readSamples(samples, count); + + //printf("AudioIO[%s:%d]::readSamples: count=%d sample_read=%d\n", audio_dev->devName().c_str(), m_channel, count, samples_read); + + if (m_gain != 1.0) + { + for (int i=0; idoFlush(); +} /* AudioIO::doFlush */ + + +bool AudioIO::isIdle(void) const +{ + //printf("AudioIO::doFlush\n"); + return audio_reader->isIdle(); +} /* AudioIO::isIdle */ + + +int AudioIO::audioRead(float *samples, int count) +{ + return sinkWriteSamples(samples, count); +} /* AudioIO::audioRead */ + + +unsigned AudioIO::samplesAvailable(void) +{ + //printf("AudioIO[%s:%d]::samplesAvailable: %d\n", audio_dev->devName().c_str(), m_channel, input_fifo->samplesInFifo(true)); + return input_fifo->samplesInFifo(); +} /* AudioIO::samplesAvailable */ + + + + +/* + * This file has not been truncated + */ + diff --git a/src/async/audio/AsyncAudioIO.h b/src/async/audio/AsyncAudioIO.h new file mode 100644 index 0000000..1884be5 --- /dev/null +++ b/src/async/audio/AsyncAudioIO.h @@ -0,0 +1,379 @@ +/** +@file AsyncAudioIO.h +@brief Contains a class for handling audio input/output to an audio device +@author Tobias Blomberg +@date 2003-03-23 + +This file contains a class for handling audio input and output to an audio +device. See usage instruction in the class documentation. + +\verbatim +Async - A library for programming event driven applications +Copyright (C) 2003-2008 Tobias Blomberg + +This program is free software; you can redistribute it and/or modify +it under the terms of the GNU General Public License as published by +the Free Software Foundation; either version 2 of the License, or +(at your option) any later version. + +This program is distributed in the hope that it will be useful, +but WITHOUT ANY WARRANTY; without even the implied warranty of +MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +GNU General Public License for more details. + +You should have received a copy of the GNU General Public License +along with this program; if not, write to the Free Software +Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA +\endverbatim +*/ + +/** @example AsyncAudioIO_demo.cpp +An example of how to use the Async::AudioIO class +*/ + + + +#ifndef ASYNC_AUDIO_IO_INCLUDED +#define ASYNC_AUDIO_IO_INCLUDED + + +/**************************************************************************** + * + * System Includes + * + ****************************************************************************/ + +#include +#include + + +/**************************************************************************** + * + * Project Includes + * + ****************************************************************************/ + +#include +#include +#include +#include + + +/**************************************************************************** + * + * Local Includes + * + ****************************************************************************/ + + + +/**************************************************************************** + * + * Forward declarations + * + ****************************************************************************/ + + + +/**************************************************************************** + * + * Namespace + * + ****************************************************************************/ + +namespace Async +{ + +/**************************************************************************** + * + * Forward declarations of classes inside of the declared namespace + * + ****************************************************************************/ + +class AudioDevice; +class AudioValve; +class AudioFifo; + + +/**************************************************************************** + * + * Defines & typedefs + * + ****************************************************************************/ + + + +/**************************************************************************** + * + * Exported Global Variables + * + ****************************************************************************/ + + + +/**************************************************************************** + * + * Class definitions + * + ****************************************************************************/ + +/** +@brief A class for handling audio input/output to an audio device +@author Tobias Blomberg +@date 2003-03-23 + +This is a class for handling audio input and output to an audio +device. For now, the AudioIO class only works with 16 bit stereo samples. +An example usage is shown below. + +Multiple AudioIO objects can use the same audio device as long as the device +name is exactly the same. + +\include AsyncAudioIO_demo.cpp +*/ +class AudioIO : public Async::AudioSource, public Async::AudioSink +{ + public: + /** + * @brief The different modes to open a device in + */ + typedef enum + { + MODE_NONE, ///< No mode. The same as close + MODE_RD, ///< Read + MODE_WR, ///< Write + MODE_RDWR ///< Both read and write + } Mode; + + /** + * @brief Set the sample rate used when doing future opens + * @param rate The sampling rate to use + * + * Use this function to set the sample rate used when opening audio + * devices. + * This is a global setting so all sound cards will be affected. Already + * opened sound cards will not be affected. + */ + static void setSampleRate(int rate); + + /** + * @brief Set the blocksize used when opening audio devices + * @param size The blocksize, in samples per channel, to use + * + * Use this function to set the block size used when opening audio + * devices. The block size is the size of the blocks used when reading + * and writing audio to/from the sound card. Smaller blocks give less + * delay but could cause choppy audio if the computer is too slow. + * The blocksize is set as samples per channel. For example, a blocksize + * of 256 samples at 8kHz sample rate will give a delay of 256/8000 = 32ms. + * This is a global setting so all sound cards will be affected. Already + * opened sound cards will not be affected. + */ + static void setBlocksize(size_t size); + + /** + * @brief Find out what the read (recording) blocksize is set to + * @return Returns the currently set blocksize in samples per channel + */ + size_t readBlocksize(void); + + /** + * @brief Find out what the write (playback) blocksize is set to + * @return Returns the currently set blocksize in samples per channel + */ + size_t writeBlocksize(void); + + /** + * @brief Set the block count used when opening audio devices + * @param count The block count to use + * + * Use this function to set the buffer count used when opening audio + * devices. The buffer count is the maximum number of blocks the driver + * will buffer when reading and writing audio to/from the sound card. + * Lower numbers give less delay but could cause choppy audio if the + * computer is too slow. + * This is a global setting so all sound cards will be affected. Already + * opened sound cards will not be affected. + */ + static void setBlockCount(size_t count); + + /** + * @brief Set the number of channels used when doing future opens + * @param channels The number of channels to use + * + * Use this function to set the number of channels used when opening audio + * devices. + * This is a global setting so all sound cards will be affected. Already + * opened sound cards will not be affected. + */ + static void setChannels(size_t channels); + + /** + * @brief Constructor + * @param dev_name The name of the device to use + * @param channel The channel number (zero is the first channel) + */ + AudioIO(const std::string& dev_name, size_t channel); + + /** + * @brief Destructor + */ + ~AudioIO(void); + + /** + * @brief Check if the audio device is capable of full duplex operation + * @return Return \em true if the device is capable of full duplex or + * \em false if it is not + */ + bool isFullDuplexCapable(void); + + /** + * @brief Open the audio device in the specified mode + * @param mode The mode to open the audio device in. See + * Async::AudioIO::Mode for more information + * @return Returns \em true on success or else \em false on failure + */ + bool open(Mode mode); + + /** + * @brief Close the adio device + */ + void close(void); + + /** + * @brief Find out how many samples there are in the output buffer + * @return Returns the number of samples in the output buffer on + * success or -1 on failure. + * + * This function can be used to find out how many samples there are + * in the output buffer at the moment. This can for example be used + * to find out how long it will take before the output buffer has + * been flushed. + */ + //int samplesToWrite(void) const; + + /* + * @brief Call this method to clear all samples in the buffer + * + * This method is used to clear all the samples that are in the buffer. + * That is, all samples in the buffer will be thrown away. Remaining + * samples that have already been written to the sound card will be + * flushed and when finished, the allSamplesFlushed signal is emitted. + */ + //void clearSamples(void); + + /* + * @brief Check if the audio device is busy flushing samples + * @return Returns \em true if flushing the buffer or else \em false + */ + //bool isFlushing(void) const { return is_flushing; } + + /* + * @brief Find out the current IO mode + * @return Returns the current IO mode + */ + Mode mode(void) const { return io_mode; } + + /** + * @brief Set the gain to use + * @param gain The new gain to set + * + * This function will setup the gain to use for this audio stream. + * The default gain is 1.0, that is no amplification or attenuation. + * A value < 1.0 will attenuate the audio stream and a value > 1.0 + * will result in an amplification of the audio stream. + */ + void setGain(float gain) { m_gain = gain; } + + /** + * @brief Return the gain + * @return Returns the gain + */ + float gain(void) const { return m_gain; } + + /** + * @brief Return the sample rate + * @return Returns the sample rate + */ + int sampleRate(void) const { return sample_rate; } + + /** + * @brief Return the audio channel used + * @return Returns the audio channel that was given to the constructor + */ + size_t channel(void) const { return m_channel; } + + /** + * @brief Resume audio output to the sink + * + * This function will be called when the registered audio sink is + * ready to accept more samples. + * This function is normally only called from a connected sink object. + */ + void resumeOutput(void) {} + + /** + * @brief The registered sink has flushed all samples + * + * This function will be called when all samples have been flushed in the + * registered sink. + * This function is normally only called from a connected sink object. + */ + void allSamplesFlushed(void) {} + +#if 0 + /** + * @brief Write samples into this audio sink + * @param samples The buffer containing the samples + * @param count The number of samples in the buffer + * @return Returns the number of samples that has been taken care of + */ + int writeSamples(const float *samples, int count); + + /** + * @brief Tell the sink to flush the previously written samples + * + * This function is used to tell the sink to flush previously written + * samples. When done flushing, the sink should call the + * sourceAllSamplesFlushed function. + */ + void flushSamples(void); +#endif + + + protected: + + private: + class InputFifo; + class DelayedFlushAudioReader; + + Mode io_mode; + AudioDevice *audio_dev; + float m_gain; + int sample_rate; + size_t m_channel; + AudioValve *input_valve; + InputFifo *input_fifo; + DelayedFlushAudioReader *audio_reader; + + // Methods accessed by the Async::AudioDevice class + friend class AudioDevice; + AudioDevice *device(void) const { return audio_dev; } + int readSamples(float *samples, int count); + bool doFlush(void) const; + bool isIdle(void) const; + int audioRead(float *samples, int count); + unsigned samplesAvailable(void); + +}; /* class AudioIO */ + + +} /* namespace */ + +#endif /* ASYNC_AUDIO_IO_INCLUDED */ + + +/* + * This file has not been truncated + */ + diff --git a/src/async/audio/AsyncAudioInterpolator.cpp b/src/async/audio/AsyncAudioInterpolator.cpp new file mode 100644 index 0000000..d85b080 --- /dev/null +++ b/src/async/audio/AsyncAudioInterpolator.cpp @@ -0,0 +1,190 @@ +/** +@file AsyncAudioInterpolator.cpp +@brief A_brief_description_for_this_file +@author Tobias Blomberg / SM0SVX +@date 2008-04-06 + +\verbatim +Original code by by Grant R. Griffin modified by Tobias Blomberg / SM0SVX. +Provided by Iowegian's "dspGuru" service (http://www.dspguru.com). +Copyright 2001, Iowegian International Corporation (http://www.iowegian.com) + + The Wide Open License (WOL) + +Permission to use, copy, modify, distribute and sell this software and its +documentation for any purpose is hereby granted without fee, provided that +the above copyright notice and this license appear in all source copies. +THIS SOFTWARE IS PROVIDED "AS IS" WITHOUT EXPRESS OR IMPLIED WARRANTY OF +ANY KIND. See http://www.dspguru.com/wol.htm for more information. +\endverbatim +*/ + + + +/**************************************************************************** + * + * System Includes + * + ****************************************************************************/ + +#include + + +/**************************************************************************** + * + * Project Includes + * + ****************************************************************************/ + + + +/**************************************************************************** + * + * Local Includes + * + ****************************************************************************/ + +#include "AsyncAudioInterpolator.h" + + + +/**************************************************************************** + * + * Namespaces to use + * + ****************************************************************************/ + +using namespace std; +using namespace Async; + + + +/**************************************************************************** + * + * Defines & typedefs + * + ****************************************************************************/ + + + +/**************************************************************************** + * + * Local class definitions + * + ****************************************************************************/ + + + +/**************************************************************************** + * + * Prototypes + * + ****************************************************************************/ + + + +/**************************************************************************** + * + * Exported Global Variables + * + ****************************************************************************/ + + + + +/**************************************************************************** + * + * Local Global Variables + * + ****************************************************************************/ + + + +/**************************************************************************** + * + * Public member functions + * + ****************************************************************************/ + +AudioInterpolator::AudioInterpolator(int interpolation_factor, + const float *filter_coeff, int taps) + : factor_L(interpolation_factor), L_size(taps), p_H(filter_coeff) +{ + //p_H = new float[taps]; + //memcpy(p_H, filter_coeff, taps * sizeof(*p_H)); + + setInputOutputSampleRate(1, factor_L); + + // FIXME: What if L_size does not divide evenly with factor_L? + size_t p_Z_size = L_size / factor_L; + p_Z = new float[p_Z_size]; + memset(p_Z, 0, sizeof(*p_Z) * p_Z_size); +} /* AudioInterpolator::AudioInterpolator */ + + +AudioInterpolator::~AudioInterpolator(void) +{ + delete [] p_Z; +} /* AudioInterpolator::~AudioInterpolator */ + + + + +/**************************************************************************** + * + * Protected member functions + * + ****************************************************************************/ + +void AudioInterpolator::processSamples(float *dest, const float *src, int count) +{ + int orig_count = count; + int num_taps_per_phase = L_size / factor_L; + + int num_out = 0; + while (count-- > 0) + { + // shift Z delay line up to make room for next sample + memmove(p_Z + 1, p_Z, (num_taps_per_phase - 1) * sizeof(float)); + + // copy next sample from input buffer to bottom of Z delay line + p_Z[0] = *src++; + + // calculate outputs + for (int phase_num = 0; phase_num < factor_L; phase_num++) + { + // point to the current polyphase filter + const float *p_coeff = p_H + phase_num; + + // calculate FIR sum + float sum = 0.0; + for (int tap = 0; tap < num_taps_per_phase; tap++) + { + sum += *p_coeff * p_Z[tap]; + p_coeff += factor_L; /* point to next coefficient */ + } + *dest++ = sum * factor_L; /* store scaled sum and point to next output */ + num_out++; + } + } + + //printf("num_out=%d orig_count=%d\n", num_out, orig_count); + assert(num_out == orig_count * factor_L); + +} /* AudioInterpolator::processSamples */ + + + + +/**************************************************************************** + * + * Private member functions + * + ****************************************************************************/ + + + +/* + * This file has not been truncated + */ + diff --git a/src/async/audio/AsyncAudioInterpolator.h b/src/async/audio/AsyncAudioInterpolator.h new file mode 100644 index 0000000..4f25c5a --- /dev/null +++ b/src/async/audio/AsyncAudioInterpolator.h @@ -0,0 +1,171 @@ +/** +@file AsyncAudioInterpolator.h +@brief Interpolates a lower sampling rate to a higher one +@author Tobias Blomberg / SM0SVX +@date 2008-04-06 + +\verbatim +Original code by by Grant R. Griffin modified by Tobias Blomberg / SM0SVX. +Provided by Iowegian's "dspGuru" service (http://www.dspguru.com). +Copyright 2001, Iowegian International Corporation (http://www.iowegian.com) + + The Wide Open License (WOL) + +Permission to use, copy, modify, distribute and sell this software and its +documentation for any purpose is hereby granted without fee, provided that +the above copyright notice and this license appear in all source copies. +THIS SOFTWARE IS PROVIDED "AS IS" WITHOUT EXPRESS OR IMPLIED WARRANTY OF +ANY KIND. See http://www.dspguru.com/wol.htm for more information. +\endverbatim +*/ + +#ifndef ASYNC_AUDIO_INTERPOLATOR_INCLUDED +#define ASYNC_AUDIO_INTERPOLATOR_INCLUDED + + +/**************************************************************************** + * + * System Includes + * + ****************************************************************************/ + + + +/**************************************************************************** + * + * Project Includes + * + ****************************************************************************/ + +#include + + + +/**************************************************************************** + * + * Local Includes + * + ****************************************************************************/ + + + +/**************************************************************************** + * + * Forward declarations + * + ****************************************************************************/ + + + +/**************************************************************************** + * + * Namespace + * + ****************************************************************************/ + +namespace Async +{ + + +/**************************************************************************** + * + * Forward declarations of classes inside of the declared namespace + * + ****************************************************************************/ + + + +/**************************************************************************** + * + * Defines & typedefs + * + ****************************************************************************/ + + + +/**************************************************************************** + * + * Exported Global Variables + * + ****************************************************************************/ + + + +/**************************************************************************** + * + * Class definitions + * + ****************************************************************************/ + +/** +@brief Interpolates a lower sampling rate to a higher one +@author Tobias Blomberg / SM0SVX +@date 2008-04-06 + +This audio pipe class will interpolate an audio stream up to a higher sampling +rate. Interpolation is a process where the sampling rate is increased by an +integer factor. After the increase in sampling rate, a lowpass filter must be +applied to avoid aliasing effects. This filter is built into this component. +However, the filter coefficients (FIR) must be calculated manually. + +Use this web page to calculate the coefficients: +http://www.dsptutor.freeuk.com/remez/RemezFIRFilterDesign.html + +This implementation is based on the multirate FAQ at dspguru.com: +http://dspguru.com/info/faqs/mrfaq.htm +*/ +class AudioInterpolator : public Async::AudioProcessor +{ + public: + /** + * @brief Constuctor + * @param interpolation_factor The factor to increase the sample rate with + * @param filter_coeff The filter coefficients + * @param taps The number of taps in the filter + */ + AudioInterpolator(int interpolation_factor, const float *filter_coeff, + int taps); + + /** + * @brief Destructor + */ + ~AudioInterpolator(void); + + + protected: + /** + * @brief Process incoming samples and put them into the output buffer + * @param dest Destination buffer + * @param src Source buffer + * @param count Number of samples in the source buffer + * + * This function should be reimplemented by the inheriting class to + * do the actual processing of the incoming samples. All samples must + * be processed, otherwise they are lost and the output buffer will + * contain garbage. + */ + virtual void processSamples(float *dest, const float *src, int count); + + + private: + const int factor_L; + float *p_Z; + int L_size; + const float *p_H; + + AudioInterpolator(const AudioInterpolator&); + AudioInterpolator& operator=(const AudioInterpolator&); + +}; /* class AudioInterpolator */ + + +} /* namespace */ + +#endif /* ASYNC_AUDIO_INTERPOLATOR_INCLUDED */ + + + +/* + * This file has not been truncated + */ + diff --git a/src/async/audio/AsyncAudioJitterFifo.cpp b/src/async/audio/AsyncAudioJitterFifo.cpp new file mode 100644 index 0000000..a31ea9b --- /dev/null +++ b/src/async/audio/AsyncAudioJitterFifo.cpp @@ -0,0 +1,325 @@ +/** +@file AsyncAudioJitterFifo.cpp +@brief A FIFO for handling audio samples +@author Tobias Blomberg / SM0SVX +@date 2007-10-06 + +Implements a jitter-tolerant FIFO for storing samples. + +\verbatim +Async - A library for programming event driven applications +Copyright (C) 2003-2009 Tobias Blomberg / SM0SVX + +This program is free software; you can redistribute it and/or modify +it under the terms of the GNU General Public License as published by +the Free Software Foundation; either version 2 of the License, or +(at your option) any later version. + +This program is distributed in the hope that it will be useful, +but WITHOUT ANY WARRANTY; without even the implied warranty of +MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +GNU General Public License for more details. + +You should have received a copy of the GNU General Public License +along with this program; if not, write to the Free Software +Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA +\endverbatim +*/ + + + +/**************************************************************************** + * + * System Includes + * + ****************************************************************************/ + +#include +#include +#include +#include + + +/**************************************************************************** + * + * Project Includes + * + ****************************************************************************/ + + + +/**************************************************************************** + * + * Local Includes + * + ****************************************************************************/ + +#include "AsyncAudioJitterFifo.h" + + + +/**************************************************************************** + * + * Namespaces to use + * + ****************************************************************************/ + +using namespace std; +using namespace Async; + + +/**************************************************************************** + * + * Defines & typedefs + * + ****************************************************************************/ + + + +/**************************************************************************** + * + * Local class definitions + * + ****************************************************************************/ + + + +/**************************************************************************** + * + * Prototypes + * + ****************************************************************************/ + + + +/**************************************************************************** + * + * Exported Global Variables + * + ****************************************************************************/ + + + + +/**************************************************************************** + * + * Local Global Variables + * + ****************************************************************************/ + +static const unsigned MAX_WRITE_SIZE = 800; + + +/**************************************************************************** + * + * Public member functions + * + ****************************************************************************/ + + +AudioJitterFifo::AudioJitterFifo(unsigned fifo_size) + : fifo_size(fifo_size), head(0), tail(0), + output_stopped(false), prebuf(true), is_flushing(false) +{ + assert(fifo_size > 0); + fifo = new float[fifo_size]; +} /* AudioJitterFifo */ + + +AudioJitterFifo::~AudioJitterFifo(void) +{ + delete [] fifo; +} /* ~AudioJitterFifo */ + + +void AudioJitterFifo::setSize(unsigned new_size) +{ + assert(fifo_size > 0); + if (new_size != fifo_size) + { + delete [] fifo; + fifo_size = new_size; + fifo = new float[fifo_size]; + } + clear(); +} /* AudioJitterFifo::setSize */ + + +unsigned AudioJitterFifo::samplesInFifo(void) const +{ + unsigned samples_in_buffer = (head - tail + fifo_size) % fifo_size; + + if (prebuf && !is_flushing) + { + if (samples_in_buffer < (fifo_size >> 1)) + { + return 0; + } + } + + return samples_in_buffer; + +} /* AudioJitterFifo::samplesInFifo */ + + +void AudioJitterFifo::clear(void) +{ + bool was_empty = empty(); + + tail = head = 0; + prebuf = true; + output_stopped = false; + + if (is_flushing) + { + is_flushing = false; + if (!was_empty) + { + sinkFlushSamples(); + } + } +} /* AudioJitterFifo::clear */ + + +int AudioJitterFifo::writeSamples(const float *samples, int count) +{ + assert(count > 0); + + if (is_flushing) + { + is_flushing = false; + prebuf = true; + } + + int samples_written = 0; + while (samples_written < count) + { + fifo[head] = samples[samples_written++]; + head = (head + 1) % fifo_size; + if (head == tail) + { + // Throw away the first half of the buffer. + tail = (tail + (fifo_size >> 1)) % fifo_size; + } + } + + if (samplesInFifo() > 0) + { + prebuf = false; + } + + writeSamplesFromFifo(); + + return samples_written; + +} /* writeSamples */ + + +void AudioJitterFifo::flushSamples(void) +{ + is_flushing = true; + if (empty()) + { + sinkFlushSamples(); + } +} /* AudioJitterFifo::flushSamples */ + + +void AudioJitterFifo::resumeOutput(void) +{ + if (output_stopped) + { + output_stopped = false; + writeSamplesFromFifo(); + } +} /* resumeOutput */ + + + +/**************************************************************************** + * + * Protected member functions + * + ****************************************************************************/ + + + +void AudioJitterFifo::allSamplesFlushed(void) +{ + if (empty()) + { + if (is_flushing) + { + is_flushing = false; + sourceAllSamplesFlushed(); + } + prebuf = true; + } +} /* AudioJitterFifo::allSamplesFlushed */ + + + +/**************************************************************************** + * + * Private member functions + * + ****************************************************************************/ + + +void AudioJitterFifo::writeSamplesFromFifo(void) +{ + if (output_stopped) + { + return; + } + + int samples_written; + if (prebuf && !empty()) + { + float silence[MAX_WRITE_SIZE]; + for (unsigned i=0; i 0) && (--timeout)); + } + else + { + do + { + int samples_to_write = min(MAX_WRITE_SIZE, samplesInFifo()); + int to_end_of_fifo = fifo_size - tail; + samples_to_write = min(samples_to_write, to_end_of_fifo); + samples_written = sinkWriteSamples(fifo+tail, samples_to_write); + tail = (tail + samples_written) % fifo_size; + } while((samples_written > 0) && !empty()); + } + + if (samples_written == 0) + { + output_stopped = true; + } + + if (empty()) + { + if (is_flushing) + { + sinkFlushSamples(); + } + else + { + prebuf = true; + } + } + +} /* writeSamplesFromFifo */ + + + +/* + * This file has not been truncated + */ diff --git a/src/async/audio/AsyncAudioJitterFifo.h b/src/async/audio/AsyncAudioJitterFifo.h new file mode 100644 index 0000000..e61e6d1 --- /dev/null +++ b/src/async/audio/AsyncAudioJitterFifo.h @@ -0,0 +1,229 @@ +/** +@file AsyncAudioJitterFifo.h +@brief A FIFO for handling audio samples +@author Tobias Blomberg / SM0SVX +@date 2007-10-06 + +Implements a jitter-tolerant FIFO for storing samples. + +\verbatim +Async - A library for programming event driven applications +Copyright (C) 2003-2009 Tobias Blomberg / SM0SVX + +This program is free software; you can redistribute it and/or modify +it under the terms of the GNU General Public License as published by +the Free Software Foundation; either version 2 of the License, or +(at your option) any later version. + +This program is distributed in the hope that it will be useful, +but WITHOUT ANY WARRANTY; without even the implied warranty of +MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +GNU General Public License for more details. + +You should have received a copy of the GNU General Public License +along with this program; if not, write to the Free Software +Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA +\endverbatim +*/ + + +#ifndef ASYNC_AUDIO_JITTER_FIFO_INCLUDED +#define ASYNC_AUDIO_JITTER_FIFO_INCLUDED + + +/**************************************************************************** + * + * System Includes + * + ****************************************************************************/ + + + +/**************************************************************************** + * + * Project Includes + * + ****************************************************************************/ + +#include +#include + + +/**************************************************************************** + * + * Local Includes + * + ****************************************************************************/ + + + +/**************************************************************************** + * + * Forward declarations + * + ****************************************************************************/ + + + +/**************************************************************************** + * + * Namespace + * + ****************************************************************************/ + +namespace Async +{ + + +/**************************************************************************** + * + * Forward declarations of classes inside of the declared namespace + * + ****************************************************************************/ + + + +/**************************************************************************** + * + * Defines & typedefs + * + ****************************************************************************/ + + + +/**************************************************************************** + * + * Exported Global Variables + * + ****************************************************************************/ + + + +/**************************************************************************** + * + * Class definitions + * + ****************************************************************************/ + +/** +@brief A FIFO class for handling audio samples +@author Tobias Blomberg / SM0SVX +@date 2007-10-06 + +This class implements a jitter-tolerant FIFO for handling audio samples. +The FIFO is intended to buffer samples that arrive with a certain amount +of sample rate or packet jitter. Under normal operation, the FIFO is kept +half full. Varying sample rates or packet rates slowly move the amount of +samples out of center. When the FIFO reaches a full or empty state, it is +automatically reset to the half-full state. +*/ +class AudioJitterFifo : public AudioSink, public AudioSource +{ + public: + /** + * @brief Constuctor + * @param fifo_size This is the size of the fifo expressed in number + * of samples. + */ + explicit AudioJitterFifo(unsigned fifo_size); + + /** + * @brief Destructor + */ + virtual ~AudioJitterFifo(void); + + /** + * @brief Set the size of the FIFO + * @param new_size This is the size of the fifo expressed in number + * of samples. + * + * Use this function to set the size of the FIFO. In doing this, the + * FIFO will also be cleared. + */ + void setSize(unsigned new_size); + + /** + * @brief Check if the FIFO is empty + * @return Returns \em true if the FIFO is empty or else \em false + */ + bool empty(void) const { return (tail == head); } + + /** + * @brief Find out how many samples there are in the FIFO + * @return Returns the number of samples in the FIFO + */ + unsigned samplesInFifo(void) const; + + /** + * @brief Clear all samples from the FIFO + * + * This will immediately reset the FIFO and discard all samples. + * The source will be told that all samples have been flushed. + */ + void clear(void); + + /** + * @brief Write samples into the FIFO + * @param samples The buffer containing the samples + * @param count The number of samples in the buffer + * @return Returns the number of samples that has been taken care of + * + * This function is used to write audio into the FIFO. If it + * returns 0, no more samples should be written until the resumeOutput + * function in the source have been called. + * This function is normally only called from a connected source object. + */ + virtual int writeSamples(const float *samples, int count); + + /** + * @brief Tell the FIFO to flush the previously written samples + * + * This function is used to tell the FIFO to flush previously written + * samples. + * This function is normally only called from a connected source object. + */ + virtual void flushSamples(void); + + /** + * @brief Resume audio output to the connected sink + * + * This function will be called when the registered audio sink is ready + * to accept more samples. + * This function is normally only called from a connected sink object. + */ + virtual void resumeOutput(void); + + + protected: + /** + * @brief The registered sink has flushed all samples + * + * This function will be called when all samples have been flushed in the + * registered sink. + * This function is normally only called from a connected sink object. + */ + virtual void allSamplesFlushed(void); + + + private: + float *fifo; + unsigned fifo_size; + unsigned head, tail; + bool output_stopped; + bool prebuf; + bool is_flushing; + + void writeSamplesFromFifo(void); + +}; /* class AudioJitterFifo */ + + +} /* namespace */ + +#endif /* ASYNC_AUDIO_JITTER_FIFO_INCLUDED */ + + +/* + * This file has not been truncated + */ + diff --git a/src/async/audio/AsyncAudioLADSPAPlugin.cpp b/src/async/audio/AsyncAudioLADSPAPlugin.cpp new file mode 100644 index 0000000..f65e399 --- /dev/null +++ b/src/async/audio/AsyncAudioLADSPAPlugin.cpp @@ -0,0 +1,732 @@ +/** +@file AsyncAudioLADSPAPlugin.cpp +@brief A class for using a LADSPA plugin as an audio processor +@author Tobias Blomberg / SM0SVX +@date 2023-12-09 + +\verbatim +Async - A library for programming event driven applications +Copyright (C) 2003-2024 Tobias Blomberg / SM0SVX + +This program is free software; you can redistribute it and/or modify +it under the terms of the GNU General Public License as published by +the Free Software Foundation; either version 2 of the License, or +(at your option) any later version. + +This program is distributed in the hope that it will be useful, +but WITHOUT ANY WARRANTY; without even the implied warranty of +MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +GNU General Public License for more details. + +You should have received a copy of the GNU General Public License +along with this program; if not, write to the Free Software +Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA +\endverbatim +*/ + +/**************************************************************************** + * + * System Includes + * + ****************************************************************************/ + +//#define _DEFAULT_SOURCE +#include +#include +#include + +#include +#include +#include +#include + + +/**************************************************************************** + * + * Project Includes + * + ****************************************************************************/ + + + +/**************************************************************************** + * + * Local Includes + * + ****************************************************************************/ + +#include "AsyncAudioLADSPAPlugin.h" + + +/**************************************************************************** + * + * Namespaces to use + * + ****************************************************************************/ + +using namespace Async; + + +/**************************************************************************** + * + * Defines & typedefs + * + ****************************************************************************/ + + + +/**************************************************************************** + * + * Static class variables + * + ****************************************************************************/ + + + +/**************************************************************************** + * + * Local class definitions + * + ****************************************************************************/ + +namespace { + + +/**************************************************************************** + * + * Local functions + * + ****************************************************************************/ + + + +}; /* End of anonymous namespace */ + +/**************************************************************************** + * + * Public member functions + * + ****************************************************************************/ + +bool AudioLADSPAPlugin::findPluginsInDir(std::string dir) +{ + if (dir.empty()) + { + return false; + } + if (dir[dir.length()-1] != '/') + { + dir += "/"; + } + //std::cout << "### dir=" << dir << std::endl; + struct dirent **namelist; + int n = scandir(dir.c_str(), &namelist, + [](const struct dirent* de) -> int + { + auto len = strlen(de->d_name); + return (len > 3) && (strcmp(de->d_name+len-3, ".so") == 0); + }, + alphasort); + if (n == -1) + { + perror(std::string("scandir(" + dir + ")").c_str()); + return false; + } + + while (n--) + { + //std::cout << "### " << namelist[n]->d_name << std::endl; + PluginIndex i = 0; + for (;;) + { + AudioLADSPAPlugin p(dir + namelist[n]->d_name, i++); + if (p.ladspaDescriptor() == nullptr) + { + break; + } + auto inst = std::make_shared(); + inst->m_path = p.path(); + inst->m_index = i-1; + inst->m_unique_id = p.uniqueId(); + inst->m_label = p.label(); + labelMap()[inst->m_label] = inst; + idMap()[inst->m_unique_id] = inst; + } + free(namelist[n]); + } + free(namelist); + return true; +} /* AudioLADSPAPlugin::findPluginsInDir */ + + +bool AudioLADSPAPlugin::findPlugins(void) +{ + std::string ladspa_path(LADSPA_PLUGIN_DIRS); + const char* env_ladspa_path = getenv("LADSPA_PATH"); + if (env_ladspa_path != nullptr) + { + ladspa_path = env_ladspa_path; + } + std::string::size_type begin = 0, end = 0; + do + { + end = ladspa_path.find(':', begin); + findPluginsInDir(ladspa_path.substr(begin, end)); + begin = end + 1; + } while (end != std::string::npos); + return true; +} /* AudioLADSPAPlugin::findPlugins */ + + +AudioLADSPAPlugin::AudioLADSPAPlugin(AudioLADSPAPlugin::UniqueID id) +{ + if (idMap().empty()) + { + findPlugins(); + } + auto instit = idMap().find(id); + if (instit == idMap().end()) + { + return; + } + auto inst_info = instit->second; + m_path = inst_info->m_path; + m_index = inst_info->m_index; +} /* AudioLADSPAPlugin::AudioLADSPAPlugin */ + + +AudioLADSPAPlugin::AudioLADSPAPlugin(const std::string& label) +{ + if (labelMap().empty()) + { + findPlugins(); + } + auto instit = labelMap().find(label); + if (instit == labelMap().end()) + { + return; + } + auto inst_info = instit->second; + m_path = inst_info->m_path; + m_index = inst_info->m_index; +} /* AudioLADSPAPlugin::AudioLADSPAPlugin */ + + +AudioLADSPAPlugin::~AudioLADSPAPlugin(void) +{ + deactivate(); + + delete [] m_ctrl_buf; + m_ctrl_buf = nullptr; + + if ((m_desc != nullptr) && (m_desc->cleanup != nullptr) && + (m_inst_handle != nullptr)) + { + m_desc->cleanup(m_inst_handle); + } + m_inst_handle = nullptr; + m_desc = nullptr; + + if (m_handle != nullptr) + { + if (dlclose(m_handle) != 0) + { + std::cerr << "*** ERROR: Failed to unload plugin " + << m_path << ": " << dlerror() << std::endl; + } + } + m_handle = nullptr; +} /* AudioLADSPAPlugin::~AudioLADSPAPlugin */ + + +bool AudioLADSPAPlugin::initialize(void) +{ + if (m_path.empty()) + { + //std::cerr << "*** ERROR: Empty LADSPA plugin path" << std::endl; + return false; + } + + if (ladspaDescriptor() == nullptr) + { + std::cerr << "*** ERROR: Could not find LADSPA instance for index " + << m_index << " in plugin " << m_path << std::endl; + return false; + } + + m_inst_handle = m_desc->instantiate(m_desc, INTERNAL_SAMPLE_RATE); + if (m_inst_handle == nullptr) + { + std::cerr << "*** ERROR: Could not instantiate LADSPA instance for index " + << m_index << " in plugin " << m_path << std::endl; + return false; + } + + m_ctrl_buf = new LADSPA_Data[m_desc->PortCount]; + + for (PortNumber i=0; iPortCount; ++i) + { + const LADSPA_PortDescriptor& port_desc = m_desc->PortDescriptors[i]; + + if (!LADSPA_IS_PORT_INPUT(port_desc) && !LADSPA_IS_PORT_OUTPUT(port_desc)) + { + std::cerr << "*** ERROR: Invalid LADSPA plugin " << m_path + << " with index " << m_index << ". Port " << i + << " is neither input nor output." << std::endl; + return false; + } + if (!LADSPA_IS_PORT_CONTROL(port_desc) && !LADSPA_IS_PORT_AUDIO(port_desc)) + { + std::cerr << "*** ERROR: Invalid LADSPA plugin " << m_path + << " with index " << m_index << ". Port " << i + << " is neither type control nor audio." << std::endl; + return false; + } + + if (LADSPA_IS_PORT_CONTROL(port_desc)) + { + if (!setDefault(i)) + { + std::cerr << "*** ERROR: Illegal default handling in LADSPA instance " + "for index " << m_index << " in plugin " << m_path + << std::endl; + return false; + } + m_desc->connect_port(m_inst_handle, i, &m_ctrl_buf[i]); + } + if (LADSPA_IS_PORT_AUDIO(port_desc)) + { + if (LADSPA_IS_PORT_INPUT(port_desc)) + { + if (m_sample_input_port != NOPORT) + { + std::cerr << "*** ERROR: Only single audio input port LADSPA " + "instances supported but index " << m_index + << " in plugin " << m_path + << " has multiple audio input ports" + << std::endl; + return false; + } + m_sample_input_port = i; + } + else + { + if (m_sample_output_port != NOPORT) + { + std::cerr << "*** ERROR: Only single audio output port LADSPA " + "instances supported but index " << m_index + << " in plugin " << m_path + << " has multiple audio output ports" + << std::endl; + return false; + } + m_sample_output_port = i; + } + } + } + + if ((m_sample_input_port == NOPORT) || (m_sample_output_port == NOPORT)) + { + std::cerr << "*** ERROR: LADSPA instances must have exactly one input " + "port and one output port but index " << m_index + << " in plugin " << m_path + << " is missing an input or output port" << std::endl; + return false; + } + + activate(); + + return true; + +} /* AudioLADSPAPlugin::initialize */ + + +bool AudioLADSPAPlugin::setControl(PortNumber portno, LADSPA_Data val) +{ + assert(m_desc != nullptr); + assert(m_ctrl_buf != nullptr); + if (portno >= m_desc->PortCount) + { + return false; + } + const LADSPA_PortDescriptor& port_desc = m_desc->PortDescriptors[portno]; + if (!LADSPA_IS_PORT_CONTROL(port_desc) || !LADSPA_IS_PORT_INPUT(port_desc)) + { + return false; + } + + float (*conv)(float) = [](float x) { return x; }; + const auto& port_range_hint = m_desc->PortRangeHints[portno]; + const auto& hint_desc = port_range_hint.HintDescriptor; + LADSPA_Data mult = 1.0f; + if (LADSPA_IS_HINT_SAMPLE_RATE(hint_desc)) + { + mult = INTERNAL_SAMPLE_RATE; + } + auto lower_bound = port_range_hint.LowerBound * mult; + auto upper_bound = port_range_hint.UpperBound * mult; + if (LADSPA_IS_HINT_INTEGER(hint_desc)) + { + conv = roundf; + } + if (LADSPA_IS_HINT_BOUNDED_BELOW(hint_desc) && (val < lower_bound)) + { + val = conv(lower_bound); + } + if (LADSPA_IS_HINT_BOUNDED_ABOVE(hint_desc) && (val > upper_bound)) + { + val = conv(upper_bound); + } + + m_ctrl_buf[portno] = val; + + return true; +} /* AudioLADSPAPlugin::setControl */ + + +AudioLADSPAPlugin::PortNumber +AudioLADSPAPlugin::findControlInputByName(const std::string& name) +{ + for (PortNumber i=0; iPortCount; ++i) + { + LADSPA_PortDescriptor port_desc = m_desc->PortDescriptors[i]; + if (LADSPA_IS_PORT_CONTROL(port_desc) && LADSPA_IS_PORT_INPUT(port_desc)) + { + std::string port_name(m_desc->PortNames[i]); + if (port_name.rfind(name, 0) == 0) + { + return i; + } + } + } + return npos; +} /* AudioLADSPAPlugin::findControlInputByName */ + + +void AudioLADSPAPlugin::activate(void) +{ + assert((m_desc != nullptr) && (m_inst_handle != nullptr)); + if ((m_desc->activate != nullptr) && !m_is_active) + { + m_desc->activate(m_inst_handle); + } + m_is_active = false; +} /* AudioLADSPAPlugin::activate */ + + +void AudioLADSPAPlugin::deactivate(void) +{ + if ((m_desc != nullptr) && (m_desc->deactivate != nullptr) && + (m_inst_handle != nullptr) && m_is_active) + { + m_desc->deactivate(m_inst_handle); + } + m_is_active = false; +} /* AudioLADSPAPlugin::deactivate */ + + +bool AudioLADSPAPlugin::portIsControl(PortNumber portno) const +{ + assert(m_desc != nullptr); + if (portno >= m_desc->PortCount) + { + return false; + } + const LADSPA_PortDescriptor& port_desc = m_desc->PortDescriptors[portno]; + return LADSPA_IS_PORT_CONTROL(port_desc); +} /* AudioLADSPAPlugin::portIsControl */ + + +bool AudioLADSPAPlugin::portIsAudio(PortNumber portno) const +{ + assert(m_desc != nullptr); + if (portno >= m_desc->PortCount) + { + return false; + } + const LADSPA_PortDescriptor& port_desc = m_desc->PortDescriptors[portno]; + return LADSPA_IS_PORT_AUDIO(port_desc); +} /* AudioLADSPAPlugin::portIsAudio */ + + +bool AudioLADSPAPlugin::portIsInput(PortNumber portno) const +{ + assert(m_desc != nullptr); + if (portno >= m_desc->PortCount) + { + return false; + } + const LADSPA_PortDescriptor& port_desc = m_desc->PortDescriptors[portno]; + return LADSPA_IS_PORT_INPUT(port_desc); +} /* AudioLADSPAPlugin::portIsInput */ + + +bool AudioLADSPAPlugin::portIsOutput(PortNumber portno) const +{ + assert(m_desc != nullptr); + if (portno >= m_desc->PortCount) + { + return false; + } + const LADSPA_PortDescriptor& port_desc = m_desc->PortDescriptors[portno]; + return LADSPA_IS_PORT_OUTPUT(port_desc); +} /* AudioLADSPAPlugin::portIsOutput */ + + +void AudioLADSPAPlugin::print(const std::string& prefix) +{ + assert(m_desc != nullptr); + + std::cout << prefix << "\"" << m_desc->Name << "\"" + << " (" << m_desc->Label << ")" + << " by \"" << m_desc->Maker << "\"" + << " (C) " << m_desc->Copyright + << std::endl; + std::cout << prefix << " Path: " << m_path << std::endl; + + for (PortNumber i=0; iPortCount; ++i) + { + LADSPA_PortDescriptor port_desc = m_desc->PortDescriptors[i]; + + if (LADSPA_IS_PORT_AUDIO(port_desc)) + { + continue; + } + + std::cout << prefix << " " + << (LADSPA_IS_PORT_INPUT(port_desc) ? "In " : "Out") + << ": \"" << m_desc->PortNames[i] << "\" "; + + //if (LADSPA_IS_PORT_CONTROL(port_desc)) + //{ + // std::cout << "control "; + //} + + float (*conv)(float) = [](float x) { return x; }; + const auto& port_range_hint = m_desc->PortRangeHints[i]; + const auto& hint_desc = port_range_hint.HintDescriptor; + if (LADSPA_IS_HINT_INTEGER(hint_desc)) + { + //std::cout << "int:"; + conv = roundf; + } + //else if (LADSPA_IS_HINT_TOGGLED(hint_desc)) + //{ + // std::cout << "bool:"; + //} + //else + //{ + // std::cout << "float:"; + //} + + bool bounded_below = LADSPA_IS_HINT_BOUNDED_BELOW(hint_desc); + bool bounded_above = LADSPA_IS_HINT_BOUNDED_ABOVE(hint_desc); + if (bounded_below || bounded_above) + { + LADSPA_Data samp_rate = 1.0f; + if (LADSPA_IS_HINT_SAMPLE_RATE(hint_desc)) + { + samp_rate = INTERNAL_SAMPLE_RATE; + } + std::cout << "["; + if (bounded_below) + { + std::cout << conv(samp_rate * port_range_hint.LowerBound); + } + std::cout << ","; + if (bounded_above) + { + std::cout << conv(samp_rate * port_range_hint.UpperBound); + } + std::cout << "]"; + } + + if (LADSPA_IS_HINT_LOGARITHMIC(hint_desc)) + { + std::cout << " (log)"; + } + + std::cout << " = " << m_ctrl_buf[i]; + + std::cout << std::endl; + } +} /* AudioLADSPAPlugin::print */ + + +/**************************************************************************** + * + * Protected member functions + * + ****************************************************************************/ + +void AudioLADSPAPlugin::processSamples(float *dest, const float *src, + int count) +{ + assert(dest != nullptr); + assert(src != nullptr); + assert(m_sample_input_port != NOPORT); + assert(m_sample_output_port != NOPORT); + if (count <= 0) + { + return; + } + m_desc->connect_port(m_inst_handle, m_sample_input_port, + const_cast(src)); + m_desc->connect_port(m_inst_handle, m_sample_output_port, dest); + m_desc->run(m_inst_handle, count); +} /* AudioLADSPAPlugin::processSamples */ + + +/**************************************************************************** + * + * Private member functions + * + ****************************************************************************/ + +const LADSPA_Descriptor* AudioLADSPAPlugin::ladspaDescriptor(void) +{ + m_handle = dlopen(m_path.c_str(), RTLD_NOW); + if (m_handle == nullptr) + { + std::cerr << "*** ERROR: Failed to load plugin " + << m_path << ": " << dlerror() << std::endl; + return nullptr; + } + + using ConstructFunc = LADSPA_Descriptor_Function; + ConstructFunc construct = (ConstructFunc)dlsym(m_handle, "ladspa_descriptor"); + if (construct == nullptr) + { + std::cerr << "*** ERROR: Could not find LADSPA descriptor function for " + "plugin " << m_path << ": " << dlerror() << std::endl; + return nullptr; + } + + m_desc = construct(m_index); + return m_desc; +} /* AudioLADSPAPlugin::ladspaDescriptor */ + + +bool AudioLADSPAPlugin::setDefault(PortNumber portno) +{ + assert(m_desc != nullptr); + + LADSPA_Data& def = m_ctrl_buf[portno]; + const auto& port_range_hint = m_desc->PortRangeHints[portno]; + const auto& hint_desc = port_range_hint.HintDescriptor; + + if (!LADSPA_IS_HINT_HAS_DEFAULT(hint_desc)) + { + def = 0.0f; + return true; + } + + LADSPA_Data mult = 1.0f; + if (LADSPA_IS_HINT_SAMPLE_RATE(hint_desc)) + { + mult = INTERNAL_SAMPLE_RATE; + } + auto lower_bound = port_range_hint.LowerBound * mult; + auto upper_bound = port_range_hint.UpperBound * mult; + + if (LADSPA_IS_HINT_DEFAULT_MINIMUM(hint_desc)) + { + if (!LADSPA_IS_HINT_BOUNDED_BELOW(hint_desc)) + { + return false; + } + def = lower_bound; + } + + if (LADSPA_IS_HINT_DEFAULT_LOW(hint_desc)) + { + if (!LADSPA_IS_HINT_BOUNDED_BELOW(hint_desc) || + !LADSPA_IS_HINT_BOUNDED_ABOVE(hint_desc)) + { + return false; + } + if (LADSPA_IS_HINT_LOGARITHMIC(hint_desc)) + { + def = expf(logf(lower_bound)*0.75 + logf(upper_bound)*0.25); + } + else + { + def = lower_bound*0.75 + upper_bound*0.25; + } + } + + if (LADSPA_IS_HINT_DEFAULT_MIDDLE(hint_desc)) + { + if (!LADSPA_IS_HINT_BOUNDED_BELOW(hint_desc) || + !LADSPA_IS_HINT_BOUNDED_ABOVE(hint_desc)) + { + return false; + } + if (LADSPA_IS_HINT_LOGARITHMIC(hint_desc)) + { + def = expf(logf(lower_bound)*0.5 + logf(upper_bound)*0.5); + } + else + { + def = lower_bound*0.5 + upper_bound*0.5; + } + } + + if (LADSPA_IS_HINT_DEFAULT_HIGH(hint_desc)) + { + if (!LADSPA_IS_HINT_BOUNDED_BELOW(hint_desc) || + !LADSPA_IS_HINT_BOUNDED_ABOVE(hint_desc)) + { + return false; + } + if (LADSPA_IS_HINT_LOGARITHMIC(hint_desc)) + { + def = expf(logf(lower_bound)*0.25 + logf(upper_bound)*0.75); + } + else + { + def = lower_bound*0.25 + upper_bound*0.75; + } + } + + if (LADSPA_IS_HINT_DEFAULT_MAXIMUM(hint_desc)) + { + if (!LADSPA_IS_HINT_BOUNDED_ABOVE(hint_desc)) + { + return false; + } + def = upper_bound; + } + + if (LADSPA_IS_HINT_DEFAULT_0(hint_desc)) + { + def = 0.0f; + } + + if (LADSPA_IS_HINT_DEFAULT_1(hint_desc)) + { + def = 1.0f; + } + + if (LADSPA_IS_HINT_DEFAULT_100(hint_desc)) + { + def = 100.0f; + } + + if (LADSPA_IS_HINT_DEFAULT_440(hint_desc)) + { + def = 440.0f; + } + + if (LADSPA_IS_HINT_INTEGER(hint_desc)) + { + def = roundf(def); + } + + return true; +} /* AudioLADSPAPlugin::setDefault */ + + +/* + * This file has not been truncated + */ diff --git a/src/async/audio/AsyncAudioLADSPAPlugin.h b/src/async/audio/AsyncAudioLADSPAPlugin.h new file mode 100644 index 0000000..a99f1e5 --- /dev/null +++ b/src/async/audio/AsyncAudioLADSPAPlugin.h @@ -0,0 +1,392 @@ +/** +@file AsyncAudioLADSPAPlugin.h +@brief A class for using a LADSPA plugin as an audio processor +@author Tobias Blomberg / SM0SVX +@date 2023-12-09 + +\verbatim +Async - A library for programming event driven applications +Copyright (C) 2003-2024 Tobias Blomberg / SM0SVX + +This program is free software; you can redistribute it and/or modify +it under the terms of the GNU General Public License as published by +the Free Software Foundation; either version 2 of the License, or +(at your option) any later version. + +This program is distributed in the hope that it will be useful, +but WITHOUT ANY WARRANTY; without even the implied warranty of +MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +GNU General Public License for more details. + +You should have received a copy of the GNU General Public License +along with this program; if not, write to the Free Software +Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA +\endverbatim +*/ + +/** @example AsyncAudioLADSPAPlugin_demo.cpp +An example of how to use the AudioLADSPAPlugin class +*/ + +#ifndef ASYNC_AUDIO_LADSPA_PLUGIN_INCLUDED +#define ASYNC_AUDIO_LADSPA_PLUGIN_INCLUDED + + +/**************************************************************************** + * + * System Includes + * + ****************************************************************************/ + +extern "C" { + #include +}; + +#include +#include +#include +#include + + +/**************************************************************************** + * + * Project Includes + * + ****************************************************************************/ + +#include + + +/**************************************************************************** + * + * Local Includes + * + ****************************************************************************/ + + + +/**************************************************************************** + * + * Forward declarations + * + ****************************************************************************/ + + + +/**************************************************************************** + * + * Namespace + * + ****************************************************************************/ + +namespace Async +{ + + +/**************************************************************************** + * + * Forward declarations of classes inside of the declared namespace + * + ****************************************************************************/ + + + +/**************************************************************************** + * + * Defines & typedefs + * + ****************************************************************************/ + + + +/**************************************************************************** + * + * Exported Global Variables + * + ****************************************************************************/ + + + +/**************************************************************************** + * + * Class definitions + * + ****************************************************************************/ + +/** +@brief Use a LADSPA plugin as an audio processor +@author Tobias Blomberg / SM0SVX +@date 2023-12-09 + +This class is used to load a LADSPA plugin and use it as an audio processor. + +\include AsyncAudioLADSPAPlugin_demo.cpp +*/ +class AudioLADSPAPlugin : public AudioProcessor +{ + public: + using PortNumber = decltype(LADSPA_Descriptor::PortCount); + using PluginIndex = unsigned long; + using UniqueID = unsigned long; + + constexpr static PortNumber npos = std::numeric_limits::max(); + + /** + * @brief Find any LADSPA plugins in the given subdirectory + * @param dir The path to the directory to look in + * + * This function will go through all files in the given subdirectory, + * reading any *.so files it can find and load them as LADSPA plugins. The + * plugins are not fully initialized in this process but rather just the + * necessary calls are made to extract enough information to determine if + * this is a plugin that is compatible with this class. Any plugin found to + * be usable is put in an index so that we later can find plugins using + * their label. + */ + static bool findPluginsInDir(std::string dir); + + /** + * @brief Find LADSPA plugins in standard subdirectories + * + * LADSPA plugins are typically installed in /usr/lib64/ladspa (on a 64 bit + * x86 system). This default value is adapted automatically to match the + * target system during compilation of the software. + * + * If the LADSPA_PATH environment variable is set it will override the + * default path. + * + * This function is called by the "label variant" of the constructor if the + * plugin index is empty. + */ + static bool findPlugins(void); + + /** + * @brief Constructor + * @param path The full path of the plugin to load + * @param index The index of the plugin to instantiate + * + * This constructor is normally not used directly unless there is a special + * requirement to load a specific LADSPA plugin file with a known plugin + * index. + */ + AudioLADSPAPlugin(const std::string& path, PluginIndex index) + : m_path(path), m_index(index) {} + + /** + * @brief Constructor + * @param id The plugin unique id to look for + * + * Use this constructor for creating a LADSPA plugin instance if you know + * its unique ID. The easiest way normally is to use the label to find the + * plugin. + */ + AudioLADSPAPlugin(UniqueID id); + + /** + * @brief Constructor + * @param label The plugin label to look for + * + * This is the main constructor for creating a LADSPA plugin instance. + */ + AudioLADSPAPlugin(const std::string& label); + + /** + * @brief Disallow copy construction + */ + AudioLADSPAPlugin(const AudioLADSPAPlugin&) = delete; + + /** + * @brief Disallow copy assignment + */ + AudioLADSPAPlugin& operator=(const AudioLADSPAPlugin&) = delete; + + /** + * @brief Destructor + */ + ~AudioLADSPAPlugin(void); + + /** + * @brief Initialize the plugin + * @return Return \em true on success or else \em false is returned + * + * All loading, instantiation and initialization of the plugin is done in + * this function. The LADSPA activate call is called at the end of the + * function so it is directly ready to process audio when this function + * returns \em true. If the function returns \em false, it's not allowed to + * call any other functions so the object should be deleted as soon as + * possible. + */ + bool initialize(void); + + /** + * @brief Find an input control port by name + * @param name The port namne to look for + * @return Return the port number or "npos" if not found + */ + PortNumber findControlInputByName(const std::string& name); + + /** + * @brief Set a control input to the given value + * @param portno The port number to set + * @param val The value to set + */ + bool setControl(PortNumber portno, LADSPA_Data val); + + /** + * @brief Activate the plugin + * + * Use this function to activate this plugin. Activation is done in the + * initialize function so manual activation is normally not needed. Read + * more about plugin activation in the LADSPA documentation. + */ + void activate(void); + + /** + * @brief Deactivate the plugin + * + * Use this function to deactivate this plugin. Read more about plugin + * activation in the LADSPA documentation. + */ + void deactivate(void); + + /** + * @brief Get the path to the plugin + * @returns Returns the path to the plugin + */ + std::string path(void) const { return m_path; } + + /** + * @brief Get the unique ID for the plugin + * @returns Returns the unique ID of the plugin + * + * All plugins have a unique ID which can be used to find it. + */ + UniqueID uniqueId(void) const { return m_desc->UniqueID; } + + /** + * @brief Get the unique label for the plugin + * @returns Returns the unique label for the plugin + * + * The label is what most often is used to find a specific plugin. + */ + std::string label(void) const { return m_desc->Label; } + + /** + * @brief Get the name of the plugin + * @returns Returns the name of the plugin + * + * This function return the free text name/display name for the plugin. + */ + std::string name(void) const { return m_desc->Name; } + + /** + * @brief Get information on the maker of the plugin + * @returns Returns a string describing the plugin author. + */ + std::string maker(void) const { return m_desc->Maker; } + + /** + * @brief Get the copyright information for the plugin + * @returns Returns a string describing the copyright + */ + std::string copyright(void) const { return m_desc->Copyright; } + + /** + * @brief Get the number of ports for the plugin + * @returns Returns the total number of control/audio input/output ports + */ + PortNumber portCount(void) const { return m_desc->PortCount; } + + /** + * @brief Check if a port is a control port + * @returns Returns \em true if this is a control port + */ + bool portIsControl(PortNumber portno) const; + + /** + * @brief Check if a port is an audio port + * @returns Returns \em true if this is an audio port + */ + bool portIsAudio(PortNumber portno) const; + + /** + * @brief Check if a port is an input port + * @returns Returns \em true if this is an input port + */ + bool portIsInput(PortNumber portno) const; + + /** + * @brief Check if a port is an output port + * @returns Returns \em true if this is an output port + */ + bool portIsOutput(PortNumber portno) const; + + /** + * @brief Print some useful information for the plugin + */ + void print(const std::string& prefix=""); + + protected: + /** + * @brief Process incoming samples and put them into the output buffer + * @param dest Destination buffer + * @param src Source buffer + * @param count Number of samples in the source buffer + * + * This function should be reimplemented by the inheriting class to + * do the actual processing of the incoming samples. All samples must + * be processed, otherwise they are lost and the output buffer will + * contain garbage. + */ + virtual void processSamples(float *dest, const float *src, + int count) override; + + private: + struct InstanceInfo + { + std::string m_path; + PluginIndex m_index; + UniqueID m_unique_id; + std::string m_label; + }; + using InstanceInfoP = std::shared_ptr; + using LabelMap = std::map; + using IDMap = std::map; + + static const PortNumber NOPORT = 9999UL; + + static IDMap& idMap(void) + { + static IDMap id_map; + return id_map; + } + + static LabelMap& labelMap(void) + { + static LabelMap label_map; + return label_map; + } + + const LADSPA_Descriptor* ladspaDescriptor(void); + bool setDefault(PortNumber portno); + + std::string m_path; + void* m_handle = nullptr; + PluginIndex m_index = 0; + const LADSPA_Descriptor* m_desc = nullptr; + LADSPA_Handle m_inst_handle = nullptr; + bool m_is_active = false; + LADSPA_Data* m_ctrl_buf = nullptr; + PortNumber m_sample_input_port = NOPORT; + PortNumber m_sample_output_port = NOPORT; + +}; /* class AudioLADSPAPlugin */ + + +} /* namespace Async */ + +#endif /* ASYNC_AUDIO_LADSPA_PLUGIN_INCLUDED */ + +/* + * This file has not been truncated + */ diff --git a/src/async/audio/AsyncAudioMixer.cpp b/src/async/audio/AsyncAudioMixer.cpp new file mode 100644 index 0000000..9d97fb3 --- /dev/null +++ b/src/async/audio/AsyncAudioMixer.cpp @@ -0,0 +1,410 @@ +/** +@file AsyncAudioMixer.cpp +@brief This file contains an audio mixer class +@author Tobias Blomberg / SM0SVX +@date 2007-10-05 + +\verbatim +Async - A library for programming event driven applications +Copyright (C) 2003-2015 Tobias Blomberg / SM0SVX + +This program is free software; you can redistribute it and/or modify +it under the terms of the GNU General Public License as published by +the Free Software Foundation; either version 2 of the License, or +(at your option) any later version. + +This program is distributed in the hope that it will be useful, +but WITHOUT ANY WARRANTY; without even the implied warranty of +MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +GNU General Public License for more details. + +You should have received a copy of the GNU General Public License +along with this program; if not, write to the Free Software +Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA +\endverbatim +*/ + + + +/**************************************************************************** + * + * System Includes + * + ****************************************************************************/ + +#include +#include + + +/**************************************************************************** + * + * Project Includes + * + ****************************************************************************/ + +#include + + +/**************************************************************************** + * + * Local Includes + * + ****************************************************************************/ + +#include "AsyncAudioMixer.h" +#include "AsyncAudioFifo.h" +#include "AsyncAudioReader.h" + + + +/**************************************************************************** + * + * Namespaces to use + * + ****************************************************************************/ + +using namespace std; +using namespace Async; + + + +/**************************************************************************** + * + * Defines & typedefs + * + ****************************************************************************/ + + + +/**************************************************************************** + * + * Local class definitions + * + ****************************************************************************/ + +class Async::AudioMixer::MixerSrc : public AudioSink +{ + public: + static const int FIFO_SIZE = AudioMixer::OUTBUF_SIZE; + + MixerSrc(AudioMixer *mixer) + : fifo(FIFO_SIZE), mixer(mixer), is_flushed(true), do_flush(false) + { + AudioSink::setHandler(&fifo); + fifo.registerSink(&reader); + } + + int writeSamples(const float *samples, int count) + { + //printf("Async::AudioMixer::MixerSrc::writeSamples: count=%d\n", count); + is_flushed = false; + do_flush = false; + mixer->setAudioAvailable(); + return fifo.writeSamples(samples, count); + } + + void flushSamples(void) + { + if (is_flushed && !do_flush && fifo.empty()) + { + fifo.flushSamples(); + } + + //printf("Async::AudioMixer::MixerSrc::flushSamples\n"); + is_flushed = true; + do_flush = true; + if (fifo.empty()) + { + mixer->flushSamples(); + } + } + + bool isActive(void) const + { + return !is_flushed || !fifo.empty(); + } + + void mixerFlushedAllSamples(void) + { + //printf("Async::AudioMixer::MixerSrc::mixerFlushedAllSamples\n"); + if (do_flush) + { + do_flush = false; + //printf("\tFlushing FIFO\n"); + fifo.flushSamples(); + } + } + + bool isFlushing(void) const { return do_flush; } + + int readSamples(float *samples, int count) + { + return reader.readSamples(samples, count); + } + + unsigned samplesInFifo(void) const { return fifo.samplesInFifo(); } + + private: + AudioFifo fifo; + AudioReader reader; + AudioMixer *mixer; + bool is_flushed; + bool do_flush; + +}; /* class Async::AudioMixer::MixerSrc */ + + + + +/**************************************************************************** + * + * Prototypes + * + ****************************************************************************/ + + + +/**************************************************************************** + * + * Exported Global Variables + * + ****************************************************************************/ + + + + +/**************************************************************************** + * + * Local Global Variables + * + ****************************************************************************/ + + + +/**************************************************************************** + * + * Public member functions + * + ****************************************************************************/ + +AudioMixer::AudioMixer(void) + : output_timer(0, Timer::TYPE_ONESHOT, false), outbuf_pos(0), + outbuf_cnt(0), is_flushed(true), output_stopped(false) +{ + output_timer.expired.connect(mem_fun(*this, &AudioMixer::outputHandler)); +} /* AudioMixer::AudioMixer */ + + +AudioMixer::~AudioMixer(void) +{ + list::iterator it; + for (it = sources.begin(); it != sources.end(); ++it) + { + delete *it; + } +} /* AudioMixer::~AudioMixer */ + + +void AudioMixer::addSource(AudioSource *source) +{ + MixerSrc *mixer_src = new MixerSrc(this); + //mixer_src->stopOutput(true); + //mixer_src->setOverwrite(false); + mixer_src->registerSource(source); + sources.push_back(mixer_src); +} /* AudioMixer::addSource */ + + +void AudioMixer::resumeOutput(void) +{ + //printf("AudioMixer::resumeOutput\n"); + output_stopped = false; + outputHandler(0); +} /* AudioMixer::resumeOutput */ + + + +/**************************************************************************** + * + * Protected member functions + * + ****************************************************************************/ + +void AudioMixer::allSamplesFlushed(void) +{ + //printf("AudioMixer::allSamplesFlushed\n"); + list::iterator it; + for (it = sources.begin(); it != sources.end(); ++it) + { + (*it)->mixerFlushedAllSamples(); + } +} /* AudioMixer::allSamplesFlushed */ + + + +/**************************************************************************** + * + * Private member functions + * + ****************************************************************************/ + + +/* + *---------------------------------------------------------------------------- + * Method: AudioMixer::setAudioAvailable + * Purpose: Called by one of the incoming stream handlers when there is + * data available. This will trigger a delayed execution of the + * outputHandler method. The execution of the real output handler + * is delayed so that all input streams have a chance to fill up. + * Input: None + * Output: None + * Created: 2007-10-07 + * Remarks: + * Bugs: + *---------------------------------------------------------------------------- + */ +void AudioMixer::setAudioAvailable(void) +{ + output_timer.setEnable(true); +} /* AudioMixer::setAudioAvailable */ + + +/* + *---------------------------------------------------------------------------- + * Method: AudioMixer::flushSamples + * Purpose: Used by the input stream handlers to tell the mixer that + * they want to flush. + * Input: None + * Output: None + * Created: 2007-10-07 + * Remarks: + * Bugs: + *---------------------------------------------------------------------------- + */ +void AudioMixer::flushSamples(void) +{ + output_timer.setEnable(true); +} /* AudioMixer::flushSamples */ + + +/* + *---------------------------------------------------------------------------- + * Method: AudioMixer::outputHandler + * Purpose: Handle the output of audio samples. All input streams are read + * and mixed together to a single output stream. + * Input: t - A pointer to the timer that triggered this function. + * Output: None + * Created: 2007-10-07 + * Remarks: + * Bugs: + *---------------------------------------------------------------------------- + */ +void AudioMixer::outputHandler(Timer *t) +{ + output_timer.setEnable(false); + + if (output_stopped) + { + return; + } + + unsigned samples_written; + do + { + // First empty the outbut buffer + samples_written = 1; // Initialize to 1 to enter the loop + while ((outbuf_pos < outbuf_cnt) && (samples_written > 0)) + { + //printf("Writing %d samples\n", outbuf_cnt-outbuf_pos); + is_flushed = false; + samples_written = sinkWriteSamples(outbuf+outbuf_pos, + outbuf_cnt-outbuf_pos); + outbuf_pos += samples_written; + } + + // If the output buffer is empty, fill it up + if (outbuf_pos >= outbuf_cnt) + { + // Calculate the maximum number of samples we can read from the FIFOs + unsigned samples_to_read = MixerSrc::FIFO_SIZE+1; + list::iterator it; + for (it = sources.begin(); it != sources.end(); ++it) + { + if ((*it)->isActive()) + { + samples_to_read = min(samples_to_read, (*it)->samplesInFifo()); + } + } + + // There are no active input streams + if (samples_to_read == MixerSrc::FIFO_SIZE+1) + { + samples_to_read = 0; + } + + //printf("samples_to_read=%d\n", samples_to_read); + + // The output buffer is empty and we have nothing to fill it with + if (samples_to_read == 0) + { + checkFlush(); + break; + } + + // Fill the output buffer with samples from all active FIFOs + memset(outbuf, 0, sizeof(outbuf)); + float tmp[OUTBUF_SIZE]; + for (it = sources.begin(); it != sources.end(); ++it) + { + if ((*it)->isActive()) + { + unsigned samples_read = (*it)->readSamples(tmp, samples_to_read); + assert(samples_read == samples_to_read); + + for (unsigned i=0; i 0); + + output_stopped = (samples_written == 0); + +} /* AudioMixer::outputHandler */ + + +void AudioMixer::checkFlush(void) +{ + if (is_flushed) + { + return; + } + + list::iterator it; + for (it = sources.begin(); it != sources.end(); ++it) + { + if ((*it)->isActive() && !(*it)->isFlushing()) + { + return; + } + } + + //printf("AudioMixer::checkFlush: Flushing!\n"); + is_flushed = true; + sinkFlushSamples(); + +} /* AudioMixer::checkFlush */ + + + + + +/* + * This file has not been truncated + */ + diff --git a/src/async/audio/AsyncAudioMixer.h b/src/async/audio/AsyncAudioMixer.h new file mode 100644 index 0000000..734bf82 --- /dev/null +++ b/src/async/audio/AsyncAudioMixer.h @@ -0,0 +1,189 @@ +/** +@file AsyncAudioMixer.h +@brief This file contains an audio mixer class +@author Tobias Blomberg / SM0SVX +@date 2007-10-05 + +\verbatim +Async - A library for programming event driven applications +Copyright (C) 2004-2015 Tobias Blomberg / SM0SVX + +This program is free software; you can redistribute it and/or modify +it under the terms of the GNU General Public License as published by +the Free Software Foundation; either version 2 of the License, or +(at your option) any later version. + +This program is distributed in the hope that it will be useful, +but WITHOUT ANY WARRANTY; without even the implied warranty of +MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +GNU General Public License for more details. + +You should have received a copy of the GNU General Public License +along with this program; if not, write to the Free Software +Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA +\endverbatim +*/ + + +#ifndef ASYNC_AUDIO_MIXER_INCLUDED +#define ASYNC_AUDIO_MIXER_INCLUDED + + +/**************************************************************************** + * + * System Includes + * + ****************************************************************************/ + +#include + + +/**************************************************************************** + * + * Project Includes + * + ****************************************************************************/ + +#include +#include + + +/**************************************************************************** + * + * Local Includes + * + ****************************************************************************/ + + + +/**************************************************************************** + * + * Forward declarations + * + ****************************************************************************/ + + + +/**************************************************************************** + * + * Namespace + * + ****************************************************************************/ + +namespace Async +{ + + +/**************************************************************************** + * + * Forward declarations of classes inside of the declared namespace + * + ****************************************************************************/ + + + +/**************************************************************************** + * + * Defines & typedefs + * + ****************************************************************************/ + + + +/**************************************************************************** + * + * Exported Global Variables + * + ****************************************************************************/ + + + +/**************************************************************************** + * + * Class definitions + * + ****************************************************************************/ + +/** +@brief A class for mixing audio streams +@author Tobias Blomberg / SM0SVX +@date 2007-10-05 + +This class is used to mix audio streams together. +*/ +class AudioMixer : public sigc::trackable, public Async::AudioSource +{ + public: + /** + * @brief Default constuctor + */ + AudioMixer(void); + + /** + * @brief Destructor + */ + ~AudioMixer(void); + + /** + * @brief Add an audio source to the mixer + * @param source The audio source to add + */ + void addSource(AudioSource *source); + + /** + * @brief Resume audio output to the sink + * + * This function will be called when the registered audio sink is ready + * to accept more samples. + * This function is normally only called from a connected sink object. + */ + void resumeOutput(void); + + + protected: + /** + * @brief The registered sink has flushed all samples + * + * This function will be called when all samples have been flushed in the + * registered sink. + * This function is normally only called from a connected sink object. + */ + void allSamplesFlushed(void); + + + private: + class MixerSrc; + + static const int OUTBUF_SIZE = 256; + + std::list sources; + Timer output_timer; + float outbuf[OUTBUF_SIZE]; + unsigned outbuf_pos; + unsigned outbuf_cnt; + bool is_flushed; + bool output_stopped; + + AudioMixer(const AudioMixer&); + AudioMixer& operator=(const AudioMixer&); + + void setAudioAvailable(void); + void flushSamples(void); + void outputHandler(Timer *t); + void checkFlush(void); + + friend class MixerSrc; + +}; /* class AudioMixer */ + + +} /* namespace */ + +#endif /* ASYNC_AUDIO_MIXER_INCLUDED */ + + + +/* + * This file has not been truncated + */ + diff --git a/src/async/audio/AsyncAudioNoiseAdder.cpp b/src/async/audio/AsyncAudioNoiseAdder.cpp new file mode 100644 index 0000000..e47be80 --- /dev/null +++ b/src/async/audio/AsyncAudioNoiseAdder.cpp @@ -0,0 +1,185 @@ +/** +@file AsyncAudioNoiseAdder.cpp +@brief A class to add white gaussian noise to an audio stream +@author Tobias Blomberg / SM0SVX +@date 2015-03-08 + +\verbatim +Async - A library for programming event driven applications +Copyright (C) 2003-2015 Tobias Blomberg / SM0SVX + +This program is free software; you can redistribute it and/or modify +it under the terms of the GNU General Public License as published by +the Free Software Foundation; either version 2 of the License, or +(at your option) any later version. + +This program is distributed in the hope that it will be useful, +but WITHOUT ANY WARRANTY; without even the implied warranty of +MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +GNU General Public License for more details. + +You should have received a copy of the GNU General Public License +along with this program; if not, write to the Free Software +Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA +\endverbatim +*/ + + + +/**************************************************************************** + * + * System Includes + * + ****************************************************************************/ + +#include + +#include +#include +#include +#include +#include + + +/**************************************************************************** + * + * Project Includes + * + ****************************************************************************/ + + + +/**************************************************************************** + * + * Local Includes + * + ****************************************************************************/ + +#include "AsyncAudioNoiseAdder.h" + + +/**************************************************************************** + * + * Namespaces to use + * + ****************************************************************************/ + +using namespace std; +using namespace Async; + + + +/**************************************************************************** + * + * Defines & typedefs + * + ****************************************************************************/ + + + +/**************************************************************************** + * + * Local class definitions + * + ****************************************************************************/ + + + +/**************************************************************************** + * + * Prototypes + * + ****************************************************************************/ + + + +/**************************************************************************** + * + * Exported Global Variables + * + ****************************************************************************/ + + + +/**************************************************************************** + * + * Local Global Variables + * + ****************************************************************************/ + + + +/**************************************************************************** + * + * Public member functions + * + ****************************************************************************/ + +AudioNoiseAdder::AudioNoiseAdder(float level_db) + : sigma(sqrt(powf(10.0f, level_db / 10.0f) / 2.0f)), z1(0.0f), + generate(false), seed(0) +{ +} /* AudioNoiseAdder::AudioNoiseAdder */ + + +AudioNoiseAdder::~AudioNoiseAdder(void) +{ +} /* AudioNoiseAdder::~AudioNoiseAdder */ + + + +/**************************************************************************** + * + * Protected member functions + * + ****************************************************************************/ + +void AudioNoiseAdder::processSamples(float *dest, const float *src, int count) +{ + //cout << "AudioNoiseAdder::processSamples: len=" << len << endl; + + for (int i=0; i::min(); + static const float two_pi = 2.0f * M_PI; + static const float mu = 0.0f; + + generate = !generate; + + if (!generate) + { + return z1 * sigma + mu; + } + + float u1, u2; + do + { + u1 = rand_r(&seed) * (1.0f / RAND_MAX); + u2 = rand_r(&seed) * (1.0f / RAND_MAX); + } + while (u1 <= epsilon); + + float z0 = sqrt(-2.0f * log(u1)) * cos(two_pi * u2); + z1 = sqrt(-2.0f * log(u1)) * sin(two_pi * u2); + return z0 * sigma + mu; +} /* AudioNoiseAdder::generateGaussianNoise */ + + +/* + * This file has not been truncated + */ diff --git a/src/async/audio/AsyncAudioNoiseAdder.h b/src/async/audio/AsyncAudioNoiseAdder.h new file mode 100644 index 0000000..752c63a --- /dev/null +++ b/src/async/audio/AsyncAudioNoiseAdder.h @@ -0,0 +1,176 @@ +/** +@file AsyncAudioNoiseAdder.h +@brief A class to add white gaussian noise to an audio stream +@author Tobias Blomberg / SM0SVX +@date 2015-03-08 + +\verbatim +Async - A library for programming event driven applications +Copyright (C) 2003-2015 Tobias Blomberg / SM0SVX + +This program is free software; you can redistribute it and/or modify +it under the terms of the GNU General Public License as published by +the Free Software Foundation; either version 2 of the License, or +(at your option) any later version. + +This program is distributed in the hope that it will be useful, +but WITHOUT ANY WARRANTY; without even the implied warranty of +MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +GNU General Public License for more details. + +You should have received a copy of the GNU General Public License +along with this program; if not, write to the Free Software +Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA +\endverbatim +*/ + + +#ifndef ASYNC_AUDIO_NOISE_ADDER +#define ASYNC_AUDIO_NOISE_ADDER + + +/**************************************************************************** + * + * System Includes + * + ****************************************************************************/ + +#include + + +/**************************************************************************** + * + * Project Includes + * + ****************************************************************************/ + + + +/**************************************************************************** + * + * Local Includes + * + ****************************************************************************/ + +#include + + + +/**************************************************************************** + * + * Forward declarations + * + ****************************************************************************/ + + + +/**************************************************************************** + * + * Namespace + * + ****************************************************************************/ + +namespace Async +{ + + +/**************************************************************************** + * + * Forward declarations of classes inside of the declared namespace + * + ****************************************************************************/ + + + +/**************************************************************************** + * + * Defines & typedefs + * + ****************************************************************************/ + + + +/**************************************************************************** + * + * Exported Global Variables + * + ****************************************************************************/ + + + +/**************************************************************************** + * + * Class definitions + * + ****************************************************************************/ + +/** +@brief A class to add gaussian white noise to an audio stream +@author Tobias Blomberg / SM0SVX +@date 2015-03-08 + +This class implement a noise generator that add white gaussian noise to an +audio stream. The noise is generated using the Box-Muller transform which for +example is described on Wikipedia: + + http://en.wikipedia.org/wiki/Box%E2%80%93Muller_transform + +The class is not implemented as a pure audio source but rather as an audio pipe +component that should be inserted in the audio path. +*/ +class AudioNoiseAdder : public AudioProcessor +{ + public: + /** + * @brief Constuctor + * @param level_db The noise level in dB + * + * The level_db parameter deserve some clarification. A level of 0dB give + * the same power as in a full scale sine wave. This make it possible to + * add noise to a generated signal under controlled conditions. + * For example, if a sine wave is generated in SvxLink with a level of 0dB + * and noise is added with a level of -10dB, we get a signal to noise ratio + * (SNR) of 10dB. + */ + AudioNoiseAdder(float level_db); + + /** + * @brief Destructor + */ + ~AudioNoiseAdder(void); + + protected: + /** + * @brief Process incoming samples and put them into the output buffer + * @param dest Destination buffer + * @param src Source buffer + * @param count Number of samples in the source buffer + * + * This function is called from the base class to do the actual + * processing of the incoming samples. All samples must + * be processed, otherwise they are lost and the output buffer will + * contain garbage. + */ + void processSamples(float *dest, const float *src, int count); + + private: + float sigma; // Standard deviation of the generated noise + float z1; + bool generate; + unsigned int seed; + + AudioNoiseAdder(const AudioNoiseAdder&); + AudioNoiseAdder& operator=(const AudioNoiseAdder&); + float generateGaussianNoise(void); + +}; /* class AudioNoiseAdder */ + + +} /* namespace */ + +#endif /* ASYNC_AUDIO_NOISE_ADDER */ + + +/* + * This file has not been truncated + */ diff --git a/src/async/audio/AsyncAudioPacer.cpp b/src/async/audio/AsyncAudioPacer.cpp new file mode 100644 index 0000000..b342c72 --- /dev/null +++ b/src/async/audio/AsyncAudioPacer.cpp @@ -0,0 +1,316 @@ +/** +@file AudioPacer.cpp +@brief An audio pipe class for pacing audio output +@author Tobias Blomberg / SM0SVX +@date 2004-04-03 + +\verbatim +Async - A library for programming event driven applications +Copyright (C) 2003-2008 Tobias Blomberg / SM0SVX + +This program is free software; you can redistribute it and/or modify +it under the terms of the GNU General Public License as published by +the Free Software Foundation; either version 2 of the License, or +(at your option) any later version. + +This program is distributed in the hope that it will be useful, +but WITHOUT ANY WARRANTY; without even the implied warranty of +MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +GNU General Public License for more details. + +You should have received a copy of the GNU General Public License +along with this program; if not, write to the Free Software +Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA +\endverbatim +*/ + + + +/**************************************************************************** + * + * System Includes + * + ****************************************************************************/ + +#include + +#include +#include +#include + + +/**************************************************************************** + * + * Project Includes + * + ****************************************************************************/ + +#include + + +/**************************************************************************** + * + * Local Includes + * + ****************************************************************************/ + +#include "AsyncAudioPacer.h" + + + +/**************************************************************************** + * + * Namespaces to use + * + ****************************************************************************/ + +using namespace std; +using namespace Async; + + + +/**************************************************************************** + * + * Defines & typedefs + * + ****************************************************************************/ + + + +/**************************************************************************** + * + * Local class definitions + * + ****************************************************************************/ + + + +/**************************************************************************** + * + * Prototypes + * + ****************************************************************************/ + + + +/**************************************************************************** + * + * Exported Global Variables + * + ****************************************************************************/ + + + + +/**************************************************************************** + * + * Local Global Variables + * + ****************************************************************************/ + + + +/**************************************************************************** + * + * Public member functions + * + ****************************************************************************/ + + +AudioPacer::AudioPacer(int sample_rate, int block_size, int prebuf_time) + : sample_rate(sample_rate), buf_size(block_size), prebuf_time(prebuf_time), + buf_pos(0), pace_timer(0), do_flush(false), input_stopped(false) +{ + assert(sample_rate > 0); + assert(block_size > 0); + assert(prebuf_time >= 0); + + buf = new float[buf_size]; + prebuf_samples = prebuf_time * sample_rate / 1000; + + pace_timer = new Timer(buf_size * 1000 / sample_rate, + Timer::TYPE_PERIODIC); + pace_timer->expired.connect(mem_fun(*this, &AudioPacer::outputNextBlock)); + + if (prebuf_samples > 0) + { + pace_timer->setEnable(false); + } + +} /* AudioPacer::AudioPacer */ + + +AudioPacer::~AudioPacer(void) +{ + delete pace_timer; + delete [] buf; +} /* AudioPacer::~AudioPacer */ + + +int AudioPacer::writeSamples(const float *samples, int count) +{ + assert(count > 0); + + if (do_flush) + { + do_flush = false; + } + + int samples_written = 0; + + if (prebuf_samples > 0) + { + prebuf_samples -= count; + if (prebuf_samples <= 0) + { + // Prebuffering done. + // Write incoming samples to sink, excluding samples that go + // beyond prebuffering (Note: prebuf_samples is zero or negative here). + samples_written = sinkWriteSamples(samples, count + prebuf_samples); + + int samples_left = count - samples_written; + if (samples_left > 0) + { + // Make a recursive call to write remaining samples into the buffer. + samples_written += writeSamples(samples + samples_written, + samples_left); + } + pace_timer->setEnable(true); + } + else + { + samples_written = sinkWriteSamples(samples, count); + if (samples_written < count) + { + prebuf_samples += count - samples_written; + } + } + } + else + { + samples_written = min(count, buf_size - buf_pos); + memcpy(buf + buf_pos, samples, samples_written * sizeof(*buf)); + buf_pos += samples_written; + + if (!pace_timer->isEnabled()) + { + pace_timer->setEnable(true); + } + } + + if (samples_written == 0) + { + input_stopped = true; + } + + return samples_written; + +} /* AudioPacer::writeSamples */ + + +void AudioPacer::flushSamples(void) +{ + input_stopped = false; + do_flush = true; + + if (buf_pos == 0) + { + sinkFlushSamples(); + } +} /* AudioPacer::flushSamples */ + + +void AudioPacer::resumeOutput(void) +{ + if (prebuf_samples <= 0) + { + pace_timer->setEnable(true); + outputNextBlock(); + } +} /* AudioPacer::resumeOutput */ + + + +/**************************************************************************** + * + * Protected member functions + * + ****************************************************************************/ + +void AudioPacer::allSamplesFlushed(void) +{ + if (do_flush) + { + do_flush = false; + sourceAllSamplesFlushed(); + } +} /* AudioPacer::allSamplesFlushed */ + + + +/**************************************************************************** + * + * Private member functions + * + ****************************************************************************/ + + +void AudioPacer::outputNextBlock(Timer *t) +{ + if (buf_pos < buf_size) + { + pace_timer->setEnable(false); + prebuf_samples = prebuf_time * sample_rate / 1000; + } + + if (buf_pos == 0) + { + return; + } + + int samples_to_write = buf_pos; + int tot_samples_written = 0; + int samples_written; + do { + samples_written = sinkWriteSamples(buf + tot_samples_written, + samples_to_write); + tot_samples_written += samples_written; + samples_to_write -= samples_written; + } while ((samples_written > 0) && (samples_to_write > 0)); + + if (tot_samples_written < buf_pos) + { + memmove(buf, buf + tot_samples_written, + (buf_pos - tot_samples_written) * sizeof(*buf)); + buf_pos -= tot_samples_written; + } + else + { + buf_pos = 0; + } + + if (samples_written == 0) + { + pace_timer->setEnable(false); + } + + if (input_stopped && (buf_pos < buf_size)) + { + input_stopped = false; + sourceResumeOutput(); + } + + if (do_flush && (buf_pos == 0)) + { + sinkFlushSamples(); + } + +} /* AudioPacer::outputNextBlock */ + + + + +/* + * This file has not been truncated + */ + diff --git a/src/async/audio/AsyncAudioPacer.h b/src/async/audio/AsyncAudioPacer.h new file mode 100644 index 0000000..d7e46de --- /dev/null +++ b/src/async/audio/AsyncAudioPacer.h @@ -0,0 +1,201 @@ +/** +@file AsyncAudioPacer.h +@brief An audio pipe class for pacing audio output +@author Tobias Blomberg / SM0SVX +@date 2007-11-17 + +\verbatim +Async - A library for programming event driven applications +Copyright (C) 2003-2008 Tobias Blomberg / SM0SVX + +This program is free software; you can redistribute it and/or modify +it under the terms of the GNU General Public License as published by +the Free Software Foundation; either version 2 of the License, or +(at your option) any later version. + +This program is distributed in the hope that it will be useful, +but WITHOUT ANY WARRANTY; without even the implied warranty of +MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +GNU General Public License for more details. + +You should have received a copy of the GNU General Public License +along with this program; if not, write to the Free Software +Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA +\endverbatim +*/ + + +#ifndef AUDIO_PACER_INCLUDED +#define AUDIO_PACER_INCLUDED + + +/**************************************************************************** + * + * System Includes + * + ****************************************************************************/ + +#include + + +/**************************************************************************** + * + * Project Includes + * + ****************************************************************************/ + +#include +#include + + +/**************************************************************************** + * + * Local Includes + * + ****************************************************************************/ + + + +/**************************************************************************** + * + * Forward declarations + * + ****************************************************************************/ + + + +/**************************************************************************** + * + * Namespace + * + ****************************************************************************/ + +namespace Async +{ + + +/**************************************************************************** + * + * Forward declarations of classes inside of the declared namespace + * + ****************************************************************************/ + +class Timer; + + +/**************************************************************************** + * + * Defines & typedefs + * + ****************************************************************************/ + + + +/**************************************************************************** + * + * Exported Global Variables + * + ****************************************************************************/ + + + +/**************************************************************************** + * + * Class definitions + * + ****************************************************************************/ + +/** +@brief An audio pipe class that pace audio output +@author Tobias Blomberg +@date 2007-11-17 + +This class is used in an audio pipe chain to pace audio output. +*/ +class AudioPacer : public AudioSink, public AudioSource, public sigc::trackable +{ + public: + /** + * @brief Constuctor + * @param sample_rate The sample rate of the incoming samples + * @param block_size The size of the audio blocks + * @param prebuf_time The time (ms) to wait before starting to send audio + */ + AudioPacer(int sample_rate, int block_size, int prebuf_time); + + /** + * @brief Destructor + */ + ~AudioPacer(void); + + /** + * @brief Write samples into this audio sink + * @param samples The buffer containing the samples + * @param count The number of samples in the buffer + * @return Returns the number of samples that has been taken care of + * + * This function is used to write audio into this audio sink. If it + * returns 0, no more samples should be written until the resumeOutput + * function in the source have been called. + * This function is normally only called from a connected source object. + */ + virtual int writeSamples(const float *samples, int count); + + /** + * @brief Tell the sink to flush the previously written samples + * + * This function is used to tell the sink to flush previously written + * samples. When done flushing, the sink should call the + * sourceAllSamplesFlushed function. + * This function is normally only called from a connected source object. + */ + virtual void flushSamples(void); + + /** + * @brief Resume audio output to the sink + * + * This function will be called when the registered audio sink is ready + * to accept more samples. + * This function is normally only called from a connected sink object. + */ + virtual void resumeOutput(void); + + + protected: + /** + * @brief The registered sink has flushed all samples + * + * This function will be called when all samples have been flushed in the + * registered sink. If it is not reimplemented, a handler must be set + * that handle the function call. + * This function is normally only called from a connected sink object. + */ + virtual void allSamplesFlushed(void); + + + private: + int sample_rate; + int buf_size; + int prebuf_time; + float *buf; + int buf_pos; + int prebuf_samples; + Async::Timer *pace_timer; + bool do_flush; + bool input_stopped; + + void outputNextBlock(Async::Timer *t=0); + +}; /* class AudioPacer */ + + +} /* namespace */ + +#endif /* AUDIO_PACER_INCLUDED */ + + + +/* + * This file has not been truncated + */ + diff --git a/src/async/audio/AsyncAudioPassthrough.h b/src/async/audio/AsyncAudioPassthrough.h new file mode 100644 index 0000000..ffdf29a --- /dev/null +++ b/src/async/audio/AsyncAudioPassthrough.h @@ -0,0 +1,201 @@ +/** +@file AsyncAudioPassthrough.h +@brief This file contains a class that just pass the audio through +@author Tobias Blomberg / SM0SVX +@date 2006-08-07 + +\verbatim +Async - A library for programming event driven applications +Copyright (C) 2004-2005 Tobias Blomberg / SM0SVX + +This program is free software; you can redistribute it and/or modify +it under the terms of the GNU General Public License as published by +the Free Software Foundation; either version 2 of the License, or +(at your option) any later version. + +This program is distributed in the hope that it will be useful, +but WITHOUT ANY WARRANTY; without even the implied warranty of +MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +GNU General Public License for more details. + +You should have received a copy of the GNU General Public License +along with this program; if not, write to the Free Software +Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA +\endverbatim +*/ + + +#ifndef AUDIO_PASSTHROUGH_INCLUDED +#define AUDIO_PASSTHROUGH_INCLUDED + + +/**************************************************************************** + * + * System Includes + * + ****************************************************************************/ + + + +/**************************************************************************** + * + * Project Includes + * + ****************************************************************************/ + +#include +#include + + +/**************************************************************************** + * + * Local Includes + * + ****************************************************************************/ + + + +/**************************************************************************** + * + * Forward declarations + * + ****************************************************************************/ + + + +/**************************************************************************** + * + * Namespace + * + ****************************************************************************/ + +namespace Async +{ + + +/**************************************************************************** + * + * Forward declarations of classes inside of the declared namespace + * + ****************************************************************************/ + + + +/**************************************************************************** + * + * Defines & typedefs + * + ****************************************************************************/ + + + +/**************************************************************************** + * + * Exported Global Variables + * + ****************************************************************************/ + + + +/**************************************************************************** + * + * Class definitions + * + ****************************************************************************/ + +/** +@brief This class just let the audio pass through +@author Tobias Blomberg / SM0SVX +@date 2006-08-07 + +This class is both a source and a sink and just let the audio pass through. +It can be used standalone but maybe is of more use when inheriting from it. +For example if you want to snoop in on an audio streem without affecting it, +just reimplement the writeSamples method and you have access to the samples. +Just remember to call the sinkWriteSamples method to forward the samples. +*/ +class AudioPassthrough : public AudioSink, public AudioSource +{ + public: + /** + * @brief Default constuctor + */ + AudioPassthrough(void) {} + + /** + * @brief Destructor + */ + virtual ~AudioPassthrough(void) {} + + /** + * @brief Write samples into this audio sink + * @param samples The buffer containing the samples + * @param count The number of samples in the buffer + * @return Returns the number of samples that has been taken care of + * + * This function is used to write audio into this audio sink. If it + * returns 0, no more samples should be written until the resumeOutput + * function in the source have been called. + * This function is normally only called from a connected source object. + */ + virtual int writeSamples(const float *samples, int count) + { + return sinkWriteSamples(samples, count); + } + + /** + * @brief Tell the sink to flush the previously written samples + * + * This function is used to tell the sink to flush previously written + * samples. When done flushing, the sink should call the + * sourceAllSamplesFlushed function. + * This function is normally only called from a connected source object. + */ + virtual void flushSamples(void) + { + sinkFlushSamples(); + } + + /** + * @brief Resume audio output to the sink + * + * This function will be called when the registered audio sink is ready + * to accept more samples. + * This function is normally only called from a connected sink object. + */ + virtual void resumeOutput(void) + { + sourceResumeOutput(); + } + + /** + * @brief The registered sink has flushed all samples + * + * This function will be called when all samples have been flushed in the + * registered sink. + * This function is normally only called from a connected sink object. + */ + virtual void allSamplesFlushed(void) + { + sourceAllSamplesFlushed(); + } + + protected: + + private: + AudioPassthrough(const AudioPassthrough&); + AudioPassthrough& operator=(const AudioPassthrough&); + +}; /* AudioPassthrough */ + + +} /* namespace */ + +#endif /* AUDIO_PASSTHROUGH_INCLUDED */ + + + +/* + * This file has not been truncated + */ + diff --git a/src/async/audio/AsyncAudioProcessor.cpp b/src/async/audio/AsyncAudioProcessor.cpp new file mode 100644 index 0000000..49de897 --- /dev/null +++ b/src/async/audio/AsyncAudioProcessor.cpp @@ -0,0 +1,366 @@ +/** +@file AsyncAudioProcessor.cpp +@brief A_brief_description_for_this_file +@author Tobias Blomberg / SM0SVX +@date 2006-04-23 + +\verbatim +Async - A library for programming event driven applications +Copyright (C) 2003-2008 Tobias Blomberg / SM0SVX + +This program is free software; you can redistribute it and/or modify +it under the terms of the GNU General Public License as published by +the Free Software Foundation; either version 2 of the License, or +(at your option) any later version. + +This program is distributed in the hope that it will be useful, +but WITHOUT ANY WARRANTY; without even the implied warranty of +MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +GNU General Public License for more details. + +You should have received a copy of the GNU General Public License +along with this program; if not, write to the Free Software +Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA +\endverbatim +*/ + + + +/**************************************************************************** + * + * System Includes + * + ****************************************************************************/ + +#include +#include + +#include + + +/**************************************************************************** + * + * Project Includes + * + ****************************************************************************/ + +#include + + +/**************************************************************************** + * + * Local Includes + * + ****************************************************************************/ + +#include "AsyncAudioProcessor.h" + + + +/**************************************************************************** + * + * Namespaces to use + * + ****************************************************************************/ + +using namespace std; +using namespace Async; + + + +/**************************************************************************** + * + * Defines & typedefs + * + ****************************************************************************/ + + + +/**************************************************************************** + * + * Local class definitions + * + ****************************************************************************/ + + + +/**************************************************************************** + * + * Prototypes + * + ****************************************************************************/ + + + +/**************************************************************************** + * + * Exported Global Variables + * + ****************************************************************************/ + + + + +/**************************************************************************** + * + * Local Global Variables + * + ****************************************************************************/ + + + +/**************************************************************************** + * + * Public member functions + * + ****************************************************************************/ + +AudioProcessor::AudioProcessor(void) + : buf_cnt(0), do_flush(false), input_stopped(false), + output_stopped(false), input_rate(1), output_rate(1), input_buf(0), + input_buf_cnt(0), input_buf_size(0) +{ + +} /* AudioProcessor::AudioProcessor */ + + +AudioProcessor::~AudioProcessor(void) +{ + delete [] input_buf; +} /* AudioProcessor::~AudioProcessor */ + + +int AudioProcessor::writeSamples(const float *samples, int len) +{ + //cout << "AudioProcessor::writeSamples: len=" << len << endl; + + assert(len > 0); + + do_flush = false; + int orig_len = len; + + writeFromBuf(); + + // Calculate the maximum number of samples we are able to process + int max_proc = (BUFSIZE - buf_cnt) * input_rate / output_rate; + if (max_proc == 0) + { + input_stopped = true; + return 0; + } + + // If we have samples in the input buffer, try to fill it up and process it + if (input_buf_cnt > 0) + { + int copy_cnt = min(input_buf_size-input_buf_cnt, len); + memcpy(input_buf + input_buf_cnt, samples, copy_cnt * sizeof(*input_buf)); + samples += copy_cnt; + len -= copy_cnt; + input_buf_cnt += copy_cnt; + + if (input_buf_cnt == input_buf_size) + { + processSamples(buf + buf_cnt, input_buf, input_buf_size); + buf_cnt += 1; + max_proc -= input_buf_size; + input_buf_cnt = 0; + } + } + + int reminder = 0; + if (input_buf_size > 0) + { + reminder = len % input_buf_size; + } + + int proc_cnt = min(max_proc, len-reminder); + if (proc_cnt > 0) + { + processSamples(buf + buf_cnt, samples, proc_cnt); + buf_cnt += proc_cnt * output_rate / input_rate; + samples += proc_cnt; + len -= proc_cnt; + writeFromBuf(); + } + + if ((len > 0) && (len < input_buf_size)) + { + memcpy(input_buf, samples, len * sizeof(*input_buf)); + input_buf_cnt = len; + len = 0; + } + + int ret_len = orig_len - len; + if (ret_len == 0) + { + input_stopped = true; + } + + return ret_len; + +} /* AudioProcessor::writeSamples */ + + +void AudioProcessor::flushSamples(void) +{ + //cout << "AudioProcessor::flushSamples" << endl; + + do_flush = true; + input_stopped = false; + if (buf_cnt == 0) + { + if (input_buf_cnt > 0) + { + memset(input_buf + input_buf_cnt, 0, + (input_buf_size - input_buf_cnt) * sizeof(*input_buf)); + processSamples(buf, input_buf, input_buf_size); + buf_cnt += 1; + input_buf_cnt = 0; + writeFromBuf(); + } + else + { + do_flush = false; + sinkFlushSamples(); + } + } +} /* AudioProcessor::flushSamples */ + + +void AudioProcessor::resumeOutput(void) +{ + //cout << "AudioProcessor::resumeOutput" << endl; + output_stopped = false; + writeFromBuf(); +} /* AudioProcessor::resumeOutput */ + + +void AudioProcessor::allSamplesFlushed(void) +{ + //cout << "AudioProcessor::allSamplesFlushed" << endl; + do_flush = false; + sourceAllSamplesFlushed(); +} /* AudioProcessor::allSamplesFlushed */ + + +void AudioProcessor::setInputOutputSampleRate(int input_rate, int output_rate) +{ + assert((input_rate % output_rate == 0) || (output_rate % input_rate == 0)); + this->input_rate = input_rate; + this->output_rate = output_rate; + + delete [] input_buf; + if (input_rate > output_rate) + { + input_buf_size = input_rate / output_rate; + input_buf = new float[input_buf_size]; + } + else + { + input_buf_size = 0; + input_buf = 0; + } +} /* AudioProcessor::setSampleRateRatio */ + + + +/**************************************************************************** + * + * Protected member functions + * + ****************************************************************************/ + + +/* + *------------------------------------------------------------------------ + * Method: + * Purpose: + * Input: + * Output: + * Author: + * Created: + * Remarks: + * Bugs: + *------------------------------------------------------------------------ + */ + + + + + + +/**************************************************************************** + * + * Private member functions + * + ****************************************************************************/ + + +/* + *---------------------------------------------------------------------------- + * Method: AudioProcessor::writeFromBuf + * Purpose: Write processed samples from the buffer to the connected sink. + * Input: None + * Output: None + * Author: Tobias Blomberg / SM0SVX + * Created: 2006-04-23 + * Remarks: + * Bugs: + *---------------------------------------------------------------------------- + */ +void AudioProcessor::writeFromBuf(void) +{ + if ((buf_cnt == 0) || output_stopped) + { + return; + } + + int written; + do + { + written = sinkWriteSamples(buf, buf_cnt); + assert((written >= 0) && (written <= buf_cnt)); + if (written > 0) + { + buf_cnt -= written; + if (buf_cnt > 0) + { + memmove(buf, buf+written, buf_cnt * sizeof(*buf)); + } + } + + if (do_flush && (buf_cnt == 0)) + { + if (input_buf_cnt > 0) + { + memset(input_buf + input_buf_cnt, 0, + (input_buf_size - input_buf_cnt) * sizeof(*input_buf)); + processSamples(buf, input_buf, input_buf_size); + buf_cnt += 1; + input_buf_cnt = 0; + } + else + { + do_flush = false; + Application::app().runTask( + mem_fun(*this, &AudioProcessor::sinkFlushSamples)); + } + } + } + while ((written > 0) && (buf_cnt > 0)); + + output_stopped = (written == 0); + + if (input_stopped && (buf_cnt < BUFSIZE)) + { + input_stopped = false; + Application::app().runTask( + mem_fun(*this, &AudioProcessor::sourceResumeOutput)); + } +} /* AudioProcessor::writeFromBuf */ + + +/* + * This file has not been truncated + */ + diff --git a/src/async/audio/AsyncAudioProcessor.h b/src/async/audio/AsyncAudioProcessor.h new file mode 100644 index 0000000..c1284ab --- /dev/null +++ b/src/async/audio/AsyncAudioProcessor.h @@ -0,0 +1,207 @@ +/** +@file AsyncAudioProcessor.h +@brief The base class for an audio processor class +@author Tobias Blomberg / SM0SVX +@date 2006-04-23 + +\verbatim +Async - A library for programming event driven applications +Copyright (C) 2003-2008 Tobias Blomberg / SM0SVX + +This program is free software; you can redistribute it and/or modify +it under the terms of the GNU General Public License as published by +the Free Software Foundation; either version 2 of the License, or +(at your option) any later version. + +This program is distributed in the hope that it will be useful, +but WITHOUT ANY WARRANTY; without even the implied warranty of +MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +GNU General Public License for more details. + +You should have received a copy of the GNU General Public License +along with this program; if not, write to the Free Software +Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA +\endverbatim +*/ + + +#ifndef ASYNC_AUDIO_PROCESSOR_INCLUDED +#define ASYNC_AUDIO_PROCESSOR_INCLUDED + + +/**************************************************************************** + * + * System Includes + * + ****************************************************************************/ + +#include +#include + + +/**************************************************************************** + * + * Project Includes + * + ****************************************************************************/ + + + +/**************************************************************************** + * + * Local Includes + * + ****************************************************************************/ + +#include +#include + + + +/**************************************************************************** + * + * Forward declarations + * + ****************************************************************************/ + + + +/**************************************************************************** + * + * Namespace + * + ****************************************************************************/ + +namespace Async +{ + + +/**************************************************************************** + * + * Forward declarations of classes inside of the declared namespace + * + ****************************************************************************/ + + + +/**************************************************************************** + * + * Defines & typedefs + * + ****************************************************************************/ + + + +/**************************************************************************** + * + * Exported Global Variables + * + ****************************************************************************/ + + + +/**************************************************************************** + * + * Class definitions + * + ****************************************************************************/ + +/** +@brief The base class for an audio processor +@author Tobias Blomberg / SM0SVX +@date 2006-04-23 + +This class is the base class for an audio processor. An audio processor is +a class that is both an audio sink and source. It receives samples, process +them in some way and send them further down the chain. +*/ +class AudioProcessor : public AudioSink, public AudioSource, public sigc::trackable +{ + public: + /** + * @brief Default constuctor + */ + AudioProcessor(void); + + /** + * @brief Destructor + */ + virtual ~AudioProcessor(void); + + /** + * @brief Write audio to the filter + * @param samples The buffer containing the samples + * @param len The number of samples in the buffer + * @return Return the number of samples processed + */ + int writeSamples(const float *samples, int len); + + /** + * @brief Order a flush of all samples + */ + void flushSamples(void); + + /** + * @brief Resume output to the sink if previously stopped + */ + void resumeOutput(void); + + /** + * @brief All samples have been flushed by the sink + */ + void allSamplesFlushed(void); + + + protected: + /** + * @brief Set the input and output sample rates + * @param input_rate The input sample rate + * @param output_rate The output sample rate + */ + void setInputOutputSampleRate(int input_rate, int output_rate); + + /** + * @brief Process incoming samples and put them into the output buffer + * @param dest Destination buffer + * @param src Source buffer + * @param count Number of samples in the source buffer + * + * This function should be reimplemented by the inheriting class to + * do the actual processing of the incoming samples. All samples must + * be processed, otherwise they are lost and the output buffer will + * contain garbage. + */ + virtual void processSamples(float *dest, const float *src, int count) = 0; + + + private: + static const int BUFSIZE = 256; + + float buf[BUFSIZE]; + int buf_cnt; + bool do_flush; + bool input_stopped; + bool output_stopped; + int input_rate; + int output_rate; + float *input_buf; + int input_buf_cnt; + int input_buf_size; + + AudioProcessor(const AudioProcessor&); + AudioProcessor& operator=(const AudioProcessor&); + void writeFromBuf(void); + +}; /* class AudioProcessor */ + + +} /* namespace */ + +#endif /* ASYNC_AUDIO_PROCESSOR_INCLUDED */ + + + +/* + * This file has not been truncated + */ + diff --git a/src/async/audio/AsyncAudioReader.cpp b/src/async/audio/AsyncAudioReader.cpp new file mode 100644 index 0000000..f36230b --- /dev/null +++ b/src/async/audio/AsyncAudioReader.cpp @@ -0,0 +1,230 @@ +/** +@file AsyncAudioReader.h +@brief An audio pipe component for on-demand reading samples +@author Tobias Blomberg / SM0SVX +@date 2008-02-22 + +\verbatim +Async - A library for programming event driven applications +Copyright (C) 2003-2008 Tobias Blomberg / SM0SVX + +This program is free software; you can redistribute it and/or modify +it under the terms of the GNU General Public License as published by +the Free Software Foundation; either version 2 of the License, or +(at your option) any later version. + +This program is distributed in the hope that it will be useful, +but WITHOUT ANY WARRANTY; without even the implied warranty of +MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +GNU General Public License for more details. + +You should have received a copy of the GNU General Public License +along with this program; if not, write to the Free Software +Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA +\endverbatim +*/ + + + +/**************************************************************************** + * + * System Includes + * + ****************************************************************************/ + +#include +#include +#include + + +/**************************************************************************** + * + * Project Includes + * + ****************************************************************************/ + + + +/**************************************************************************** + * + * Local Includes + * + ****************************************************************************/ + +#include "AsyncAudioReader.h" + + + +/**************************************************************************** + * + * Namespaces to use + * + ****************************************************************************/ + +using namespace std; +using namespace Async; + + + +/**************************************************************************** + * + * Defines & typedefs + * + ****************************************************************************/ + + + +/**************************************************************************** + * + * Local class definitions + * + ****************************************************************************/ + + + +/**************************************************************************** + * + * Prototypes + * + ****************************************************************************/ + + + +/**************************************************************************** + * + * Exported Global Variables + * + ****************************************************************************/ + + + + +/**************************************************************************** + * + * Local Global Variables + * + ****************************************************************************/ + + + +/**************************************************************************** + * + * Public member functions + * + ****************************************************************************/ + +AudioReader::AudioReader(void) + : buf(0), buf_size(0), input_stopped(false), samples_in_buf(0) +{ + +} /* AudioReader::AudioReader */ + + +AudioReader::~AudioReader(void) +{ + +} /* AudioReader::~AudioReader */ + + +int AudioReader::readSamples(float *samples, int count) +{ + assert(count > 0); + + buf = samples; + buf_size = count; + samples_in_buf = 0; + if (input_stopped) + { + input_stopped = false; + sourceResumeOutput(); + } + buf = 0; + buf_size = 0; + + //printf("AudioReader::readSamples: samples_in_buf=%d\n", samples_in_buf); + + return samples_in_buf; + +} /* AudioReader::readSamples */ + + +int AudioReader::writeSamples(const float *samples, int count) +{ + int samples_to_read = 0; + if (buf != 0) + { + samples_to_read = min(count, buf_size - samples_in_buf); + memcpy(buf + samples_in_buf, samples, samples_to_read * sizeof(*buf)); + samples_in_buf += samples_to_read; + } + + input_stopped = (samples_to_read == 0); + + return samples_to_read; + +} /* AudioReader::writeSamples */ + + +void AudioReader::flushSamples(void) +{ + sourceAllSamplesFlushed(); +} /* AudioReader::flushSamples */ + + + + +/**************************************************************************** + * + * Protected member functions + * + ****************************************************************************/ + + +/* + *------------------------------------------------------------------------ + * Method: + * Purpose: + * Input: + * Output: + * Author: + * Created: + * Remarks: + * Bugs: + *------------------------------------------------------------------------ + */ + + + + + + +/**************************************************************************** + * + * Private member functions + * + ****************************************************************************/ + + +/* + *---------------------------------------------------------------------------- + * Method: + * Purpose: + * Input: + * Output: + * Author: + * Created: + * Remarks: + * Bugs: + *---------------------------------------------------------------------------- + */ + + + + + + + +/* + * This file has not been truncated + */ + diff --git a/src/async/audio/AsyncAudioReader.h b/src/async/audio/AsyncAudioReader.h new file mode 100644 index 0000000..d601b2f --- /dev/null +++ b/src/async/audio/AsyncAudioReader.h @@ -0,0 +1,181 @@ +/** +@file AsyncAudioReader.h +@brief An audio pipe component for on-demand reading samples +@author Tobias Blomberg / SM0SVX +@date 2008-02-22 + +\verbatim +Async - A library for programming event driven applications +Copyright (C) 2003-2008 Tobias Blomberg / SM0SVX + +This program is free software; you can redistribute it and/or modify +it under the terms of the GNU General Public License as published by +the Free Software Foundation; either version 2 of the License, or +(at your option) any later version. + +This program is distributed in the hope that it will be useful, +but WITHOUT ANY WARRANTY; without even the implied warranty of +MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +GNU General Public License for more details. + +You should have received a copy of the GNU General Public License +along with this program; if not, write to the Free Software +Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA +\endverbatim +*/ + +#ifndef ASYNC_AUDIO_READER_INCLUDED +#define ASYNC_AUDIO_READER_INCLUDED + + +/**************************************************************************** + * + * System Includes + * + ****************************************************************************/ + + + +/**************************************************************************** + * + * Project Includes + * + ****************************************************************************/ + +#include + + +/**************************************************************************** + * + * Local Includes + * + ****************************************************************************/ + + + +/**************************************************************************** + * + * Forward declarations + * + ****************************************************************************/ + + + +/**************************************************************************** + * + * Namespace + * + ****************************************************************************/ + +namespace Async +{ + + +/**************************************************************************** + * + * Forward declarations of classes inside of the declared namespace + * + ****************************************************************************/ + + + +/**************************************************************************** + * + * Defines & typedefs + * + ****************************************************************************/ + + + +/**************************************************************************** + * + * Exported Global Variables + * + ****************************************************************************/ + + + +/**************************************************************************** + * + * Class definitions + * + ****************************************************************************/ + +/** +@brief An audio pipe component for on demand reading samples +@author Tobias Blomberg / SM0SVX +@date 2008-02-22 + +This audio pipe component is used when reading samples on demand is preferred +rather than getting them pushed at you. +*/ +class AudioReader : public AudioSink +{ + public: + /** + * @brief Default constuctor + */ + AudioReader(void); + + /** + * @brief Destructor + */ + ~AudioReader(void); + + /** + * @brief Read at most the specified number of samples + * @param samples A buffer to put the read samples into + * @param count The maximum number of samples to read. This value must + * not be larger than the size of the "samples" buffer + * given in the first argument. + * @return Returns the number of samples actually read + */ + int readSamples(float *samples, int count); + + /** + * @brief Write samples into this audio sink + * @param samples The buffer containing the samples + * @param count The number of samples in the buffer + * @return Returns the number of samples that has been taken care of + * + * This function is used to write audio into this audio sink. If it + * returns 0, no more samples should be written until the resumeOutput + * function in the source have been called. + * This function is normally only called from a connected source object. + */ + virtual int writeSamples(const float *samples, int count); + + /** + * @brief Tell the sink to flush the previously written samples + * + * This function is used to tell the sink to flush previously written + * samples. When done flushing, the sourceAllSamplesFlushed function + * will be called. + * This function is normally only called from a connected source object. + */ + virtual void flushSamples(void); + + protected: + + private: + AudioReader(const AudioReader&); + AudioReader& operator=(const AudioReader&); + + float *buf; + int buf_size; + bool input_stopped; + int samples_in_buf; + +}; /* class AudioReader */ + + +} /* namespace */ + +#endif /* ASYNC_AUDIO_READER_INCLUDED */ + + + +/* + * This file has not been truncated + */ + diff --git a/src/async/audio/AsyncAudioRecorder.cpp b/src/async/audio/AsyncAudioRecorder.cpp new file mode 100644 index 0000000..c584328 --- /dev/null +++ b/src/async/audio/AsyncAudioRecorder.cpp @@ -0,0 +1,415 @@ +/** +@file AudioRecorder.cpp +@brief Contains a class for recording raw audio to a file +@author Tobias Blomberg / SM0SVX +@date 2005-08-29 + +\verbatim +Async - A library for programming event driven applications +Copyright (C) 2004-2009 Tobias Blomberg / SM0SVX + +This program is free software; you can redistribute it and/or modify +it under the terms of the GNU General Public License as published by +the Free Software Foundation; either version 2 of the License, or +(at your option) any later version. + +This program is distributed in the hope that it will be useful, +but WITHOUT ANY WARRANTY; without even the implied warranty of +MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +GNU General Public License for more details. + +You should have received a copy of the GNU General Public License +along with this program; if not, write to the Free Software +Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA +\endverbatim +*/ + + + +/**************************************************************************** + * + * System Includes + * + ****************************************************************************/ + +#include +#include +#include +#include +#include +#include +#include + + +/**************************************************************************** + * + * Project Includes + * + ****************************************************************************/ + + + +/**************************************************************************** + * + * Local Includes + * + ****************************************************************************/ + +#include "AsyncAudioRecorder.h" + + + +/**************************************************************************** + * + * Namespaces to use + * + ****************************************************************************/ + +using namespace std; +using namespace Async; + + + +/**************************************************************************** + * + * Defines & typedefs + * + ****************************************************************************/ + +#define WAVE_HEADER_SIZE 44 + + +/**************************************************************************** + * + * Local class definitions + * + ****************************************************************************/ + + + +/**************************************************************************** + * + * Prototypes + * + ****************************************************************************/ + + + +/**************************************************************************** + * + * Exported Global Variables + * + ****************************************************************************/ + + + + +/**************************************************************************** + * + * Local Global Variables + * + ****************************************************************************/ + + + +/**************************************************************************** + * + * Public member functions + * + ****************************************************************************/ + +AudioRecorder::AudioRecorder(const string& filename, + AudioRecorder::Format fmt, + int sample_rate) + : filename(filename), file(NULL), samples_written(0), format(fmt), + sample_rate(sample_rate), max_samples(0), high_water_mark(0), + high_water_mark_reached(false) +{ + timerclear(&begin_timestamp); + timerclear(&end_timestamp); + + if (format == FMT_AUTO) + { + format = FMT_RAW; + + string::size_type dot_pos = filename.rfind('.'); + if (dot_pos > 0) + { + string ext(filename.substr(dot_pos+1)); + if (ext == "wav") + { + format = FMT_WAV; + } + } + } +} /* AudioRecorder::AudioRecorder */ + + +AudioRecorder::~AudioRecorder(void) +{ + closeFile(); +} /* AudioRecorder::~AudioRecorder */ + + +bool AudioRecorder::initialize(void) +{ + assert(file == NULL); + + file = fopen(filename.c_str(), "w"); + if (file == NULL) + { + setErrMsgFromErrno("fopen"); + return false; + } + + if (format == FMT_WAV) + { + // Leave room for the wave file header + if (fseek(file, WAVE_HEADER_SIZE, SEEK_SET) != 0) + { + setErrMsgFromErrno("fseek"); + fclose(file); + file = NULL; + return false; + } + } + + samples_written = 0; + high_water_mark_reached = false; + timerclear(&begin_timestamp); + timerclear(&end_timestamp); + errmsg = ""; + + return true; + +} /* AudioRecorder::initialize */ + + +void AudioRecorder::setMaxRecordingTime(unsigned time_ms, unsigned hw_time_ms) +{ + max_samples = time_ms * (sample_rate / 1000); + high_water_mark = hw_time_ms * (sample_rate / 1000); +} /* AudioRecorder::setMaxRecordingTime */ + + +bool AudioRecorder::closeFile(void) +{ + bool success = true; + if (file != NULL) + { + if (format == FMT_WAV) + { + success = writeWaveHeader(); + } + if (fclose(file) != 0) + { + setErrMsgFromErrno("fclose"); + success = false; + } + file = NULL; + } + return success; +} /* AudioRecorder::closeFile */ + + +int AudioRecorder::writeSamples(const float *samples, int count) +{ + assert(count > 0); + + if (file == NULL) + { + return count; + } + + if (max_samples > 0) + { + if (samples_written >= max_samples) + { + return count; + } + count = min(static_cast(count), max_samples - samples_written); + } + + gettimeofday(&end_timestamp, NULL); + if (!timerisset(&begin_timestamp)) + { + long usec = static_cast(1000000LL * count / sample_rate); + struct timeval block_time = { 0, usec }; + timersub(&end_timestamp, &block_time, &begin_timestamp); + } + + short buf[count]; + for (int i=0; i 1) + { + buf[i] = 32767; + } + else if (sample < -1) + { + buf[i] = -32767; + } + else + { + buf[i] = static_cast(32767.0 * sample); + } + } + + int written = fwrite(buf, sizeof(*buf), count, file); + if ((written != count) && ferror(file)) + { + setErrMsgFromErrno("fwrite"); + errorOccurred(); + closeFile(); + return count; + } + + samples_written += written; + + if ((high_water_mark > 0) && (samples_written >= high_water_mark)) + { + high_water_mark = 0; + high_water_mark_reached = true; + } + + if ((max_samples > 0) && (samples_written >= max_samples)) + { + closeFile(); + maxRecordingTimeReached(); + } + + return written; + +} /* AudioRecorder::writeSamples */ + + +void AudioRecorder::flushSamples(void) +{ + if (high_water_mark_reached) + { + closeFile(); + sourceAllSamplesFlushed(); + maxRecordingTimeReached(); + } + else + { + sourceAllSamplesFlushed(); + } +} /* AudioRecorder::flushSamples */ + + + + +/**************************************************************************** + * + * Protected member functions + * + ****************************************************************************/ + + + + + +/**************************************************************************** + * + * Private member functions + * + ****************************************************************************/ + +bool AudioRecorder::writeWaveHeader(void) +{ + rewind(file); + + char buf[WAVE_HEADER_SIZE]; + char *ptr = buf; + + // ChunkID + memcpy(ptr, "RIFF", 4); + ptr += 4; + + // ChunkSize + ptr += store32bitValue(ptr, 36 + samples_written * sizeof(short)); + + // Format + memcpy(ptr, "WAVE", 4); + ptr += 4; + + // Subchunk1ID + memcpy(ptr, "fmt ", 4); + ptr += 4; + + // Subchunk1Size + ptr += store32bitValue(ptr, 16); + + // AudioFormat (PCM) + ptr += store16bitValue(ptr, 1); + + // NumChannels + ptr += store16bitValue(ptr, 1); + + // SampleRate + ptr += store32bitValue(ptr, sample_rate); + + // ByteRate (sample rate * num channels * bytes per sample) + ptr += store32bitValue(ptr, sample_rate * 1 * sizeof(short)); + + // BlockAlign (num channels * bytes per sample) + ptr += store16bitValue(ptr, 1 * sizeof(short)); + + // BitsPerSample + ptr += store16bitValue(ptr, 16); + + // Subchunk2ID + memcpy(ptr, "data", 4); + ptr += 4; + + // Subchunk2Size (num samples * num channels * bytes per sample) + ptr += store32bitValue(ptr, samples_written * 1 * sizeof(short)); + + assert(ptr - buf == WAVE_HEADER_SIZE); + + if (fwrite(buf, 1, WAVE_HEADER_SIZE, file) != WAVE_HEADER_SIZE) + { + setErrMsgFromErrno("fwrite"); + return false; + } + return true; +} /* AudioRecorder::writeWaveHeader */ + + +int AudioRecorder::store32bitValue(char *ptr, uint32_t val) +{ + *ptr++ = val & 0xff; + val >>= 8; + *ptr++ = val & 0xff; + val >>= 8; + *ptr++ = val & 0xff; + val >>= 8; + *ptr++ = val & 0xff; + return 4; +} /* AudioRecorder::store32bitValue */ + + +int AudioRecorder::store16bitValue(char *ptr, uint16_t val) +{ + *ptr++ = val & 0xff; + val >>= 8; + *ptr++ = val & 0xff; + return 2; +} /* AudioRecorder::store32bitValue */ + + +void AudioRecorder::setErrMsgFromErrno(const std::string &fname) +{ + ostringstream ss; + ss << fname << ": " << strerror(errno); + errmsg = ss.str(); +} /* AudioRecorder::setErrMsgFromErrno */ + + + +/* + * This file has not been truncated + */ + diff --git a/src/async/audio/AsyncAudioRecorder.h b/src/async/audio/AsyncAudioRecorder.h new file mode 100644 index 0000000..61a85d3 --- /dev/null +++ b/src/async/audio/AsyncAudioRecorder.h @@ -0,0 +1,280 @@ +/** +@file AsyncAudioRecorder.h +@brief Contains a class for recording raw audio to a file +@author Tobias Blomberg / SM0SVX +@date 2005-08-29 + +\verbatim +Async - A library for programming event driven applications +Copyright (C) 2004-2009 Tobias Blomberg / SM0SVX + +This program is free software; you can redistribute it and/or modify +it under the terms of the GNU General Public License as published by +the Free Software Foundation; either version 2 of the License, or +(at your option) any later version. + +This program is distributed in the hope that it will be useful, +but WITHOUT ANY WARRANTY; without even the implied warranty of +MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +GNU General Public License for more details. + +You should have received a copy of the GNU General Public License +along with this program; if not, write to the Free Software +Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA +\endverbatim +*/ + +#ifndef ASYNC_AUDIO_RECORDER_INCLUDED +#define ASYNC_AUDIO_RECORDER_INCLUDED + + +/**************************************************************************** + * + * System Includes + * + ****************************************************************************/ + +#include +#include +#include +#include + +#include + +#include + + +/**************************************************************************** + * + * Project Includes + * + ****************************************************************************/ + + + +/**************************************************************************** + * + * Local Includes + * + ****************************************************************************/ + + + +/**************************************************************************** + * + * Forward declarations + * + ****************************************************************************/ + + + +/**************************************************************************** + * + * Namespace + * + ****************************************************************************/ + +namespace Async +{ + + +/**************************************************************************** + * + * Forward declarations of classes inside of the declared namespace + * + ****************************************************************************/ + + + +/**************************************************************************** + * + * Defines & typedefs + * + ****************************************************************************/ + + + +/**************************************************************************** + * + * Exported Global Variables + * + ****************************************************************************/ + + + +/**************************************************************************** + * + * Class definitions + * + ****************************************************************************/ + +/** +@brief A class for recording raw audio to a file +@author Tobias Blomberg / SM0SVX +@date 2005-08-29 + +Use this class to stream audio into a file. The audio is stored in raw format, +(only samples no header) or WAV format. +*/ +class AudioRecorder : public Async::AudioSink +{ + public: + typedef enum { FMT_AUTO, FMT_RAW, FMT_WAV } Format; + + /** + * @brief Default constuctor + * @param filename The name of the file to record audio to + * @param fmt The file format (@see Format) + * @param sample_rate The sample rate (defaults to INTERNAL_SAMPLE_RATE) + */ + explicit AudioRecorder(const std::string& filename, + AudioRecorder::Format fmt=FMT_AUTO, + int sample_rate=INTERNAL_SAMPLE_RATE); + + /** + * @brief Destructor + */ + ~AudioRecorder(void); + + /** + * @brief Initialize the recorder + * @return Return \em true if the initialization was successful + * + * This function will initialize the recorder and open the file. + * On error, this function returns \em false. The error message can be + * retrieved using the errorMsg function. + */ + bool initialize(void); + + /** + * @brief Set the maximum length of this recording + * @param time_ms The maximum time in milliseconds + * @param hw_time_ms The high watermark time in milliseconds + * + * Use this function to set the maximum time for a recording. When the + * limit has been reached, the file will be closed and any incoming samples + * will be thrown away. + * The high watermark time is a soft limit. After reaching the high + * watermark, the recording will be ended as soon as the audio source + * call flushSamples. The high watermark need to be set to less than + * the maximum time to have any effect. A time of 0 disables it. + * The sampling rate given in the constructor call is used to calculate + * the time. Setting the time to 0 will allow the file to grow indefinetly. + */ + void setMaxRecordingTime(unsigned time_ms, unsigned hw_time_ms=0); + + /** + * @brief Close the file + * @returns Return \em true if closing went well or \em false otherwise + * + * This function will close the file being recorded to. When the file has + * been closed, all samples coming in after that will be discarded. + * If an error occurr, this function will return \em false. The error + * message can be retrieved using the errorMsg function. + */ + bool closeFile(void); + + /** + * @brief Find out how many samples that have been written so far + * @return Returns the number of samples written so far + */ + unsigned samplesWritten(void) const { return samples_written; } + + /** + * @brief The timestamp of the first stored sample + * @returns Returns the timestamp + */ + const struct timeval &beginTimestamp(void) const { return begin_timestamp; } + + /** + * @brief The timestamp of the last stored sample + * @returns Returns the timestamp + */ + const struct timeval &endTimestamp(void) const { return end_timestamp; } + + /** + * @brief Write samples into this audio sink + * @param samples The buffer containing the samples + * @param count The number of samples in the buffer + * @return Returns the number of samples that has been taken care of + * + * This function is used to write audio into this audio sink. If it + * returns 0, no more samples should be written until the resumeOutput + * function in the source have been called. + * This function is normally only called from a connected source object. + */ + virtual int writeSamples(const float *samples, int count); + + /** + * @brief Tell the sink to flush the previously written samples + * + * This function is used to tell the sink to flush previously written + * samples. When done flushing, the sink should call the + * sourceAllSamplesFlushed function. + * This function is normally only called from a connected source object. + */ + virtual void flushSamples(void); + + /** + * @brief Return the current error message + * @returns Returns the currently set error message + * + * This function is used to retrieve the last set error message. It can + * for example be used when the initialize method return false or when the + * error signal is emitted. + */ + std::string errorMsg(void) const { return errmsg; } + + /** + * @brief A signal that's emitted when the max recording time is reached + * + * This signal will be emitted when any of the maximum recording time or + * the high watermark limits are reached. Before this signal is emitted, + * the file is closed. It is safe to delete the audio recorder from the + * slot that is connected to this signal. + */ + sigc::signal maxRecordingTimeReached; + + /** + * @brief This signal is emitted when an error occurrs in the recorder + * + * This signal will be emitted when an error occurrs in the recorder. + * After emitting the signal the associated file will be closed. All audio + * that is written after an error has occurred will be thrown away. To open + * a new file, call the initialize method again. + */ + sigc::signal errorOccurred; + + private: + std::string filename; + FILE *file; + unsigned samples_written; + Format format; + int sample_rate; + unsigned max_samples; + unsigned high_water_mark; + bool high_water_mark_reached; + struct timeval begin_timestamp; + struct timeval end_timestamp; + std::string errmsg; + + AudioRecorder(const AudioRecorder&); + AudioRecorder& operator=(const AudioRecorder&); + bool writeWaveHeader(void); + int store32bitValue(char *ptr, uint32_t val); + int store16bitValue(char *ptr, uint16_t val); + void setErrMsgFromErrno(const std::string &fname); + +}; /* class AudioRecorder */ + + +} /* namespace */ + +#endif /* ASYNC_AUDIO_RECORDER_INCLUDED */ + + + +/* + * This file has not been truncated + */ + diff --git a/src/async/audio/AsyncAudioSelector.cpp b/src/async/audio/AsyncAudioSelector.cpp new file mode 100644 index 0000000..e0ffecd --- /dev/null +++ b/src/async/audio/AsyncAudioSelector.cpp @@ -0,0 +1,508 @@ +/** +@file AsyncAudioSelector.cpp +@brief This file contains a class that is used to select one of many audio + streams. +@author Tobias Blomberg / SM0SVX +@date 2006-08-01 + +\verbatim +Async - A library for programming event driven applications +Copyright (C) 2003-2019 Tobias Blomberg / SM0SVX + +This program is free software; you can redistribute it and/or modify +it under the terms of the GNU General Public License as published by +the Free Software Foundation; either version 2 of the License, or +(at your option) any later version. + +This program is distributed in the hope that it will be useful, +but WITHOUT ANY WARRANTY; without even the implied warranty of +MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +GNU General Public License for more details. + +You should have received a copy of the GNU General Public License +along with this program; if not, write to the Free Software +Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA +\endverbatim +*/ + + + +/**************************************************************************** + * + * System Includes + * + ****************************************************************************/ + + + +/**************************************************************************** + * + * Project Includes + * + ****************************************************************************/ + +#include + + +/**************************************************************************** + * + * Local Includes + * + ****************************************************************************/ + +#include "AsyncAudioSelector.h" + + + +/**************************************************************************** + * + * Namespaces to use + * + ****************************************************************************/ + +using namespace std; +using namespace Async; + + + +/**************************************************************************** + * + * Defines & typedefs + * + ****************************************************************************/ + + + +/**************************************************************************** + * + * Local class definitions + * + ****************************************************************************/ + +class Async::AudioSelector::Branch : public AudioSink +{ + public: + Branch(AudioSelector *selector) + : m_selector(selector), m_auto_select(false), m_prio(0), + m_stream_state(STATE_IDLE), m_flush_wait(true) + { + assert(selector != 0); + } + + StreamState streamState(void) const { return m_stream_state; } + void setSelectionPrio(int prio) { m_prio = prio; } + int selectionPrio(void) const { return m_prio; } + void enableAutoSelect(void) { m_auto_select = true; } + bool autoSelectEnabled(void) const { return m_auto_select; } + void setFlushWait(bool flush_wait) { m_flush_wait = flush_wait; } + bool flushWait(void) const { return m_flush_wait; } + + void disableAutoSelect(void) + { + m_auto_select = false; + if (isSelected()) + { + m_selector->selectHighestPrioActiveBranch(true); + } + } + + bool isSelected(void) const + { + return (m_selector->selectedBranch() == this); + } + + virtual int writeSamples(const float *samples, int count) + { + assert(count > 0); + m_stream_state = STATE_WRITING; + if (m_auto_select && !isSelected()) + { + const Branch *selected_branch = m_selector->selectedBranch(); + if ((selected_branch == 0) || + (selected_branch->selectionPrio() < m_prio)) + { + m_selector->selectBranch(this); + } + } + int ret(count); + if (isSelected()) + { + ret = m_selector->branchWriteSamples(samples, count); + if (ret == 0) + { + m_stream_state = STATE_STOPPED; + } + } + return ret; + } + + virtual void flushSamples(void) + { + switch (m_stream_state) + { + case STATE_IDLE: + sourceAllSamplesFlushed(); + break; + + case STATE_WRITING: + case STATE_STOPPED: + if (isSelected()) + { + m_stream_state = STATE_FLUSHING; + m_selector->branchFlushSamples(); + } + else + { + m_stream_state = STATE_IDLE; + sourceAllSamplesFlushed(); + } + break; + + case STATE_FLUSHING: + break; + } + } + + void resumeOutput(void) + { + if (m_stream_state == STATE_STOPPED) + { + m_stream_state = STATE_WRITING; + sourceResumeOutput(); + } + } + + void allSamplesFlushed(void) + { + if (m_stream_state == STATE_FLUSHING) + { + m_stream_state = STATE_IDLE; + if (m_auto_select) + { + m_selector->selectBranch(0); + } + sourceAllSamplesFlushed(); + } + } + + void unselect(void) + { + switch (m_stream_state) + { + case STATE_IDLE: + case STATE_WRITING: + break; + + case STATE_STOPPED: + m_stream_state = STATE_WRITING; + sourceResumeOutput(); + break; + + case STATE_FLUSHING: + m_stream_state = STATE_IDLE; + sourceAllSamplesFlushed(); + break; + } + } + + private: + AudioSelector * m_selector; + bool m_auto_select; + int m_prio; + StreamState m_stream_state; + bool m_flush_wait; + +}; /* class Async::AudioSelector::Branch */ + + +/**************************************************************************** + * + * Prototypes + * + ****************************************************************************/ + + + +/**************************************************************************** + * + * Exported Global Variables + * + ****************************************************************************/ + + + + +/**************************************************************************** + * + * Local Global Variables + * + ****************************************************************************/ + + + +/**************************************************************************** + * + * Public member functions + * + ****************************************************************************/ + +AudioSelector::AudioSelector(void) + : m_selected_branch(0), m_stream_state(STATE_IDLE) +{ +} /* AudioSelector::AudioSelector */ + + +AudioSelector::~AudioSelector(void) +{ + //selectSource(0); + BranchMap::iterator it; + for (it = m_branch_map.begin(); it != m_branch_map.end(); ++it) + { + delete (*it).second; + } +} /* AudioSelector::~AudioSelector */ + + +void AudioSelector::addSource(Async::AudioSource *source) +{ + assert(source != 0); + assert(m_branch_map.find(source) == m_branch_map.end()); + Branch *branch = new Branch(this); + source->registerSink(branch); + m_branch_map[source] = branch; +} /* AudioSelector::addSource */ + + +void AudioSelector::removeSource(AudioSource *source) +{ + BranchMap::iterator it = m_branch_map.find(source); + assert(it != m_branch_map.end()); + Branch *branch = (*it).second; + m_branch_map.erase(it); + assert(m_branch_map.find(source) == m_branch_map.end()); + if (branch == selectedBranch()) + { + selectHighestPrioActiveBranch(true); + } + delete branch; +} /* AudioSelector::removeSource */ + + +void AudioSelector::setSelectionPrio(AudioSource *source, int prio) +{ + BranchMap::iterator it = m_branch_map.find(source); + assert(it != m_branch_map.end()); + Branch *branch = (*it).second; + branch->setSelectionPrio(prio); +} /* AudioSelector::setAutoSelectPrio */ + + +void AudioSelector::enableAutoSelect(AudioSource *source, int prio) +{ + BranchMap::iterator it = m_branch_map.find(source); + assert(it != m_branch_map.end()); + Branch *branch = (*it).second; + branch->setSelectionPrio(prio); + branch->enableAutoSelect(); +} /* AudioSelector::enableAutoSelect */ + + +void AudioSelector::disableAutoSelect(AudioSource *source) +{ + BranchMap::iterator it = m_branch_map.find(source); + assert(it != m_branch_map.end()); + Branch *branch = (*it).second; + branch->disableAutoSelect(); +} /* AudioSelector::disableAutoSelect */ + + +bool AudioSelector::autoSelectEnabled(const AudioSource *source) const +{ + BranchMap::const_iterator it = m_branch_map.find( + const_cast(source)); + assert(it != m_branch_map.end()); + const Branch *branch = (*it).second; + return branch->autoSelectEnabled(); +} /* AudioSelector::autoSelectEnabled */ + + +void AudioSelector::selectSource(AudioSource *source) +{ + Branch *branch = 0; + if (source != 0) + { + BranchMap::iterator it = m_branch_map.find(source); + assert(it != m_branch_map.end()); + branch = (*it).second; + } + selectBranch(branch); +} /* AudioSelector::selectSource */ + + +AudioSource *AudioSelector::selectedSource(void) const +{ + for (BranchMap::const_iterator it = m_branch_map.begin(); + it != m_branch_map.end(); ++it) + { + if (it->second == selectedBranch()) + { + return it->first; + } + } + return 0; +} /* AudioSelector::selectedSource */ + + +void AudioSelector::setFlushWait(AudioSource *source, bool flush_wait) +{ + BranchMap::iterator it = m_branch_map.find(source); + assert(it != m_branch_map.end()); + Branch *branch = (*it).second; + branch->setFlushWait(flush_wait); +} /* AudioSelector::setFlushWait */ + + +void AudioSelector::resumeOutput(void) +{ + if (m_stream_state == STATE_STOPPED) + { + m_stream_state = STATE_WRITING; + assert(m_selected_branch != 0); + m_selected_branch->resumeOutput(); + } +} /* AudioSelector::resumeOutput */ + + +/**************************************************************************** + * + * Protected member functions + * + ****************************************************************************/ + +void AudioSelector::allSamplesFlushed(void) +{ + if (m_stream_state == STATE_FLUSHING) + { + m_stream_state = STATE_IDLE; + if (m_selected_branch != 0) + { + m_selected_branch->allSamplesFlushed(); + } + } +} /* AudioSelector::allSamplesFlushed */ + + +/**************************************************************************** + * + * Private member functions + * + ****************************************************************************/ + +int AudioSelector::branchWriteSamples(const float *samples, int count) +{ + m_stream_state = STATE_WRITING; + int ret = sinkWriteSamples(samples, count); + assert(ret >= 0); + if (ret == 0) + { + m_stream_state = STATE_STOPPED; + } + return ret; +} /* AudioSelector::branchWriteSamples */ + + +void AudioSelector::branchFlushSamples(void) +{ + assert(m_selected_branch != 0); + Branch *flusher = m_selected_branch; + if (!m_selected_branch->flushWait()) + { + selectHighestPrioActiveBranch(false); + } + + if (m_selected_branch == flusher) + { + switch (m_stream_state) + { + case STATE_IDLE: + m_selected_branch->allSamplesFlushed(); + break; + + case STATE_WRITING: + case STATE_STOPPED: + m_stream_state = STATE_FLUSHING; + sinkFlushSamples(); + break; + + case STATE_FLUSHING: + break; + } + } +} /* AudioSelector::branchFlushSamples */ + + +void AudioSelector::selectBranch(Branch *branch) +{ + if (branch == m_selected_branch) + { + return; + } + + Branch *prev_branch = m_selected_branch; + m_selected_branch = branch; + if (prev_branch != 0) + { + prev_branch->unselect(); + } + + assert((m_selected_branch == 0) || + (m_selected_branch->streamState() == STATE_IDLE) || + (m_selected_branch->streamState() == STATE_WRITING)); + + switch (m_stream_state) + { + case STATE_IDLE: + case STATE_FLUSHING: + break; + + case STATE_WRITING: + case STATE_STOPPED: + if ((m_selected_branch == 0) || + (m_selected_branch->streamState() == STATE_IDLE)) + { + m_stream_state = STATE_FLUSHING; + sinkFlushSamples(); + } + break; + } +} /* AudioSelector::selectBranch */ + + +void AudioSelector::selectHighestPrioActiveBranch(bool clear_if_no_active) +{ + Branch *new_branch = 0; + for (BranchMap::iterator it = m_branch_map.begin(); + it != m_branch_map.end(); ++it) + { + Branch *branch = (*it).second; + if (branch->autoSelectEnabled()) + { + if (((branch->streamState() == STATE_WRITING) || + (branch->streamState() == STATE_STOPPED)) && + ((new_branch == 0) || + (branch->selectionPrio() > new_branch->selectionPrio()))) + { + new_branch = branch; + } + } + } + if ((new_branch != 0) || clear_if_no_active) + { + selectBranch(new_branch); + } +} /* AudioSelector::selectHighestPrioActiveBranch */ + + +/* + * This file has not been truncated + */ diff --git a/src/async/audio/AsyncAudioSelector.h b/src/async/audio/AsyncAudioSelector.h new file mode 100644 index 0000000..3ce8c30 --- /dev/null +++ b/src/async/audio/AsyncAudioSelector.h @@ -0,0 +1,241 @@ +/** +@file AsyncAudioSelector.h +@brief This file contains a class that is used to select one of many audio + streams. +@author Tobias Blomberg / SM0SVX +@date 2006-08-01 + +\verbatim +Async - A library for programming event driven applications +Copyright (C) 2004-2019 Tobias Blomberg / SM0SVX + +This program is free software; you can redistribute it and/or modify +it under the terms of the GNU General Public License as published by +the Free Software Foundation; either version 2 of the License, or +(at your option) any later version. + +This program is distributed in the hope that it will be useful, +but WITHOUT ANY WARRANTY; without even the implied warranty of +MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +GNU General Public License for more details. + +You should have received a copy of the GNU General Public License +along with this program; if not, write to the Free Software +Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA +\endverbatim +*/ + +#ifndef ASYNC_AUDIO_SELECTOR_INCLUDED +#define ASYNC_AUDIO_SELECTOR_INCLUDED + + +/**************************************************************************** + * + * System Includes + * + ****************************************************************************/ + +#include + + +/**************************************************************************** + * + * Project Includes + * + ****************************************************************************/ + +#include + + +/**************************************************************************** + * + * Local Includes + * + ****************************************************************************/ + + + +/**************************************************************************** + * + * Forward declarations + * + ****************************************************************************/ + + + +/**************************************************************************** + * + * Namespace + * + ****************************************************************************/ + +namespace Async +{ + + +/**************************************************************************** + * + * Forward declarations of classes inside of the declared namespace + * + ****************************************************************************/ + + + +/**************************************************************************** + * + * Defines & typedefs + * + ****************************************************************************/ + + + +/**************************************************************************** + * + * Exported Global Variables + * + ****************************************************************************/ + + + +/**************************************************************************** + * + * Class definitions + * + ****************************************************************************/ + +/** +@brief This class is used to select one of many audio streams +@author Tobias Blomberg / SM0SVX +@date 2006-08-01 + +This class is used to select one of many incoming audio streams. Incoming +samples on non-selected branches will be thrown away. +*/ +class AudioSelector : public AudioSource +{ + public: + /** + * @brief Default constuctor + */ + AudioSelector(void); + + /** + * @brief Destructor + */ + ~AudioSelector(void); + + /** + * @brief Add an audio source to the selector + * @param source The audio source to add + */ + void addSource(AudioSource *source); + + /** + * @brief Remove a previously added audio source from the selector + * @param source The audio source to remove + */ + void removeSource(AudioSource *source); + + /** + * @brief Set the prio to be used for selection + * @param source The audio source + * @param prio The priority to set. Higher numbers give higher priority. + */ + void setSelectionPrio(AudioSource *source, int prio); + + /** + * @brief Enable autoselection on the given source + * @param source The audio source + * @param prio The priority to set. Higher numbers give higher priority. + */ + void enableAutoSelect(AudioSource *source, int prio); + + /** + * @brief Disable autoselection on the given source + * @param source The audio source + */ + void disableAutoSelect(AudioSource *source); + + /** + * @brief Find out if auto select is enabled or not for the given source + * @param source The audio source + * @return Returns \em true if auto select is enabled for the given source + * or else \em false is returned + */ + bool autoSelectEnabled(const AudioSource *source) const; + + /** + * @brief Select one of the previously added audio sources + * @param source The audio source to select. 0 = none selected. + */ + void selectSource(AudioSource *source); + + /** + * @brief Find out which source that is currently selected + * @return Returns the selected source or 0 if no source is selected + */ + AudioSource *selectedSource(void) const; + + /** + * @brief Set if this souce want to wait for allSamplesFlushed + * @param source The audio source to select. 0 = none selected. + * @param flush_wait Set to \em true to wait for flush or else \em false + * + * Normally after a source signals flush, the audio selector will wait until + * the connected sink signals that all samples have been flushed before + * any other source with the same or lower priority can be selected. + * If flush_wait is set to false, the selector will immediately signal + * all samples flushed to the source and if any other source is active, + * that source will immediately be switched in without sending a flush + * command to the sink. + */ + void setFlushWait(AudioSource *source, bool flush_wait); + + /** + * @brief Resume audio output to the sink + * + * This function will be called when the registered audio sink is ready to + * accept more samples. It is normally only called from a connected sink + * object. + */ + virtual void resumeOutput(void); + + protected: + virtual void allSamplesFlushed(void); + + private: + typedef enum + { + STATE_IDLE, STATE_WRITING, STATE_STOPPED, STATE_FLUSHING + } StreamState; + + class Branch; + typedef std::map BranchMap; + + BranchMap m_branch_map; + Branch * m_selected_branch; + StreamState m_stream_state; + + AudioSelector(const AudioSelector&); + AudioSelector& operator=(const AudioSelector&); + void selectBranch(Branch *branch); + Branch *selectedBranch(void) const { return m_selected_branch; } + void selectHighestPrioActiveBranch(bool clear_if_no_active); + int branchWriteSamples(const float *samples, int count); + void branchFlushSamples(void); + + friend class Branch; + +}; /* class AudioSelector */ + + +} /* namespace */ + +#endif /* ASYNC_AUDIO_SELECTOR_INCLUDED */ + + + +/* + * This file has not been truncated + */ + diff --git a/src/async/audio/AsyncAudioSink.cpp b/src/async/audio/AsyncAudioSink.cpp new file mode 100644 index 0000000..a3f76b0 --- /dev/null +++ b/src/async/audio/AsyncAudioSink.cpp @@ -0,0 +1,290 @@ +/** +@file AsyncAudioSink.cpp +@brief This file contains the base class for an audio sink +@author Tobias Blomberg / SM0SVX +@date 2005-04-17 + +\verbatim +Async - A library for programming event driven applications +Copyright (C) 2003-2015 Tobias Blomberg / SM0SVX + +This program is free software; you can redistribute it and/or modify +it under the terms of the GNU General Public License as published by +the Free Software Foundation; either version 2 of the License, or +(at your option) any later version. + +This program is distributed in the hope that it will be useful, +but WITHOUT ANY WARRANTY; without even the implied warranty of +MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +GNU General Public License for more details. + +You should have received a copy of the GNU General Public License +along with this program; if not, write to the Free Software +Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA +\endverbatim +*/ + + + +/**************************************************************************** + * + * System Includes + * + ****************************************************************************/ + + + +/**************************************************************************** + * + * Project Includes + * + ****************************************************************************/ + + + +/**************************************************************************** + * + * Local Includes + * + ****************************************************************************/ + +#include "AsyncAudioSink.h" +#include "AsyncAudioSource.h" + + + +/**************************************************************************** + * + * Namespaces to use + * + ****************************************************************************/ + +using namespace Async; + + + +/**************************************************************************** + * + * Defines & typedefs + * + ****************************************************************************/ + + + +/**************************************************************************** + * + * Local class definitions + * + ****************************************************************************/ + + + +/**************************************************************************** + * + * Prototypes + * + ****************************************************************************/ + + + +/**************************************************************************** + * + * Exported Global Variables + * + ****************************************************************************/ + + + + +/**************************************************************************** + * + * Local Global Variables + * + ****************************************************************************/ + + + +/**************************************************************************** + * + * Public member functions + * + ****************************************************************************/ + + +AudioSink::~AudioSink(void) +{ + unregisterSource(); + clearHandler(); +} /* AudioSink::~AudioSink */ + + +bool AudioSink::registerSource(AudioSource *source) +{ + return registerSourceInternal(source, true); +} /* AudioSink::registerSource */ + + +void AudioSink::unregisterSource(void) +{ + if (m_source == 0) + { + return; + } + + AudioSource *source = m_source; + m_source = 0; + + if (m_auto_unreg_sink) + { + source->unregisterSink(); + } + + if (m_handler != 0) + { + m_handler->unregisterSource(); + } +} /* AudioSink::unregisterSource */ + + + +/**************************************************************************** + * + * Protected member functions + * + ****************************************************************************/ + + +/* + *------------------------------------------------------------------------ + * Method: + * Purpose: + * Input: + * Output: + * Author: + * Created: + * Remarks: + * Bugs: + *------------------------------------------------------------------------ + */ +void AudioSink::sourceResumeOutput(void) +{ + if (m_source != 0) + { + m_source->resumeOutput(); + } +} /* AudioSink::sourceResumeOutput */ + + +void AudioSink::sourceAllSamplesFlushed(void) +{ + if (m_source != 0) + { + m_source->handleAllSamplesFlushed(); + } +} /* AudioSink::sourceAllSamplesFlushed */ + + +bool AudioSink::setHandler(AudioSink *handler) +{ + clearHandler(); + + if (handler == 0) + { + return true; + } + + if (m_source != 0) + { + if (!handler->registerSourceInternal(m_source, false)) + { + return false; + } + } + + m_handler = handler; + + return true; + +} /* AudioSink::setHandler */ + + +void AudioSink::clearHandler(void) +{ + if (m_handler == 0) + { + return; + } + + if (m_source != 0) + { + m_handler->unregisterSource(); + } + + m_handler = 0; +} /* AudioSink::clearHandler */ + + + +/**************************************************************************** + * + * Private member functions + * + ****************************************************************************/ + + +/* + *---------------------------------------------------------------------------- + * Method: + * Purpose: + * Input: + * Output: + * Author: + * Created: + * Remarks: + * Bugs: + *---------------------------------------------------------------------------- + */ +bool AudioSink::registerSourceInternal(AudioSource *source, bool reg_sink) +{ + assert(source != 0); + + if (m_source != 0) + { + return m_source == source; + } + + m_source = source; + m_auto_unreg_sink = reg_sink; + if (reg_sink) + { + if (!m_source->registerSink(this)) + { + m_source = 0; + return false; + } + } + + if (m_handler != 0) + { + if (!m_handler->registerSourceInternal(source, false)) + { + if (reg_sink) + { + m_source->unregisterSink(); + } + m_source = 0; + return false; + } + } + + return true; + +} /* AudioSink::registerSourceInternal */ + + + + +/* + * This file has not been truncated + */ + diff --git a/src/async/audio/AsyncAudioSink.h b/src/async/audio/AsyncAudioSink.h new file mode 100644 index 0000000..dd16470 --- /dev/null +++ b/src/async/audio/AsyncAudioSink.h @@ -0,0 +1,244 @@ +/** +@file AsyncAudioSink.h +@brief This file contains the base class for an audio sink +@author Tobias Blomberg / SM0SVX +@date 2005-04-17 + +\verbatim +Async - A library for programming event driven applications +Copyright (C) 2003-2015 Tobias Blomberg / SM0SVX + +This program is free software; you can redistribute it and/or modify +it under the terms of the GNU General Public License as published by +the Free Software Foundation; either version 2 of the License, or +(at your option) any later version. + +This program is distributed in the hope that it will be useful, +but WITHOUT ANY WARRANTY; without even the implied warranty of +MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +GNU General Public License for more details. + +You should have received a copy of the GNU General Public License +along with this program; if not, write to the Free Software +Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA +\endverbatim +*/ + + +#ifndef ASYNC_AUDIO_SINK_INCLUDED +#define ASYNC_AUDIO_SINK_INCLUDED + + +/**************************************************************************** + * + * System Includes + * + ****************************************************************************/ + + +#include + + +/**************************************************************************** + * + * Project Includes + * + ****************************************************************************/ + + + +/**************************************************************************** + * + * Local Includes + * + ****************************************************************************/ + + + +/**************************************************************************** + * + * Forward declarations + * + ****************************************************************************/ + + + +/**************************************************************************** + * + * Namespace + * + ****************************************************************************/ + +namespace Async +{ + + +/**************************************************************************** + * + * Forward declarations of classes inside of the declared namespace + * + ****************************************************************************/ + +class AudioSource; + + +/**************************************************************************** + * + * Defines & typedefs + * + ****************************************************************************/ + + + +/**************************************************************************** + * + * Exported Global Variables + * + ****************************************************************************/ + + + +/**************************************************************************** + * + * Class definitions + * + ****************************************************************************/ + +/** +@brief The base class for an audio sink +@author Tobias Blomberg +@date 2005-04-17 + +This is the base class for an audio sink. An audio sink is a class that +can consume audio. +*/ +class AudioSink +{ + public: + /** + * @brief Default constuctor + */ + AudioSink(void) : m_source(0), m_handler(0), m_auto_unreg_sink(false) {} + + /** + * @brief Destructor + */ + virtual ~AudioSink(void); + + /** + * @brief Register an audio source to provide samples to this sink + * @param source The audio source to use + * @return Returns \em true on success or else \em false + */ + bool registerSource(AudioSource *source); + + /** + * @brief Unregister the previously registered audio source + */ + void unregisterSource(void); + + /** + * @brief Check if an audio source has been registered + * @return Returns \em true if there is an audio source registerd + */ + bool isRegistered(void) const { return m_source != 0; } + + /** + * @brief Get the registered audio source + * @return Returns the registered audio source if any registered or else + * returns 0. + */ + AudioSource *source(void) const { return m_source; } + + /** + * @brief Write samples into this audio sink + * @param samples The buffer containing the samples + * @param count The number of samples in the buffer + * @return Returns the number of samples that has been taken care of + * + * This function is used to write audio into this audio sink. If it + * returns 0, no more samples should be written until the resumeOutput + * function in the source have been called. + * This function is normally only called from a connected source object. + */ + virtual int writeSamples(const float *samples, int count) + { + assert(m_handler != 0); + return m_handler->writeSamples(samples, count); + } + + /** + * @brief Tell the sink to flush the previously written samples + * + * This function is used to tell the sink to flush previously written + * samples. When done flushing, the sink should call the + * sourceAllSamplesFlushed function. + * This function is normally only called from a connected source object. + */ + virtual void flushSamples(void) + { + assert(m_handler != 0); + m_handler->flushSamples(); + } + + + protected: + /** + * @brief Tell the source that we are ready to accept more samples + */ + void sourceResumeOutput(void); + + /** + * @brief Tell the source that all samples have been flushed + * + * This function is called by the inheriting class to indicate that + * all samples have been flushed. It may only be called after a + * flushSamples call has been received and no more samples has been + * written to the sink. + */ + void sourceAllSamplesFlushed(void); + + /** + * @brief Setup another sink to handle the incoming audio + * @param handler The sink to handle the audio + * @return Returns \em true on success or else \em false + * + * This function will setup another sink to handle incoming audio. + * This can be used when an internal object should handle the audio + * for this object. + */ + bool setHandler(AudioSink *handler); + + /** + * @brief Clear a handler that was previously setup with setHandler. + */ + void clearHandler(void); + + /* + * @brief Return the handler + * @return Returns the handler previously set with setHandler or 0 + * if none have been set + */ + AudioSink *handler(void) const { return m_handler; } + + + private: + AudioSource *m_source; + AudioSink *m_handler; + bool m_auto_unreg_sink; + + bool registerSourceInternal(AudioSource *source, bool reg_sink); + +}; /* class AudioSink */ + + +} /* namespace */ + +#endif /* ASYNC_AUDIO_SINK_INCLUDED */ + + + +/* + * This file has not been truncated + */ + diff --git a/src/async/audio/AsyncAudioSource.cpp b/src/async/audio/AsyncAudioSource.cpp new file mode 100644 index 0000000..32fb208 --- /dev/null +++ b/src/async/audio/AsyncAudioSource.cpp @@ -0,0 +1,341 @@ +/** +@file AsyncAudioSource.cpp +@brief This file contains the base class for an audio source +@author Tobias Blomberg / SM0SVX +@date 2005-04-17 + +\verbatim +Async - A library for programming event driven applications +Copyright (C) 2003-2025 Tobias Blomberg / SM0SVX + +This program is free software; you can redistribute it and/or modify +it under the terms of the GNU General Public License as published by +the Free Software Foundation; either version 2 of the License, or +(at your option) any later version. + +This program is distributed in the hope that it will be useful, +but WITHOUT ANY WARRANTY; without even the implied warranty of +MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +GNU General Public License for more details. + +You should have received a copy of the GNU General Public License +along with this program; if not, write to the Free Software +Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA +\endverbatim +*/ + + + +/**************************************************************************** + * + * System Includes + * + ****************************************************************************/ + + + +/**************************************************************************** + * + * Project Includes + * + ****************************************************************************/ + + + +/**************************************************************************** + * + * Local Includes + * + ****************************************************************************/ + +#include "AsyncAudioSource.h" +#include "AsyncAudioSink.h" + + + +/**************************************************************************** + * + * Namespaces to use + * + ****************************************************************************/ + +using namespace Async; + + + +/**************************************************************************** + * + * Defines & typedefs + * + ****************************************************************************/ + + + +/**************************************************************************** + * + * Local class definitions + * + ****************************************************************************/ + + + +/**************************************************************************** + * + * Prototypes + * + ****************************************************************************/ + + + +/**************************************************************************** + * + * Exported Global Variables + * + ****************************************************************************/ + + + + +/**************************************************************************** + * + * Local Global Variables + * + ****************************************************************************/ + + + +/**************************************************************************** + * + * Public member functions + * + ****************************************************************************/ + + +AudioSource::~AudioSource(void) +{ + if (m_sink_managed) + { + AudioSink *sink = m_sink; + m_sink = 0; + delete sink; + } + else + { + unregisterSinkInternal(true); + } + + clearHandler(); + +} /* AudioSource::~AudioSource */ + + +bool AudioSource::registerSink(AudioSink *sink, bool managed) +{ + return registerSinkInternal(sink, managed, true); +} /* AudioSource::registerSink */ + + +void AudioSource::unregisterSink(void) +{ + unregisterSinkInternal(false); +} /* AudioSource::unregisterSink */ + + + + + +/**************************************************************************** + * + * Protected member functions + * + ****************************************************************************/ + + +/* + *------------------------------------------------------------------------ + * Method: + * Purpose: + * Input: + * Output: + * Author: + * Created: + * Remarks: + * Bugs: + *------------------------------------------------------------------------ + */ +int AudioSource::sinkWriteSamples(const float *samples, int len) +{ + assert(len > 0); + + is_flushing = false; + + if (m_sink != 0) + { + len = m_sink->writeSamples(samples, len); + } + + return len; + +} /* AudioSource::sinkWriteSamples */ + + +void AudioSource::sinkFlushSamples(void) +{ + if (m_sink != 0) + { + is_flushing = true; + m_sink->flushSamples(); + } + else + { + handleAllSamplesFlushed(); + } +} /* AudioSource::sinkFlushSamples */ + + +bool AudioSource::setHandler(AudioSource *handler) +{ + clearHandler(); + + if (handler == 0) + { + return true; + } + + if (m_sink != 0) + { + if (!handler->registerSinkInternal(m_sink, false, false)) + { + return false; + } + } + + m_handler = handler; + + return true; + +} /* AudioSource::setHandler */ + + +void AudioSource::clearHandler(void) +{ + if (m_handler == 0) + { + return; + } + + if (m_sink != 0) + { + m_handler->unregisterSink(); + } + + m_handler = 0; +} /* AudioSource::clearHandler */ + + + + +/**************************************************************************** + * + * Private member functions + * + ****************************************************************************/ + + +/* + *---------------------------------------------------------------------------- + * Method: + * Purpose: + * Input: + * Output: + * Author: + * Created: + * Remarks: + * Bugs: + *---------------------------------------------------------------------------- + */ +bool AudioSource::registerSinkInternal(AudioSink *sink, bool managed, bool reg) +{ + assert(sink != 0); + + if (m_sink != 0) + { + return m_sink == sink; + } + + m_sink = sink; + m_auto_unreg_source = reg; + if (reg) + { + if (!m_sink->registerSource(this)) + { + m_sink = 0; + return false; + } + } + + if (m_handler != 0) + { + if (!m_handler->registerSinkInternal(sink, false, false)) + { + if (reg) + { + m_sink->unregisterSource(); + } + m_sink = 0; + return false; + } + } + + m_sink_managed = managed; + + return true; + +} /* AudioSource::registerSinkInternal */ + + +void AudioSource::unregisterSinkInternal(bool is_being_destroyed) +{ + if (m_sink == 0) + { + return; + } + + AudioSink *sink = m_sink; + m_sink = 0; + + if (m_auto_unreg_source) + { + sink->unregisterSource(); + } + + m_sink_managed = false; + + if (m_handler != 0) + { + m_handler->unregisterSink(); + } + + if (!is_being_destroyed) + { + if (is_flushing) + { + handleAllSamplesFlushed(); + } + else + { + resumeOutput(); + } + } + +} /* AudioSource::unregisterSinkInternal */ + + + + + + +/* + * This file has not been truncated + */ + diff --git a/src/async/audio/AsyncAudioSource.h b/src/async/audio/AsyncAudioSource.h new file mode 100644 index 0000000..3ec26fe --- /dev/null +++ b/src/async/audio/AsyncAudioSource.h @@ -0,0 +1,283 @@ +/** +@file AsyncAudioSource.h +@brief This file contains the base class for an audio source +@author Tobias Blomberg / SM0SVX +@date 2005-04-17 + +\verbatim +Async - A library for programming event driven applications +Copyright (C) 2004-2006 Tobias Blomberg / SM0SVX + +This program is free software; you can redistribute it and/or modify +it under the terms of the GNU General Public License as published by +the Free Software Foundation; either version 2 of the License, or +(at your option) any later version. + +This program is distributed in the hope that it will be useful, +but WITHOUT ANY WARRANTY; without even the implied warranty of +MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +GNU General Public License for more details. + +You should have received a copy of the GNU General Public License +along with this program; if not, write to the Free Software +Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA +\endverbatim +*/ + + +#ifndef ASYNC_AUDIO_SOURCE_INCLUDED +#define ASYNC_AUDIO_SOURCE_INCLUDED + + +/**************************************************************************** + * + * System Includes + * + ****************************************************************************/ + +#include + + +/**************************************************************************** + * + * Project Includes + * + ****************************************************************************/ + + + +/**************************************************************************** + * + * Local Includes + * + ****************************************************************************/ + + + +/**************************************************************************** + * + * Forward declarations + * + ****************************************************************************/ + + + +/**************************************************************************** + * + * Namespace + * + ****************************************************************************/ + +namespace Async +{ + + +/**************************************************************************** + * + * Forward declarations of classes inside of the declared namespace + * + ****************************************************************************/ + +class AudioSink; + + +/**************************************************************************** + * + * Defines & typedefs + * + ****************************************************************************/ + + + +/**************************************************************************** + * + * Exported Global Variables + * + ****************************************************************************/ + + + +/**************************************************************************** + * + * Class definitions + * + ****************************************************************************/ + +/** +@brief The base class for an audio source +@author Tobias Blomberg +@date 2005-04-17 + +This is the base class for an audio source. An audio source is a class that +can produce audio. +*/ +class AudioSource +{ + public: + /** + * @brief Default constuctor + */ + AudioSource(void) + : m_sink(0), m_sink_managed(false), m_handler(0), + m_auto_unreg_source(false), is_flushing(false) + { + } + + /** + * @brief Destructor + */ + virtual ~AudioSource(void); + + /** + * @brief Register an audio sink to provide samples to + * @param sink The audio sink to register + * @param managed If \em true, the registered sink will be destroyed + * when this object is destroyed. + * @return Returns \em true on success or else \em false + */ + bool registerSink(AudioSink *sink, bool managed=false); + + /** + * @brief Unregister the previously registered audio sink + */ + void unregisterSink(void); + + /** + * @brief Check if an audio sink has been registered + * @return Returns \em true if there is an audio sink registerd + */ + bool isRegistered(void) const { return m_sink != 0; } + + /** + * @brief Get the registered audio sink + * @return Returns the registered audio sink if any registered or else + * returns 0. + */ + AudioSink *sink(void) const { return m_sink; } + + /** + * @brief Check if the sink is managed or not + * @returns Returns \em true if the sink is managed or \em false if not + * + * This function is used to find out if the connected sink is managed + * or not. A managed sink will automatically be deleted when this + * source object is deleted. + */ + bool sinkManaged(void) const { return m_sink_managed; } + + /** + * @brief The registered sink has flushed all samples + * + * This function will be called when all samples have been flushed in + * the registered sink. + * This function is normally only called from a connected sink object. + */ + void handleAllSamplesFlushed(void) + { + is_flushing = false; + allSamplesFlushed(); + } + + /** + * @brief Resume audio output to the sink + * + * This function must be reimplemented by the inheriting class. It + * will be called when the registered audio sink is ready to accept + * more samples. + * This function is normally only called from a connected sink object. + */ + virtual void resumeOutput(void) + { + assert(m_handler != 0); + m_handler->resumeOutput(); + } + + + protected: + /** + * @brief The registered sink has flushed all samples + * + * This function should be implemented by the inheriting class. It + * will be called when all samples have been flushed in the + * registered sink. If it is not reimplemented, a handler must be set + * that handle the function call. + * This function is normally only called from a connected sink object. + */ + virtual void allSamplesFlushed(void) + { + assert(m_handler != 0); + m_handler->handleAllSamplesFlushed(); + } + + /* + * @brief Write samples to the connected sink + * @param samples The buffer containing the samples to write + * @param len The number of samples in the buffer + * @return Return the number of samples that was taken care of + * + * This function is used by the inheriting class to write samples to + * the connected sink, if any. If there is no connected sink, the samples + * will be thrown away. This function will return the number of samples + * that was taken care of. Samples that was not taken care of should + * normally be written again to the sink. + */ + int sinkWriteSamples(const float *samples, int len); + + /* + * @brief Tell the sink to flush any buffered samples + * + * This function is used by the inheriting class to tell the connected + * sink to flush its buffered samples. When the sink have flushed all its + * samples it will call the allSamplesFlushed function in this class. + * If there is no registered sink the allSamplesFlushed function will be + * called right away. + */ + void sinkFlushSamples(void); + + /** + * @brief Setup another source to handle the outgoing audio + * @param handler The source to handle the audio + * @return Returns \em true on success or else \em false + * + * This function will setup another source to handle outgoing audio. + * This can be used when an internal object should handle the audio + * for this object. + */ + bool setHandler(AudioSource *handler); + + /* + * @brief Return the handler + * @return Returns the handler previously set with setHandler or 0 + * if none have been set + */ + AudioSource *handler(void) const { return m_handler; } + + /** + * @brief Clear a handler that was previously setup with setHandler. + */ + void clearHandler(void); + + + private: + AudioSink *m_sink; + bool m_sink_managed; + AudioSource *m_handler; + bool m_auto_unreg_source; + bool is_flushing; + + bool registerSinkInternal(AudioSink *sink, bool managed, bool reg); + void unregisterSinkInternal(bool is_being_destroyed); + +}; /* class AudioSource */ + + +} /* namespace */ + +#endif /* ASYNC_AUDIO_SOURCE_INCLUDED */ + + + +/* + * This file has not been truncated + */ + diff --git a/src/async/audio/AsyncAudioSplitter.cpp b/src/async/audio/AsyncAudioSplitter.cpp new file mode 100644 index 0000000..77a4b37 --- /dev/null +++ b/src/async/audio/AsyncAudioSplitter.cpp @@ -0,0 +1,545 @@ +/** +@file AsyncAudioSplitter.cpp +@brief A class that splits an audio stream into multiple streams +@author Tobias Blomberg / SM0SVX +@date 2005-05-05 + +\verbatim +Async - A library for programming event driven applications +Copyright (C) 2004-2024 Tobias Blomberg / SM0SVX + +This program is free software; you can redistribute it and/or modify +it under the terms of the GNU General Public License as published by +the Free Software Foundation; either version 2 of the License, or +(at your option) any later version. + +This program is distributed in the hope that it will be useful, +but WITHOUT ANY WARRANTY; without even the implied warranty of +MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +GNU General Public License for more details. + +You should have received a copy of the GNU General Public License +along with this program; if not, write to the Free Software +Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA +\endverbatim +*/ + + + +/**************************************************************************** + * + * System Includes + * + ****************************************************************************/ + +#include +#include +#include + + +/**************************************************************************** + * + * Project Includes + * + ****************************************************************************/ + +#include + + +/**************************************************************************** + * + * Local Includes + * + ****************************************************************************/ + +#include "AsyncAudioSource.h" +#include "AsyncAudioSplitter.h" + + +/**************************************************************************** + * + * Namespaces to use + * + ****************************************************************************/ + +using namespace std; +using namespace Async; + + + +/**************************************************************************** + * + * Defines & typedefs + * + ****************************************************************************/ + + + +/**************************************************************************** + * + * Local class definitions + * + ****************************************************************************/ + +class Async::AudioSplitter::Branch : public AudioSource +{ + public: + int current_buf_pos; + bool is_flushed; + + Branch(AudioSplitter *splitter) + : current_buf_pos(0), is_flushed(true), is_enabled(true), + is_stopped(false), is_flushing(false), splitter(splitter) + { + } + + virtual ~Branch(void) + { + if (is_stopped) + { + splitter->branchResumeOutput(); + } + } + + void setEnabled(bool enabled) + { + if (enabled == is_enabled) + { + return; + } + is_enabled = enabled; + + if (!enabled) + { + if (is_stopped) + { + is_stopped = false; + splitter->branchResumeOutput(); + } + if (is_flushing) + { + is_flushing = false; + splitter->branchAllSamplesFlushed(); + } + else if (!is_flushed) + { + AudioSource::sinkFlushSamples(); + } + } + } + + int sinkWriteSamples(const float *samples, int len) + { + is_flushed = false; + is_flushing = false; + + if (is_enabled) + { + if (is_stopped) + { + return 0; + } + + len = AudioSource::sinkWriteSamples(samples, len); + is_stopped = (len == 0); + } + + current_buf_pos += len; + + return len; + + } /* sinkWriteSamples */ + + void sinkFlushSamples(void) + { + if (is_enabled) + { + is_flushing = true; + AudioSource::sinkFlushSamples(); + } + else + { + is_flushed = true; + splitter->branchAllSamplesFlushed(); + } + } /* sinkFlushSamples */ + + + private: + bool is_enabled; + bool is_stopped; + bool is_flushing; + AudioSplitter *splitter; + + virtual void resumeOutput(void) + { + is_stopped = false; + if (is_enabled) + { + splitter->branchResumeOutput(); + } + } /* resumeOutput */ + + virtual void allSamplesFlushed(void) + { + bool was_flushing = is_flushing; + is_flushing = false; + is_flushed = true; + if (is_enabled && was_flushing) + { + splitter->branchAllSamplesFlushed(); + } + } /* allSamplesFlushed */ + +}; /* class Branch */ + + + +/**************************************************************************** + * + * Prototypes + * + ****************************************************************************/ + + + +/**************************************************************************** + * + * Exported Global Variables + * + ****************************************************************************/ + + + + +/**************************************************************************** + * + * Local Global Variables + * + ****************************************************************************/ + + + +/**************************************************************************** + * + * Public member functions + * + ****************************************************************************/ + +AudioSplitter::AudioSplitter(void) + : buf(0), buf_size(0), buf_len(0), do_flush(false), input_stopped(false), + flushed_branches(0), main_branch(0) +{ + main_branch = new Branch(this); + branches.push_back(main_branch); + AudioSource::setHandler(main_branch); +} /* AudioSplitter::AudioSplitter */ + + +AudioSplitter::~AudioSplitter(void) +{ + delete [] buf; + removeAllSinks(); + AudioSource::clearHandler(); + delete main_branch; + main_branch = 0; + branches.clear(); +} /* AudioSplitter::~AudioSplitter */ + + +void AudioSplitter::addSink(AudioSink *sink, bool managed) +{ + Branch *branch = new Branch(this); + branch->registerSink(sink, managed); + branches.push_back(branch); + if (do_flush) + { + branch->sinkFlushSamples(); + } +} /* AudioSplitter::addSink */ + + +void AudioSplitter::removeSink(AudioSink *sink) +{ + if (sink == main_branch->sink()) + { + return; + } + + list::iterator it; + for (it = branches.begin(); it != branches.end(); ++it) + { + if ((*it)->sink() == sink) + { + if ((*it)->sinkManaged()) + { + delete (*it)->sink(); + } + else + { + (*it)->unregisterSink(); + } + Async::Application::app().runTask( + mem_fun(*this, &AudioSplitter::cleanupBranches)); + break; + } + } +} /* AudioSplitter::removeSink */ + + +void AudioSplitter::removeAllSinks(void) +{ + list::iterator it; + for (it = branches.begin(); it != branches.end(); ++it) + { + if (*it != main_branch) + { + delete *it; + } + } + branches.clear(); + branches.push_back(main_branch); +} /* AudioSplitter::removeAllSinks */ + + +void AudioSplitter::enableSink(AudioSink *sink, bool enable) +{ + if (sink == main_branch->sink()) + { + return; + } + + list::iterator it; + for (it = branches.begin(); it != branches.end(); ++it) + { + if ((*it)->sink() == sink) + { + (*it)->setEnabled(enable); + break; + } + } +} /* AudioSplitter::enableSink */ + + +int AudioSplitter::writeSamples(const float *samples, int len) +{ + do_flush = false; + + if (len <= 0) + { + return 0; + } + + if (buf_len > 0) + { + input_stopped = true; + return 0; + } + + bool samples_written = false; + + list::iterator it; + for (it = branches.begin(); it != branches.end(); ++it) + { + (*it)->current_buf_pos = 0; + int written = (*it)->sinkWriteSamples(samples, len); + if (written != len) + { + if (buf_len == 0) // Only copy the buffer one time + { + if (buf_size < len) + { + delete [] buf; + buf_size = len; + buf = new float[buf_size]; + } + memcpy(buf, samples, len * sizeof(*samples)); + buf_len = len; + } + } + samples_written |= (written > 0); + } + + writeFromBuffer(); + + return len; + +} /* AudioSplitter::writeSamples */ + + +void AudioSplitter::flushSamples(void) +{ + if (do_flush) + { + return; + } + + if (branches.empty()) + { + sourceAllSamplesFlushed(); + return; + } + + do_flush = true; + flushed_branches = 0; + + if (buf_len > 0) + { + return; + } + + flushAllBranches(); + +} /* AudioSplitter::flushSamples */ + + + + +/**************************************************************************** + * + * Protected member functions + * + ****************************************************************************/ + + +/* + *------------------------------------------------------------------------ + * Method: + * Purpose: + * Input: + * Output: + * Author: + * Created: + * Remarks: + * Bugs: + *------------------------------------------------------------------------ + */ + + + + + + +/**************************************************************************** + * + * Private member functions + * + ****************************************************************************/ + + +/* + *---------------------------------------------------------------------------- + * Method: + * Purpose: + * Input: + * Output: + * Author: + * Created: + * Remarks: + * Bugs: + *---------------------------------------------------------------------------- + */ +void AudioSplitter::writeFromBuffer(void) +{ + bool samples_written = true; + bool all_written = (buf_len == 0); + + //cout << "samples_written=" << samples_written << " all_written=" + // << all_written << endl; + + while (samples_written && !all_written) + { + samples_written = false; + all_written = true; + list::iterator it; + for (it = branches.begin(); it != branches.end(); ++it) + { + //cout << "(*it)->current_buf_pos=" << (*it)->current_buf_pos + // << " buf_len=" << buf_len << endl; + if ((*it)->current_buf_pos < buf_len) + { + int written = (*it)->sinkWriteSamples(buf+(*it)->current_buf_pos, + buf_len-(*it)->current_buf_pos); + //cout << "written=" << written << endl; + samples_written |= (written > 0); + all_written &= ((*it)->current_buf_pos == buf_len); + } + } + + if (all_written) + { + buf_len = 0; + if (do_flush) + { + flushAllBranches(); + } + } + } +} /* AudioSplitter::writeFromBuffer */ + + +void AudioSplitter::flushAllBranches(void) +{ + list::iterator it; + for (it = branches.begin(); it != branches.end(); ++it) + { + (*it)->sinkFlushSamples(); + } +} /* AudioSplitter::flushAllBranches */ + + +void AudioSplitter::branchResumeOutput(void) +{ + writeFromBuffer(); + if (input_stopped && (buf_len == 0)) + { + input_stopped = false; + sourceResumeOutput(); + } +} /* AudioSplitter::branchResumeOutput */ + + +void AudioSplitter::branchAllSamplesFlushed(void) +{ + //cout << "AudioSplitter::branchAllSamplesFlushed: flushed_branches=" + // << flushed_branches << endl; + + if (static_cast(++flushed_branches) == branches.size()) + { + do_flush = false; + sourceAllSamplesFlushed(); + } +} /* AudioSplitter::branchAllSamplesFlushed */ + + +/* + * @brief: Delete removed branches + * + * This functions is called by a zero second timer to cleanup removed branches. + * Branches cannot be removed directly from the list since that could + * corrupt iterators when looping through the list. + */ +void AudioSplitter::cleanupBranches(void) +{ + list::iterator it = branches.begin(); + while (it != branches.end()) + { + if ((*it != main_branch) && !(*it)->isRegistered()) + { + list::iterator delete_it = it; + ++it; + delete *delete_it; + branches.erase(delete_it); + } + else + { + ++it; + } + } +} /* AudioSplitter::cleanupBranches */ + + + +/* + * This file has not been truncated + */ diff --git a/src/async/audio/AsyncAudioSplitter.h b/src/async/audio/AsyncAudioSplitter.h new file mode 100644 index 0000000..4fa6ce3 --- /dev/null +++ b/src/async/audio/AsyncAudioSplitter.h @@ -0,0 +1,214 @@ +/** +@file AsyncAudioSplitter.h +@brief A class that splits an audio stream into multiple streams +@author Tobias Blomberg / SM0SVX +@date 2005-05-05 + +\verbatim +Async - A library for programming event driven applications +Copyright (C) 2004-2024 Tobias Blomberg / SM0SVX + +This program is free software; you can redistribute it and/or modify +it under the terms of the GNU General Public License as published by +the Free Software Foundation; either version 2 of the License, or +(at your option) any later version. + +This program is distributed in the hope that it will be useful, +but WITHOUT ANY WARRANTY; without even the implied warranty of +MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +GNU General Public License for more details. + +You should have received a copy of the GNU General Public License +along with this program; if not, write to the Free Software +Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA +\endverbatim +*/ + + +#ifndef ASYNC_AUDIO_SPLITTER_INCLUDED +#define ASYNC_AUDIO_SPLITTER_INCLUDED + + +/**************************************************************************** + * + * System Includes + * + ****************************************************************************/ + +#include +#include + + +/**************************************************************************** + * + * Project Includes + * + ****************************************************************************/ + +#include + + +/**************************************************************************** + * + * Local Includes + * + ****************************************************************************/ + +#include +#include + + +/**************************************************************************** + * + * Forward declarations + * + ****************************************************************************/ + + + +/**************************************************************************** + * + * Namespace + * + ****************************************************************************/ + +namespace Async +{ + + +/**************************************************************************** + * + * Forward declarations of classes inside of the declared namespace + * + ****************************************************************************/ + + + +/**************************************************************************** + * + * Defines & typedefs + * + ****************************************************************************/ + + + +/**************************************************************************** + * + * Exported Global Variables + * + ****************************************************************************/ + + + +/**************************************************************************** + * + * Class definitions + * + ****************************************************************************/ + +/** +@brief A class that splits an audio stream into multiple streams +@author Tobias Blomberg +@date 2005-05-05 + +This class is part of the audio pipe framework. It is used to split one +incoming audio source into multiple outgoing sources. +*/ +class AudioSplitter : public Async::AudioSink, public Async::AudioSource, + public sigc::trackable +{ + public: + /** + * @brief Default constuctor + */ + AudioSplitter(void); + + /** + * @brief Destructor + */ + ~AudioSplitter(void); + + /** + * @brief Add an audio sink to the splitter + * @param sink The sink object to add + * @param managed If managed is \em true the attached sink will be + * deleted when the splitter is deleted + */ + void addSink(AudioSink *sink, bool managed=false); + + /** + * @brief Remove an audio sink from the splitter + * @param sink The sink object to remove + */ + void removeSink(AudioSink *sink); + + /** + * @brief Remove all audio sinks from this splitter + */ + void removeAllSinks(void); + + /** + * @brief Enable or disable audio output to the given audio sink + * @param sink The audio sink to enable/disable + * @param enable Set to \em true to enable the sink or \em false to + * disable it + */ + void enableSink(AudioSink *sink, bool enable); + + /** + * @brief Write samples into this audio sink + * @param samples The buffer containing the samples + * @param len The number of samples in the buffer + * @return Returns the number of samples that has been taken care of + * + * This function is used to write audio into this audio sink. If it + * returns 0, no more samples should be written until the resumeOutput + * function in the source have been called. + * This function is normally only called from a connected source object. + */ + int writeSamples(const float *samples, int len) override; + + /** + * @brief Tell the sink to flush the previously written samples + * + * This function is used to tell the sink to flush previously written + * samples. When done flushing, the sink should call the + * sourceAllSamplesFlushed function. + * This function is normally only called from a connected source object. + */ + void flushSamples(void) override; + + protected: + + private: + class Branch; + + std::list branches; + float *buf; + int buf_size; + int buf_len; + bool do_flush; + bool input_stopped; + int flushed_branches; + Branch *main_branch; + + void writeFromBuffer(void); + void flushAllBranches(void); + + friend class Branch; + void branchResumeOutput(void); + void branchAllSamplesFlushed(void); + void cleanupBranches(void); + +}; /* class AudioSplitter */ + + +} /* namespace */ + +#endif /* ASYNC_AUDIO_SPLITTER_INCLUDED */ + + + +/* + * This file has not been truncated + */ diff --git a/src/async/audio/AsyncAudioStreamStateDetector.h b/src/async/audio/AsyncAudioStreamStateDetector.h new file mode 100644 index 0000000..00f47ee --- /dev/null +++ b/src/async/audio/AsyncAudioStreamStateDetector.h @@ -0,0 +1,291 @@ +/** +@file AsyncAudioStreamStateDetector.h +@brief This file contains a class that just passes the audio through and + fires an event when the stream state changes. +@author Tobias Blomberg / SM0SVX +@date 2008-05-30 + +\verbatim +Async - A library for programming event driven applications +Copyright (C) 2004-2025 Tobias Blomberg / SM0SVX + +This program is free software; you can redistribute it and/or modify +it under the terms of the GNU General Public License as published by +the Free Software Foundation; either version 2 of the License, or +(at your option) any later version. + +This program is distributed in the hope that it will be useful, +but WITHOUT ANY WARRANTY; without even the implied warranty of +MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +GNU General Public License for more details. + +You should have received a copy of the GNU General Public License +along with this program; if not, write to the Free Software +Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA +\endverbatim +*/ + + +#ifndef ASYNC_AUDIO_STREAM_STATE_DETECTOR_INCLUDED +#define ASYNC_AUDIO_STREAM_STATE_DETECTOR_INCLUDED + + +/**************************************************************************** + * + * System Includes + * + ****************************************************************************/ + +#include + + +/**************************************************************************** + * + * Project Includes + * + ****************************************************************************/ + +#include + + +/**************************************************************************** + * + * Local Includes + * + ****************************************************************************/ + + + +/**************************************************************************** + * + * Forward declarations + * + ****************************************************************************/ + + + +/**************************************************************************** + * + * Namespace + * + ****************************************************************************/ + +namespace Async +{ + + +/**************************************************************************** + * + * Forward declarations of classes inside of the declared namespace + * + ****************************************************************************/ + + + +/**************************************************************************** + * + * Defines & typedefs + * + ****************************************************************************/ + + + +/**************************************************************************** + * + * Exported Global Variables + * + ****************************************************************************/ + + + +/**************************************************************************** + * + * Class definitions + * + ****************************************************************************/ + +/** + * @brief Emit a signal when the state changes for the audio stream + * + * A class that just passes the audio through and emit signals when the + * stream state changes. The audio stream may be in three different states. In + * each of these states two booleans are set up. + * + * State Active Idle + * IDLE false true + * ACTIVE true false + * FLUSHING false false + * + * The stream state start at IDLE. + * When the audio stream source write a sample the state go to ACTIVE. + * When the audio stream source flush the stream the state go to FLUSHING. + * When the audio sink indicate that all samples has been flushed the state go + * to IDLE again. + */ +class AudioStreamStateDetector + : public AudioPassthrough, virtual public sigc::trackable +{ + public: + /** + * @brief An enum representing the stream state + */ + enum class State + { + IDLE, ///< There are no samples left in the stream + ACTIVE, ///< Samples have been written to the stream + FLUSHING ///< The stream source have requested stream flush + }; + + /** + * @brief Default constuctor + */ + AudioStreamStateDetector(void) {} + + /** + * @brief Destructor + */ + virtual ~AudioStreamStateDetector(void) override {} + + /** + * @brief Write samples into this audio sink + * @param samples The buffer containing the samples + * @param count The number of samples in the buffer + * @return Returns the number of samples that has been taken care of + * + * This function is used to write audio into this audio sink. If it + * returns 0, no more samples should be written until the resumeOutput + * function in the source have been called. + * This function is normally only called from a connected source object. + */ + virtual int writeSamples(const float *samples, int count) override + { + setState(State::ACTIVE); + return AudioPassthrough::writeSamples(samples, count); + } + + /** + * @brief Tell the sink to flush the previously written samples + * + * This function is used to tell the sink to flush previously written + * samples. When done flushing, the sink should call the + * sourceAllSamplesFlushed function. + * This function is normally only called from a connected source object. + */ + virtual void flushSamples(void) override + { + setState(State::FLUSHING); + AudioPassthrough::flushSamples(); + } + + /** + * @brief The registered sink has flushed all samples + * + * This function will be called when all samples have been flushed in the + * registered sink. + * This function is normally only called from a connected sink object. + */ + virtual void allSamplesFlushed(void) override + { + setState(State::IDLE); + AudioPassthrough::allSamplesFlushed(); + } + + /** + * @brief Check if the steam is idle or not + * @returns Returns \em true if the stream is idle or \em false if it's not + */ + bool isIdle(void) const { return (m_state == State::IDLE); } + + /** + * @brief Check if the steam is active or not + * @returns Returns \em true if the stream is active or \em false if it's + * not + */ + bool isActive(void) const { return (m_state == State::ACTIVE); } + + /** + * @brief Check if the steam is flushing or not + * @returns Returns \em true if the stream is flushing or \em false if + * it's not + */ + bool isFlushing(void) const { return (m_state == State::FLUSHING); } + + /** + * @brief Get the stream state + * @returns Returns the state of the audio stream + */ + State state(void) const { return m_state; } + + /** + * @brief A signal that is emitted when the stream state changes + * @param is_active Is \em true if the stream is active + * @param is_idle Is \em true if the stream is idle + */ + sigc::signal sigStreamStateChanged; + + /** + * @brief A signal that is emitted when stream activity state changes + * @param is_active Is \em true if the stream is active + */ + sigc::signal sigStreamIsActive; + + /** + * @brief A signal that is emitted when stream idle state changes + * @param is_idle Is \em true if the stream is idle + */ + sigc::signal sigStreamIsIdle; + + private: + AudioStreamStateDetector(const AudioStreamStateDetector&); + AudioStreamStateDetector& operator=(const AudioStreamStateDetector&); + + struct StateMapValue + { + bool is_active; + bool is_idle; + }; + using StateMap = std::map; + + const StateMap STATE_MAP { + {State::IDLE, {false, true} }, + {State::ACTIVE, {true, false} }, + {State::FLUSHING, {false, false}} + }; + State m_state {State::IDLE}; + + void setState(State new_state) + { + if (new_state == m_state) + { + return; + } + + auto& old_mapping = STATE_MAP.at(m_state); + auto& new_mapping = STATE_MAP.at(new_state); + m_state = new_state; + + if (new_mapping.is_active != old_mapping.is_active) + { + sigStreamIsActive(new_mapping.is_active); + } + + if (new_mapping.is_idle != old_mapping.is_idle) + { + sigStreamIsIdle(new_mapping.is_idle); + } + + sigStreamStateChanged(new_mapping.is_active, new_mapping.is_idle); + } +}; /* AudioStreamStateDetector */ + + +} /* namespace */ + +#endif /* ASYNC_AUDIO_STREAM_STATE_DETECTOR_INCLUDED */ + + + +/* + * This file has not been truncated + */ + diff --git a/src/async/audio/AsyncAudioValve.h b/src/async/audio/AsyncAudioValve.h new file mode 100644 index 0000000..8d3682b --- /dev/null +++ b/src/async/audio/AsyncAudioValve.h @@ -0,0 +1,341 @@ +/** +@file AsyncAudioValve.h +@brief This file contains a class that implements a "valve" for audio. +@author Tobias Blomberg / SM0SVX +@date 2006-07-08 + +\verbatim +Async - A library for programming event driven applications +Copyright (C) 2004-2008 Tobias Blomberg / SM0SVX + +This program is free software; you can redistribute it and/or modify +it under the terms of the GNU General Public License as published by +the Free Software Foundation; either version 2 of the License, or +(at your option) any later version. + +This program is distributed in the hope that it will be useful, +but WITHOUT ANY WARRANTY; without even the implied warranty of +MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +GNU General Public License for more details. + +You should have received a copy of the GNU General Public License +along with this program; if not, write to the Free Software +Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA +\endverbatim +*/ + + +#ifndef ASYNC_AUDIO_VALVE_INCLUDED +#define ASYNC_AUDIO_VALVE_INCLUDED + + +/**************************************************************************** + * + * System Includes + * + ****************************************************************************/ + + + +/**************************************************************************** + * + * Project Includes + * + ****************************************************************************/ + +#include +#include + + + +/**************************************************************************** + * + * Local Includes + * + ****************************************************************************/ + + + +/**************************************************************************** + * + * Forward declarations + * + ****************************************************************************/ + + + +/**************************************************************************** + * + * Namespace + * + ****************************************************************************/ + +namespace Async +{ + + +/**************************************************************************** + * + * Forward declarations of classes inside of the declared namespace + * + ****************************************************************************/ + + + +/**************************************************************************** + * + * Defines & typedefs + * + ****************************************************************************/ + + + +/**************************************************************************** + * + * Exported Global Variables + * + ****************************************************************************/ + + + +/**************************************************************************** + * + * Class definitions + * + ****************************************************************************/ + +/** +@brief Implements a "valve" for audio +@author Tobias Blomberg / SM0SVX +@date 2006-07-08 + +This class implements a "valve" for audio. That is, the audio stream can be +turned on or off. It's named "valve" since the whole Async audio concept is +called audio pipe. +*/ +class AudioValve : public Async::AudioSink, public Async::AudioSource +{ + public: + /** + * @brief Default constuctor + */ + explicit AudioValve(void) + : block_when_closed(false), is_open(true), + is_idle(true), is_flushing(false), input_stopped(false) + { + } + + /** + * @brief Destructor + */ + ~AudioValve(void) {} + + /** + * @brief Open or close the valve + * @param do_open If \em true the valve is open or else it's closed + * + * This function is used to open or close the audio valve. When the valve + * is closed, the connected sink is flushed. What is done with the incoming + * audio when the valve is closed depends on the block_when_closed + * parameter to the constructor. + */ + void setOpen(bool do_open) + { + if (is_open == do_open) + { + return; + } + + is_open = do_open; + + if (do_open) + { + if (input_stopped) + { + input_stopped = false; + sourceResumeOutput(); + } + } + else + { + if (!is_idle && !is_flushing) + { + sinkFlushSamples(); + } + if (!block_when_closed && input_stopped) + { + input_stopped = false; + sourceResumeOutput(); + } + if (is_flushing) + { + is_idle = true; + is_flushing = false; + sourceAllSamplesFlushed(); + } + } + } + + /** + * @brief Setup audio stream blocking when valve is closed + * @param block_when_closed When \em true, block the incoming audio + * stream when the valve is closed. When \em false, incoming + * audio is thrown away when the valve is closed. + * + * Use this method to set if the valve should block or drop the incoming + * audio stream when the valve is closed. + */ + void setBlockWhenClosed(bool block_when_closed) + { + if (block_when_closed == this->block_when_closed) + { + return; + } + + this->block_when_closed = block_when_closed; + + if (!block_when_closed && input_stopped) + { + input_stopped = false; + sourceResumeOutput(); + } + } + + /** + * @brief Check if the valve is open + * @returns Return \em true if the valve is open or else \em false + */ + bool isOpen(void) const + { + return is_open; + } + + /** + * @brief Check if the valve is idle + * @returns Return \em true if the valve is idle or else \em false + */ + bool isIdle(void) const + { + return is_idle; + } + + /** + * @brief Write samples into the valve + * @param samples The buffer containing the samples + * @param count The number of samples in the buffer + * @return Returns the number of samples that has been taken care of + * + * This function is used to write audio into the valve. If it + * returns 0, no more samples should be written until the resumeOutput + * function in the source have been called. + * This function is normally only called from a connected source object. + */ + int writeSamples(const float *samples, int count) + { + int ret = 0; + is_idle = false; + is_flushing = false; + if (is_open) + { + ret = sinkWriteSamples(samples, count); + } + else + { + ret = (block_when_closed ? 0 : count); + } + + if (ret == 0) + { + input_stopped = true; + } + + return ret; + } + + /** + * @brief Tell the valve to flush the previously written samples + * + * This function is used to tell the valve to flush previously written + * samples. When done flushing, the valve will call the + * allSamplesFlushed function in the connected source object. + * This function is normally only called from a connected source object. + */ + void flushSamples(void) + { + if (is_open) + { + is_flushing = true; + sinkFlushSamples(); + } + else + { + is_flushing = false; + is_idle = true; + sourceAllSamplesFlushed(); + } + } + + /** + * @brief Resume audio output to the sink + * + * This function must be reimplemented by the inheriting class. It + * will be called when the registered audio sink is ready to accept + * more samples. + * This function is normally only called from a connected sink object. + */ + void resumeOutput(void) + { + if (is_open) + { + if (input_stopped) + { + input_stopped = false; + sourceResumeOutput(); + } + } + } + + /** + * @brief The registered sink has flushed all samples + * + * This function must be implemented by the inheriting class. This + * function will be called when all samples have been flushed in + * the registered sink. + */ + virtual void allSamplesFlushed(void) + { + bool was_flushing = is_flushing; + is_flushing = false; + is_idle = true; + if (is_open && was_flushing) + { + sourceAllSamplesFlushed(); + } + } + + + protected: + + private: + AudioValve(const AudioValve&); + AudioValve& operator=(const AudioValve&); + + bool block_when_closed; + bool is_open; + bool is_idle; + bool is_flushing; + bool input_stopped; + +}; /* class AudioValve */ + + +} /* namespace */ + +#endif /* ASYNC_AUDIO_VALVE_INCLUDED */ + + + +/* + * This file has not been truncated + */ + diff --git a/src/async/audio/AsyncSigCAudioSink.h b/src/async/audio/AsyncSigCAudioSink.h new file mode 100644 index 0000000..01bec5a --- /dev/null +++ b/src/async/audio/AsyncSigCAudioSink.h @@ -0,0 +1,216 @@ +/** +@file AsyncSigCAudioSink.h +@brief Contains an adapter class to connect to an AudioSource using SigC +@author Tobias Blomberg / SM0SVX +@date 2005-04-17 + +\verbatim +Async - A library for programming event driven applications +Copyright (C) 2003-2013 Tobias Blomberg / SM0SVX + +This program is free software; you can redistribute it and/or modify +it under the terms of the GNU General Public License as published by +the Free Software Foundation; either version 2 of the License, or +(at your option) any later version. + +This program is distributed in the hope that it will be useful, +but WITHOUT ANY WARRANTY; without even the implied warranty of +MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +GNU General Public License for more details. + +You should have received a copy of the GNU General Public License +along with this program; if not, write to the Free Software +Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA +\endverbatim +*/ + + +#ifndef ASYNC_SIGC_AUDIO_SINK_INCLUDED +#define ASYNC_SIGC_AUDIO_SINK_INCLUDED + + +/**************************************************************************** + * + * System Includes + * + ****************************************************************************/ + +#include + + +/**************************************************************************** + * + * Project Includes + * + ****************************************************************************/ + +#include + + +/**************************************************************************** + * + * Local Includes + * + ****************************************************************************/ + + + +/**************************************************************************** + * + * Forward declarations + * + ****************************************************************************/ + + + +/**************************************************************************** + * + * Namespace + * + ****************************************************************************/ + +namespace Async +{ + + +/**************************************************************************** + * + * Forward declarations of classes inside of the declared namespace + * + ****************************************************************************/ + + + +/**************************************************************************** + * + * Defines & typedefs + * + ****************************************************************************/ + + + +/**************************************************************************** + * + * Exported Global Variables + * + ****************************************************************************/ + + + +/**************************************************************************** + * + * Class definitions + * + ****************************************************************************/ + +/** +@brief An adapter class to connect to an AudioSource class using SigC +@author Tobias Blomberg +@date 2005-04-17 + +This is an adapter class that can be used to interact with an AudioSource +class using SigC signals and slots. +*/ +class SigCAudioSink : public AudioSink, public sigc::trackable +{ + public: + /** + * @brief Default constuctor + */ + SigCAudioSink(void) {} + + /** + * @brief Destructor + */ + ~SigCAudioSink(void) {} + + /** + * @brief Write samples into this audio sink + * @param samples The buffer containing the samples + * @param count The number of samples in the buffer + * @return Returns the number of samples that has been taken care of + * + * This function is used to write audio into this audio sink. If it + * returns 0, no more samples should be written until the resumeOutput + * function in the source have been called. + * This function is normally only called from a connected source object. + * When called by the source, the sigWriteSamples function will be + * emitted. + */ + virtual int writeSamples(const float *samples, int count) + { + return sigWriteSamples(const_cast(samples), count); + } + + /** + * @brief Tell the sink to flush the previously written samples + * + * This function is used to tell the sink to flush previously written + * samples. When done flushing, the sink should call the + * sourceAllSamplesFlushed function. + * This function is normally only called from a connected source object. + * When called by the conected source, the sigFlushSamples signal will + * be emitted. + */ + virtual void flushSamples(void) + { + sigFlushSamples(); + } + + /** + * @brief Tell the source that we are ready to accept more samples + * + * This function should be called by the object user to indicate to the + * connected source that we are now ready to accept more samples. + */ + void resumeOutput(void) + { + sourceResumeOutput(); + } + + /** + * @brief Tell the source that all samples have been flushed + * + * This function is called by the object user to indicate that + * all samples have been flushed. It may only be called after a + * flushSamples call has been received and no more samples has been + * written to the sink. + */ + void allSamplesFlushed(void) + { + sourceAllSamplesFlushed(); + } + + /** + * @brief Signal that is emitted when the source write samples + * @param samples The buffer containing the samples + * @param count The number of samples in the buffer + * @return Returns the number of samples that has been taken care of + * + * This signal is emitted when the connected source object write samples. + * If 0 is returned, the source should write no more samples until the + * resumeOutput function in the source have been called. + */ + sigc::signal sigWriteSamples; + + /** + * @brief Signal emitted when the source are finished writing samples + * + * This signal is emitted when the connected source object have finished + * writing samples. When done flushing, the allSamplesFlushed function + * should be called. + */ + sigc::signal sigFlushSamples; + +}; /* class SigCAudioSink */ + + +} /* namespace */ + +#endif /* ASYNC_SIGC_AUDIO_SINK_INCLUDED */ + + + +/* + * This file has not been truncated + */ diff --git a/src/async/audio/AsyncSigCAudioSource.h b/src/async/audio/AsyncSigCAudioSource.h new file mode 100644 index 0000000..0ce4913 --- /dev/null +++ b/src/async/audio/AsyncSigCAudioSource.h @@ -0,0 +1,204 @@ +/** +@file AsyncSigCAudioSource.h +@brief Contains an adapter class to connect to an AudioSink using SigC +@author Tobias Blomberg / SM0SVX +@date 2005-04-17 + +\verbatim +Async - A library for programming event driven applications +Copyright (C) 2003-2013 Tobias Blomberg / SM0SVX + +This program is free software; you can redistribute it and/or modify +it under the terms of the GNU General Public License as published by +the Free Software Foundation; either version 2 of the License, or +(at your option) any later version. + +This program is distributed in the hope that it will be useful, +but WITHOUT ANY WARRANTY; without even the implied warranty of +MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +GNU General Public License for more details. + +You should have received a copy of the GNU General Public License +along with this program; if not, write to the Free Software +Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA +\endverbatim +*/ + + +#ifndef ASYNC_SIGC_AUDIO_SOURCE_INCLUDED +#define ASYNC_SIGC_AUDIO_SOURCE_INCLUDED + + +/**************************************************************************** + * + * System Includes + * + ****************************************************************************/ + +#include + + +/**************************************************************************** + * + * Project Includes + * + ****************************************************************************/ + +#include + + +/**************************************************************************** + * + * Local Includes + * + ****************************************************************************/ + + + +/**************************************************************************** + * + * Forward declarations + * + ****************************************************************************/ + + + +/**************************************************************************** + * + * Namespace + * + ****************************************************************************/ + +namespace Async +{ + + +/**************************************************************************** + * + * Forward declarations of classes inside of the declared namespace + * + ****************************************************************************/ + + + +/**************************************************************************** + * + * Defines & typedefs + * + ****************************************************************************/ + + + +/**************************************************************************** + * + * Exported Global Variables + * + ****************************************************************************/ + + + +/**************************************************************************** + * + * Class definitions + * + ****************************************************************************/ + +/** +@brief An adapter class to connect to an AudioSink class using SigC +@author Tobias Blomberg +@date 2005-04-17 + +This is an adapter class that can be used to interact with an AudioSink +class using SigC signals and slots. +*/ +class SigCAudioSource : public AudioSource, public sigc::trackable +{ + public: + /** + * @brief Default constuctor + */ + SigCAudioSource(void) {} + + /** + * @brief Destructor + */ + ~SigCAudioSource(void) {} + + /** + * @brief Resume audio output to the sink + * + * This function will be called when the registered audio sink is ready + * to accept more samples. It will then emit the sigResumeOutput signal. + */ + virtual void resumeOutput(void) + { + sigResumeOutput(); + } + + /** + * @brief The registered sink has flushed all samples + * + * This function will be called when all samples have been flushed in the + * registered sink. It will then emit the sigAllSamplesFlushed signal. + */ + virtual void allSamplesFlushed(void) + { + sigAllSamplesFlushed(); + } + + /** + * @brief Write samples into this audio sink + * @param samples The buffer containing the samples + * @param count The number of samples in the buffer + * @return Returns the number of samples that has been taken care of + * + * This function is used to write audio into this object. If it + * returns 0, no more samples should be written until the sigResumeOutput + * signal have been emitted. + */ + int writeSamples(float *samples, int count) + { + return sinkWriteSamples(samples, count); + } + + /** + * @brief Tell the sink to flush the previously written samples + * + * This function is used to tell this object to flush previously written + * samples. When done flushing, the sigAllSamplesFlushed signal will be + * emitted. + */ + void flushSamples(void) + { + sinkFlushSamples(); + } + + /** + * @brief A signal that is emitted when more samples can be written + * + * This signal will be emitted when the registered audio sink is ready + * to accept more samples. + */ + sigc::signal sigResumeOutput; + + /** + * @brief Signal that is emitted when the connected sink is done flushing + * + * This signal will be emitted when all samples have been flushed in the + * registered sink. + */ + sigc::signal sigAllSamplesFlushed; + +}; /* class SigCAudioSource */ + + +} /* namespace */ + +#endif /* ASYNC_SIGC_AUDIO_SOURCE_INCLUDED */ + + + +/* + * This file has not been truncated + */ + diff --git a/src/async/audio/CMakeLists.txt b/src/async/audio/CMakeLists.txt new file mode 100644 index 0000000..6e9fcaf --- /dev/null +++ b/src/async/audio/CMakeLists.txt @@ -0,0 +1,155 @@ +option(USE_ALSA "Alsa audio support" ON) +option(USE_OSS "OSS audio support" ON) + +# Find Speex +find_package(Speex) +if(Speex_FOUND) + if(DEFINED Speex_VERSION_MAJOR) + include_directories(${Speex_INCLUDE_DIRS}) + add_definitions(${Speex_DEFINITIONS}) + add_definitions("-DSPEEX_MAJOR=${Speex_VERSION_MAJOR}") + set(LIBS ${LIBS} ${Speex_LIBRARIES}) + else() + message(WARNING + "Found Speex but version could not be resolved. " + "Will proceed without Speex.") + endif() +else(Speex_FOUND) + message("-- Speex is an optional dependency. The build will complete") + message("-- without it but support for the Speex audio codec will") + message("-- be unavailable.") +endif(Speex_FOUND) + +# Find Opus +find_package(Opus) +if(Opus_FOUND) + if(DEFINED Opus_VERSION_MAJOR) + include_directories(${Opus_INCLUDE_DIRS}) + add_definitions(${Opus_DEFINITIONS}) + add_definitions("-DOPUS_MAJOR=${Opus_VERSION_MAJOR}") + set(LIBS ${LIBS} ${Opus_LIBRARIES}) + else() + message(WARNING + "Found Opus but version could not be resolved. " + "Will proceed without Opus.") + endif() +else(Opus_FOUND) + message("-- Opus is an optional dependency. The build will complete") + message("-- without it but support for the Opus audio codec will") + message("-- be unavailable.") +endif(Opus_FOUND) + +# Find OGG +find_package(OGG) +if(OGG_FOUND) + if(DEFINED OGG_VERSION_MAJOR) + include_directories(${OGG_INCLUDE_DIRS}) + add_definitions(${OGG_DEFINITIONS}) + add_definitions("-DOGG_MAJOR=${OGG_VERSION_MAJOR}") + set(LIBS ${LIBS} ${OGG_LIBRARIES}) + else() + message(WARNING + "Found OGG but version could not be resolved. " + "Will proceed without Opus.") + endif() +else(OGG_FOUND) + message("-- OGG is an optional dependency. The build will complete") + message("-- without it but support for the OGG audio format will") + message("-- be unavailable.") +endif(OGG_FOUND) + +find_package(GSM REQUIRED) +include_directories(${GSM_INCLUDE_DIR}) +set(LIBS ${LIBS} ${GSM_LIBRARY}) + +set(LIBNAME asyncaudio) + +set(EXPINC AsyncAudioSource.h AsyncAudioSink.h AsyncAudioProcessor.h + AsyncAudioClipper.h AsyncAudioCompressor.h + AsyncAudioFilter.h AsyncSigCAudioSink.h AsyncSigCAudioSource.h + AsyncAudioIO.h AsyncAudioSplitter.h AsyncAudioDelayLine.h + AsyncAudioValve.h AsyncAudioAmp.h AsyncAudioSelector.h + AsyncAudioPassthrough.h AsyncAudioMixer.h AsyncAudioFifo.h + AsyncAudioDebugger.h AsyncAudioPacer.h AsyncAudioReader.h + AsyncAudioDecimator.h AsyncAudioInterpolator.h + AsyncAudioStreamStateDetector.h AsyncAudioEncoder.h + AsyncAudioDecoder.h AsyncAudioRecorder.h + AsyncAudioJitterFifo.h AsyncAudioDeviceFactory.h + AsyncAudioDevice.h AsyncAudioNoiseAdder.h AsyncAudioGenerator.h + AsyncAudioFsf.h AsyncAudioContainer.h AsyncAudioContainerWav.h + AsyncAudioContainerPcm.h + ) + +set(LIBSRC AsyncAudioSource.cpp AsyncAudioSink.cpp + AsyncAudioProcessor.cpp AsyncAudioCompressor.cpp + AsyncAudioFilter.cpp fidlib.c AsyncAudioDevice.cpp + AsyncAudioIO.cpp AsyncAudioSplitter.cpp + AsyncAudioDelayLine.cpp AsyncAudioSelector.cpp + AsyncAudioMixer.cpp AsyncAudioFifo.cpp AsyncAudioPacer.cpp + AsyncAudioReader.cpp AsyncAudioDecimator.cpp + AsyncAudioInterpolator.cpp AsyncAudioDecoder.cpp + AsyncAudioEncoder.cpp AsyncAudioEncoderS16.cpp + AsyncAudioDecoderS16.cpp AsyncAudioEncoderGsm.cpp + AsyncAudioDecoderGsm.cpp AsyncAudioRecorder.cpp + AsyncAudioDeviceFactory.cpp AsyncAudioJitterFifo.cpp + AsyncAudioDeviceUDP.cpp AsyncAudioNoiseAdder.cpp + AsyncAudioFsf.cpp AsyncAudioContainer.cpp AsyncAudioContainerWav.cpp + AsyncAudioContainerPcm.cpp + ) + +if(Speex_FOUND) + set(LIBSRC ${LIBSRC} AsyncAudioEncoderSpeex.cpp AsyncAudioDecoderSpeex.cpp) +endif(Speex_FOUND) + +if(Opus_FOUND) + set(LIBSRC ${LIBSRC} AsyncAudioEncoderOpus.cpp AsyncAudioDecoderOpus.cpp) +endif(Opus_FOUND) + +if(OGG_FOUND AND Opus_FOUND) + set(EXPINC ${EXPINC} AsyncAudioContainerOpus.h) + set(LIBSRC ${LIBSRC} AsyncAudioContainerOpus.cpp) +endif(OGG_FOUND AND Opus_FOUND) + +if(LADSPA_FOUND) + set(EXPINC ${EXPINC} AsyncAudioLADSPAPlugin.h) + set(LIBSRC ${LIBSRC} AsyncAudioLADSPAPlugin.cpp) +endif(LADSPA_FOUND) + +if(USE_ALSA) + set(LIBSRC ${LIBSRC} AsyncAudioDeviceAlsa.cpp) + find_package(ALSA REQUIRED QUIET) + set(LIBS ${LIBS} ${ALSA_LIBRARIES}) + include_directories(${ALSA_INCLUDE_DIRS}) +endif(USE_ALSA) + +if(USE_OSS) + set(LIBSRC ${LIBSRC} AsyncAudioDeviceOSS.cpp) +endif(USE_OSS) + +set(LIBS ${LIBS} asynccore) + +# Copy exported include files to the global include directory +foreach(incfile ${EXPINC}) + expinc(${incfile}) +endforeach(incfile) + +# Set up additional defines +add_definitions(-DT_LINUX) + +# Build a shared library and a static library if configured +add_library(${LIBNAME} SHARED ${LIBSRC}) +set_target_properties(${LIBNAME} PROPERTIES VERSION ${VER_LIBASYNC} + SOVERSION ${VER_LIBASYNC_SOVERSION}) +target_link_libraries(${LIBNAME} ${LIBS}) +if (BUILD_STATIC_LIBS) + add_library(${LIBNAME}_static STATIC ${LIBSRC}) + set_target_properties(${LIBNAME}_static PROPERTIES OUTPUT_NAME ${LIBNAME}) + target_link_libraries(${LIBNAME}_static ${LIBS}) +endif(BUILD_STATIC_LIBS) + +# Install files +install(TARGETS ${LIBNAME} DESTINATION ${LIB_INSTALL_DIR}) +if (BUILD_STATIC_LIBS) + install(TARGETS ${LIBNAME}_static DESTINATION ${LIB_INSTALL_DIR}) +endif(BUILD_STATIC_LIBS) +install(FILES ${EXPINC} DESTINATION ${SVX_INCLUDE_INSTALL_DIR}) diff --git a/src/async/audio/fidlib.c b/src/async/audio/fidlib.c new file mode 100644 index 0000000..d89fdf4 --- /dev/null +++ b/src/async/audio/fidlib.c @@ -0,0 +1,2321 @@ +// +// Fidlib digital filter designer code +// ----------------------------------- +// +// Copyright (c) 2002-2004 Jim Peters . This +// file is released under the GNU Lesser General Public License +// (LGPL) version 2.1 as published by the Free Software +// Foundation. See the file COPYING_LIB for details, or visit +// . +// +// The code in this file was written to go with the Fiview app +// (http://uazu.net/fiview/), but it may be used as a library for +// other applications. The idea behind this library is to allow +// filters to be designed at run-time, which gives much greater +// flexibility to filtering applications. +// +// This file depends on the fidmkf.h file which provides the +// filter types from Tony Fisher's 'mkfilter' package. See that +// file for references and links used there. +// +// +// Here are some of the sources I used whilst writing this code: +// +// Robert Bristow-Johnson's EQ cookbook formulae: +// http://www.harmony-central.com/Computer/Programming/Audio-EQ-Cookbook.txt +// + +#define VERSION "0.9.9" + +// +// Filter specification string +// --------------------------- +// +// The filter specification string can be used to completely +// specify the filter, or it can be used with the frequency or +// frequency range missing, in which case default values are +// picked up from values passed directly to the routine. +// +// The spec consists of a series of letters usually followed by +// the order of the filter and then by any other parameters +// required, preceded by slashes. For example: +// +// LpBu4/20.4 Lowpass butterworth, 4th order, -3.01dB at 20.4Hz +// BpBu2/3-4 Bandpass butterworth, 2nd order, from 3 to 4Hz +// BpBu2/=3-4 Same filter, but adjusted exactly to the range given +// BsRe/1000/10 Bandstop resonator, Q=1000, frequency 10Hz +// +// The routines fid_design() or fid_parse() are used to convert +// this spec-string into filter coefficients and a description +// (if required). +// +// +// Typical usage: +// ------------- +// +// FidFilter *filt, *filt2; +// char *desc; +// FidRun *run; +// FidFunc *funcp; +// void *fbuf1, *fbuf2; +// int delay; +// void my_error_func(char *err); +// +// // Design a filter, and optionally get its long description +// filt= fid_design(spec, rate, freq0, freq1, adj, &desc); +// +// // List all the possible filter types +// fid_list_filters(stdout); +// okay= fid_list_filters_buf(buf, buf+sizeof(buf)); +// +// // Calculate the response of the filter at a given frequency +// // (frequency is given as a proportion of the sampling rate, in +// // the range 0 to 0.5). If phase is returned, then this is +// // given in the range 0 to 1 (for 0 to 2*pi). +// resp= fid_response(filt, freq); +// resp= fid_response_pha(filt, freq, &phase); +// +// // Estimate the signal delay caused by a particular filter, in samples +// delay= fid_calc_delay(filt); +// +// // Run a given filter (this will do JIT filter compilation if this is +// // implemented for this processor / OS) +// run= fid_run_new(filt, &funcp); +// fbuf1= fid_run_newbuf(run); +// fbuf2= fid_run_newbuf(run); +// while (...) { +// out_1= funcp(fbuf1, in_1); +// out_2= funcp(fbuf2, in_2); +// if (restart_required) fid_run_zapbuf(fbuf1); +// ... +// } +// fid_run_freebuf(fbuf2); +// fid_run_freebuf(fbuf1); +// fid_run_free(run); +// +// // If you need to allocate your own buffers separately for some +// // reason, then do it this way: +// run= fid_run_new(filt, &funcp); +// len= fid_run_bufsize(run); +// fbuf1= Alloc(len); fid_run_initbuf(run, fbuf1); +// fbuf2= Alloc(len); fid_run_initbuf(run, fbuf2); +// while (...) { +// out_1= funcp(fbuf1, in_1); +// out_2= funcp(fbuf2, in_2); +// if (restart_required) fid_run_zapbuf(fbuf1); +// ... +// } +// free(fbuf2); +// free(fbuf1); +// fid_run_free(run); +// +// // Convert an arbitrary filter into a new filter which is a single +// // IIR/FIR pair. This is done by convolving the coefficients. This +// // flattened filter will give the same result, in theory. However, +// // in practice this will be less accurate, especially in cases where +// // the limits of the floating point format are being reached (e.g. +// // subtracting numbers with small highly significant differences). +// // The routine also ensures that the IIR first coefficient is 1.0. +// filt2= fid_flatten(filt); +// free(filt); +// +// // Parse an entire filter-spec string possibly containing several FIR, +// // IIR and predefined filters and return it as a FidFilter at the given +// // location. Stops at the first ,; or unmatched )]} character, or the end +// // of the string. Returns a strdup'd error string on error, or else 0. +// err= fid_parse(double rate, char **pp, FidFilter **ffp); +// +// // Set up your own fatal-error handler (default is to dump a message +// // to STDERR and exit on fatal conditions) +// fid_set_error_handler(&my_error_func); +// +// // Get the version number of the library as a string (e.g. "1.0.0") +// txt= fid_version(); +// +// // Design a filter and reduce it to a list of all the non-const +// // coefficients, which is returned in the given double[]. The number +// // of coefficients expected must be provided (as a check). +// #define N_COEF +// double coef[N_COEF], gain; +// gain= fid_design_coef(coef, N_COEF, spec, rate, freq0, freq1, adj); +// +// // Rewrite a filter spec in a full and/or separated-out form +// char *full, *min; +// double minf0, minf1; +// int minadj; +// fid_rewrite_spec(spec, freq0, freq1, adj, &full, &min, &minf0, &minf1, &minadj); +// ... +// free(full); free(min); +// +// // Create a FidFilter based on coefficients provided in the +// // given double array. +// static double array[]= { 'I', 3, 1.0, 0.55, 0.77, 'F', 3, 1, -2, 1, 0 }; +// filt= fid_cv_array(array); +// +// // Join a number of filters into a single filter (and free them too, +// // if the first argument is 1) +// filt= fid_cat(0, filt1, filt2, filt3, filt4, 0); +// +// + +// +// Format of returned filter +// ------------------------- +// +// The filter returned is a single chunk of allocated memory in +// which is stored a number of FidFilter instances. Each +// instance has variable length according to the coefficients +// contained in it. It is probably easier to think of this as a +// stream of items in memory. Each sub-filter starts with its +// type as a short -- either 'I' for IIR filters, or 'F' for FIR +// filters. (Other types may be added later, e.g. AM modulation +// elements, or whatever). This is followed by a short bitmap +// which indicates which of the coefficients are constants, +// aiding code-generation. Next comes the count of the following +// coefficients, as an int. (These header fields normally takes 8 +// bytes, the same as a double, but this might depend on the +// platform). Then follow the coefficients, as doubles. The next +// sub-filter follows on straight after that. The end of the list +// is marked by 8 zero bytes, meaning typ==0, cbm==0 and len==0. +// +// The filter can be read with the aid of the FidFilter structure +// (giving typ, cbm, len and val[] elements) and the FFNEXT() +// macro: using ff= FFNEXT(ff) steps to the next FidFilter +// structure along the chain. +// +// Note that within the sub-filters, coefficients are listed in +// the order that they apply to data, from current-sample +// backwards in time, i.e. most recent first (so an FIR val[] of +// 0, 0, 1 represents a two-sample delay FIR filter). IIR +// filters are *not* necessarily adjusted so that their first +// coefficient is 1. +// +// Most filters have their gain pre-adjusted so that some +// suitable part of the response is at gain==1.0. However, this +// depends on the filter type. +// + +// +// Check that a target macro has been set. This macro selects +// various fixes required on various platforms: +// +// T_LINUX Linux, or probably any UNIX-like platform with GCC +// T_MINGW MinGW -- either building on Win32 or cross-compiling +// T_MSVC Microsoft Visual C +// +// (On MSVC, add "T_MSVC" to the preprocessor definitions in the +// project settings, or add /D "T_MSVC" to the compiler +// command-line.) +// + +#ifndef T_LINUX +#ifndef T_MINGW +#ifndef T_MSVC +#error Please define one of the T_* target macros (e.g. -DT_LINUX); see fidlib.c +#endif +#endif +#endif + + +// +// Select which method of filter execution is preferred. +// RF_CMDLIST is recommended (and is the default). +// +// RF_COMBINED -- easy to understand code, lower accuracy +// RF_CMDLIST -- faster pre-compiled code +// RF_JIT -- fastest JIT run-time generated code (no longer supported) +// + +#ifndef RF_COMBINED +#ifndef RF_CMDLIST +#ifndef RF_JIT + +#define RF_CMDLIST + +#endif +#endif +#endif + +// +// Includes +// + +#include +#include +#include +#include +#include +#include +#include "fidlib.h" + +#ifndef M_PI +#define M_PI 3.14159265358979323846 +#endif + +extern FidFilter *mkfilter(char *, ...); + +// +// Target-specific fixes +// + +// Macro for local inline routines that shouldn't be visible externally +#ifdef T_MSVC + #define STATIC_INLINE static __inline +#else + #define STATIC_INLINE static inline +#endif + +// MinGW and MSVC fixes +#if defined(T_MINGW) || defined(T_MSVC) + #ifndef vsnprintf + #define vsnprintf _vsnprintf + #endif + #ifndef snprintf + #define snprintf _snprintf + #endif + STATIC_INLINE double + asinh(double val) { + return log(val + sqrt(val*val + 1.0)); + } +#endif + + +// +// Support code +// + +static void (*error_handler)(char *err)= 0; + +static void +error(char *fmt, ...) { + char buf[1024]; + va_list ap; + va_start(ap, fmt); + + vsnprintf(buf, sizeof(buf), fmt, ap); // Ignore overflow + buf[sizeof(buf)-1]= 0; + if (error_handler) error_handler(buf); + + // If error handler routine returns, we dump to STDERR and exit anyway + fprintf(stderr, "fidlib error: %s\n", buf); + exit(1); +} + +static char * +strdupf(char *fmt, ...) { + va_list ap; + char buf[1024], *rv; + int len; + va_start(ap, fmt); + len= vsnprintf(buf, sizeof(buf), fmt, ap); + va_end(ap); + if (len < 0 || len >= sizeof(buf)-1) + error("strdupf exceeded buffer"); + rv= strdup(buf); + if (!rv) error("Out of memory"); + return rv; +} + +static void * +Alloc(int size) { + void *vp= calloc(1, size); + if (!vp) error("Out of memory"); + return vp; +} + +#define ALLOC(type) ((type*)Alloc(sizeof(type))) +#define ALLOC_ARR(cnt, type) ((type*)Alloc((cnt) * sizeof(type))) + + +// +// Complex multiply: aa *= bb; +// + +STATIC_INLINE void +cmul(double *aa, double *bb) { + double rr= aa[0] * bb[0] - aa[1] * bb[1]; + double ii= aa[0] * bb[1] + aa[1] * bb[0]; + aa[0]= rr; + aa[1]= ii; +} + +// +// Complex square: aa *= aa; +// + +STATIC_INLINE void +csqu(double *aa) { + double rr= aa[0] * aa[0] - aa[1] * aa[1]; + double ii= 2 * aa[0] * aa[1]; + aa[0]= rr; + aa[1]= ii; +} + +// +// Complex multiply by real: aa *= bb; +// + +STATIC_INLINE void +cmulr(double *aa, double fact) { + aa[0] *= fact; + aa[1] *= fact; +} + +// +// Complex conjugate: aa= aa* +// + +STATIC_INLINE void +cconj(double *aa) { + aa[1]= -aa[1]; +} + +// +// Complex divide: aa /= bb; +// + +STATIC_INLINE void +cdiv(double *aa, double *bb) { + double rr= aa[0] * bb[0] + aa[1] * bb[1]; + double ii= -aa[0] * bb[1] + aa[1] * bb[0]; + double fact= 1.0 / (bb[0] * bb[0] + bb[1] * bb[1]); + aa[0]= rr * fact; + aa[1]= ii * fact; +} + +// +// Complex reciprocal: aa= 1/aa +// + +STATIC_INLINE void +crecip(double *aa) { + double fact= 1.0 / (aa[0] * aa[0] + aa[1] * aa[1]); + aa[0] *= fact; + aa[1] *= -fact; +} + +// +// Complex assign: aa= bb +// + +STATIC_INLINE void +cass(double *aa, double *bb) { + memcpy(aa, bb, 2*sizeof(double)); // Assigning doubles is really slow +} + +// +// Complex assign: aa= (rr + ii*j) +// + +STATIC_INLINE void +cassz(double *aa, double rr, double ii) { + aa[0]= rr; + aa[1]= ii; +} + +// +// Complex add: aa += bb +// + +STATIC_INLINE void +cadd(double *aa, double *bb) { + aa[0] += bb[0]; + aa[1] += bb[1]; +} + +// +// Complex add: aa += (rr + ii*j) +// + +STATIC_INLINE void +caddz(double *aa, double rr, double ii) { + aa[0] += rr; + aa[1] += ii; +} + +// +// Complex subtract: aa -= bb +// + +STATIC_INLINE void +csub(double *aa, double *bb) { + aa[0] -= bb[0]; + aa[1] -= bb[1]; +} + +// +// Complex subtract: aa -= (rr + ii*j) +// + +STATIC_INLINE void +csubz(double *aa, double rr, double ii) { + aa[0] -= rr; + aa[1] -= ii; +} + +// +// Complex negate: aa= -aa +// + +STATIC_INLINE void +cneg(double *aa) { + aa[0]= -aa[0]; + aa[1]= -aa[1]; +} + +// +// Evaluate a complex polynomial given the coefficients. +// rv[0]+i*rv[1] is the result, in[0]+i*in[1] is the input value. +// Coefficients are real values. +// + +STATIC_INLINE void +evaluate(double *rv, double *coef, int n_coef, double *in) { + double pz[2]; // Powers of Z + + // Handle first iteration by hand + rv[0]= *coef++; + rv[1]= 0; + + if (--n_coef > 0) { + // Handle second iteration by hand + pz[0]= in[0]; + pz[1]= in[1]; + rv[0] += *coef * pz[0]; + rv[1] += *coef * pz[1]; + coef++; n_coef--; + + // Loop for remainder + while (n_coef > 0) { + cmul(pz, in); + rv[0] += *coef * pz[0]; + rv[1] += *coef * pz[1]; + coef++; + n_coef--; + } + } +} + + +// +// Housekeeping +// + +void +fid_set_error_handler(void (*rout)(char*)) { + error_handler= rout; +} + +char * +fid_version(void) { + return VERSION; +} + + +// +// Get the response and phase of a filter at the given frequency +// (expressed as a proportion of the sampling rate, 0->0.5). +// Phase is returned as a number from 0 to 1, representing a +// phase between 0 and two-pi. +// + +double +fid_response_pha(FidFilter *filt, double freq, double *phase) { + double top[2], bot[2]; + double theta= freq * 2 * M_PI; + double zz[2]; + + top[0]= 1; + top[1]= 0; + bot[0]= 1; + bot[1]= 0; + zz[0]= cos(theta); + zz[1]= sin(theta); + + while (filt->len) { + double resp[2]; + int cnt= filt->len; + evaluate(resp, filt->val, cnt, zz); + if (filt->typ == 'I') + cmul(bot, resp); + else if (filt->typ == 'F') + cmul(top, resp); + else + error("Unknown filter type %d in fid_response_pha()", filt->typ); + filt= FFNEXT(filt); + } + + cdiv(top, bot); + + if (phase) { + double pha= atan2(top[1], top[0]) / (2 * M_PI); + if (pha < 0) pha += 1.0; + *phase= pha; + } + + return hypot(top[1], top[0]); +} + +// +// Get the response of a filter at the given frequency (expressed +// as a proportion of the sampling rate, 0->0.5). +// +// Code duplicate, as I didn't want the overhead of a function +// call to fid_response_pha. Almost every call in this routine +// can be inlined. +// + +double +fid_response(FidFilter *filt, double freq) { + double top[2], bot[2]; + double theta= freq * 2 * M_PI; + double zz[2]; + + top[0]= 1; + top[1]= 0; + bot[0]= 1; + bot[1]= 0; + zz[0]= cos(theta); + zz[1]= sin(theta); + + while (filt->len) { + double resp[2]; + int cnt= filt->len; + evaluate(resp, filt->val, cnt, zz); + if (filt->typ == 'I') + cmul(bot, resp); + else if (filt->typ == 'F') + cmul(top, resp); + else + error("Unknown filter type %d in fid_response()", filt->typ); + filt= FFNEXT(filt); + } + + cdiv(top, bot); + + return hypot(top[1], top[0]); +} + + +// +// Estimate the delay that a filter causes to the signal by +// looking for the point at which 50% of the filter calculations +// are complete. This involves running test impulses through the +// filter several times. The estimated delay in samples is +// returned. +// +// Delays longer than 8,000,000 samples are not handled well, as +// the code drops out at this point rather than get stuck in an +// endless loop. +// + +int +fid_calc_delay(FidFilter *filt) { + FidRun *run; + FidFunc *dostep; + void *f1, *f2; + double tot, tot100, tot50; + int cnt; + + run= fid_run_new(filt, &dostep); + + // Run through to find at least the 99.9% point of filter; the r2 + // (tot100) filter runs at 4x the speed of the other one to act as + // a reference point much further ahead in the impulse response. + f1= fid_run_newbuf(run); + f2= fid_run_newbuf(run); + + tot= fabs(dostep(f1, 1.0)); + tot100= fabs(dostep(f2, 1.0)); + tot100 += fabs(dostep(f2, 0.0)); + tot100 += fabs(dostep(f2, 0.0)); + tot100 += fabs(dostep(f2, 0.0)); + + for (cnt= 1; cnt < 0x1000000; cnt++) { + tot += fabs(dostep(f1, 0.0)); + tot100 += fabs(dostep(f2, 0.0)); + tot100 += fabs(dostep(f2, 0.0)); + tot100 += fabs(dostep(f2, 0.0)); + tot100 += fabs(dostep(f2, 0.0)); + + if (tot/tot100 >= 0.999) break; + } + fid_run_freebuf(f1); + fid_run_freebuf(f2); + + // Now find the 50% point + tot50= tot100/2; + f1= fid_run_newbuf(run); + tot= fabs(dostep(f1, 1.0)); + for (cnt= 0; tot < tot50; cnt++) + tot += fabs(dostep(f1, 0.0)); + fid_run_freebuf(f1); + + // Clean up, return + fid_run_free(run); + return cnt; +} + + +// +// 'mkfilter'-derived code +// + +#include "fidmkf.h" + + +// +// Stack a number of identical filters, generating the required +// FidFilter* return value +// + +static FidFilter* +stack_filter(int order, int n_head, int n_val, ...) { + FidFilter *rv= FFALLOC(n_head * order, n_val * order); + FidFilter *p, *q; + va_list ap; + int a, b, len; + + if (order == 0) return rv; + + // Copy from ap + va_start(ap, n_val); + p= q= rv; + for (a= 0; atyp= va_arg(ap, int); + p->cbm= va_arg(ap, int); + p->len= va_arg(ap, int); + for (b= 0; blen; b++) + p->val[b]= va_arg(ap, double); + p= FFNEXT(p); + } + va_end(ap); + order--; + + // Check length + len= ((char*)p)-((char*)q); + if (len != FFCSIZE(n_head-1, n_val)) + error("Internal error; bad call to stack_filter(); length mismatch (%d,%d)", + len, FFCSIZE(n_head-1, n_val)); + + // Make as many additional copies as necessary + while (order-- > 0) { + memcpy(p, q, len); + p= (void*)(len + (char*)p); + } + + // List is already terminated due to zeroed allocation + return rv; +} + +// +// Search for a peak between two given frequencies. It is +// assumed that the gradient goes upwards from 'f0' to the peak, +// and then down again to 'f3'. If there are any other curves, +// this routine will get confused and will come up with some +// frequency, although probably not the right one. +// +// Returns the frequency of the peak. +// + +static double +search_peak(FidFilter *ff, double f0, double f3) { + double f1, f2; + double r1, r2; + int a; + + // Binary search, modified, taking two intermediate points. Do 20 + // subdivisions, which should give 1/2^20 == 1e-6 accuracy compared + // to original range. + for (a= 0; a<20; a++) { + f1= 0.51 * f0 + 0.49 * f3; + f2= 0.49 * f0 + 0.51 * f3; + if (f1 == f2) break; // We're hitting FP limit + r1= fid_response(ff, f1); + r2= fid_response(ff, f2); + if (r1 > r2) // Peak is either to the left, or between f1/f2 + f3= f2; + else // Peak is either to the right, or between f1/f2 + f0= f1; + } + return (f0+f3)*0.5; +} + +// +// Handle the different 'back-ends' for Bessel, Butterworth and +// Chebyshev filters. First argument selects between bilinear +// (0) and matched-Z (non-0). The BL and MZ macros makes this a +// bit more obvious in the code. +// +// Overall filter gain is adjusted to give the peak at 1.0. This +// is easy for all types except for band-pass, where a search is +// required to find the precise peak. This is much slower than +// the other types. +// + +#define BL 0 +#define MZ 1 + +static FidFilter* +do_lowpass(int mz, double freq) { + FidFilter *rv; + lowpass(prewarp(freq)); + if (mz) s2z_matchedZ(); else s2z_bilinear(); + rv= z2fidfilter(1.0, ~0); // FIR is constant + rv->val[0]= 1.0 / fid_response(rv, 0.0); + return rv; +} + +static FidFilter* +do_highpass(int mz, double freq) { + FidFilter *rv; + highpass(prewarp(freq)); + if (mz) s2z_matchedZ(); else s2z_bilinear(); + rv= z2fidfilter(1.0, ~0); // FIR is constant + rv->val[0]= 1.0 / fid_response(rv, 0.5); + return rv; +} + +static FidFilter* +do_bandpass(int mz, double f0, double f1) { + FidFilter *rv; + bandpass(prewarp(f0), prewarp(f1)); + if (mz) s2z_matchedZ(); else s2z_bilinear(); + rv= z2fidfilter(1.0, ~0); // FIR is constant + rv->val[0]= 1.0 / fid_response(rv, search_peak(rv, f0, f1)); + return rv; +} + +static FidFilter* +do_bandstop(int mz, double f0, double f1) { + FidFilter *rv; + bandstop(prewarp(f0), prewarp(f1)); + if (mz) s2z_matchedZ(); else s2z_bilinear(); + rv= z2fidfilter(1.0, 5); // FIR second coefficient is *non-const* for bandstop + rv->val[0]= 1.0 / fid_response(rv, 0.0); // Use 0Hz response as reference + return rv; +} + + +// +// Information passed to individual filter design routines: +// +// double* rout(double rate, double f0, double f1, +// int order, int n_arg, double *arg); +// +// 'rate' is the sampling rate, or 1 if not set +// 'f0' and 'f1' give the frequency or frequency range as a +// proportion of the sampling rate +// 'order' is the order of the filter (the integer passed immediately +// after the name) +// 'n_arg' is the number of additional arguments for the filter +// 'arg' gives the additional argument values: arg[n] +// +// Note that #O #o #F and #R are mapped to the f0/f1/order +// arguments, and are not included in the arg[] array. +// +// See the previous description for the required meaning of the +// return value FidFilter list. +// + +// +// Filter design routines and supporting code +// + +static FidFilter* +des_bpre(double rate, double f0, double f1, int order, int n_arg, double *arg) { + bandpass_res(f0, arg[0]); + return z2fidfilter(1.0, ~0); // FIR constant +} + +static FidFilter* +des_bsre(double rate, double f0, double f1, int order, int n_arg, double *arg) { + bandstop_res(f0, arg[0]); + return z2fidfilter(1.0, 0); // FIR not constant, depends on freq +} + +static FidFilter* +des_apre(double rate, double f0, double f1, int order, int n_arg, double *arg) { + allpass_res(f0, arg[0]); + return z2fidfilter(1.0, 0); // FIR not constant, depends on freq +} + +static FidFilter* +des_pi(double rate, double f0, double f1, int order, int n_arg, double *arg) { + prop_integral(prewarp(f0)); + s2z_bilinear(); + return z2fidfilter(1.0, 0); // FIR not constant, depends on freq +} + +static FidFilter* +des_piz(double rate, double f0, double f1, int order, int n_arg, double *arg) { + prop_integral(prewarp(f0)); + s2z_matchedZ(); + return z2fidfilter(1.0, 0); // FIR not constant, depends on freq +} + +static FidFilter* +des_lpbe(double rate, double f0, double f1, int order, int n_arg, double *arg) { + bessel(order); + return do_lowpass(BL, f0); +} + +static FidFilter* +des_hpbe(double rate, double f0, double f1, int order, int n_arg, double *arg) { + bessel(order); + return do_highpass(BL, f0); +} + +static FidFilter* +des_bpbe(double rate, double f0, double f1, int order, int n_arg, double *arg) { + bessel(order); + return do_bandpass(BL, f0, f1); +} + +static FidFilter* +des_bsbe(double rate, double f0, double f1, int order, int n_arg, double *arg) { + bessel(order); + return do_bandstop(BL, f0, f1); +} + +static FidFilter* +des_lpbez(double rate, double f0, double f1, int order, int n_arg, double *arg) { + bessel(order); + return do_lowpass(MZ, f0); +} + +static FidFilter* +des_hpbez(double rate, double f0, double f1, int order, int n_arg, double *arg) { + bessel(order); + return do_highpass(MZ, f0); +} + +static FidFilter* +des_bpbez(double rate, double f0, double f1, int order, int n_arg, double *arg) { + bessel(order); + return do_bandpass(MZ, f0, f1); +} + +static FidFilter* +des_bsbez(double rate, double f0, double f1, int order, int n_arg, double *arg) { + bessel(order); + return do_bandstop(MZ, f0, f1); +} + +static FidFilter* // Butterworth-Bessel cross +des_lpbube(double rate, double f0, double f1, int order, int n_arg, double *arg) { + double tmp[MAXPZ]; + int a; + bessel(order); memcpy(tmp, pol, order * sizeof(double)); + butterworth(order); + for (a= 0; atyp= 'F'; + ff->cbm= 0; + ff->len= max*2+1; + ff->val[max]= tot= 1.0; + for (a= 1; a<=max; a++) { + double val= 0.42 + + 0.5 * cos(M_PI * a / wid) + + 0.08 * cos(M_PI * 2.0 * a / wid); + ff->val[max-a]= val; + ff->val[max+a]= val; + tot += val * 2.0; + } + adj= 1/tot; + for (a= 0; a<=max*2; a++) ff->val[a] *= adj; + return ff; +} + +static FidFilter* +des_lphm(double rate, double f0, double f1, int order, int n_arg, double *arg) { + double wid= 0.3262096/f0; + double tot, adj; + int max= (int)floor(wid); + int a; + FidFilter *ff= Alloc(FFCSIZE(1, max*2+1)); + ff->typ= 'F'; + ff->cbm= 0; + ff->len= max*2+1; + ff->val[max]= tot= 1.0; + for (a= 1; a<=max; a++) { + double val= 0.54 + + 0.46 * cos(M_PI * a / wid); + ff->val[max-a]= val; + ff->val[max+a]= val; + tot += val * 2.0; + } + adj= 1/tot; + for (a= 0; a<=max*2; a++) ff->val[a] *= adj; + return ff; +} + +static FidFilter* +des_lphn(double rate, double f0, double f1, int order, int n_arg, double *arg) { + double wid= 0.360144/f0; + double tot, adj; + int max= (int)floor(wid); + int a; + FidFilter *ff= Alloc(FFCSIZE(1, max*2+1)); + ff->typ= 'F'; + ff->cbm= 0; + ff->len= max*2+1; + ff->val[max]= tot= 1.0; + for (a= 1; a<=max; a++) { + double val= 0.5 + + 0.5 * cos(M_PI * a / wid); + ff->val[max-a]= val; + ff->val[max+a]= val; + tot += val * 2.0; + } + adj= 1/tot; + for (a= 0; a<=max*2; a++) ff->val[a] *= adj; + return ff; +} + +static FidFilter* +des_lpba(double rate, double f0, double f1, int order, int n_arg, double *arg) { + double wid= 0.3189435/f0; + double tot, adj; + int max= (int)floor(wid); + int a; + FidFilter *ff= Alloc(FFCSIZE(1, max*2+1)); + ff->typ= 'F'; + ff->cbm= 0; + ff->len= max*2+1; + ff->val[max]= tot= 1.0; + for (a= 1; a<=max; a++) { + double val= 1.0 - a/wid; + ff->val[max-a]= val; + ff->val[max+a]= val; + tot += val * 2.0; + } + adj= 1/tot; + for (a= 0; a<=max*2; a++) ff->val[a] *= adj; + return ff; +} + + +// +// Filter table +// + +static struct { + FidFilter *(*rout)(double,double,double,int,int,double*); // Designer routine address + char *fmt; // Format for spec-string + char *txt; // Human-readable description of filter +} filter[]= { + { des_bpre, "BpRe/#V/#F", + "Bandpass resonator, Q=#V (0 means Inf), frequency #F" }, + { des_bsre, "BsRe/#V/#F", + "Bandstop resonator, Q=#V (0 means Inf), frequency #F" }, + { des_apre, "ApRe/#V/#F", + "Allpass resonator, Q=#V (0 means Inf), frequency #F" }, + { des_pi, "Pi/#F", + "Proportional-integral filter, frequency #F" }, + { des_piz, "PiZ/#F", + "Proportional-integral filter, matched z-transform, frequency #F" }, + { des_lpbe, "LpBe#O/#F", + "Lowpass Bessel filter, order #O, -3.01dB frequency #F" }, + { des_hpbe, "HpBe#O/#F", + "Highpass Bessel filter, order #O, -3.01dB frequency #F" }, + { des_bpbe, "BpBe#O/#R", + "Bandpass Bessel filter, order #O, -3.01dB frequencies #R" }, + { des_bsbe, "BsBe#O/#R", + "Bandstop Bessel filter, order #O, -3.01dB frequencies #R" }, + { des_lpbu, "LpBu#O/#F", + "Lowpass Butterworth filter, order #O, -3.01dB frequency #F" }, + { des_hpbu, "HpBu#O/#F", + "Highpass Butterworth filter, order #O, -3.01dB frequency #F" }, + { des_bpbu, "BpBu#O/#R", + "Bandpass Butterworth filter, order #O, -3.01dB frequencies #R" }, + { des_bsbu, "BsBu#O/#R", + "Bandstop Butterworth filter, order #O, -3.01dB frequencies #R" }, + { des_lpch, "LpCh#O/#V/#F", + "Lowpass Chebyshev filter, order #O, passband ripple #VdB, -3.01dB frequency #F" }, + { des_hpch, "HpCh#O/#V/#F", + "Highpass Chebyshev filter, order #O, passband ripple #VdB, -3.01dB frequency #F" }, + { des_bpch, "BpCh#O/#V/#R", + "Bandpass Chebyshev filter, order #O, passband ripple #VdB, -3.01dB frequencies #R" }, + { des_bsch, "BsCh#O/#V/#R", + "Bandstop Chebyshev filter, order #O, passband ripple #VdB, -3.01dB frequencies #R" }, + { des_lpbez, "LpBeZ#O/#F", + "Lowpass Bessel filter, matched z-transform, order #O, -3.01dB frequency #F" }, + { des_hpbez, "HpBeZ#O/#F", + "Highpass Bessel filter, matched z-transform, order #O, -3.01dB frequency #F" }, + { des_bpbez, "BpBeZ#O/#R", + "Bandpass Bessel filter, matched z-transform, order #O, -3.01dB frequencies #R" }, + { des_bsbez, "BsBeZ#O/#R", + "Bandstop Bessel filter, matched z-transform, order #O, -3.01dB frequencies #R" }, + { des_lpbuz, "LpBuZ#O/#F", + "Lowpass Butterworth filter, matched z-transform, order #O, -3.01dB frequency #F" }, + { des_hpbuz, "HpBuZ#O/#F", + "Highpass Butterworth filter, matched z-transform, order #O, -3.01dB frequency #F" }, + { des_bpbuz, "BpBuZ#O/#R", + "Bandpass Butterworth filter, matched z-transform, order #O, -3.01dB frequencies #R" }, + { des_bsbuz, "BsBuZ#O/#R", + "Bandstop Butterworth filter, matched z-transform, order #O, -3.01dB frequencies #R" }, + { des_lpchz, "LpChZ#O/#V/#F", + "Lowpass Chebyshev filter, matched z-transform, order #O, " + "passband ripple #VdB, -3.01dB frequency #F" }, + { des_hpchz, "HpChZ#O/#V/#F", + "Highpass Chebyshev filter, matched z-transform, order #O, " + "passband ripple #VdB, -3.01dB frequency #F" }, + { des_bpchz, "BpChZ#O/#V/#R", + "Bandpass Chebyshev filter, matched z-transform, order #O, " + "passband ripple #VdB, -3.01dB frequencies #R" }, + { des_bschz, "BsChZ#O/#V/#R", + "Bandstop Chebyshev filter, matched z-transform, order #O, " + "passband ripple #VdB, -3.01dB frequencies #R" }, + { des_lpbube, "LpBuBe#O/#V/#F", + "Lowpass Butterworth-Bessel #V% cross, order #O, -3.01dB frequency #F" }, + { des_lpbq, "LpBq#o/#V/#F", + "Lowpass biquad filter, order #O, Q=#V, -3.01dB frequency #F" }, + { des_hpbq, "HpBq#o/#V/#F", + "Highpass biquad filter, order #O, Q=#V, -3.01dB frequency #F" }, + { des_bpbq, "BpBq#o/#V/#F", + "Bandpass biquad filter, order #O, Q=#V, centre frequency #F" }, + { des_bsbq, "BsBq#o/#V/#F", + "Bandstop biquad filter, order #O, Q=#V, centre frequency #F" }, + { des_apbq, "ApBq#o/#V/#F", + "Allpass biquad filter, order #O, Q=#V, centre frequency #F" }, + { des_pkbq, "PkBq#o/#V/#V/#F", + "Peaking biquad filter, order #O, Q=#V, dBgain=#V, frequency #F" }, + { des_lsbq, "LsBq#o/#V/#V/#F", + "Lowpass shelving biquad filter, S=#V, dBgain=#V, frequency #F" }, + { des_hsbq, "HsBq#o/#V/#V/#F", + "Highpass shelving biquad filter, S=#V, dBgain=#V, frequency #F" }, + { des_lpbl, "LpBl/#F", + "Lowpass Blackman window, -3.01dB frequency #F" }, + { des_lphm, "LpHm/#F", + "Lowpass Hamming window, -3.01dB frequency #F" }, + { des_lphn, "LpHn/#F", + "Lowpass Hann window, -3.01dB frequency #F" }, + { des_lpba, "LpBa/#F", + "Lowpass Bartlet (triangular) window, -3.01dB frequency #F" }, + { 0, 0, 0 } +}; + +// +// Design a filter. Spec and range are passed as arguments. The +// return value is a pointer to a FidFilter as documented earlier +// in this file. This needs to be free()d once finished with. +// +// If 'f_adj' is set, then the frequencies fed to the design code +// are adjusted automatically to get true sqrt(0.5) (-3.01dB) +// values at the provided frequencies. (This is obviously a +// slower operation) +// +// If 'descp' is non-0, then a long description of the filter is +// generated and returned as a strdup'd string at the given +// location. +// +// Any problem with the spec causes the program to die with an +// error message. +// +// 'spec' gives the specification string. The 'rate' argument +// gives the sampling rate for the data that will be passed to +// the filter. This is only used to interpret the frequencies +// given in the spec or given in 'freq0' and 'freq1'. Use 1.0 if +// the frequencies are given as a proportion of the sampling +// rate, in the range 0 to 0.5. 'freq0' and 'freq1' provide the +// default frequency or frequency range if this is not included +// in the specification string. These should be -ve if there is +// no default range (causing an error if they are omitted from +// the 'spec'). +// + +typedef struct Spec Spec; +static char* parse_spec(Spec*); +static FidFilter *auto_adjust_single(Spec *sp, double rate, double f0); +static FidFilter *auto_adjust_dual(Spec *sp, double rate, double f0, double f1); +struct Spec { +#define MAXARG 10 + char *spec; + double in_f0, in_f1; + int in_adj; + double argarr[MAXARG]; + double f0, f1; + int adj; + int n_arg; + int order; + int minlen; // Minimum length of spec-string, assuming f0/f1 passed separately + int n_freq; // Number of frequencies provided: 0,1,2 + int fi; // Filter index (filter[fi]) +}; + +FidFilter * +fid_design(char *spec, double rate, double freq0, double freq1, int f_adj, char **descp) { + FidFilter *rv; + Spec sp; + double f0, f1; + char *err; + + // Parse the filter-spec + sp.spec= spec; + sp.in_f0= freq0; + sp.in_f1= freq1; + sp.in_adj= f_adj; + err= parse_spec(&sp); + if (err) error("%s", err); + f0= sp.f0; + f1= sp.f1; + + // Adjust frequencies to range 0-0.5, and check them + f0 /= rate; + if (f0 > 0.5) error("Frequency of %gHz out of range with sampling rate of %gHz", f0*rate, rate); + f1 /= rate; + if (f1 > 0.5) error("Frequency of %gHz out of range with sampling rate of %gHz", f1*rate, rate); + + // Okay we now have a successful spec-match to filter[sp.fi], and sp.n_arg + // args are now in sp.argarr[] + + // Generate the filter + if (!sp.adj) + rv= filter[sp.fi].rout(rate, f0, f1, sp.order, sp.n_arg, sp.argarr); + else if (strstr(filter[sp.fi].fmt, "#R")) + rv= auto_adjust_dual(&sp, rate, f0, f1); + else + rv= auto_adjust_single(&sp, rate, f0); + + // Generate a long description if required + if (descp) { + char *fmt= filter[sp.fi].txt; + int max= strlen(fmt) + 60 + sp.n_arg * 20; + char *desc= Alloc(max); + char *p= desc; + char ch; + double *arg= sp.argarr; + int n_arg= sp.n_arg; + + while ((ch= *fmt++)) { + if (ch != '#') { + *p++= ch; + continue; + } + + switch (*fmt++) { + case 'O': + p += sprintf(p, "%d", sp.order); + break; + case 'F': + p += sprintf(p, "%g", f0*rate); + break; + case 'R': + p += sprintf(p, "%g-%g", f0*rate, f1*rate); + break; + case 'V': + if (n_arg <= 0) + error("Internal error -- disagreement between filter short-spec\n" + " and long-description over number of arguments"); + n_arg--; + p += sprintf(p, "%g", *arg++); + break; + default: + error("Internal error: unknown format in long description: #%c", fmt[-1]); + } + } + *p++= 0; + if (p-desc >= max) error("Internal error: exceeded estimated description buffer"); + *descp= desc; + } + + return rv; +} + +// +// Auto-adjust input frequency to give correct sqrt(0.5) +// (~-3.01dB) point to 6 figures +// + +#define M301DB (0.707106781186548) + +static FidFilter * +auto_adjust_single(Spec *sp, double rate, double f0) { + double a0, a1, a2; + FidFilter *(*design)(double,double,double,int,int,double*)= filter[sp->fi].rout; + FidFilter *rv= 0; + double resp; + double r0, r2; + int incr; // Increasing (1) or decreasing (0) + int a; + +#define DESIGN(aa) design(rate, aa, aa, sp->order, sp->n_arg, sp->argarr) +#define TEST(aa) { if (rv) {free(rv);rv= 0;} rv= DESIGN(aa); resp= fid_response(rv, f0); } + + // Try and establish a range within which we can find the point + a0= f0; TEST(a0); r0= resp; + for (a= 2; 1; a*=2) { + a2= f0/a; TEST(a2); r2= resp; + if ((r0 < M301DB) != (r2 < M301DB)) break; + a2= 0.5-((0.5-f0)/a); TEST(a2); r2= resp; + if ((r0 < M301DB) != (r2 < M301DB)) break; + if (a == 32) // No success + error("auto_adjust_single internal error -- can't establish enclosing range"); + } + + incr= r2 > r0; + if (a0 > a2) { + a1= a0; a0= a2; a2= a1; + incr= !incr; + } + + // Binary search + while (1) { + a1= 0.5 * (a0 + a2); + if (a1 == a0 || a1 == a2) break; // Limit of double, sanity check + TEST(a1); + if (resp >= 0.9999995 * M301DB && resp < 1.0000005 * M301DB) break; + if (incr == (resp > M301DB)) + a2= a1; + else + a0= a1; + } + +#undef TEST +#undef DESIGN + + return rv; +} + + +// +// Auto-adjust input frequencies to give response of sqrt(0.5) +// (~-3.01dB) correct to 6sf at the given frequency-points +// + +static FidFilter * +auto_adjust_dual(Spec *sp, double rate, double f0, double f1) { + double mid= 0.5 * (f0+f1); + double wid= 0.5 * fabs(f1-f0); + FidFilter *(*design)(double,double,double,int,int,double*)= filter[sp->fi].rout; + FidFilter *rv= 0; + int bpass= -1; + double delta; + double mid0, mid1; + double wid0, wid1; + double r0, r1, err0, err1; + double perr; + int cnt; + int cnt_design= 0; + +#define DESIGN(mm,ww) { if (rv) {free(rv);rv= 0;} \ + rv= design(rate, mm-ww, mm+ww, sp->order, sp->n_arg, sp->argarr); \ + r0= fid_response(rv, f0); r1= fid_response(rv, f1); \ + err0= fabs(M301DB-r0); err1= fabs(M301DB-r1); cnt_design++; } + +#define INC_WID ((r0+r1 < 1.0) == bpass) +#define INC_MID ((r0 > r1) == bpass) +#define MATCH (err0 < 0.000000499 && err1 < 0.000000499) +#define PERR (err0+err1) + + DESIGN(mid, wid); + bpass= (fid_response(rv, 0) < 0.5); + delta= wid * 0.5; + + // Try delta changes until we get there + for (cnt= 0; 1; cnt++, delta *= 0.51) { + DESIGN(mid, wid); // I know -- this is redundant + perr= PERR; + + mid0= mid; + wid0= wid; + mid1= mid + (INC_MID ? delta : -delta); + wid1= wid + (INC_WID ? delta : -delta); + + if (mid0 - wid1 > 0.0 && mid0 + wid1 < 0.5) { + DESIGN(mid0, wid1); + if (MATCH) break; + if (PERR < perr) { perr= PERR; mid= mid0; wid= wid1; } + } + + if (mid1 - wid0 > 0.0 && mid1 + wid0 < 0.5) { + DESIGN(mid1, wid0); + if (MATCH) break; + if (PERR < perr) { perr= PERR; mid= mid1; wid= wid0; } + } + + if (mid1 - wid1 > 0.0 && mid1 + wid1 < 0.5) { + DESIGN(mid1, wid1); + if (MATCH) break; + if (PERR < perr) { perr= PERR; mid= mid1; wid= wid1; } + } + + if (cnt > 1000) + error("auto_adjust_dual -- design not converging"); + } + +#undef INC_WID +#undef INC_MID +#undef MATCH +#undef PERR +#undef DESIGN + + return rv; +} + + +// +// Expand a specification string to the given buffer; if out of +// space, drops dead +// + +static void +expand_spec(char *buf, char *bufend, char *str) { + int ch; + char *p= buf; + + while ((ch= *str++)) { + if (p + 10 >= bufend) + error("Buffer overflow in fidlib expand_spec()"); + if (ch == '#') { + switch (*str++) { + case 'o': p += sprintf(p, ""); break; + case 'O': p += sprintf(p, ""); break; + case 'F': p += sprintf(p, ""); break; + case 'R': p += sprintf(p, ""); break; + case 'V': p += sprintf(p, ""); break; + default: p += sprintf(p, "<%c>", str[-1]); break; + } + } else { + *p++= ch; + } + } + *p= 0; +} + +// +// Design a filter and reduce it to a list of all the non-const +// coefficients. Arguments are as for fid_filter(). The +// coefficients are written into the given double array. If the +// number of coefficients doesn't match the array length given, +// then a fatal error is generated. +// +// Note that all 1-element FIRs and IIR first-coefficients are +// merged into a single gain coefficient, which is returned +// rather than being included in the coefficient list. This is +// to allow it to be merged with other gains within a stack of +// filters. +// +// The algorithm used here (merging 1-element FIRs and adjusting +// IIR first-coefficients) must match that used in the code- +// generating code, or else the coefficients won't match up. The +// 'n_coef' argument provides a partial safeguard. +// + +double +fid_design_coef(double *coef, int n_coef, char *spec, double rate, + double freq0, double freq1, int adj) { + FidFilter *filt= fid_design(spec, rate, freq0, freq1, adj, 0); + FidFilter *ff= filt; + int a, len; + int cnt= 0; + double gain= 1.0; + double *iir, *fir, iir_adj = 1.0; + static double const_one= 1; + int n_iir, n_fir; + int iir_cbm, fir_cbm; + + while (ff->typ) { + if (ff->typ == 'F' && ff->len == 1) { + gain *= ff->val[0]; + ff= FFNEXT(ff); + continue; + } + + if (ff->typ != 'I' && ff->typ != 'F') + error("fid_design_coef can't handle FidFilter type: %c", ff->typ); + + // Initialise to safe defaults + iir= fir= &const_one; + n_iir= n_fir= 1; + iir_cbm= fir_cbm= ~0; + + // See if we have an IIR filter + if (ff->typ == 'I') { + iir= ff->val; + n_iir= ff->len; + iir_cbm= ff->cbm; + iir_adj= 1.0 / ff->val[0]; + ff= FFNEXT(ff); + gain *= iir_adj; + } + + // See if we have an FIR filter + if (ff->typ == 'F') { + fir= ff->val; + n_fir= ff->len; + fir_cbm= ff->cbm; + ff= FFNEXT(ff); + } + + // Dump out all non-const coefficients in reverse order + len= n_fir > n_iir ? n_fir : n_iir; + for (a= len-1; a>=0; a--) { + // Output IIR if present and non-const + if (a < n_iir && a>0 && + !(iir_cbm & (1<<(a<15?a:15)))) { + if (cnt++ < n_coef) *coef++= iir_adj * iir[a]; + } + + // Output FIR if present and non-const + if (a < n_fir && + !(fir_cbm & (1<<(a<15?a:15)))) { + if (cnt++ < n_coef) *coef++= fir[a]; + } + } + } + + if (cnt != n_coef) + error("fid_design_coef called with the wrong number of coefficients.\n" + " Given %d, expecting %d: (\"%s\",%g,%g,%g,%d)", + n_coef, cnt, spec, rate, freq0, freq1, adj); + + free(filt); + return gain; +} + +// +// List all the known filters to the given file handle +// + +void +fid_list_filters(FILE *out) { + int a; + + for (a= 0; filter[a].fmt; a++) { + char buf[4096]; + expand_spec(buf, buf+sizeof(buf), filter[a].fmt); + fprintf(out, "%s\n ", buf); + expand_spec(buf, buf+sizeof(buf), filter[a].txt); + fprintf(out, "%s\n", buf); + } +} + +// +// List all the known filters to the given buffer; the buffer is +// NUL-terminated; returns 1 okay, 0 not enough space +// + +int +fid_list_filters_buf(char *buf, char *bufend) { + int a, cnt; + char tmp[4096]; + + for (a= 0; filter[a].fmt; a++) { + expand_spec(tmp, tmp+sizeof(tmp), filter[a].fmt); + buf += (cnt= snprintf(buf, bufend-buf, "%s\n ", tmp)); + if (cnt < 0 || buf >= bufend) return 0; + expand_spec(tmp, tmp+sizeof(tmp), filter[a].txt); + buf += (cnt= snprintf(buf, bufend-buf, "%s\n", tmp)); + if (cnt < 0 || buf >= bufend) return 0; + } + return 1; +} + +// +// Do a convolution of parameters in place +// + +STATIC_INLINE int +convolve(double *dst, int n_dst, double *src, int n_src) { + int len= n_dst + n_src - 1; + int a, b; + + for (a= len-1; a>=0; a--) { + double val= 0; + for (b= 0; b= 0 && a-b < n_dst) + val += src[b] * dst[a-b]; + dst[a]= val; + } + + return len; +} + +// +// Generate a combined filter -- merge all the IIR/FIR +// sub-filters into a single IIR/FIR pair, and make sure the IIR +// first coefficient is 1.0. +// + +FidFilter * +fid_flatten(FidFilter *filt) { + int m_fir= 1; // Maximum values + int m_iir= 1; + int n_fir, n_iir; // Stored counts during convolution + FidFilter *ff; + FidFilter *rv; + double *fir, *iir; + double adj; + int a; + + // Find the size of the output filter + ff= filt; + while (ff->len) { + if (ff->typ == 'I') + m_iir += ff->len-1; + else if (ff->typ == 'F') + m_fir += ff->len-1; + else + error("fid_flatten doesn't know about type %d", ff->typ); + ff= FFNEXT(ff); + } + + // Setup the output array + rv= FFALLOC(2, m_iir + m_fir); + rv->typ= 'I'; + rv->len= m_iir; + iir= rv->val; + ff= FFNEXT(rv); + ff->typ= 'F'; + ff->len= m_fir; + fir= ff->val; + + iir[0]= 1.0; n_iir= 1; + fir[0]= 1.0; n_fir= 1; + + // Do the convolution + ff= filt; + while (ff->len) { + if (ff->typ == 'I') + n_iir= convolve(iir, n_iir, ff->val, ff->len); + else + n_fir= convolve(fir, n_fir, ff->val, ff->len); + ff= FFNEXT(ff); + } + + // Sanity check + if (n_iir != m_iir || + n_fir != m_fir) + error("Internal error in fid_combine() -- array under/overflow"); + + // Fix iir[0] + adj= 1.0/iir[0]; + for (a= 0; aargarr; + sp->n_arg= 0; + sp->order= 0; + sp->f0= 0; + sp->f1= 0; + sp->adj= 0; + sp->minlen= -1; + sp->n_freq= 0; + + for (a= 0; 1; a++) { + char *fmt= filter[a].fmt; + char *p= sp->spec; + char ch, *q; + + if (!fmt) return strdupf("Spec-string \"%s\" matches no known format", sp->spec); + + while (*p && (ch= *fmt++)) { + if (ch != '#') { + if (ch == *p++) continue; + p= 0; break; + } + + if (isalpha(*p)) { p= 0; break; } + + // Handling a format character + switch (ch= *fmt++) { + default: + return strdupf("Internal error: Unknown format #%c in format: %s", + fmt[-1], filter[a].fmt); + case 'o': + case 'O': + sp->order= (int)strtol(p, &q, 10); + if (p == q) { + if (ch == 'O') goto bad; + sp->order= 1; + } + if (sp->order <= 0) + return strdupf("Bad order %d in spec-string \"%s\"", sp->order, sp->spec); + p= q; break; + case 'V': + sp->n_arg++; + *arg++= strtod(p, &q); + if (p == q) goto bad; + p= q; break; + case 'F': + sp->minlen= p-1-sp->spec; + sp->n_freq= 1; + sp->adj= (p[0] == '='); + if (sp->adj) p++; + sp->f0= strtod(p, &q); + sp->f1= 0; + if (p == q) goto bad; + p= q; break; + case 'R': + sp->minlen= p-1-sp->spec; + sp->n_freq= 2; + sp->adj= (p[0] == '='); + if (sp->adj) p++; + sp->f0= strtod(p, &q); + if (p == q) goto bad; + p= q; + if (*p++ != '-') goto bad; + sp->f1= strtod(p, &q); + if (p == q) goto bad; + if (sp->f0 > sp->f1) + return strdupf("Backwards frequency range in spec-string \"%s\"", sp->spec); + p= q; break; + } + } + + if (p == 0) continue; + + if (fmt[0] == '/' && fmt[1] == '#' && fmt[2] == 'F') { + sp->minlen= p-sp->spec; + sp->n_freq= 1; + if (sp->in_f0 < 0.0) + return strdupf("Frequency omitted from filter-spec, and no default provided"); + sp->f0= sp->in_f0; + sp->f1= 0; + sp->adj= sp->in_adj; + fmt += 3; + } else if (fmt[0] == '/' && fmt[1] == '#' && fmt[2] == 'R') { + sp->minlen= p-sp->spec; + sp->n_freq= 2; + if (sp->in_f0 < 0.0 || sp->in_f1 < 0.0) + return strdupf("Frequency omitted from filter-spec, and no default provided"); + sp->f0= sp->in_f0; + sp->f1= sp->in_f1; + sp->adj= sp->in_adj; + fmt += 3; + } + + // Check for trailing unmatched format characters + if (*fmt) { + bad: + return strdupf("Bad match of spec-string \"%s\" to format \"%s\"", + sp->spec, filter[a].fmt); + } + if (sp->n_arg > MAXARG) + return strdupf("Internal error -- maximum arguments exceeded"); + + // Set the minlen to the whole string if unset + if (sp->minlen < 0) sp->minlen= p-sp->spec; + + // Save values, return + sp->fi= a; + return 0; + } + return 0; +} + + +// +// Parse a filter-spec and freq0/freq1 arguments and rewrite them +// to give an all-in-one filter spec and/or a minimum spec plus +// separate freq0/freq1 arguments. The all-in-one spec is +// returned in *spec1p (strdup'd), and the minimum separated-out +// spec is returned in *spec2p (strdup'd), *freq0p and *freq1p. +// If either of spec1p or spec2p is 0, then that particular +// spec-string is not generated. +// + +void +fid_rewrite_spec(char *spec, double freq0, double freq1, int adj, + char **spec1p, + char **spec2p, double *freq0p, double *freq1p, int *adjp) { + Spec sp; + char *err; + sp.spec= spec; + sp.in_f0= freq0; + sp.in_f1= freq1; + sp.in_adj= adj; + err= parse_spec(&sp); + if (err) error("%s", err); + + if (spec1p) { + char buf[128]; + int len; + char *rv; + switch (sp.n_freq) { + case 1: sprintf(buf, "/%s%.15g", sp.adj ? "=" : "", sp.f0); break; + case 2: sprintf(buf, "/%s%.15g-%.15g", sp.adj ? "=" : "", sp.f0, sp.f1); break; + default: buf[0]= 0; + } + len= strlen(buf); + rv= Alloc(sp.minlen + len + 1); + memcpy(rv, spec, sp.minlen); + strcpy(rv+sp.minlen, buf); + *spec1p= rv; + } + + if (spec2p) { + char *rv= Alloc(sp.minlen + 1); + memcpy(rv, spec, sp.minlen); + *spec2p= rv; + *freq0p= sp.f0; + *freq1p= sp.f1; + *adjp= sp.adj; + } +} + +// +// Create a FidFilter from the given double array. The double[] +// should contain one or more sections, each starting with the +// filter type (either 'I' or 'F', as a double), then a count of +// the number of coefficients following, then the coefficients +// themselves. The end of the list is marked with a type of 0. +// +// This is really just a convenience function, allowing a filter +// to be conveniently dumped to C source code and then +// reconstructed. +// +// Note that for more general filter generation, FidFilter +// instances can be created simply by allocating the memory and +// filling them in (see fidlib.h). +// + +FidFilter * +fid_cv_array(double *arr) { + double *dp; + FidFilter *ff, *rv; + int n_head= 0; + int n_val= 0; + + // Scan through for sizes + for (dp= arr; *dp; ) { + int len, typ; + + typ= (int)(*dp++); + if (typ != 'F' && typ != 'I') + error("Bad type in array passed to fid_cv_array: %g", dp[-1]); + + len= (int)(*dp++); + if (len < 1) + error("Bad length in array passed to fid_cv_array: %g", dp[-1]); + + n_head++; + n_val += len; + dp += len; + } + + rv= ff= Alloc(FFCSIZE(n_head, n_val)); + + // Scan through to fill in FidFilter + for (dp= arr; *dp; ) { + int len, typ; + typ= (int)(*dp++); + len= (int)(*dp++); + + ff->typ= typ; + ff->cbm= ~0; + ff->len= len; + memcpy(ff->val, dp, len * sizeof(double)); + dp += len; + ff= FFNEXT(ff); + } + + // Final element already zero'd thanks to allocation + + return rv; +} + +// +// Create a single filter from the given list of filters in +// order. If 'freeme' is set, then all the listed filters are +// free'd once read; otherwise they are left untouched. The +// newly allocated resultant filter is returned, which should be +// released with free() when finished with. +// + +FidFilter * +fid_cat(int freeme, ...) { + va_list ap; + FidFilter *rv, *ff, *ff0; + int len= 0; + int cnt; + char *dst; + + // Find the memory required to store the combined filter + va_start(ap, freeme); + while ((ff0= va_arg(ap, FidFilter*))) { + for (ff= ff0; ff->typ; ff= FFNEXT(ff)) + ; + len += ((char*)ff) - ((char*)ff0); + } + va_end(ap); + + rv= Alloc(FFCSIZE(0,0) + len); + dst= (char*)rv; + + va_start(ap, freeme); + while ((ff0= va_arg(ap, FidFilter*))) { + for (ff= ff0; ff->typ; ff= FFNEXT(ff)) + ; + cnt= ((char*)ff) - ((char*)ff0); + memcpy(dst, ff0, cnt); + dst += cnt; + if (freeme) free(ff0); + } + va_end(ap); + + // Final element already zero'd + return rv; +} + +// +// Support for fid_parse +// + +// Skip white space (including comments) +static void +skipWS(char **pp) { + char *p= *pp; + + while (*p) { + if (isspace(*p)) { p++; continue; } + if (*p == '#') { + while (*p && *p != '\n') p++; + continue; + } + break; + } + *pp= p; +} + +// Grab a word from the input into the given buffer. Returns 0: end +// of file or error, else 1: success. Error is indicated when the +// word doesn't fit in the buffer. +static int +grabWord(char **pp, char *buf, int buflen) { + char *p, *q; + int len; + + skipWS(pp); + p= *pp; + if (!*p) return 0; + + q= p; + if (*q == ',' || *q == ';' || *q == ')' || *q == ']' || *q == '}') { + q++; + } else { + while (*q && *q != '#' && !isspace(*q) && + (*q != ',' && *q != ';' && *q != ')' && *q != ']' && *q != '}')) + q++; + } + len= q-p; + if (len >= buflen) return 0; + + memcpy(buf, p, len); + buf[len]= 0; + + *pp= q; + return 1; +} + +// +// Parse an entire filter specification, perhaps consisting of +// several FIR, IIR and predefined filters. Stops at the first +// ,; or unmatched )]}. Returns either 0 on success, or else a +// strdup'd error string. +// +// This duplicates code from Fiview filter.c, I know, but this +// may have to expand in the future to handle '+' operations, and +// special filter types like tunable heterodyne filters. At that +// point, the filter.c code will have to be modified to call a +// version of this routine. +// + +char * +fid_parse(double rate, char **pp, FidFilter **ffp) { + char buf[128]; + char *p= *pp, *rew; +#define INIT_LEN 128 + char *rv= Alloc(INIT_LEN); + char *rvend= rv + INIT_LEN; + char *rvp= rv; +#undef INIT_LEN + FidFilter *curr; + int xtra= FFCSIZE(0,0); + int typ= -1; // First time through + double val; + char dmy; + +#define ERR(ptr, msg) { free(rv); *pp= ptr; *ffp= 0; return msg; } + +#if 0 +#define INCBUF { tmp= realloc(rv, (rvend-rv) * 2); if (!tmp) error("Out of memory"); \ + rvend= (rvend-rv) * 2 + tmp; rvp= (rvp-rv) + tmp; \ + curr= (void*)(((char*)curr) - rv + tmp); rv= tmp; } +#else +/* Fix warning: pointer ‘rv’ used after ‘realloc’ [-Wuse-after-free] */ +#define INCBUF { \ + int fsize= rvend - rv; \ + int fpsize= rvp - rv; \ + int fcsize= ((char*)curr) - rv; \ + rv= realloc(rv, fsize * 2); \ + if (!rv) error("Out of memory"); \ + rvend= fsize * 2 + rv; \ + rvp= fpsize + rv; \ + curr= (void*)(fcsize + rv); \ +} +#endif + + while (1) { + rew= p; + if (!grabWord(&p, buf, sizeof(buf))) { + if (*p) ERR(p, strdupf("Filter element unexpectedly long -- syntax error?")); + buf[0]= 0; + } + if (!buf[0] || !buf[1]) switch (buf[0]) { + default: + break; + case 0: + case ',': + case ';': + case ')': + case ']': + case '}': + { + // End of filter, return it + int fpsize = rvp - rv; + rv= realloc(rv, fpsize + xtra); + if (!rv) error("Out of memory"); + curr= (void*)(fpsize + rv); + curr->typ= 0; curr->cbm= 0; curr->len= 0; + *pp= buf[0] ? (p-1) : p; + *ffp= (FidFilter*)rv; + return 0; + } + case '/': + if (typ > 0) ERR(rew, strdupf("Filter syntax error; unexpected '/'")); + typ= 'I'; + continue; + case 'x': + if (typ > 0) ERR(rew, strdupf("Filter syntax error; unexpected 'x'")); + typ= 'F'; + continue; + } + + if (typ < 0) typ= 'F'; // Assume 'x' if missing + if (!typ) ERR(p, strdupf("Expecting a 'x' or '/' before this")); + + if (1 != sscanf(buf, "%lf %c", &val, &dmy)) { + // Must be a predefined filter + FidFilter *ff; + FidFilter *ff1; + Spec sp; + double f0, f1; + char *err; + int len; + + if (typ != 'F') ERR(rew, strdupf("Predefined filters cannot be used with '/'")); + + // Parse the filter-spec + memset(&sp, 0, sizeof(sp)); + sp.spec= buf; + sp.in_f0= sp.in_f1= -1; + if ((err= parse_spec(&sp))) ERR(rew, err); + f0= sp.f0; + f1= sp.f1; + + // Adjust frequencies to range 0-0.5, and check them + f0 /= rate; + if (f0 > 0.5) ERR(rew, strdupf("Frequency of %gHz out of range with " + "sampling rate of %gHz", f0*rate, rate)); + f1 /= rate; + if (f1 > 0.5) ERR(rew, strdupf("Frequency of %gHz out of range with " + "sampling rate of %gHz", f1*rate, rate)); + + // Okay we now have a successful spec-match to filter[sp.fi], and sp.n_arg + // args are now in sp.argarr[] + + // Generate the filter + if (!sp.adj) + ff= filter[sp.fi].rout(rate, f0, f1, sp.order, sp.n_arg, sp.argarr); + else if (strstr(filter[sp.fi].fmt, "#R")) + ff= auto_adjust_dual(&sp, rate, f0, f1); + else + ff= auto_adjust_single(&sp, rate, f0); + + // Append it to our FidFilter to return + for (ff1= ff; ff1->typ; ff1= FFNEXT(ff1)) ; + len= ((char*)ff1-(char*)ff); + while (rvp + len + xtra >= rvend) INCBUF; + memcpy(rvp, ff, len); rvp += len; + free(ff); + typ= 0; + continue; + } + + // Must be a list of coefficients + curr= (void*)rvp; + rvp += xtra; + while (rvp + sizeof(double) >= rvend) INCBUF; + curr->typ= typ; + curr->cbm= ~0; + curr->len= 1; + *(double*)rvp= val; + rvp += sizeof(double); + + // See how many more coefficients we can pick up + while (1) { + rew= p; + if (!grabWord(&p, buf, sizeof(buf))) { + if (*p) ERR(p, strdupf("Filter element unexpectedly long -- syntax error?")); + buf[0]= 0; + } + if (1 != sscanf(buf, "%lf %c", &val, &dmy)) { + p= rew; + break; + } + while (rvp + sizeof(double) >= rvend) INCBUF; + curr->len++; + *(double*)rvp= val; + rvp += sizeof(double); + } + typ= 0; + continue; + } + +#undef INCBUF +#undef ERR + + return strdupf("Internal error, shouldn't reach here"); +} + + +// +// Filter-running code +// + +#ifdef RF_COMBINED +#include "fidrf_combined.h" +#endif + +#ifdef RF_CMDLIST +#include "fidrf_cmdlist.h" +#endif + +#ifdef RF_JIT +#include "fidrf_jit.h" +#endif + + +// END // diff --git a/src/async/audio/fidlib.h b/src/async/audio/fidlib.h new file mode 100644 index 0000000..b66365e --- /dev/null +++ b/src/async/audio/fidlib.h @@ -0,0 +1,74 @@ +// +// fidlib include file +// + +#ifndef FIDLIB_INCLUDED +#define FIDLIB_INCLUDED + + +typedef struct FidFilter FidFilter; +struct FidFilter { + short typ; // Type of filter element 'I' IIR, 'F' FIR, or 0 for end of list + short cbm; // Constant bitmap. Bits 0..14, if set, indicate that val[0..14] + // is a constant across changes in frequency for this filter type + // Bit 15, if set, indicates that val[15..inf] are constant. + int len; // Number of doubles stored in val[], or 0 for end of list + double val[1]; +}; + +// Lets you write: for (; ff->typ; ff= FFNEXT(ff)) { ... } +#define FFNEXT(ff) ((FidFilter*)((ff)->val + (ff)->len)) + +// Size of a sub-filter with 'cnt' double values attached +#define FFSIZE(cnt) (sizeof(FidFilter) + ((cnt)-1)*sizeof(double)) + +// Size required for the memory chunk to contain the given number +// headers and values, plus termination +#define FFCSIZE(n_head,n_val) ((sizeof(FidFilter)-sizeof(double))*((n_head)+1) + sizeof(double)*(n_val)) + +// Allocate the chunk of memory to hold a list of FidFilters, with +// n_head FidFilters and n_val total double values in the whole list. +// Includes space for the final termination, and zeros the memory. +#define FFALLOC(n_head,n_val) (FidFilter*)Alloc(FFCSIZE(n_head, n_val)) + +// These are so you can use easier names to refer to running filters +typedef void FidRun; +typedef double (FidFunc)(void*, double); + + +// +// Prototypes +// + +extern void fid_set_error_handler(void(*rout)(char *)); +extern char *fid_version(void); +extern double fid_response_pha(FidFilter *filt, double freq, double *phase); +extern double fid_response(FidFilter *filt, double freq); +extern int fid_calc_delay(FidFilter *filt); +extern FidFilter *fid_design(char *spec, double rate, double freq0, double freq1, + int f_adj, char **descp); +extern double fid_design_coef(double *coef, int n_coef, char *spec, + double rate, double freq0, double freq1, int adj); +extern void fid_list_filters(FILE *out); +extern int fid_list_filters_buf(char *buf, char *bufend); +extern FidFilter *fid_flatten(FidFilter *filt); +extern void fid_rewrite_spec(char *spec, double freq0, double freq1, int adj, + char **spec1p, char **spec2p, + double *freq0p, double *freq1p, int *adjp); +extern FidFilter *fid_cv_array(double *arr); +extern FidFilter *fid_cat(int freeme, ...); +extern char *fid_parse(double rate, char **pp, FidFilter **ffp); + +// +// Filter running prototypes +// + +extern void *fid_run_new(FidFilter *filt, double(**funcpp)(void *, double)); +extern void *fid_run_newbuf(void *run); +extern int fid_run_bufsize(void *run); +extern void fid_run_initbuf(void *run, void *buf); +extern void fid_run_zapbuf(void *buf); +extern void fid_run_freebuf(void *runbuf); +extern void fid_run_free(void *run); + +#endif /* FIDLIB_INCLUDED */ diff --git a/src/async/audio/fidlib.txt b/src/async/audio/fidlib.txt new file mode 100644 index 0000000..bc1e3a4 --- /dev/null +++ b/src/async/audio/fidlib.txt @@ -0,0 +1,642 @@ +Fidlib: Run-time filter design and execution library +---------------------------------------------------- + + Copyright (c) 2002-2004 Jim Peters . This library + is released under the GNU Lesser General Public License (LGPL) version + 2.1 as published by the Free Software Foundation. See the file + COPYING_LIB, or visit . + + "This library is distributed in the hope that it will be useful, + but WITHOUT ANY WARRANTY; without even the implied warranty of + MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE." + +This library was originally designed as a backend for the 'Fiview' +application, and to provide performance filtering services to EEG +applications, such as those in the OpenEEG project: + + http://uazu.net/fiview/ + http://openeeg.sf.net/ + +See Fiview for a more interactive introduction to the filters, more +related documentation, and for the opportunity to generate even higher +performing filters through generating C code for a single class of +filters, compiling it, and at run-time filling in the coefficients +using fidlib. + +Jim + +-- + Jim Peters (_)/=\~/_(_) jim@uazu.net + (_) /=\ ~/_ (_) + Uazú (_) /=\ ~/_ (_) http:// + Brighton, UK (_) ____ /=\ ____ ~/_ ____ (_) uazu.net + +------------------------------------------------------------------------ + +0. Contents +=========== + +1. General notes +1.1 Building +1.2 Fidlib predefined filter specifications +1.3 Fidlib extended filter specification + +2. Fidlib C API +2.1 Housekeeping routines +2.2 Filter creation +2.3 Operations on filters +2.4 Running the filter + +3. Predefined filter notes +3.1 Filters from Dr Tony Fisher's "mkfilter" +3.2 Filters from Robert Bristow-Johnson's "audio EQ cookbook" +3.3 Miscellaneous filters + + +------------------------------------------------------------------------ + +1. General notes +================ + +1.1 Building +------------ + +To include this library in your application, build fidlib.c along with +your other C files. The fidlib.c file #includes all the other source +files it needs. You will also need to #include "fidlib.h" in your +source files to get the structure and function declarations. + +There are three choices for the filter engine, but only one of these +(the default) is actually useful. The 'combined' option can be slower +and is unstable for larger filters (this is the equivalent to the +original 'mkfilter' method) and the JIT compiler option is no longer +maintained as it was only slightly faster than the 'cmdlist' option. +This leaves 'cmdlist' as the recommended and default option. The +'cmdlist' option should be portable to other processors as well (tested +on ix86 and PowerPC), which the JIT option certainly wasn't. + +Target-specific fixes are selected according to which of the T_* +macros is defined at build-time. See the source. Targets defined so +far are: + + T_LINUX + T_MINGW + T_MSVC + + +1.2 Fidlib predefined filter specifications +------------------------------------------- + +The filter specification string can be used to completely specify a +predefined filter, or for fid_design() it can also be used with the +frequency or frequency range missing, in which case default values are +picked up from values passed directly to the routine. + +The spec consists of a series of letters usually followed by the order +of the filter and then by any other parameters required, preceded by +slashes. For example: + + LpBu4/20.4 Lowpass butterworth, 4th order, -3.01dB at 20.4Hz + BpBu2/3-4 Bandpass butterworth, 2nd order, from 3 to 4Hz + BpBu2/=3-4 Same filter, but adjusted exactly to the range given + BsRe/1000/10 Bandstop resonator, Q=1000, frequency 10Hz + +The routines fid_design() or fid_parse() are used to convert this +spec-string into filter coefficients and a description (if required). + +The list of available filters can be generated with 'firun -L'. For +more details of the predefined filter types, see section 3 of this +document. + +Note that filter frequencies should be specified in the same units as +the sampling rate, i.e. normally in Hz. However, all filters work +internally with the ratio of freq/sampling_rate, so you can use a +sampling rate of 1.0 and frequency values of 0 to 0.5 (Nyquist) if you +prefer. + +The auto-adjust option, selected by prefixing the frequency or frequency +range with '=', automatically adjusts the -3.01dB points to the given +frequencies with a kind of binary search. See Fiview for more details +on this. This might be useful with some lower-order filters where the +-3.01dB points don't come out quite right otherwise. + + +1.3 Fidlib extended filter specification +---------------------------------------- + +With fid_parse() and the 'fiview' and 'firun' tools it is possible to +define filters that consist of chains of predefined filters and/or raw +IIR/FIR filters. It is perhaps easier to demonstrate this with +examples before explaining the format: + +Just using coefficients: + + 0.9972 / -1 1.9955 -0.9971 x 1 -1.9985 1 / -1 1.9959 -0.9973 x 1 -1.9985 1 + +or using a predefined filter types: + + LpBe3/0.05 + + BpBu5/=0.05-0.08 + +or using combinations: + + 0.9972 / -1 1.9955 -0.9971 x 1 -1.9985 1 x LpBe3/0.05 + + LpBe3/0.05 x LpBe5/0.03 + + BsBu10/139-147 x HsBq/0.8/-15/12000 + +When a filter is given in terms of FIR/IIR coefficients, use 'x' to +start an FIR list, and '/' to start an IIR list. The first list is +assumed to be an FIR list if no 'x' or '/' is included. The order of +coefficients compared to time zero is closest first to farthest last. +So 'x 0 0 1' would be a delay of two samples. + +To make this clearer, the filter "x A B C" corresponds to: + + y[n] == A.x[n] + B.x[n-1] + C.x[n-2] + +and "/ D E F" corresponds to: + + D.y[n] + E.y[n-1] + F.y[n-2] == x[n] + +where y[] is the output, and x[] is the input. This is the most +symmetrical way of expressing the filters, although obviously the IIR +expression has to be rearranged for computation. + +It is good to keep IIR/FIR filters in pairs because they can share +buffers that way, allowing them to run faster. For examples, see the +output of the predefined filters in Fiview or with 'firun -D'. + + +------------------------------------------------------------------------ + +2. Fidlib C API +=============== + +2.1 Housekeeping routines +------------------------- + +str= fid_version(); + + Return a static string containing the fidlib version number, for + example "0.9.9". + + +void my_error_func(char *err) { ... }; +fid_set_error_handler(&my_error_func); + + Setup a routine that will be called if there is a fatal error in + fidlib. The string passed to the error function describes the + error. You can handle the error in your own way (e.g. doing a + longjmp() or whatever). If your routine returns, then fidlib will + follow its default fatal error procedure, i.e. dump the error to + 'stderr' and call exit(1). + + +fid_list_filters(stdout); +okay= fid_list_filters_buf(buf, buf+sizeof(buf)); + + List all the known filter types to either a FILE* or to a buffer. + 'okay' is true if the text fitted inside the buffer provided. This + is the output of the 'firun -L' command. + + +2.2 Filter creation +------------------- + +Filters are generated from specifications of various types, resulting +in a FidFilter object in memory. This should be free()d when no +longer required. + + +char *p; +FidFilter *ff; +double rate; +char *err; + +err= fid_parse(rate, &p, &ff); + + This is the most general-purpose filter-creation routine. It parses + an extended filter specification, possibly containing a chain of + several predefined and/or directly-specified FIR/IIR filters. It + stops at the first comma, semicolon, or unmatched closing ")]}" + brace. (This allows filter specifications to be used as part of a + higher level specification or configuration language). 'rate' is + the sampling rate. 'p' is the pointer to data to parse, and is + updated to point to the next character on successful return. 'ff' + points to the new FidFilter on successful return, which should be + free()d once finished with. On success, the 'err' return is zero. + Otherwise 'err' contains a strdup'd error string describing the + problem with the filter spec, which should be free()d once reported. + + +char *spec; +double rate, freq0, freq1; +int adj; +char *desc; + +filt= fid_design(spec, rate, freq0, freq1, adj, &desc); + + This routine may be changed/removed in a future version (it is here + to support Fiview at the moment). + + Create a FidFilter based on a single predefined filter, specifying + sampling rate and default freq0/1 parameters (pass -ve values if + there is no default) and adjust flag 'adj'. Returns a strdup'd + description of the filter in 'desc' if a non-zero pointer is passed. + This routine drops dead with a fatal error if there is any problem + at all with the filter spec. + + +#define N_COEF +double coef[N_COEF], gain; + +gain= fid_design_coef(coef, N_COEF, spec, rate, freq0, freq1, adj); + + Design a filter and dump out all the non-constant coefficients to + the given array. This is a support routine for the code generated + by Fiview. It allows a class of filters to be generated at + run-time, but using a hard-coded (Fiview-generated) filter + processing routine to run them, for maximum speed. The generated + code picks up all the non-constant coefficients from the coef[] + array filled in by this routine. + + +double arr[]= { ... }; +filt= fid_cv_array(arr); + + Generate a filter from a list of IIR and FIR coefficients in the + given double-array. Each sub-list of coefficients should take the + form: + + 'I' or 'F', , , , ... + + Use 'I' for a list of IIR coefficients ('/ A B C ...') or 'F' for a + list for FIR coefficients ('x A B C ...'). Many 'I' or 'F' + sub-lists may be chained, and the end of the double[] list is marked + with a 0.0. + + The purpose of this routine is to allow pre-prepared filters to be + included conveniently in C source as a double array. The returned + filter should be free()d once finished with. + + +char *spec, *fullspec, *minspec; +double freq0, freq1, minfreq0, minfreq1; +int minadj; + +fid_rewrite_spec(spec, freq0, freq1, adj, + &fullspec, &minspec, &minfreq0, &minfreq1, &minadj); +free(fullspec); +free(minspec); + + This is a support routine for Fiview, and may be adjusted in future. + + It helps with handling incomplete filter-specs, merging in default + values and returning the result as both a fully-specified filter + 'fullspec', and also as a minimally-specified filter and associated + default values: 'minspec' plus 'minfreq0', 'minfreq1', 'minadj'. + + +2.3 Operations on filters +------------------------- + +filt= fid_cat(freeme, filt1, filt2, filt3, ..., NULL); + + Merge a list of existing FidFilters into one FidFilter by chaining + them serially. If 'freeme' is non-0, the original filters + 'filt1'/etc are free()d once read. The resulting filter should be + free()d once finished with. + + +filt2= fid_flatten(filt); + + Flatten a filter, merging all component FIRs into one FIR, and all + component IIRs into one IIR. The resulting filter may run slightly + faster, but it will suffer from lower accuracy, and will be unstable + for higher-order filters. This is the equivalent of the original + 'mkfilter' filter-running code. Really it is better to leave the + filter unflattened. The new filter returned should be free()d when + finished with. The original filter 'filt' is untouched. + + +double resp, freq, phase; +resp= fid_response(filt, freq); +resp= fid_response_pha(filt, freq, &phase); + + Calculate the response of the filter at the given frequency. Note + that the frequency should be given as (freq/rate) in the range 0 to + 0.5, i.e. expressed as a proportion of the sampling rate, from 0 to + the Nyquist frequency. The response at that frequency is returned. + In the case of fid_response_pha(), the phase is also returned in + 'phase'. The phase is scaled to the range 0 to 1, for 0 to two-PI. + + +int delay; +delay= fid_calc_delay(filt); + + Calculate the approximate signal delay that the filter introduces, + in samples. This searches the impulse response for the time at + which 50% of the weighted filter calculations are complete. Delays + longer than about 8,000,000 samples are not reported accurately. + + +2.4 Running the filter +---------------------- + +Fidlib's filter-running code is designed to go as fast as possible in a +general-purpose way. Two approaches were attempted, a JIT-compiler +approach, based on writing ix86 instructions to memory for the filter +core, and a command-list method, switching between fragments of +pre-generated code. The JIT version only gave a 20% advantage and was +non-portable, so it was abandoned. The command-list version has been +tested on ix86 and PowerPC and should be portable anywhere. For really +time-critical applications, though, to gain maximum speed for a +particular filter type, Fiview can be used to generate a special-purpose +routine. + +To run a filter with 'fidlib' requires setting up a filter-run +instance and a buffer for each channel you wish to run through that +filter. A function pointer is returned that should be used to process +each input sample through its buffer to produce an output sample. + +Here is an illustration of typical code: + + FidRun *run; + FidFunc *funcp; + void *fbuf1, *fbuf2; + + run= fid_run_new(filt, &funcp); + fbuf1= fid_run_newbuf(run); + fbuf2= fid_run_newbuf(run); + while (...) { + out_1= funcp(fbuf1, in_1); + out_2= funcp(fbuf2, in_2); + if (restart_required) fid_run_zapbuf(fbuf1); + ... + } + fid_run_freebuf(fbuf2); + fid_run_freebuf(fbuf1); + fid_run_free(run); + +If you are running hundreds of filters in parallel, you may prefer to +allocate your own buffer memory separately (e.g. as a large block). +In which case, use it in this way (assuming malloc() doesn't fail): + + FidRun *run; + FidFunc *funcp; + int len; + void *fbuf1, *fbuf2; + + run= fid_run_new(filt, &funcp); + len= fid_run_bufsize(run); + fbuf1= malloc(len); fid_run_initbuf(run, fbuf1); + fbuf2= malloc(len); fid_run_initbuf(run, fbuf2); + while (...) { + out_1= funcp(fbuf1, in_1); + out_2= funcp(fbuf2, in_2); + if (restart_required) fid_run_zapbuf(fbuf1); + ... + } + free(fbuf2); + free(fbuf1); + fid_run_free(run); + +------------------------------------------------------------------------ + + +3. Predefined filter notes +========================== + +The full list of filters as of fidlib 0.9.9 is as follows. This list +can also be generated with the fid_list_filters() call and the 'firun +-L' command: + +BpRe// + Bandpass resonator, Q= (0 means Inf), frequency +BsRe// + Bandstop resonator, Q= (0 means Inf), frequency +ApRe// + Allpass resonator, Q= (0 means Inf), frequency +Pi/ + Proportional-integral filter, frequency +PiZ/ + Proportional-integral filter, matched z-transform, frequency +LpBe/ + Lowpass Bessel filter, order , -3.01dB frequency +HpBe/ + Highpass Bessel filter, order , -3.01dB frequency +BpBe/ + Bandpass Bessel filter, order , -3.01dB frequencies +BsBe/ + Bandstop Bessel filter, order , -3.01dB frequencies +LpBu/ + Lowpass Butterworth filter, order , -3.01dB frequency +HpBu/ + Highpass Butterworth filter, order , -3.01dB frequency +BpBu/ + Bandpass Butterworth filter, order , -3.01dB frequencies +BsBu/ + Bandstop Butterworth filter, order , -3.01dB frequencies +LpCh// + Lowpass Chebyshev filter, order , passband ripple dB, + -3.01dB frequency +HpCh// + Highpass Chebyshev filter, order , passband ripple dB, + -3.01dB frequency +BpCh// + Bandpass Chebyshev filter, order , passband ripple dB, + -3.01dB frequencies +BsCh// + Bandstop Chebyshev filter, order , passband ripple dB, + -3.01dB frequencies +LpBeZ/ + Lowpass Bessel filter, matched z-transform, order , -3.01dB + frequency +HpBeZ/ + Highpass Bessel filter, matched z-transform, order , -3.01dB + frequency +BpBeZ/ + Bandpass Bessel filter, matched z-transform, order , -3.01dB + frequencies +BsBeZ/ + Bandstop Bessel filter, matched z-transform, order , -3.01dB + frequencies +LpBuZ/ + Lowpass Butterworth filter, matched z-transform, order , -3.01dB + frequency +HpBuZ/ + Highpass Butterworth filter, matched z-transform, order , -3.01dB + frequency +BpBuZ/ + Bandpass Butterworth filter, matched z-transform, order , -3.01dB + frequencies +BsBuZ/ + Bandstop Butterworth filter, matched z-transform, order , -3.01dB + frequencies +LpChZ// + Lowpass Chebyshev filter, matched z-transform, order , passband + ripple dB, -3.01dB frequency +HpChZ// + Highpass Chebyshev filter, matched z-transform, order , passband + ripple dB, -3.01dB frequency +BpChZ// + Bandpass Chebyshev filter, matched z-transform, order , passband + ripple dB, -3.01dB frequencies +BsChZ// + Bandstop Chebyshev filter, matched z-transform, order , passband + ripple dB, -3.01dB frequencies +LpBuBe// + Lowpass Butterworth-Bessel % cross, order , -3.01dB + frequency +LpBq// + Lowpass biquad filter, order , Q=, -3.01dB frequency +HpBq// + Highpass biquad filter, order , Q=, -3.01dB frequency + +BpBq// + Bandpass biquad filter, order , Q=, centre frequency +BsBq// + Bandstop biquad filter, order , Q=, centre frequency +ApBq// + Allpass biquad filter, order , Q=, centre frequency +PkBq/// + Peaking biquad filter, order , Q=, dBgain=, + frequency +LsBq/// + Lowpass shelving biquad filter, S=, dBgain=, frequency + +HsBq/// + Highpass shelving biquad filter, S=, dBgain=, frequency + +LpBl/ + Lowpass Blackman window, -3.01dB frequency +LpHm/ + Lowpass Hamming window, -3.01dB frequency +LpHn/ + Lowpass Hann window, -3.01dB frequency +LpBa/ + Lowpass Bartlet (triangular) window, -3.01dB frequency + + +3.1 Filters from Dr Tony Fisher's "mkfilter" +-------------------------------------------- + +See fidmkf.h for details of the derivation of these filters from Dr +Fisher's code. Tony Fisher died in Feb-2000, aged 43, but his +mkfilter code is still available and in use on his site. See his +pages below: + + http://www-users.cs.york.ac.uk/~fisher/ + http://www-users.cs.york.ac.uk/~fisher/tribute.html + http://www-users.cs.york.ac.uk/~fisher/mkfilter/ + +Bessel: This IIR filter gives a nice domed response in frequency, and + also a nice domed response in time. This is something approaching + the ideal balance between time-locality and frequency-locality for + an IIR filter. + +Butterworth: This IIR filter gives flat responses away from the + transition areas and sharp transitions, especially as the order + increases. However, this sharp frequency response is paid for with + a wider and wider time-response. In other words it gives sharply + defined frequency-locality, but less well-defined time-locality. + +Chebychev: This IIR filter exchanges some of the flatness in the + passband of the Butterworth filter for a sharper fall-off at the + cut-off frequency. The resulting filter also has a less + well-defined time-locality (see the time response). The amount of + permitted passband ripple is specified in dB, and should be + negative, for example: "LpCh4/-0.1/100" or "LpCh4/-1/100". + +All of these types come also in a matched-Z version (LpBeZ,HpBuZ,etc) +which uses the matched-Z transform instead of the bilinear transform. +This eliminates the FIR part of the filter for low-pass filters, making +them pure IIR. You really need to read what Tony Fisher has to say +about this on his site to understand whether to use it or not. Briefly +he suggests this option is useful for generating Bessel filters with +optimally linear phase response, or for generating filters that execute +slightly faster, but the bilinear transform is otherwise superior. + +Resonators: These IIR filters are based on oscillators, equivalent to + a tuned circuit. The gives the narrowness of the resonator + response. Low values give wider response and faster decay to the + oscillations. Infinity can be represented using a value of '0', + which gives a pure oscillator -- for example: "BpRe/0/0.1". The + all-pass version (ApRe) has unit response, but the phase varies with + frequency, changing rapidly around the resonant frequency. These + are all from mkfilter -- Tony Fisher there suggests that band-stop + and band-pass filters made with resonators are often more efficient + and better-behaved than Butterworth filters. See the mkfilter docs + for more info on all of this. + +Proportional-integral: I don't know what these are for, and they're + not documented on the mkfilter site either. The IIR part, at least, + just totals up all the samples received (an 'integral'). I guess + you'll know if you want this. This is probably the digital version + of some analogue op-amp technique. Also note that they don't + display at all well in either mkfilter or Fiview! + +"LpBuBe" is an experiment of mine, producing a low-pass filter that is a +percentage cross between a Butterworth and a Bessel filter. Whether +this has any practical signal-processing use, I'm not sure. + + +3.2 Filters from Robert Bristow-Johnson's "audio EQ cookbook" +------------------------------------------------------------- + +These filters are designed for manipulating audio -- they provide +peaking and shelving filters in addition to more conventional +low-pass/high-pass filters. The filters are documented here: + + http://www.harmony-central.com/Computer/Programming/Audio-EQ-Cookbook.txt + +Although an order may be specified for these filters, it is optional. +Without an order, or with order == 1, the original filters from the +document are reproduced. If you specify an order N, then N identical +biquad filters are placed in series. (Actually the order is really 2N, +as a the base biquad filters are 2nd order.) + +Biquad low-pass and high-pass filters: For a low-pass filter, the + significant Q values are Q=0.5, which is the maximum value to give no + overshoot in the time response, and Q=0.707, which gives the most + flat-topped frequency response without having a resonance 'lip'. + +Biquad band-pass, band-stop, all-pass and peaking filters: For these, + larger Q values are good -- try up to Q=10 or more. The peaking + filter is designed to emphasise a frequency range. The second value + in this case is the peak gain in dB. For example: PkBq1/3/10/0.1 + +Biquad audio shelving filters: These use a 'shelf slope' parameter + instead of Q. S=1 gives the steepest slope without a lip, and lower + values give lower values of slope. The second parameter is the gain + in the affected area in dB. + + +3.3 Miscellaneous filters +------------------------- + +Window functions: Bartlett, Hann, Hamming and Blackman window functions are + available here. These act as FIR low-pass filters. To understand these + filters better, it is good to try them in Fiview with a log display: type + 'L5' and adjust to view the full frequency range. Also compare these + filters to Bessel IIR filters (e.g. LpBe6). + +Note that fidlib doesn't provide a huge comprehensive list of filter +types, just the ones that seemed useful to add, mostly oriented towards +IIR filters, and building on Dr Tony Fisher's 'mkfilter' as a base. You +can construct other types of filters in coefficient form and run them +through fidlib directly using a call to fid_cv_array(). Other filters +may be added in future. + +Note that for pure FIR filters, especially longer ones, fidlib may be +useful for calculating their response, however they may be better +processed using other code. For example, you might not need to apply +the FIR on every sample, or maybe you can use an FFT-based +acceleration of a convolution instead of calculating it the slow way, +sample by sample. + + +------------------------------------------------------------------------ diff --git a/src/async/audio/fidmkf.h b/src/async/audio/fidmkf.h new file mode 100644 index 0000000..9f7adb1 --- /dev/null +++ b/src/async/audio/fidmkf.h @@ -0,0 +1,831 @@ +// +// mkfilter-derived code +// --------------------- +// +// Copyright (c) 2002-2004 Jim Peters . This +// file is released under the GNU Lesser General Public License +// (LGPL) version 2.1 as published by the Free Software +// Foundation. See the file COPYING_LIB for details, or visit +// . +// +// This is all code derived from 'mkfilter' by Tony Fisher of the +// University of York. I've rewritten it all in C, and given it +// a thorough overhaul, so there is actually none of his code +// here any more, but it is all still very strongly based on the +// algorithms and techniques that he used in 'mkfilter'. +// +// For those who didn't hear, Tony Fisher died in February 2000 +// at the age of 43. See his web-site for information and a +// tribute: +// +// http://www-users.cs.york.ac.uk/~fisher/ +// http://www-users.cs.york.ac.uk/~fisher/tribute.html +// +// The original C++ sources and the rest of the mkfilter tool-set +// are still available from his site: +// +// http://www-users.cs.york.ac.uk/~fisher/mkfilter/ +// +// +// I've made a number of improvements and changes whilst +// rewriting the code in C. For example, I halved the +// calculations required in designing the filters by storing only +// one complex pole/zero out of each conjugate pair. This also +// made it much easier to output the filter as a list of +// sub-filters without lots of searching around to match up +// conjugate pairs later on. Outputting as a list of subfilters +// permits greater accuracy in calculation of the response, and +// also in the execution of the final filter. Also, some FIR +// coefficients can be marked as 'constant', allowing optimised +// routines to be generated for whole classes of filters, with +// just the variable coefficients filled in at run-time. +// +// On the down-side, complex numbers are not portably available +// in C before C99, so complex calculations here are done on +// double[] arrays with inline functions, which ends up looking +// more like assembly language than C. Never mind. +// + +// +// LEGAL STUFF +// ----------- +// +// Tony Fisher released his software on his University of York +// pages for free use and free download. The software itself has +// no licence terms attached, nor copyright messages, just the +// author's name, E-mail address and date. Nor are there any +// licence terms indicated on the website. I understand that +// under the Berne convention copyright does not have to be +// claimed explicitly, so these are in fact copyright files by +// legal default. However, the intention was obviously that +// these files should be used by others. +// +// None of this really helps, though, if we're going to try to be +// 100% legally correct, so I wrote to Anthony Moulds who is the +// contact name on Tony Fisher's pages now. I explained what I +// planned to do with the code, and he answered as follows: +// +// (Note that I was planning to use it 'as-is' at that time, +// rather than rewrite it as I have done now) +// +// > To: "Jim Peters" +// > From: "Anthony Moulds" +// > Subject: RE: mkfilter source +// > Date: Tue, 29 Oct 2002 15:30:19 -0000 +// > +// > Hi Jim, +// > +// > Thanks for your email. +// > +// > The University will be happy to let you use Dr Fisher's mkfilter +// > code since your intention is not to profit financially from his work. +// > +// > It would be nice if in some way you could acknowledge his contribution. +// > +// > Best wishes and good luck with your work, +// > +// > Anthony Moulds +// > Senior Experimental Officer, +// > Computer Science Department, University of York, +// > York, England, UK. Tel: 44(0)1904 434758 Fax: 44(0)19042767 +// > ============================================================ +// > +// > +// > > -----Original Message----- +// > > From: Jim Peters [mailto:jim@uazu.net] +// > > Sent: Monday, October 28, 2002 12:36 PM +// > > To: anthony@cs.york.ac.uk +// > > Subject: mkfilter source +// > > +// > > +// > > I'm very sorry to hear (rather late, I know) that Tony Fisher died -- +// > > I've always gone straight to the filter page, rather than through his +// > > home page. I hope his work remains available for the future. +// > > +// > > Anyway, the reason I'm writing is to clarify the status of the +// > > mkfilter source code. Because copyright is not claimed on the web +// > > page nor in the source distribution, I guess that Tony's intention was +// > > that this code should be in the public domain. However, I would like +// > > to check this now to avoid complications later. +// > > +// > > I am using his code, modified, to provide a library of filter-design +// > > routines for a GPL'd filter design app, which is not yet released. +// > > The library could also be used standalone, permitting apps to design +// > > filters at run-time rather than using hard-coded compile-time filters. +// > > My interest in filters is as a part of my work on the OpenEEG project +// +// So this looks pretty clear to me. I am not planning to profit +// from the work, so everything is fine with the University. I +// guess others might profit from the work, indirectly, as with +// any free software release, but so long as I don't, we're fine. +// +// I hope this is watertight enough for Debian/etc. Otherwise +// I'll have to go back to Anthony Moulds for clarification. +// +// Even though there is no code cut-and-pasted from 'mkfilter' +// here, it is all very obviously based on that code, so it +// probably counts as a derived work -- although as ever "I Am +// Not A Lawyer". +// + + +#ifdef HUGE_VAL + #define INF HUGE_VAL +#else + #define INF (1.0/0.0) +#endif + +#define TWOPI (2*M_PI) + + +// +// Complex square root: aa= aa^0.5 +// + +STATIC_INLINE double +my_sqrt(double aa) { + return aa <= 0.0 ? 0.0 : sqrt(aa); +} + +STATIC_INLINE void +my_csqrt(double *aa) { + double mag= hypot(aa[0], aa[1]); + double rr= my_sqrt((mag + aa[0]) * 0.5); + double ii= my_sqrt((mag - aa[0]) * 0.5); + if (aa[1] < 0.0) ii= -ii; + aa[0]= rr; + aa[1]= ii; +} + +// +// Complex imaginary exponent: aa= e^i.theta +// + +STATIC_INLINE void +cexpj(double *aa, double theta) { + aa[0]= cos(theta); + aa[1]= sin(theta); +} + +// +// Complex exponent: aa= e^aa +// + +STATIC_INLINE void +my_cexp(double *aa) { + double mag= exp(aa[0]); + aa[0]= mag * cos(aa[1]); + aa[1]= mag * sin(aa[1]); +} + +// +// Global temp buffer for generating filters. *NOT THREAD SAFE* +// +// Note that the poles and zeros are stored in a strange way. +// Rather than storing both a pole (or zero) and its complex +// conjugate, I'm storing just one of the pair. Also, for real +// poles, I'm not storing the imaginary part (which is zero). +// This results in a list of numbers exactly half the length you +// might otherwise expect. However, since some of these numbers +// are in pairs, and some are single, we need a separate flag +// array to indicate which is which. poltyp[] serves this +// purpose. An entry is 1 if the corresponding offset is a real +// pole, or 2 if it is the first of a pair of values making up a +// complex pole. The second value of the pair has an entry of 0 +// attached. (Similarly for zeros in zertyp[]) +// + +#define MAXPZ 64 + +static int n_pol; // Number of poles +static double pol[MAXPZ]; // Pole values (see above) +static char poltyp[MAXPZ]; // Pole value types: 1 real, 2 first of complex pair, 0 second +static int n_zer; // Same for zeros ... +static double zer[MAXPZ]; +static char zertyp[MAXPZ]; + + +// +// Pre-warp a frequency +// + +STATIC_INLINE double +prewarp(double val) { + return tan(val * M_PI) / M_PI; +} + + +// +// Bessel poles; final one is a real value for odd numbers of +// poles +// + +static double bessel_1[]= { + -1.00000000000e+00 +}; + +static double bessel_2[]= { + -1.10160133059e+00, 6.36009824757e-01, +}; + +static double bessel_3[]= { + -1.04740916101e+00, 9.99264436281e-01, + -1.32267579991e+00, +}; + +static double bessel_4[]= { + -9.95208764350e-01, 1.25710573945e+00, + -1.37006783055e+00, 4.10249717494e-01, +}; + +static double bessel_5[]= { + -9.57676548563e-01, 1.47112432073e+00, + -1.38087732586e+00, 7.17909587627e-01, + -1.50231627145e+00, +}; + +static double bessel_6[]= { + -9.30656522947e-01, 1.66186326894e+00, + -1.38185809760e+00, 9.71471890712e-01, + -1.57149040362e+00, 3.20896374221e-01, +}; + +static double bessel_7[]= { + -9.09867780623e-01, 1.83645135304e+00, + -1.37890321680e+00, 1.19156677780e+00, + -1.61203876622e+00, 5.89244506931e-01, + -1.68436817927e+00, +}; + +static double bessel_8[]= { + -8.92869718847e-01, 1.99832584364e+00, + -1.37384121764e+00, 1.38835657588e+00, + -1.63693941813e+00, 8.22795625139e-01, + -1.75740840040e+00, 2.72867575103e-01, +}; + +static double bessel_9[]= { + -8.78399276161e-01, 2.14980052431e+00, + -1.36758830979e+00, 1.56773371224e+00, + -1.65239648458e+00, 1.03138956698e+00, + -1.80717053496e+00, 5.12383730575e-01, + -1.85660050123e+00, +}; + +static double bessel_10[]= { + -8.65756901707e-01, 2.29260483098e+00, + -1.36069227838e+00, 1.73350574267e+00, + -1.66181024140e+00, 1.22110021857e+00, + -1.84219624443e+00, 7.27257597722e-01, + -1.92761969145e+00, 2.41623471082e-01, +}; + +static double *bessel_poles[10]= { + bessel_1, bessel_2, bessel_3, bessel_4, bessel_5, + bessel_6, bessel_7, bessel_8, bessel_9, bessel_10 +}; + +// +// Generate Bessel poles for the given order. +// + +static void +bessel(int order) { + int a; + + if (order > 10) error("Maximum Bessel order is 10"); + n_pol= order; + memcpy(pol, bessel_poles[order-1], n_pol * sizeof(double)); + + for (a= 0; a MAXPZ) + error("Maximum butterworth/chebyshev order is %d", MAXPZ); + n_pol= order; + for (a= 0; a= 0.0) error("Chebyshev ripple in dB should be -ve"); + + eps= sqrt(-1.0 + pow(10.0, -0.1 * ripple)); + y= asinh(1.0 / eps) / order; + if (y <= 0.0) error("Internal error; chebyshev y-value <= 0.0: %g", y); + sh= sinh(y); + ch= cosh(y); + + for (a= 0; a MAXPZ) + error("Maximum order for bandpass filters is %d", MAXPZ/2); + + // Run through the list backwards, expanding as we go + for (a= n_pol, b= n_pol*2; a>0; ) { + // hba= pole * bw; + // temp= my_csqrt(1.0 - square(w0 / hba)); + // pole1= hba * (1.0 + temp); + // pole2= hba * (1.0 - temp); + + if (poltyp[a-1] == 1) { + double hba; + a--; b -= 2; + poltyp[b]= 2; poltyp[b+1]= 0; + hba= pol[a] * bw; + cassz(pol+b, 1.0 - (w0 / hba) * (w0 / hba), 0.0); + my_csqrt(pol+b); + caddz(pol+b, 1.0, 0.0); + cmulr(pol+b, hba); + } else { // Assume poltyp[] data is valid + double hba[2]; + a -= 2; b -= 4; + poltyp[b]= 2; poltyp[b+1]= 0; + poltyp[b+2]= 2; poltyp[b+3]= 0; + cass(hba, pol+a); + cmulr(hba, bw); + cass(pol+b, hba); + crecip(pol+b); + cmulr(pol+b, w0); + csqu(pol+b); + cneg(pol+b); + caddz(pol+b, 1.0, 0.0); + my_csqrt(pol+b); + cmul(pol+b, hba); + cass(pol+b+2, pol+b); + cneg(pol+b+2); + cadd(pol+b, hba); + cadd(pol+b+2, hba); + } + } + n_pol *= 2; + + // Add zeros + n_zer= n_pol; + for (a= 0; a MAXPZ) + error("Maximum order for bandstop filters is %d", MAXPZ/2); + + // Run through the list backwards, expanding as we go + for (a= n_pol, b= n_pol*2; a>0; ) { + // hba= bw / pole; + // temp= my_csqrt(1.0 - square(w0 / hba)); + // pole1= hba * (1.0 + temp); + // pole2= hba * (1.0 - temp); + + if (poltyp[a-1] == 1) { + double hba; + a--; b -= 2; + poltyp[b]= 2; poltyp[b+1]= 0; + hba= bw / pol[a]; + cassz(pol+b, 1.0 - (w0 / hba) * (w0 / hba), 0.0); + my_csqrt(pol+b); + caddz(pol+b, 1.0, 0.0); + cmulr(pol+b, hba); + } else { // Assume poltyp[] data is valid + double hba[2]; + a -= 2; b -= 4; + poltyp[b]= 2; poltyp[b+1]= 0; + poltyp[b+2]= 2; poltyp[b+3]= 0; + cass(hba, pol+a); + crecip(hba); + cmulr(hba, bw); + cass(pol+b, hba); + crecip(pol+b); + cmulr(pol+b, w0); + csqu(pol+b); + cneg(pol+b); + caddz(pol+b, 1.0, 0.0); + my_csqrt(pol+b); + cmul(pol+b, hba); + cass(pol+b+2, pol+b); + cneg(pol+b+2); + cadd(pol+b, hba); + cadd(pol+b+2, hba); + } + } + n_pol *= 2; + + // Add zeros + n_zer= n_pol; + for (a= 0; atyp= 'F'; + ff->len= 1; + ff->val[0]= gain; + ff= FFNEXT(ff); + + // Output as much as possible as 2x2 IIR/FIR filters + for (a= 0; a <= n_pol-2 && a <= n_zer-2; a += 2) { + // Look for a pair of values for an IIR + if (poltyp[a] == 1 && poltyp[a+1] == 1) { + // Two real values + ff->typ= 'I'; + ff->len= 3; + ff->val[0]= 1; + ff->val[1]= -(pol[a] + pol[a+1]); + ff->val[2]= pol[a] * pol[a+1]; + ff= FFNEXT(ff); + } else if (poltyp[a] == 2) { + // A complex value and its conjugate pair + ff->typ= 'I'; + ff->len= 3; + ff->val[0]= 1; + ff->val[1]= -2 * pol[a]; + ff->val[2]= pol[a] * pol[a] + pol[a+1] * pol[a+1]; + ff= FFNEXT(ff); + } else error("Internal error -- bad poltyp[] values for z2fidfilter()"); + + // Look for a pair of values for an FIR + if (zertyp[a] == 1 && zertyp[a+1] == 1) { + // Two real values + // Skip if constant and 0/0 + if (!cbm || zer[a] != 0.0 || zer[a+1] != 0.0) { + ff->typ= 'F'; + ff->cbm= cbm; + ff->len= 3; + ff->val[0]= 1; + ff->val[1]= -(zer[a] + zer[a+1]); + ff->val[2]= zer[a] * zer[a+1]; + ff= FFNEXT(ff); + } + } else if (zertyp[a] == 2) { + // A complex value and its conjugate pair + // Skip if constant and 0/0 + if (!cbm || zer[a] != 0.0 || zer[a+1] != 0.0) { + ff->typ= 'F'; + ff->cbm= cbm; + ff->len= 3; + ff->val[0]= 1; + ff->val[1]= -2 * zer[a]; + ff->val[2]= zer[a] * zer[a] + zer[a+1] * zer[a+1]; + ff= FFNEXT(ff); + } + } else error("Internal error -- bad zertyp[] values"); + } + + // Clear up any remaining bits and pieces. Should only be a 1x1 + // IIR/FIR. + if (n_pol-a == 0 && n_zer-a == 0) + ; + else if (n_pol-a == 1 && n_zer-a == 1) { + if (poltyp[a] != 1 || zertyp[a] != 1) + error("Internal error; bad poltyp or zertyp for final pole/zero"); + ff->typ= 'I'; + ff->len= 2; + ff->val[0]= 1; + ff->val[1]= -pol[a]; + ff= FFNEXT(ff); + + // Skip FIR if it is constant and zero + if (!cbm || zer[a] != 0.0) { + ff->typ= 'F'; + ff->cbm= cbm; + ff->len= 2; + ff->val[0]= 1; + ff->val[1]= -zer[a]; + ff= FFNEXT(ff); + } + } else + error("Internal error: unexpected poles/zeros at end of list"); + + // End of list + ff->typ= 0; + ff->len= 0; + ff= FFNEXT(ff); + + rv= realloc(rv, ((char*)ff)-((char*)rv)); + if (!rv) error("Out of memory"); + return rv; +} + +// +// Setup poles/zeros for a band-pass resonator. 'qfact' gives +// the Q-factor; 0 is a special value indicating +infinity, +// giving an oscillator. +// + +static void +bandpass_res(double freq, double qfact) { + double mag; + double th0, th1, th2; + double theta= freq * TWOPI; + double val[2]; + double tmp1[2], tmp2[2], tmp3[2], tmp4[2]; + int cnt; + + n_pol= 2; + poltyp[0]= 2; poltyp[1]= 0; + n_zer= 2; + zertyp[0]= 1; zertyp[1]= 1; + zer[0]= 1; zer[1]= -1; + + if (qfact == 0.0) { + cexpj(pol, theta); + return; + } + + // Do a full binary search, rather than seeding it as Tony Fisher does + cexpj(val, theta); + mag= exp(-theta / (2.0 * qfact)); + th0= 0; th2= M_PI; + for (cnt= 60; cnt > 0; cnt--) { + th1= 0.5 * (th0 + th2); + cexpj(pol, th1); + cmulr(pol, mag); + + // Evaluate response of filter for Z= val + memcpy(tmp1, val, 2*sizeof(double)); + memcpy(tmp2, val, 2*sizeof(double)); + memcpy(tmp3, val, 2*sizeof(double)); + memcpy(tmp4, val, 2*sizeof(double)); + csubz(tmp1, 1, 0); + csubz(tmp2, -1, 0); + cmul(tmp1, tmp2); + csub(tmp3, pol); cconj(pol); + csub(tmp4, pol); cconj(pol); + cmul(tmp3, tmp4); + cdiv(tmp1, tmp3); + + if (fabs(tmp1[1] / tmp1[0]) < 1e-10) break; + + //printf("%-24.16g%-24.16g -> %-24.16g%-24.16g\n", th0, th2, tmp1[0], tmp1[1]); + + if (tmp1[1] > 0.0) th2= th1; + else th0= th1; + } + + if (cnt <= 0) fprintf(stderr, "Resonator binary search failed to converge"); +} + +// +// Setup poles/zeros for a bandstop resonator +// + +static void +bandstop_res(double freq, double qfact) { + bandpass_res(freq, qfact); + zertyp[0]= 2; zertyp[1]= 0; + cexpj(zer, TWOPI * freq); +} + +// +// Setup poles/zeros for an allpass resonator +// + +static void +allpass_res(double freq, double qfact) { + bandpass_res(freq, qfact); + zertyp[0]= 2; zertyp[1]= 0; + memcpy(zer, pol, 2*sizeof(double)); + cmulr(zer, 1.0 / (zer[0]*zer[0] + zer[1]*zer[1])); +} + +// +// Setup poles/zeros for a proportional-integral filter +// + +static void +prop_integral(double freq) { + n_pol= 1; + poltyp[0]= 1; + pol[0]= 0.0; + n_zer= 1; + zertyp[0]= 1; + zer[0]= -TWOPI * freq; +} + +// END // diff --git a/src/async/audio/fidrf_cmdlist.h b/src/async/audio/fidrf_cmdlist.h new file mode 100644 index 0000000..abe53b5 --- /dev/null +++ b/src/async/audio/fidrf_cmdlist.h @@ -0,0 +1,479 @@ +// +// Command-list based filter-running code. +// +// Copyright (c) 2002-2003 Jim Peters . This +// file is released under the GNU Lesser General Public License +// (LGPL) version 2.1 as published by the Free Software +// Foundation. See the file COPYING_LIB for details, or visit +// . +// +// This version of the filter-running code is based on getting +// the filter to go as fast as possible with a pre-compiled +// routine, but without flattening the filter structure. This +// gives greater accuracy than the combined filter. The result +// is mostly faster than the combined filter (tested on ix86 with +// gcc -O6), except where the combined filter gets a big +// advantage from flattening the filter list. This code is also +// portable (unlike the JIT option). +// + +typedef unsigned char uchar; + +typedef struct Run { + int magic; // Magic: 0x64966325 + int buf_size; // Length of working buffer required in doubles + double *coef; // Coefficient list + uchar *cmd; // Command list +} Run; + +typedef struct RunBuf { + double *coef; + uchar *cmd; + int mov_cnt; // Number of bytes to memmove + double buf[0]; +} RunBuf; + + +// +// Filter processing routine. This is designed to avoid too many +// branches, and references are very localized in the code, +// keeping the optimizer from trying to store and remember old +// values. +// + +// +// Step commands: +// 0 END +// 1 IIR coefficient (1+0) +// 2 2x IIR coefficient (2+0) +// 3 3x IIR coefficient (3+0) +// 4 4Nx IIR coefficient (4N+0) +// 5 FIR coefficient (0+1) +// 6 2x FIR coefficient (0+2) +// 7 3x FIR coefficient (0+3) +// 8 4Nx FIR coefficient (0+4N) +// 9 IIR+FIR coefficients (1+1) +// 10 2x IIR+FIR coefficients (2+2) +// 11 3x IIR+FIR coefficients (3+3) +// 12 4Nx IIR+FIR coefficients (4N+4N) +// 13 End-stage, pure IIR, assume no FIR done at all (1+0) +// 14 End-stage with just FIR coeff (0+2) +// 15 End-stage with IIR+FIR coeff (1+2) +// 16 IIR + pure-IIR endstage (2+0) +// 17 FIR + FIR end-stage (0+3) +// 18 IIR+FIR + IIR+FIR end-stage (2+3) +// 19 Nx (IIR + pure-IIR endstage) (2+0) +// 20 Nx (FIR + FIR end-stage) (0+3) +// 21 Nx (IIR+FIR + IIR+FIR end-stage) (2+3) +// 22 Gain coefficient (0+1) +// +// Most filters are made up of 2x2 IIR/FIR pairs, which means a +// list of command 18 bytes. The other big job would be long FIR +// filters. These have to be handled with a list of 7,6,5 +// commands, plus a 13 command. +// + +static double +filter_step(void *fbuf, double iir) { + double *coef= ((RunBuf*)fbuf)->coef; + uchar *cmd= ((RunBuf*)fbuf)->cmd; + double *buf= &((RunBuf*)fbuf)->buf[0]; + uchar ch; + double fir= 0; + double tmp= buf[0]; + int cnt; + + // Using a memmove first is faster on gcc -O6 / ix86 than moving + // the values whilst working through the buffers. + memmove(buf, buf+1, ((RunBuf*)fbuf)->mov_cnt); + +#define IIR \ + iir -= *coef++ * tmp; \ + tmp= *buf++; +#define FIR \ + fir += *coef++ * tmp; \ + tmp= *buf++; +#define BOTH \ + iir -= *coef++ * tmp; \ + fir += *coef++ * tmp; \ + tmp= *buf++; +#define ENDIIR \ + iir -= *coef++ * tmp; \ + tmp= *buf++; \ + buf[-1]= iir; +#define ENDFIR \ + fir += *coef++ * tmp; \ + tmp= *buf++; \ + buf[-1]= iir; \ + iir= fir + *coef++ * iir; \ + fir= 0 +#define ENDBOTH \ + iir -= *coef++ * tmp; \ + fir += *coef++ * tmp; \ + tmp= *buf++; \ + buf[-1]= iir; \ + iir= fir + *coef++ * iir; \ + fir= 0 +#define GAIN \ + iir *= *coef++ + + while ((ch= *cmd++)) switch (ch) { + case 1: + IIR; break; + case 2: + IIR; IIR; break; + case 3: + IIR; IIR; IIR; break; + case 4: + cnt= *cmd++; + do { IIR; IIR; IIR; IIR; } while (--cnt > 0); + break; + case 5: + FIR; break; + case 6: + FIR; FIR; break; + case 7: + FIR; FIR; FIR; break; + case 8: + cnt= *cmd++; + do { FIR; FIR; FIR; FIR; } while (--cnt > 0); + break; + case 9: + BOTH; break; + case 10: + BOTH; BOTH; break; + case 11: + BOTH; BOTH; BOTH; break; + case 12: + cnt= *cmd++; + do { BOTH; BOTH; BOTH; BOTH; } while (--cnt > 0); + break; + case 13: + ENDIIR; break; + case 14: + ENDFIR; break; + case 15: + ENDBOTH; break; + case 16: + IIR; ENDIIR; break; + case 17: + FIR; ENDFIR; break; + case 18: + BOTH; ENDBOTH; break; + case 19: + cnt= *cmd++; + do { IIR; ENDIIR; } while (--cnt > 0); + break; + case 20: + cnt= *cmd++; + do { FIR; ENDFIR; } while (--cnt > 0); + break; + case 21: + cnt= *cmd++; + do { BOTH; ENDBOTH; } while (--cnt > 0); + break; + case 22: + GAIN; break; + } + +#undef IIR +#undef FIR +#undef BOTH +#undef ENDIIR +#undef ENDFIR +#undef ENDBOTH +#undef GAIN + + return iir; +} + + +// +// Create an instance of a filter, ready to run. This returns a +// void* handle, and a function to call to execute the filter. +// Working buffers for the filter instances must be allocated +// separately using fid_run_newbuf(). This allows many +// simultaneous instances of the filter to be run. +// +// The sub-filters are executed in the precise order that they +// are given. This may lead to some inefficiency. Normally when +// an IIR filter is followed by an FIR filter, the buffers can be +// shared. However, if the sub-filters are not in IIR/FIR pairs, +// then extra memory accesses are required. +// +// In any case, factors are extracted from IIR filters (so that +// the first coefficient is 1), and single-element FIR filters +// are merged into the global gain factor, and are ignored. +// +// The returned handle must be released using fid_run_free(). +// + +void * +fid_run_new(FidFilter *filt, double (**funcpp)(void *,double)) { + int buf_size= 0; + uchar *cp, prev; + FidFilter *ff; + double *dp; + double gain= 1.0; + int a; + double *coef_tmp; + uchar *cmd_tmp; + int coef_cnt, coef_max; + int cmd_cnt, cmd_max; + int filt_cnt= 0; + Run *rr; + + for (ff= filt; ff->len; ff= FFNEXT(ff)) + filt_cnt += ff->len; + + // Allocate worst-case sizes for temporary arrays + coef_tmp= ALLOC_ARR(coef_max= filt_cnt + 1, double); + cmd_tmp= ALLOC_ARR(cmd_max= filt_cnt + 4, uchar); + dp= coef_tmp; + cp= cmd_tmp; + prev= 0; + + // Generate command and coefficient lists + while (filt->len) { + int n_iir= 0, n_fir= 0, cnt; + double *iir= 0, *fir= 0; + double adj = 1.0; + if (filt->typ == 'F' && filt->len == 1) { + gain *= filt->val[0]; + filt= FFNEXT(filt); + continue; + } + if (filt->typ == 'F') { + iir= 0; n_iir= 0; + fir= filt->val; n_fir= filt->len; + filt= FFNEXT(filt); + } else if (filt->typ == 'I') { + iir= filt->val; n_iir= filt->len; + fir= 0; n_fir= 0; + filt= FFNEXT(filt); + while (filt->typ == 'F' && filt->len == 1) { + gain *= filt->val[0]; + filt= FFNEXT(filt); + } + if (filt->typ == 'F') { + fir= filt->val; n_fir= filt->len; + filt= FFNEXT(filt); + } + } else + error("Internal error: fid_run_new can only handle IIR + FIR types"); + + // Okay, we now have an IIR/FIR pair to process, possibly with + // n_iir or n_fir == 0 if one half is missing + cnt= n_iir > n_fir ? n_iir : n_fir; + buf_size += cnt-1; + if (n_iir) { + adj= 1.0 / iir[0]; + gain *= adj; + } + if (n_fir == 3 && n_iir == 3) { + if (prev == 18) { cp[-1]= prev= 21; *cp++= 2; } + else if (prev == 21) { cp[-1]++; } + else *cp++= prev= 18; + *dp++= iir[2]*adj; *dp++= fir[2]; + *dp++= iir[1]*adj; *dp++= fir[1]; + *dp++= fir[0]; + } else if (n_fir == 3 && n_iir == 0) { + if (prev == 17) { cp[-1]= prev= 20; *cp++= 2; } + else if (prev == 20) { cp[-1]++; } + else *cp++= prev= 17; + *dp++= fir[2]; + *dp++= fir[1]; + *dp++= fir[0]; + } else if (n_fir == 0 && n_iir == 3) { + if (prev == 16) { cp[-1]= prev= 19; *cp++= 2; } + else if (prev == 19) { cp[-1]++; } + else *cp++= prev= 16; + *dp++= iir[2]*adj; + *dp++= iir[1]*adj; + } else { + prev= 0; // Just cancel 'prev' as we only use it for 16-18,19-21 + if (cnt > n_fir) { + a= 0; + while (cnt > n_fir && cnt > 2) { + *dp++= iir[--cnt] * adj; a++; + } + while (a >= 4) { + int nn= a/4; if (nn > 255) nn= 255; + *cp++= 4; *cp++= nn; a -= nn*4; + } + if (a) *cp++= a; + } + if (cnt > n_iir) { + a= 0; + while (cnt > n_iir && cnt > 2) { + *dp++= fir[--cnt]; a++; + } + while (a >= 4) { + int nn= a/4; if (nn > 255) nn= 255; + *cp++= 8; *cp++= nn; a -= nn*4; + } + if (a) *cp++= 4+a; + } + a= 0; + while (cnt > 2) { + cnt--; a++; + *dp++= iir[cnt]*adj; *dp++= fir[cnt]; + } + while (a >= 4) { + int nn= a/4; if (nn > 255) nn= 255; + *cp++= 12; *cp++= nn; a -= nn*4; + } + if (a) *cp++= 8+a; + + if (!n_fir) { + *cp++= 13; + *dp++= iir[1]; + } else if (!n_iir) { + *cp++= 14; + *dp++= fir[1]; + *dp++= fir[0]; + } else { + *cp++= 15; + *dp++= iir[1]; + *dp++= fir[1]; + *dp++= fir[0]; + } + } + } + + if (gain != 1.0) { + *cp++= 22; + *dp++= gain; + } + *cp++= 0; + + // Sanity checks + coef_cnt= dp-coef_tmp; + cmd_cnt= cp-cmd_tmp; + if (coef_cnt > coef_max || + cmd_cnt > cmd_max) + error("fid_run_new internal error; arrays exceeded"); + + // Allocate the final Run structure to return + rr= (Run*)Alloc(sizeof(Run) + + coef_cnt*sizeof(double) + + cmd_cnt*sizeof(char)); + rr->magic= 0x64966325; + rr->buf_size= buf_size; + rr->coef= (double*)(rr+1); + rr->cmd= (uchar*)(rr->coef + coef_cnt); + memcpy(rr->coef, coef_tmp, coef_cnt*sizeof(double)); + memcpy(rr->cmd, cmd_tmp, cmd_cnt*sizeof(char)); + + //DEBUG { + //DEBUG int a; + //DEBUG for (cp= cmd_tmp; *cp; cp++) printf("%d ", *cp); + //DEBUG printf("\n"); + //DEBUG //for (a= 0; amagic != 0x64966325) + error("Bad handle passed to fid_run_newbuf()"); + + siz= rr->buf_size ? rr->buf_size : 1; // Minimum one element to avoid problems + rb= Alloc(sizeof(RunBuf) + siz * sizeof(double)); + rb->coef= rr->coef; + rb->cmd= rr->cmd; + rb->mov_cnt= (siz-1) * sizeof(double); + // rb->buf[] already zerod + + return rb; +} + +// +// Find the space required for a filter buffer +// + +int +fid_run_bufsize(void *run) { + Run *rr= run; + int siz; + + if (rr->magic != 0x64966325) + error("Bad handle passed to fid_run_bufsize()"); + + siz= rr->buf_size ? rr->buf_size : 1; // Minimum one element to avoid problems + return sizeof(RunBuf) + siz * sizeof(double); +} + +// +// Initialise a filter buffer allocated separately. Usually +// fid_run_newbuf() is easier, but in the case where filter +// buffers must be allocated as part of a larger structure, a +// call to fid_run_newbuf can be replaced with a call to +// fid_run_bufsize() to get the space required, and then a call +// to fid_run_initbuf() once it has been allocated. +// + +void +fid_run_initbuf(void *run, void *buf) { + Run *rr= run; + RunBuf *rb= buf; + int siz; + + if (rr->magic != 0x64966325) + error("Bad handle passed to fid_run_initbuf()"); + + siz= rr->buf_size ? rr->buf_size : 1; // Minimum one element to avoid problems + rb->coef= rr->coef; + rb->cmd= rr->cmd; + rb->mov_cnt= (siz-1) * sizeof(double); + memset(rb->buf, 0, rb->mov_cnt + sizeof(double)); +} + +// +// Reinitialise an instance of the filter, allowing it to start +// afresh. It assumes that the buffer was correctly initialised +// previously, either through a call to fid_run_newbuf() or +// fid_run_initbuf(). +// + +void +fid_run_zapbuf(void *buf) { + RunBuf *rb= buf; + memset(rb->buf, 0, rb->mov_cnt + sizeof(double)); +} + + +// +// Delete an instance +// + +void +fid_run_freebuf(void *runbuf) { + free(runbuf); +} + +// +// Delete the filter +// + +void +fid_run_free(void *run) { + free(run); +} + +// END // diff --git a/src/async/core/.gitignore b/src/async/core/.gitignore new file mode 100644 index 0000000..13b166a --- /dev/null +++ b/src/async/core/.gitignore @@ -0,0 +1,3 @@ +/depend +/makefile.root +/Makefile diff --git a/src/async/core/AsyncApplication.cpp b/src/async/core/AsyncApplication.cpp new file mode 100644 index 0000000..dd6e73b --- /dev/null +++ b/src/async/core/AsyncApplication.cpp @@ -0,0 +1,202 @@ +/** +@file AsyncApplication.cpp +@brief The core class for writing asyncronous applications +@author Tobias Blomberg +@date 2003-03-16 + +This file contains the AsyncApplication class which is the core of an +application that use the Async classes. + +\verbatim +Async - A library for programming event driven applications +Copyright (C) 2003-2015 Tobias Blomberg + +This program is free software; you can redistribute it and/or modify +it under the terms of the GNU General Public License as published by +the Free Software Foundation; either version 2 of the License, or +(at your option) any later version. + +This program is distributed in the hope that it will be useful, +but WITHOUT ANY WARRANTY; without even the implied warranty of +MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +GNU General Public License for more details. + +You should have received a copy of the GNU General Public License +along with this program; if not, write to the Free Software +Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA +\endverbatim +*/ + + + +/**************************************************************************** + * + * System Includes + * + ****************************************************************************/ + +#include +#include +#include + +#include +#include + + +/**************************************************************************** + * + * Project Includes + * + ****************************************************************************/ + + + +/**************************************************************************** + * + * Local Includes + * + ****************************************************************************/ + +#include "AsyncFdWatch.h" +#include "AsyncTimer.h" +#include "AsyncApplication.h" + + + +/**************************************************************************** + * + * Namespaces to use + * + ****************************************************************************/ + +using namespace std; +using namespace Async; + + + +/**************************************************************************** + * + * Defines & typedefs + * + ****************************************************************************/ + + + +/**************************************************************************** + * + * Local class definitions + * + ****************************************************************************/ + + + +/**************************************************************************** + * + * Prototypes + * + ****************************************************************************/ + + + +/**************************************************************************** + * + * Exported Global Variables + * + ****************************************************************************/ + +Application *Application::app_ptr = 0; + + +/**************************************************************************** + * + * Local Global Variables + * + ****************************************************************************/ + + + +/**************************************************************************** + * + * Public member functions + * + ****************************************************************************/ + + +Application &Application::app(void) +{ + assert(app_ptr != 0); + return *app_ptr; +} /* Application::app */ + + +/* + *------------------------------------------------------------------------ + * Method: + * Purpose: + * Input: + * Output: + * Author: + * Created: + * Remarks: + * Bugs: + *------------------------------------------------------------------------ + */ +Application::Application(void) +{ + assert(app_ptr == 0); + app_ptr = this; + task_timer = new Async::Timer(0, Timer::TYPE_ONESHOT, false); + task_timer->expired.connect( + sigc::hide(mem_fun(*this, &Application::taskTimerExpired))); +} /* Application::Application */ + + +Application::~Application(void) +{ + delete task_timer; + task_timer = 0; +} /* Application::~Application */ + + +void Application::runTask(sigc::slot task) +{ + task_list.push_back(task); + task_timer->setEnable(true); +} /* Application::runTask */ + + + +/**************************************************************************** + * + * Protected member functions + * + ****************************************************************************/ + +void Application::clearTasks(void) +{ + task_list.clear(); + task_timer->setEnable(false); +} /* Application::clearTasks */ + + +/**************************************************************************** + * + * Private member functions + * + ****************************************************************************/ + +void Application::taskTimerExpired(void) +{ + SlotList::iterator it; + for (it=task_list.begin(); it!=task_list.end(); ++it) + { + (*it)(); + } + clearTasks(); +} /* Application::taskTimerExpired */ + + + +/* + * This file has not been truncated + */ diff --git a/src/async/core/AsyncApplication.h b/src/async/core/AsyncApplication.h new file mode 100644 index 0000000..b5edcd3 --- /dev/null +++ b/src/async/core/AsyncApplication.h @@ -0,0 +1,215 @@ +/** +@file AsyncApplication.h +@brief The core class for writing asyncronous applications +@author Tobias Blomberg +@date 2003-03-16 + +This file contains the AsyncApplication class which is the core of an +application that use the Async classes. + +\verbatim +Async - A library for programming event driven applications +Copyright (C) 2003-2022 Tobias Blomberg + +This program is free software; you can redistribute it and/or modify +it under the terms of the GNU General Public License as published by +the Free Software Foundation; either version 2 of the License, or +(at your option) any later version. + +This program is distributed in the hope that it will be useful, +but WITHOUT ANY WARRANTY; without even the implied warranty of +MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +GNU General Public License for more details. + +You should have received a copy of the GNU General Public License +along with this program; if not, write to the Free Software +Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA +\endverbatim +*/ + +#ifndef ASYNC_APPLICATION_INCLUDED +#define ASYNC_APPLICATION_INCLUDED + + +/**************************************************************************** + * + * System Includes + * + ****************************************************************************/ + +#include + +#include + + +/**************************************************************************** + * + * Project Includes + * + ****************************************************************************/ + + + +/**************************************************************************** + * + * Local Includes + * + ****************************************************************************/ + + + +/**************************************************************************** + * + * Forward declarations + * + ****************************************************************************/ + + + +/**************************************************************************** + * + * Namespace + * + ****************************************************************************/ + +namespace Async +{ + +/**************************************************************************** + * + * Forward declarations of classes inside of the declared namespace + * + ****************************************************************************/ + +class Timer; +class FdWatch; +class DnsLookup; +class DnsLookupWorker; + + +/**************************************************************************** + * + * Defines & typedefs + * + ****************************************************************************/ + + + +/**************************************************************************** + * + * Exported Global Variables + * + ****************************************************************************/ + + + +/**************************************************************************** + * + * Class definitions + * + ****************************************************************************/ + +/** + * @brief The base class for asynchronous applications + * + * This is the base class for all asynchronous applications. It is an abstract + * class and so it must be inherited from to create a class that can be + * instantiated. + */ +class Application : public sigc::trackable +{ + public: + /** + * @brief Get the one and only application instance + * + * Use this static member function to get the one and only instance of the + * application object. If an application object has not been previously + * created, the application will crash with a failed assertion. + * @return Returns a reference to the applicaton instance + */ + static Application &app(void); + + /** + * @brief Default constructor + */ + Application(void); + + /** + * @brief Destructor + */ + virtual ~Application(void); + + /** + * @brief Execute the application main loop + * + * When this member function is called the application core will enter the + * core main loop. It will not exit from this loop until the + * Async::Application::quit method is called. + */ + virtual void exec(void) = 0; + + /** + * @brief Exit the application main loop + * + * This function should be called to exit the application core main loop. + */ + virtual void quit(void) = 0; + + /** + * @brief Run a task from the Async main loop + * @param task The task (SigC++ slot) to run + * + * This function can be used to delay a function call until the call chain + * have returned to the Async main loop. This may be required in some + * cases where one otherwise would get into trouble with complex callback + * chains causing strange errors. + * + * Use it something like this to call a function taking no arguments: + * + * runTask(mem_fun(*this, &MyClass::func)); + * + * If the function need arguments passed to it, they must be bound to the + * slot using the sigc::bind function: + * + * runTask(sigc::bind(mem_fun(*this, &MyClass::func), true, my_int)); + * + * In this case the function take two arguments where the first is a bool + * and the second is an integer. + */ + void runTask(sigc::slot task); + + protected: + void clearTasks(void); + + private: + friend class FdWatch; + friend class Timer; + friend class DnsLookup; + + typedef std::list> SlotList; + + static Application *app_ptr; + + SlotList task_list; + Timer *task_timer; + + void taskTimerExpired(void); + virtual void addFdWatch(FdWatch *fd_watch) = 0; + virtual void delFdWatch(FdWatch *fd_watch) = 0; + virtual void addTimer(Timer *timer) = 0; + virtual void delTimer(Timer *timer) = 0; + virtual DnsLookupWorker *newDnsLookupWorker(const DnsLookup& lookup) = 0; + +}; /* class Application */ + + +} /* namespace */ + +#endif /* ASYNC_APPLICATION_INCLUDED */ + + + +/* + * This file has not been truncated + */ + diff --git a/src/async/core/AsyncAtTimer.cpp b/src/async/core/AsyncAtTimer.cpp new file mode 100644 index 0000000..4bfc64b --- /dev/null +++ b/src/async/core/AsyncAtTimer.cpp @@ -0,0 +1,274 @@ +/** +@file AsyncAtTimer.cpp +@brief A timer that times out at a specified absolute time +@author Tobias Blomberg / SM0SVX +@date 2013-04-06 + +This class is used to get a timeout at a specified absolute time. That is, +you can specify a time of day, like 2013-04-06 12:43:00, when you would +like the timer to expire. + +\verbatim +Async - A library for programming event driven applications +Copyright (C) 2003-2013 Tobias Blomberg / SM0SVX + +This program is free software; you can redistribute it and/or modify +it under the terms of the GNU General Public License as published by +the Free Software Foundation; either version 2 of the License, or +(at your option) any later version. + +This program is distributed in the hope that it will be useful, +but WITHOUT ANY WARRANTY; without even the implied warranty of +MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +GNU General Public License for more details. + +You should have received a copy of the GNU General Public License +along with this program; if not, write to the Free Software +Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA +\endverbatim +*/ + + + +/**************************************************************************** + * + * System Includes + * + ****************************************************************************/ + +#include +#include +#include + + +/**************************************************************************** + * + * Project Includes + * + ****************************************************************************/ + + + +/**************************************************************************** + * + * Local Includes + * + ****************************************************************************/ + +#include "AsyncAtTimer.h" + + +/**************************************************************************** + * + * Namespaces to use + * + ****************************************************************************/ + +using namespace std; +using namespace Async; + + + +/**************************************************************************** + * + * Defines & typedefs + * + ****************************************************************************/ + + + +/**************************************************************************** + * + * Local class definitions + * + ****************************************************************************/ + + + +/**************************************************************************** + * + * Prototypes + * + ****************************************************************************/ + + + +/**************************************************************************** + * + * Exported Global Variables + * + ****************************************************************************/ + + + + +/**************************************************************************** + * + * Local Global Variables + * + ****************************************************************************/ + + + +/**************************************************************************** + * + * Public member functions + * + ****************************************************************************/ + +AtTimer::AtTimer(void) +{ + timerclear(&m_expire_at); + m_timer.expired.connect(mem_fun(*this, &AtTimer::onTimerExpired)); +} /* AtTimer::AtTimer */ + + +AtTimer::AtTimer(struct tm &tm, bool do_start) +{ + timerclear(&m_expire_at); + m_timer.expired.connect(mem_fun(*this, &AtTimer::onTimerExpired)); + setTimeout(tm); + if (do_start) + { + start(); + } +} /* AtTimer::AtTimer */ + + +AtTimer::~AtTimer(void) +{ + +} /* AtTimer::~AtTimer */ + + +bool AtTimer::setTimeout(time_t t) +{ + m_expire_at.tv_sec = t; + if (m_timer.isEnabled()) + { + return start(); + } + return true; +} /* AtTimer::setTimeout */ + + +bool AtTimer::setTimeout(struct tm &tm) +{ + time_t t = mktime(&tm); + if (t == -1) + { + cerr << "mktime[AtTimer::setTimeout]: Could not set the timeout due to " + "an invalid time format\n"; + return false; + } + return setTimeout(t); +} /* AtTimer::setTimeout */ + + +void AtTimer::setExpireOffset(int offset_ms) +{ + m_expire_offset = offset_ms; +} /* AtTimer::setExpireOffset */ + + +bool AtTimer::start(void) +{ + int msec = msecToTimeout(); + if (msec == -1) + { + return false; + } + m_timer.setTimeout(msec); + m_timer.setEnable(true); + return true; +} /* AtTimer::start */ + + +void AtTimer::stop(void) +{ + m_timer.setEnable(false); +} /* AtTimer::stop */ + + + +/**************************************************************************** + * + * Protected member functions + * + ****************************************************************************/ + + + +/**************************************************************************** + * + * Private member functions + * + ****************************************************************************/ + +/** + * @brief This function will calculate what to set the timer to + * @return Returns the number of milliseconds to set the timer to + * + * This function will calculate how many milliseconds there is left to the + * wanted timeout time. If the time is larger than one minute it will be + * chopped up in 59 second chunks so as to not loose precision. + * One second before the wanted timeout time occurrs, a last timer is + * schedules so as to obtain good precision for the final timeout. + */ +int AtTimer::msecToTimeout(void) +{ + struct timeval now; + if (gettimeofday(&now, 0) == -1) + { + perror("gettimeofday[AtTimer::msecToTimeout]"); + return -1; + } + + struct timeval diff; + timersub(&m_expire_at, &now, &diff); + + long long msec = static_cast(diff.tv_sec) * 1000 + + diff.tv_usec / 1000 + m_expire_offset + 1; + if (msec < 0) + { + msec = 0; + } + else if (msec > 60000) + { + msec = 59000; + } + else if (msec > 1500) + { + msec -= 1000; + } + + return static_cast(msec); +} /* AtTimer::msecToTimeout */ + + +/** + * @brief Called by the timer when it expires + * @param t The timer object + * + * This function will be called by the timer when it expires. If the specified + * time of day have not been reached, the timer will be restarted. + */ +void AtTimer::onTimerExpired(Timer *t) +{ + int msec = msecToTimeout(); + if (msec > 0) + { + m_timer.setTimeout(msec); + } + else + { + expired(this); + } +} /* AtTimer::onTimerExpired */ + + + +/* + * This file has not been truncated + */ + diff --git a/src/async/core/AsyncAtTimer.h b/src/async/core/AsyncAtTimer.h new file mode 100644 index 0000000..e36d708 --- /dev/null +++ b/src/async/core/AsyncAtTimer.h @@ -0,0 +1,229 @@ +/** +@file AsyncAtTimer.h +@brief A timer that times out at a specified absolute time +@author Tobias Blomberg / SM0SVX +@date 2013-04-06 + +This class is used to get a timeout at a specified absolute time. That is, +you can specify a time of day, like 2013-04-06 12:43:00, when you would +like the timer to expire. + +\verbatim +Async - A library for programming event driven applications +Copyright (C) 2003-2013 Tobias Blomberg / SM0SVX + +This program is free software; you can redistribute it and/or modify +it under the terms of the GNU General Public License as published by +the Free Software Foundation; either version 2 of the License, or +(at your option) any later version. + +This program is distributed in the hope that it will be useful, +but WITHOUT ANY WARRANTY; without even the implied warranty of +MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +GNU General Public License for more details. + +You should have received a copy of the GNU General Public License +along with this program; if not, write to the Free Software +Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA +\endverbatim +*/ + +/** @example AsyncAtTimer_demo.cpp +An example of how to use the AsyncAtTimer class +*/ + + +#ifndef ASYNC_AT_TIMER_INCLUDED +#define ASYNC_AT_TIMER_INCLUDED + + +/**************************************************************************** + * + * System Includes + * + ****************************************************************************/ + +#include +#include +#include + + +/**************************************************************************** + * + * Project Includes + * + ****************************************************************************/ + +#include + + +/**************************************************************************** + * + * Local Includes + * + ****************************************************************************/ + + + +/**************************************************************************** + * + * Forward declarations + * + ****************************************************************************/ + + + +/**************************************************************************** + * + * Namespace + * + ****************************************************************************/ + +namespace Async +{ + + +/**************************************************************************** + * + * Forward declarations of classes inside of the declared namespace + * + ****************************************************************************/ + + + +/**************************************************************************** + * + * Defines & typedefs + * + ****************************************************************************/ + + + +/**************************************************************************** + * + * Exported Global Variables + * + ****************************************************************************/ + + + +/**************************************************************************** + * + * Class definitions + * + ****************************************************************************/ + +/** +@brief A timer that times out at a specified absolute time +@author Tobias Blomberg / SM0SVX +@date 2013-04-06 + +This class is used to get a timeout at a specified absolute time. That is, +you can specify a time of day, like 2013-04-06 12:43:00, when you would +like the timer to expire. + +This class use the gettimeofday() function as its time reference. If reading +time using another function, like time(), in the expire callback, you can +not be sure to get the same time value. The gettimeofday() and time() +functions may return different values for the second. The offset usually seem +to be small (~10ms) but this has not been tested very much. One way to get +around the problem, if it's not possible to use the gettimeofday() function, +is to set an offset using the setExpireOffset() method. An offset of 100ms +will probably do. + +\include AsyncAtTimer_demo.cpp +*/ +class AtTimer : public sigc::trackable +{ + public: + /** + * @brief Default constructor + * + * After default construction the timer will be disabled. + */ + AtTimer(void); + + /** + * @brief Constuctor + * @param tm When the timer should expire in local time + * @param do_start Set to \em true (default) if the timer should start + * upon creation + */ + explicit AtTimer(struct tm &tm, bool do_start=true); + + /** + * @brief Destructor + */ + ~AtTimer(void); + + /** + * @brief Set the timeout time + * @param t When the timer should expire in seconds since the epoch + * @return Returns \em true on success or else \em false + */ + bool setTimeout(time_t t); + + /** + * @brief Set the timeout time + * @param tm When the timer should expire in broken down local time + * @return Returns \em true on success or else \em false + */ + bool setTimeout(struct tm &tm); + + /** + * @brief Set the expire offset + * @param offset_ms The expire offset in milliseconds + * + * Use this function to set an offset for the timer expiration. For + * example, if the offset is set to 100ms, the timer will expire 100ms + * after the time of day specification. It is also possible to set the + * offset to a negative value. + */ + void setExpireOffset(int offset_ms); + + /** + * @brief Start the timer + * @return Returns \em true on sucess or else \em false + */ + bool start(void); + + /** + * @brief Stop the timer + */ + void stop(void); + + /** + * @brief A signal that is emitted when the timer expires + * @param timer A pointer to the timer that has expired + * + * This signal is emitted when the timer expires. It is perfectly legal + * to delete the timer in the connected slot if it is known to be the + * only connected slot. + */ + sigc::signal expired; + + protected: + + private: + Timer m_timer {-1}; + struct timeval m_expire_at; + int m_expire_offset {0}; + + AtTimer(const AtTimer&); + AtTimer& operator=(const AtTimer&); + int msecToTimeout(void); + void onTimerExpired(Timer *t); + +}; /* class AtTimer */ + + +} /* namespace */ + +#endif /* ASYNC_AT_TIMER_INCLUDED */ + + + +/* + * This file has not been truncated + */ + diff --git a/src/async/core/AsyncConfig.cpp b/src/async/core/AsyncConfig.cpp new file mode 100644 index 0000000..f710f83 --- /dev/null +++ b/src/async/core/AsyncConfig.cpp @@ -0,0 +1,548 @@ +/** +@file AsyncConfig.cpp +@brief A class for reading "INI-foramtted" configuration files +@author Tobias Blomberg / SM0SVX +@date 2004-03-17 + +This file contains a class that is used to read configuration files that is +in the famous MS Windows INI file format. An example of a configuration file +is shown below. + +\include test.cfg + +\verbatim +Async - A library for programming event driven applications +Copyright (C) 2003-2025 Tobias Blomberg / SM0SVX + +This program is free software; you can redistribute it and/or modify +it under the terms of the GNU General Public License as published by +the Free Software Foundation; either version 2 of the License, or +(at your option) any later version. + +This program is distributed in the hope that it will be useful, +but WITHOUT ANY WARRANTY; without even the implied warranty of +MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +GNU General Public License for more details. + +You should have received a copy of the GNU General Public License +along with this program; if not, write to the Free Software +Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA +\endverbatim +*/ + + + +/**************************************************************************** + * + * System Includes + * + ****************************************************************************/ + +#include +#include +#include + +#include +#include +#include + + +/**************************************************************************** + * + * Project Includes + * + ****************************************************************************/ + + + +/**************************************************************************** + * + * Local Includes + * + ****************************************************************************/ + +#include "AsyncConfig.h" + + + +/**************************************************************************** + * + * Namespaces to use + * + ****************************************************************************/ + +using namespace std; +using namespace Async; + + +/**************************************************************************** + * + * Defines & typedefs + * + ****************************************************************************/ + + + +/**************************************************************************** + * + * Local class definitions + * + ****************************************************************************/ + + + +/**************************************************************************** + * + * Prototypes + * + ****************************************************************************/ + + + +/**************************************************************************** + * + * Exported Global Variables + * + ****************************************************************************/ + + + + +/**************************************************************************** + * + * Local Global Variables + * + ****************************************************************************/ + + + +/**************************************************************************** + * + * Public member functions + * + ****************************************************************************/ + + +Config::~Config(void) +{ +} /* Config::~Config */ + + +bool Config::open(const string& name) +{ + errno = 0; + + FILE *file = fopen(name.c_str(), "r"); + if (file == NULL) + { + return false; + } + + bool success = parseCfgFile(file); + + fclose(file); + file = NULL; + + return success; + +} /* Config::open */ + + +bool Config::getValue(const std::string& section, const std::string& tag, + std::string& value, bool missing_ok) const +{ + Sections::const_iterator sec_it = sections.find(section); + if (sec_it == sections.end()) + { + return missing_ok; + } + + Values::const_iterator val_it = sec_it->second.find(tag); + if (val_it == sec_it->second.end()) + { + return missing_ok; + } + + value = val_it->second.val; + return true; +} /* Config::getValue */ + + +bool Config::getValue(const std::string& section, const std::string& tag, + char& value, bool missing_ok) const +{ + Sections::const_iterator sec_it = sections.find(section); + if (sec_it == sections.end()) + { + return missing_ok; + } + + Values::const_iterator val_it = sec_it->second.find(tag); + if (val_it == sec_it->second.end()) + { + return missing_ok; + } + + if (val_it->second.val.size() != 1) + { + return false; + } + + value = val_it->second.val[0]; + return true; +} /* Config::getValue */ + + +const string &Config::getValue(const string& section, const string& tag) const +{ + static const string empty_strng; + + Sections::const_iterator sec_it = sections.find(section); + if (sec_it == sections.end()) + { + return empty_strng; + } + + Values::const_iterator val_it = sec_it->second.find(tag); + if (val_it == sec_it->second.end()) + { + return empty_strng; + } + + return val_it->second.val; +} /* Config::getValue */ + + +list Config::listSections(void) +{ + list section_list; + for (Sections::const_iterator it=sections.begin(); it!=sections.end(); ++it) + { + section_list.push_back((*it).first); + } + return section_list; +} /* Config::listSections */ + + +list Config::listSection(const string& section) +{ + list tags; + + if (sections.count(section) == 0) + { + return tags; + } + + Values& values = sections[section]; + Values::iterator it = values.begin(); + for (it=values.begin(); it!=values.end(); ++it) + { + tags.push_back(it->first); + } + + return tags; + +} /* Config::listSection */ + + +void Config::setValue(const std::string& section, const std::string& tag, + const std::string& value) +{ + Values &values = sections[section]; + if (value != values[tag].val) + { + values[tag].val = value; + valueUpdated(section, tag); + for (const auto& func : values[tag].subs) + { + func(value); + } + } +} /* Config::setValue */ + + +/**************************************************************************** + * + * Protected member functions + * + ****************************************************************************/ + + +/* + *------------------------------------------------------------------------ + * Method: + * Purpose: + * Input: + * Output: + * Author: + * Created: + * Remarks: + * Bugs: + *------------------------------------------------------------------------ + */ + + + + + + +/**************************************************************************** + * + * Private member functions + * + ****************************************************************************/ + + +/* + *---------------------------------------------------------------------------- + * Method: + * Purpose: + * Input: + * Output: + * Author: + * Created: + * Remarks: + * Bugs: + *---------------------------------------------------------------------------- + */ +bool Config::parseCfgFile(FILE *file) +{ + char line[16384]; + int line_no = 0; + string current_sec; + string current_tag; + + while (fgets(line, sizeof(line), file) != 0) + { + ++line_no; + char *l = trimSpaces(line); + //printf("%s\n", l); + switch (l[0]) + { + case 0: // Ignore empty rows and + case '#': // rows starting with a #-character == comments + break; + + case '[': + { + char *sec = parseSection(l); + if ((sec == 0) || (sec[0] == 0)) + { + cerr << "*** ERROR: Configuration file parse error. Illegal section " + "name syntax on line " << line_no << endl; + return false; + } + //printf("New section=%s\n", sec); + current_sec = sec; + current_tag = ""; + if (sections.count(current_sec) == 0) + { + sections[current_sec]; // Create a new empty section + //cerr << "*** ERROR: Configuration file parse error: Section " + // "previously defined on line " << line_no << endl; + //return false; + } + break; + } + + case '"': + { + char *val = parseValue(l); + if (val == 0) + { + cerr << "*** ERROR: Configuration file parse error. Illegal value " + "syntax on line " << line_no << endl; + return false; + } + //printf("Continued line=\"%s\"", val); + + if (current_tag.empty()) + { + cerr << "*** ERROR: Configuration file parse error. Line " + "continuation without previous value on line " + << line_no << endl; + return false; + } + assert(!current_sec.empty()); + + Values &values = sections[current_sec]; + string& value = values[current_tag].val; + value += val; + break; + } + + default: + { + string tag, value; + if (!parseValueLine(l, tag, value)) + { + cerr << "*** ERROR: Configuration file parse error. Illegal value " + "line syntax on line " << line_no << endl; + return false; + } + //printf("tag=\"%s\" value=\"%s\"\n", tag.c_str(), value.c_str()); + + if (current_sec.empty()) + { + cerr << "*** ERROR: Configuration file parse error. Value without " + "section on line " << line_no << endl; + return false; + } + Values &values = sections[current_sec]; + current_tag = tag; + values[current_tag].val = value; + break; + } + } + } + + return true; + +} /* Config::parseCfgFile */ + + +char *Config::trimSpaces(char *line) +{ + char *begin = line; + while ((*begin != 0) && isspace(*begin)) + { + ++begin; + } + + char *end = begin + strlen(begin); + while ((end != begin) && (isspace(*end) || (*end == 0))) + { + *end-- = 0; + } + + return begin; + +} /* Config::trimSpaces */ + + +char *Config::parseSection(char *line) +{ + return parseDelimitedString(line, '[', ']'); +} /* Config::parseSection */ + + +char *Config::parseDelimitedString(char *str, char begin_tok, char end_tok) +{ + if (str[0] != begin_tok) + { + return 0; + } + + char *end = str + strlen(str) - 1; + if (*end != end_tok) + { + return 0; + } + *end = 0; + + /* + if (end == str+1) + { + return 0; + } + */ + + return str + 1; + +} /* Config::parseDelimitedString */ + + +bool Config::parseValueLine(char *line, string& tag, string& value) +{ + char *eq = strchr(line, '='); + if (eq == 0) + { + return false; + } + *eq = 0; + + tag = trimSpaces(line); + char *val = parseValue(eq + 1); + if (val == 0) + { + return false; + } + + value = val; + + return true; + +} /* Config::parseValueLine */ + + +char *Config::parseValue(char *value) +{ + value = trimSpaces(value); + if (value[0] == '"') + { + value = parseDelimitedString(value, '"', '"'); + } + + if (value == 0) + { + return 0; + } + + return translateEscapedChars(value); + +} /* Config::parseValue */ + + +char *Config::translateEscapedChars(char *val) +{ + char *head = val; + char *tail = head; + + while (*head != 0) + { + if (*head == '\\') + { + ++head; + switch (*head) + { + case 'n': + *tail = '\n'; + break; + + case 'r': + *tail = '\r'; + break; + + case 't': + *tail = '\t'; + break; + + case '\\': + *tail = '\\'; + break; + + case '"': + *tail = '"'; + break; + + default: + return 0; + } + } + else + { + *tail = *head; + } + ++head; + ++tail; + } + *tail = 0; + + return val; + +} /* Config::translateEscapedChars */ + + +/* + * This file has not been truncated + */ diff --git a/src/async/core/AsyncConfig.h b/src/async/core/AsyncConfig.h new file mode 100644 index 0000000..ca9d03c --- /dev/null +++ b/src/async/core/AsyncConfig.h @@ -0,0 +1,779 @@ +/** +@file AsyncConfig.h +@brief A class for reading "INI-foramtted" configuration files +@author Tobias Blomberg +@date 2004-03-17 + +This file contains a class that is used to read configuration files that is +in the famous MS Windows INI file format. An example of a configuration file +is shown below. + +\include test.cfg + +\verbatim +Async - A library for programming event driven applications +Copyright (C) 2004-2025 Tobias Blomberg / SM0SVX + +This program is free software; you can redistribute it and/or modify +it under the terms of the GNU General Public License as published by +the Free Software Foundation; either version 2 of the License, or +(at your option) any later version. + +This program is distributed in the hope that it will be useful, +but WITHOUT ANY WARRANTY; without even the implied warranty of +MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +GNU General Public License for more details. + +You should have received a copy of the GNU General Public License +along with this program; if not, write to the Free Software +Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA +\endverbatim +*/ + +/** @example AsyncConfig_demo.cpp +An example of how to use the Config class +*/ + + +#ifndef ASYNC_CONFIG_INCLUDED +#define ASYNC_CONFIG_INCLUDED + + +/**************************************************************************** + * + * System Includes + * + ****************************************************************************/ + +#include +#include + +#include +#include +#include +#include +#include +#include +#include +#include + + +/**************************************************************************** + * + * Project Includes + * + ****************************************************************************/ + + + +/**************************************************************************** + * + * Local Includes + * + ****************************************************************************/ + + + +/**************************************************************************** + * + * Forward declarations + * + ****************************************************************************/ + + + +/**************************************************************************** + * + * Namespace + * + ****************************************************************************/ + +namespace Async +{ + + +/**************************************************************************** + * + * Forward declarations of classes inside of the declared namespace + * + ****************************************************************************/ + + + +/**************************************************************************** + * + * Defines & typedefs + * + ****************************************************************************/ + + + +/**************************************************************************** + * + * Exported Global Variables + * + ****************************************************************************/ + + + +/**************************************************************************** + * + * Class definitions + * + ****************************************************************************/ + +/** +@brief A class for reading INI-formatted configuration files +@author Tobias Blomberg +@date 2004-03-17 + +This class is used to read configuration files that is in the famous MS Windows +INI file format. An example of a configuration file and how to use the class +is shown below. + +\include test.cfg + +\include AsyncConfig_demo.cpp +*/ +class Config +{ + public: + /** + * @brief Default constuctor + */ + Config(void) {} + + /** + * @brief Destructor + */ + ~Config(void); + + /** + * @brief Open the given config file + * @param name The name of the configuration file to open + * @return Returns \em true on success or else \em false. + * + * This function will read the given configuration file into memory. + * If this function return false and errno != 0, the errno variable may + * give a hint what the problem was. + */ + bool open(const std::string& name); + + /** + * @brief Return the string value of the given configuration variable + * @param section The name of the section where the configuration + * variable is located + * @param tag The name of the configuration variable to get + * @return Returns String with content of the configuration variable. + * If no variable is found an empty string is returned + * + * This function will return the string value corresponding to the given + * configuration variable. If the configuration variable is unset, an + * empty sting is returned. + */ + const std::string &getValue(const std::string& section, + const std::string& tag) const; + + /** + * @brief Get the string value of the given configuration variable + * @param section The name of the section where the configuration + * variable is located + * @param tag The name of the configuration variable to get + * @param value The value is returned in this argument. Any previous + * contents is wiped + * @return Returns \em true on success or else \em false on failure + * + * This function is used to get the value for a configuration variable + * of type "string". + */ + bool getValue(const std::string& section, const std::string& tag, + std::string& value, bool missing_ok = false) const; + + /** + * @brief Get the char value of the given configuration variable + * @param section The name of the section where the configuration + * variable is located + * @param tag The name of the configuration variable to get + * @param value The value is returned in this argument. Any previous + * contents is wiped + * @return Returns \em true on success or else \em false on failure + * + * This function is used to get the value for a configuration variable + * of type "char". It is an error if the size of the value is anything but + * 1 byte. + */ + bool getValue(const std::string& section, const std::string& tag, + char& value, bool missing_ok = false) const; + /** + * @brief Get the value of the given configuration variable. + * @param section The name of the section where the configuration + * variable is located + * @param tag The name of the configuration variable to get + * @param rsp The value is returned in this argument. + * Successful completion overwrites previous contents + * @param missing_ok If set to \em true, return \em true if the + * configuration variable is missing + * @return Returns \em true on success or else \em false on failure. + * + * This function is used to get the value of a configuraiton variable. + * It's a template function meaning that it can take any value type + * that supports the operator>> function. Note that when the value is of + * type string, the overloaded getValue is used rather than this function. + * Normally a missing configuration variable is seen as an error and the + * function returns \em false. If the missing_ok parameter is set to + * \em true, this function returns \em true for a missing variable but + * still returns \em false if an illegal value is specified. + */ + template + bool getValue(const std::string& section, const std::string& tag, + Rsp &rsp, bool missing_ok = false) const + { + std::string str_val; + if (!getValue(section, tag, str_val)) + { + return missing_ok; + } + std::stringstream ssval(str_val); + Rsp tmp; + ssval >> tmp; + if(!ssval.eof()) + { + ssval >> std::ws; + } + if (ssval.fail() || !ssval.eof()) + { + return false; + } + rsp = tmp; + return true; + } /* Config::getValue */ + + /** + * @brief Get the value of the given config variable into container + * @param section The name of the section where the configuration + * variable is located + * @param tag The name of the configuration variable to get + * @param c The value is returned in this argument. + * Successful completion overwrites previous contents + * @param missing_ok If set to \em true, return \em true if the + * configuration variable is missing + * @return Returns \em true on success or else \em false on failure. + * + * This function is used to get the value of a configuraiton variable. + * The config variable is read into a container (e.g. vector, list etc). + * It's a template function meaning that it can take any value type + * that supports the operator>> function. + * Normally a missing configuration variable is seen as an error and the + * function returns \em false. If the missing_ok parameter is set to + * \em true, this function returns \em true for a missing variable but + * still returns \em false if an illegal value is specified. + */ + template