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
This commit is contained in:
commit
fca7c33794
276 files changed
+73533
No files matched your search
+34
@@ -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
|
||||||
+41
@@ -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"]
|
||||||
@@ -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.
|
||||||
@@ -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:
|
||||||
@@ -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_<PREFIX_A>_<PREFIX_B>` (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.
|
||||||
@@ -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_A>_<PREFISSO_B>` (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.
|
||||||
+149
@@ -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 <file>` | Config file path (default used by the image: `/etc/svxlink/svxreflector.conf`) |
|
||||||
|
| `--logfile <file>` | Redirect stdout/stderr to a file instead of the container log |
|
||||||
|
| `--runasuser <user>` | Drop privileges to the given user after startup |
|
||||||
|
| `--pidfile <file>` | 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.
|
||||||
+206
@@ -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
|
||||||
@@ -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, <frame>) ─►│
|
||||||
|
│ 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<uint32_t, std::string>` 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 |
|
||||||
@@ -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)
|
||||||
@@ -0,0 +1,2 @@
|
|||||||
|
/makefile.root
|
||||||
|
/Makefile
|
||||||
@@ -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)
|
||||||
@@ -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 <CR> or <LF> 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<float,float> 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.
|
||||||
|
|
||||||
@@ -0,0 +1,3 @@
|
|||||||
|
/depend
|
||||||
|
/makefile.root
|
||||||
|
/Makefile
|
||||||
@@ -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 <cmath>
|
||||||
|
|
||||||
|
|
||||||
|
/****************************************************************************
|
||||||
|
*
|
||||||
|
* Project Includes
|
||||||
|
*
|
||||||
|
****************************************************************************/
|
||||||
|
|
||||||
|
#include <AsyncAudioProcessor.h>
|
||||||
|
|
||||||
|
|
||||||
|
/****************************************************************************
|
||||||
|
*
|
||||||
|
* 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<count; ++i)
|
||||||
|
{
|
||||||
|
dest[i] = src[i] * m_gain;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
private:
|
||||||
|
AudioAmp(const AudioAmp&);
|
||||||
|
AudioAmp& operator=(const AudioAmp&);
|
||||||
|
|
||||||
|
float m_gain;
|
||||||
|
|
||||||
|
}; /* class AudioAmp */
|
||||||
|
|
||||||
|
|
||||||
|
} /* namespace */
|
||||||
|
|
||||||
|
#endif /* ASYNC_AUDIO_AMP_INCLUDED */
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
/*
|
||||||
|
* This file has not been truncated
|
||||||
|
*/
|
||||||
|
|
||||||
@@ -0,0 +1,173 @@
|
|||||||
|
/**
|
||||||
|
@file AsyncAudioClipper.h
|
||||||
|
@brief Contains an audio pipe class to clip audio to a given maximum level
|
||||||
|
@author Tobias Blomberg / SM0SVX
|
||||||
|
@date 2005-08-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_CLIPPER_INCLUDED
|
||||||
|
#define ASYNC_AUDIO_CLIPPER_INCLUDED
|
||||||
|
|
||||||
|
|
||||||
|
/****************************************************************************
|
||||||
|
*
|
||||||
|
* System Includes
|
||||||
|
*
|
||||||
|
****************************************************************************/
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
/****************************************************************************
|
||||||
|
*
|
||||||
|
* Project Includes
|
||||||
|
*
|
||||||
|
****************************************************************************/
|
||||||
|
|
||||||
|
#include <AsyncAudioProcessor.h>
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
/****************************************************************************
|
||||||
|
*
|
||||||
|
* 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<count; ++i)
|
||||||
|
{
|
||||||
|
if (src[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
|
||||||
|
*/
|
||||||
|
|
||||||
@@ -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 <iostream>
|
||||||
|
|
||||||
|
|
||||||
|
/****************************************************************************
|
||||||
|
*
|
||||||
|
* 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<count; ++i)
|
||||||
|
{
|
||||||
|
double rect = fabs(src[i]); // rectify input
|
||||||
|
|
||||||
|
/* if desired, one could use another EnvelopeDetector to smooth
|
||||||
|
* the rectified signal.
|
||||||
|
*/
|
||||||
|
|
||||||
|
rect += DC_OFFSET; // add DC offset to avoid log( 0 )
|
||||||
|
double keydB = lin2dB( rect ); // convert linear -> 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
|
||||||
|
*/
|
||||||
|
|
||||||
@@ -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 <cmath>
|
||||||
|
|
||||||
|
|
||||||
|
/****************************************************************************
|
||||||
|
*
|
||||||
|
* Project Includes
|
||||||
|
*
|
||||||
|
****************************************************************************/
|
||||||
|
|
||||||
|
#include <AsyncAudioProcessor.h>
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
/****************************************************************************
|
||||||
|
*
|
||||||
|
* 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
|
||||||
|
*/
|
||||||
|
|
||||||
@@ -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<AudioContainerWav> wav;
|
||||||
|
#ifdef OGG_MAJOR
|
||||||
|
static AudioContainerSpecificFactory<AudioContainerOpus> opus;
|
||||||
|
#endif
|
||||||
|
static AudioContainerSpecificFactory<AudioContainerPcm> pcm;
|
||||||
|
return AudioContainerFactory::createNamedObject(name);
|
||||||
|
} /* Async::createAudioContainer */
|
||||||
|
|
||||||
|
|
||||||
|
/****************************************************************************
|
||||||
|
*
|
||||||
|
* Protected member functions
|
||||||
|
*
|
||||||
|
****************************************************************************/
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
/****************************************************************************
|
||||||
|
*
|
||||||
|
* Private member functions
|
||||||
|
*
|
||||||
|
****************************************************************************/
|
||||||
|
|
||||||
|
/*
|
||||||
|
* This file has not been truncated
|
||||||
|
*/
|
||||||
@@ -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 <sigc++/sigc++.h>
|
||||||
|
|
||||||
|
|
||||||
|
/****************************************************************************
|
||||||
|
*
|
||||||
|
* Project Includes
|
||||||
|
*
|
||||||
|
****************************************************************************/
|
||||||
|
|
||||||
|
#include <AsyncAudioSink.h>
|
||||||
|
#include <AsyncFactory.h>
|
||||||
|
|
||||||
|
|
||||||
|
/****************************************************************************
|
||||||
|
*
|
||||||
|
* 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<void(const char*, size_t)> 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<AudioContainer> 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<AudioContainerWav> wav;
|
||||||
|
*/
|
||||||
|
template <class T>
|
||||||
|
struct AudioContainerSpecificFactory
|
||||||
|
: public Async::SpecificFactory<AudioContainer, T>
|
||||||
|
{
|
||||||
|
AudioContainerSpecificFactory(void)
|
||||||
|
: Async::SpecificFactory<AudioContainer, T>(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
|
||||||
|
*/
|
||||||
@@ -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 <cstring>
|
||||||
|
#include <iostream>
|
||||||
|
|
||||||
|
|
||||||
|
/****************************************************************************
|
||||||
|
*
|
||||||
|
* 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<unsigned char*>(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<unsigned char*>(
|
||||||
|
reinterpret_cast<const unsigned char*>(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<unsigned char*>(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<const char*> &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<char>& 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
|
||||||
|
*/
|
||||||
@@ -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 <ogg/ogg.h>
|
||||||
|
#include <vector>
|
||||||
|
#include <cstring>
|
||||||
|
|
||||||
|
|
||||||
|
/****************************************************************************
|
||||||
|
*
|
||||||
|
* Project Includes
|
||||||
|
*
|
||||||
|
****************************************************************************/
|
||||||
|
|
||||||
|
#include <AsyncAudioContainer.h>
|
||||||
|
|
||||||
|
|
||||||
|
/****************************************************************************
|
||||||
|
*
|
||||||
|
* 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<char> m_header;
|
||||||
|
std::vector<char> 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<const char*> &comments);
|
||||||
|
bool writePage(const ogg_page& page, std::vector<char>& buf);
|
||||||
|
bool writeOggOpusHeader(void);
|
||||||
|
|
||||||
|
}; /* class AudioContainerOpus */
|
||||||
|
|
||||||
|
|
||||||
|
} /* namespace */
|
||||||
|
|
||||||
|
#endif /* ASYNC_AUDIO_CONTAINER_OPUS_INCLUDED */
|
||||||
|
|
||||||
|
/*
|
||||||
|
* This file has not been truncated
|
||||||
|
*/
|
||||||
@@ -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 <iostream>
|
||||||
|
|
||||||
|
|
||||||
|
/****************************************************************************
|
||||||
|
*
|
||||||
|
* 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<count; ++i)
|
||||||
|
{
|
||||||
|
int16_t sample;
|
||||||
|
if (samples[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<char*>(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<char*>(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
|
||||||
|
*/
|
||||||
@@ -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 <vector>
|
||||||
|
#include <cstdint>
|
||||||
|
|
||||||
|
|
||||||
|
/****************************************************************************
|
||||||
|
*
|
||||||
|
* Project Includes
|
||||||
|
*
|
||||||
|
****************************************************************************/
|
||||||
|
|
||||||
|
#include <AsyncAudioContainer.h>
|
||||||
|
|
||||||
|
|
||||||
|
/****************************************************************************
|
||||||
|
*
|
||||||
|
* 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<int16_t> m_block;
|
||||||
|
|
||||||
|
AudioContainerPcm(const AudioContainerPcm&);
|
||||||
|
AudioContainerPcm& operator=(const AudioContainerPcm&);
|
||||||
|
|
||||||
|
}; /* class AudioContainerPcm */
|
||||||
|
|
||||||
|
|
||||||
|
} /* namespace */
|
||||||
|
|
||||||
|
#endif /* ASYNC_AUDIO_CONTAINER_PCM_INCLUDED */
|
||||||
|
|
||||||
|
/*
|
||||||
|
* This file has not been truncated
|
||||||
|
*/
|
||||||
@@ -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 <cstring>
|
||||||
|
|
||||||
|
|
||||||
|
/****************************************************************************
|
||||||
|
*
|
||||||
|
* Project Includes
|
||||||
|
*
|
||||||
|
****************************************************************************/
|
||||||
|
|
||||||
|
#include <iostream>
|
||||||
|
|
||||||
|
|
||||||
|
/****************************************************************************
|
||||||
|
*
|
||||||
|
* 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<count; ++i)
|
||||||
|
{
|
||||||
|
int16_t sample;
|
||||||
|
if (samples[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<char*>(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<char*>(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
|
||||||
|
*/
|
||||||
@@ -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 <sigc++/sigc++.h>
|
||||||
|
#include <cstring>
|
||||||
|
#include <cstdint>
|
||||||
|
|
||||||
|
|
||||||
|
/****************************************************************************
|
||||||
|
*
|
||||||
|
* Project Includes
|
||||||
|
*
|
||||||
|
****************************************************************************/
|
||||||
|
|
||||||
|
#include <AsyncAudioContainer.h>
|
||||||
|
|
||||||
|
|
||||||
|
/****************************************************************************
|
||||||
|
*
|
||||||
|
* 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<AudioContainerWav>
|
||||||
|
//{
|
||||||
|
// Factory(void) : AudioContainerSpecificFactory<AudioContainerWav>("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
|
||||||
|
*/
|
||||||
@@ -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 <sys/time.h>
|
||||||
|
#include <iostream>
|
||||||
|
#include <string>
|
||||||
|
#include <stdint.h>
|
||||||
|
|
||||||
|
|
||||||
|
/****************************************************************************
|
||||||
|
*
|
||||||
|
* Project Includes
|
||||||
|
*
|
||||||
|
****************************************************************************/
|
||||||
|
|
||||||
|
#include <AsyncAudioSink.h>
|
||||||
|
#include <AsyncAudioSource.h>
|
||||||
|
|
||||||
|
|
||||||
|
/****************************************************************************
|
||||||
|
*
|
||||||
|
* 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<count; ++i)
|
||||||
|
{
|
||||||
|
if (samples[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
|
||||||
|
*/
|
||||||
|
|
||||||
@@ -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 <cstring>
|
||||||
|
|
||||||
|
|
||||||
|
/****************************************************************************
|
||||||
|
*
|
||||||
|
* 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
|
||||||
|
*/
|
||||||
|
|
||||||
@@ -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 <AsyncAudioProcessor.h>
|
||||||
|
|
||||||
|
|
||||||
|
/****************************************************************************
|
||||||
|
*
|
||||||
|
* 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
|
||||||
|
*/
|
||||||
|
|
||||||
@@ -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
|
||||||
|
*/
|
||||||
|
|
||||||
@@ -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 <string>
|
||||||
|
#include <sigc++/sigc++.h>
|
||||||
|
|
||||||
|
|
||||||
|
/****************************************************************************
|
||||||
|
*
|
||||||
|
* Project Includes
|
||||||
|
*
|
||||||
|
****************************************************************************/
|
||||||
|
|
||||||
|
#include <AsyncAudioSource.h>
|
||||||
|
|
||||||
|
|
||||||
|
/****************************************************************************
|
||||||
|
*
|
||||||
|
* 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<void()> 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
|
||||||
|
*/
|
||||||
|
|
||||||
@@ -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 <AsyncAudioDecoder.h>
|
||||||
|
|
||||||
|
|
||||||
|
/****************************************************************************
|
||||||
|
*
|
||||||
|
* 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
|
||||||
|
*/
|
||||||
@@ -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<size; ++i)
|
||||||
|
{
|
||||||
|
frame[frame_len++] = ptr[i];
|
||||||
|
if (frame_len == sizeof(frame))
|
||||||
|
{
|
||||||
|
gsm_signal s16_samples[FRAME_SAMPLE_CNT];
|
||||||
|
gsm_decode(gsmh, frame, s16_samples);
|
||||||
|
|
||||||
|
float samples[FRAME_SAMPLE_CNT];
|
||||||
|
for (int j=0; j<FRAME_SAMPLE_CNT; ++j)
|
||||||
|
{
|
||||||
|
samples[j] = static_cast<float>(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
|
||||||
|
*/
|
||||||
|
|
||||||
@@ -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 <gsm.h>
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
/****************************************************************************
|
||||||
|
*
|
||||||
|
* Project Includes
|
||||||
|
*
|
||||||
|
****************************************************************************/
|
||||||
|
|
||||||
|
#include <AsyncAudioDecoder.h>
|
||||||
|
|
||||||
|
|
||||||
|
/****************************************************************************
|
||||||
|
*
|
||||||
|
* 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
|
||||||
|
*/
|
||||||
|
|
||||||
@@ -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 <cstring>
|
||||||
|
#include <stdint.h>
|
||||||
|
|
||||||
|
|
||||||
|
/****************************************************************************
|
||||||
|
*
|
||||||
|
* Project Includes
|
||||||
|
*
|
||||||
|
****************************************************************************/
|
||||||
|
|
||||||
|
#include <AsyncAudioDecoder.h>
|
||||||
|
|
||||||
|
|
||||||
|
/****************************************************************************
|
||||||
|
*
|
||||||
|
* 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<uint8_t*>(buf);
|
||||||
|
uint16_t cnt = static_cast<uint16_t>(ptr[0]);
|
||||||
|
cnt |= static_cast<uint16_t>(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
|
||||||
|
*/
|
||||||
@@ -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 <stdint.h>
|
||||||
|
#include <iostream>
|
||||||
|
#include <cstdlib>
|
||||||
|
#include <cmath>
|
||||||
|
|
||||||
|
|
||||||
|
/****************************************************************************
|
||||||
|
*
|
||||||
|
* 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<opus_int32>(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<unsigned char *>(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
|
||||||
|
*/
|
||||||
|
|
||||||
@@ -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 <opus.h>
|
||||||
|
|
||||||
|
|
||||||
|
/****************************************************************************
|
||||||
|
*
|
||||||
|
* Project Includes
|
||||||
|
*
|
||||||
|
****************************************************************************/
|
||||||
|
|
||||||
|
#include <AsyncAudioDecoder.h>
|
||||||
|
|
||||||
|
|
||||||
|
/****************************************************************************
|
||||||
|
*
|
||||||
|
* 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
|
||||||
|
*/
|
||||||
|
|
||||||
@@ -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 <AsyncAudioDecoder.h>
|
||||||
|
|
||||||
|
|
||||||
|
/****************************************************************************
|
||||||
|
*
|
||||||
|
* 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<const float *>(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
|
||||||
|
*/
|
||||||
|
|
||||||
@@ -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 <stdint.h>
|
||||||
|
|
||||||
|
|
||||||
|
/****************************************************************************
|
||||||
|
*
|
||||||
|
* 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<int16_t *>(buf);
|
||||||
|
int count = size / sizeof(int16_t);
|
||||||
|
float samples[count];
|
||||||
|
for (int i=0; i<count; ++i)
|
||||||
|
{
|
||||||
|
samples[i] = static_cast<float>(s16_samples[i]) / 32768.0;
|
||||||
|
}
|
||||||
|
sinkWriteSamples(samples, count);
|
||||||
|
} /* AudioDecoderS16::writeEncodedSamples */
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
/****************************************************************************
|
||||||
|
*
|
||||||
|
* Protected member functions
|
||||||
|
*
|
||||||
|
****************************************************************************/
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
/****************************************************************************
|
||||||
|
*
|
||||||
|
* Private member functions
|
||||||
|
*
|
||||||
|
****************************************************************************/
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
/*
|
||||||
|
* This file has not been truncated
|
||||||
|
*/
|
||||||
|
|
||||||
@@ -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 <AsyncAudioDecoder.h>
|
||||||
|
|
||||||
|
|
||||||
|
/****************************************************************************
|
||||||
|
*
|
||||||
|
* 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
|
||||||
|
*/
|
||||||
|
|
||||||
@@ -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 <iostream>
|
||||||
|
#include <cstdlib>
|
||||||
|
|
||||||
|
|
||||||
|
/****************************************************************************
|
||||||
|
*
|
||||||
|
* 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<frame_size; ++i)
|
||||||
|
{
|
||||||
|
samples[i] = samples [i] / 32767.0;
|
||||||
|
}
|
||||||
|
sinkWriteSamples(samples, frame_size);
|
||||||
|
}
|
||||||
|
} /* AudioDecoderSpeex::writeEncodedSamples */
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
/****************************************************************************
|
||||||
|
*
|
||||||
|
* Protected member functions
|
||||||
|
*
|
||||||
|
****************************************************************************/
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
/****************************************************************************
|
||||||
|
*
|
||||||
|
* Private member functions
|
||||||
|
*
|
||||||
|
****************************************************************************/
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
/*
|
||||||
|
* This file has not been truncated
|
||||||
|
*/
|
||||||
|
|
||||||
@@ -0,0 +1,193 @@
|
|||||||
|
/**
|
||||||
|
@file AsyncAudioDecoderSpeex.h
|
||||||
|
@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
|
||||||
|
*/
|
||||||
|
|
||||||
|
#ifndef ASYNC_AUDIO_DECODER_SPEEX_INCLUDED
|
||||||
|
#define ASYNC_AUDIO_DECODER_SPEEX_INCLUDED
|
||||||
|
|
||||||
|
|
||||||
|
/****************************************************************************
|
||||||
|
*
|
||||||
|
* System Includes
|
||||||
|
*
|
||||||
|
****************************************************************************/
|
||||||
|
|
||||||
|
#include <speex/speex.h>
|
||||||
|
|
||||||
|
|
||||||
|
/****************************************************************************
|
||||||
|
*
|
||||||
|
* Project Includes
|
||||||
|
*
|
||||||
|
****************************************************************************/
|
||||||
|
|
||||||
|
#include <AsyncAudioDecoder.h>
|
||||||
|
|
||||||
|
|
||||||
|
/****************************************************************************
|
||||||
|
*
|
||||||
|
* 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
|
||||||
|
*/
|
||||||
|
|
||||||
@@ -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 <cstring>
|
||||||
|
#include <cmath>
|
||||||
|
#include <algorithm>
|
||||||
|
|
||||||
|
|
||||||
|
/****************************************************************************
|
||||||
|
*
|
||||||
|
* 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<fade_len-1; ++i)
|
||||||
|
{
|
||||||
|
fade_gain[i] = pow(2.0f, -15.0f * (static_cast<float>(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<mute_ext; ++i)
|
||||||
|
{
|
||||||
|
ptr = (ptr < size-1) ? ptr+1 : 0;
|
||||||
|
buf[ptr] *= currentFadeGain();
|
||||||
|
}
|
||||||
|
is_muted = true;
|
||||||
|
mute_cnt = 0;
|
||||||
|
}
|
||||||
|
else
|
||||||
|
{
|
||||||
|
if (mute_ext == 0)
|
||||||
|
{
|
||||||
|
fade_dir = -1; // Fade in
|
||||||
|
is_muted = false;
|
||||||
|
}
|
||||||
|
else
|
||||||
|
{
|
||||||
|
mute_cnt = mute_ext;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
} /* AudioDelayLine::mute */
|
||||||
|
|
||||||
|
|
||||||
|
void AudioDelayLine::clear(int time_ms)
|
||||||
|
{
|
||||||
|
int count;
|
||||||
|
if (time_ms < 0)
|
||||||
|
{
|
||||||
|
count = size;
|
||||||
|
}
|
||||||
|
else
|
||||||
|
{
|
||||||
|
count = min(size, time_ms * INTERNAL_SAMPLE_RATE / 1000);
|
||||||
|
}
|
||||||
|
|
||||||
|
//fade_pos = 0; // Reset fade gain
|
||||||
|
fade_dir = 1; // Fade out
|
||||||
|
ptr = (ptr + size - count) % size;
|
||||||
|
for (int i=0; i<count; ++i)
|
||||||
|
{
|
||||||
|
ptr = (ptr < size-1) ? ptr+1 : 0;
|
||||||
|
buf[ptr] *= currentFadeGain();
|
||||||
|
}
|
||||||
|
|
||||||
|
if (!is_muted)
|
||||||
|
{
|
||||||
|
fade_dir = -1; // Fade in again when new samples arrive
|
||||||
|
}
|
||||||
|
|
||||||
|
last_clear = max(0, count - fade_len);
|
||||||
|
|
||||||
|
} /* AudioDelayLine::clear */
|
||||||
|
|
||||||
|
|
||||||
|
int AudioDelayLine::writeSamples(const float *samples, int count)
|
||||||
|
{
|
||||||
|
flush_cnt = 0;
|
||||||
|
last_clear = 0;
|
||||||
|
|
||||||
|
count = min(count, size);
|
||||||
|
float output[count];
|
||||||
|
int out_ptr = ptr;
|
||||||
|
for (int i=0; i<count; ++i)
|
||||||
|
{
|
||||||
|
output[i] = buf[out_ptr];
|
||||||
|
out_ptr = (out_ptr < size-1) ? out_ptr+1 : 0;
|
||||||
|
}
|
||||||
|
|
||||||
|
int written = sinkWriteSamples(output, count);
|
||||||
|
|
||||||
|
for (int i=0; i<written; ++i)
|
||||||
|
{
|
||||||
|
buf[ptr] = samples[i] * currentFadeGain();
|
||||||
|
if (is_muted && (mute_cnt > 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<count; ++i)
|
||||||
|
{
|
||||||
|
output[i] = buf[out_ptr];
|
||||||
|
out_ptr = (out_ptr < size-1) ? out_ptr+1 : 0;
|
||||||
|
}
|
||||||
|
|
||||||
|
written = sinkWriteSamples(output, count);
|
||||||
|
|
||||||
|
for (int i=0; i<written; ++i)
|
||||||
|
{
|
||||||
|
buf[ptr] = 0;
|
||||||
|
ptr = (ptr < size-1) ? ptr+1 : 0;
|
||||||
|
}
|
||||||
|
|
||||||
|
flush_cnt -= written;
|
||||||
|
}
|
||||||
|
|
||||||
|
if (flush_cnt == 0)
|
||||||
|
{
|
||||||
|
sinkFlushSamples();
|
||||||
|
}
|
||||||
|
} /* AudioDelayLine::writeRemainingSamples */
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
/*
|
||||||
|
* This file has not been truncated
|
||||||
|
*/
|
||||||
|
|
||||||
@@ -0,0 +1,265 @@
|
|||||||
|
/**
|
||||||
|
@file AsyncAudioDelayLine.h
|
||||||
|
@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
|
||||||
|
*/
|
||||||
|
|
||||||
|
|
||||||
|
#ifndef ASYNC_AUDIO_DELAY_LINE_INCLUDED
|
||||||
|
#define ASYNC_AUDIO_DELAY_LINE_INCLUDED
|
||||||
|
|
||||||
|
|
||||||
|
/****************************************************************************
|
||||||
|
*
|
||||||
|
* System Includes
|
||||||
|
*
|
||||||
|
****************************************************************************/
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
/****************************************************************************
|
||||||
|
*
|
||||||
|
* Project Includes
|
||||||
|
*
|
||||||
|
****************************************************************************/
|
||||||
|
|
||||||
|
#include <AsyncAudioSink.h>
|
||||||
|
#include <AsyncAudioSource.h>
|
||||||
|
|
||||||
|
|
||||||
|
/****************************************************************************
|
||||||
|
*
|
||||||
|
* 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
|
||||||
|
*/
|
||||||
|
|
||||||
@@ -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 <cstring>
|
||||||
|
#include <algorithm>
|
||||||
|
#include <iostream>
|
||||||
|
#include <sstream>
|
||||||
|
|
||||||
|
|
||||||
|
/****************************************************************************
|
||||||
|
*
|
||||||
|
* 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<string, AudioDevice*> 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<AudioIO*>::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<string, AudioDevice*>::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<AudioIO*>::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<channels; ch++)
|
||||||
|
{
|
||||||
|
for (size_t i=0; i<frame_cnt; i++)
|
||||||
|
{
|
||||||
|
// no divisions in index calculation (embedded system performance !!!)
|
||||||
|
samples[i] = static_cast<float>(buf[i * channels + ch]) / 32768.0;
|
||||||
|
}
|
||||||
|
list<AudioIO*>::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<AudioIO*>::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<static_cast<size_t>(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<int16_t>(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
|
||||||
|
*/
|
||||||
|
|
||||||
@@ -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 <sigc++/sigc++.h>
|
||||||
|
#include <stdint.h>
|
||||||
|
|
||||||
|
#include <string>
|
||||||
|
#include <map>
|
||||||
|
#include <list>
|
||||||
|
|
||||||
|
|
||||||
|
/****************************************************************************
|
||||||
|
*
|
||||||
|
* Project Includes
|
||||||
|
*
|
||||||
|
****************************************************************************/
|
||||||
|
|
||||||
|
#include <AsyncTimer.h>
|
||||||
|
|
||||||
|
|
||||||
|
/****************************************************************************
|
||||||
|
*
|
||||||
|
* 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<std::string, AudioDevice*> devices;
|
||||||
|
|
||||||
|
Mode current_mode;
|
||||||
|
size_t use_count;
|
||||||
|
std::list<AudioIO*> 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
|
||||||
|
*/
|
||||||
|
|
||||||
@@ -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 <sigc++/sigc++.h>
|
||||||
|
#include <poll.h>
|
||||||
|
#include <iostream>
|
||||||
|
#include <sstream>
|
||||||
|
#include <cmath>
|
||||||
|
#include <cstring>
|
||||||
|
|
||||||
|
|
||||||
|
/****************************************************************************
|
||||||
|
*
|
||||||
|
* Project Includes
|
||||||
|
*
|
||||||
|
****************************************************************************/
|
||||||
|
|
||||||
|
#include <AsyncFdWatch.h>
|
||||||
|
|
||||||
|
|
||||||
|
/****************************************************************************
|
||||||
|
*
|
||||||
|
* 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<FdWatch*>::const_iterator cii;
|
||||||
|
for(cii = watch_list.begin(); cii != watch_list.end(); ++cii)
|
||||||
|
{
|
||||||
|
delete *cii;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
void setEnabled(bool enable)
|
||||||
|
{
|
||||||
|
std::list<FdWatch*>::const_iterator cii;
|
||||||
|
for(cii = watch_list.begin(); cii != watch_list.end(); ++cii)
|
||||||
|
{
|
||||||
|
(*cii)->setEnabled(enable);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
sigc::signal<void(FdWatch*, unsigned short)> activity;
|
||||||
|
|
||||||
|
private:
|
||||||
|
std::map<int, pollfd> pfd_map;
|
||||||
|
std::list<FdWatch*> 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<size_t>(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<size_t>(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<int>(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
|
||||||
|
*/
|
||||||
@@ -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 <alsa/asoundlib.h>
|
||||||
|
|
||||||
|
|
||||||
|
/****************************************************************************
|
||||||
|
*
|
||||||
|
* 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
|
||||||
|
*/
|
||||||
|
|
||||||
@@ -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 <iostream>
|
||||||
|
|
||||||
|
|
||||||
|
/****************************************************************************
|
||||||
|
*
|
||||||
|
* 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
|
||||||
|
*/
|
||||||
|
|
||||||
@@ -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 <string>
|
||||||
|
#include <map>
|
||||||
|
|
||||||
|
|
||||||
|
/****************************************************************************
|
||||||
|
*
|
||||||
|
* 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<std::string, CreatorFunc> 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
|
||||||
|
*/
|
||||||
|
|
||||||
@@ -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 <sys/types.h>
|
||||||
|
#include <sys/stat.h>
|
||||||
|
#include <fcntl.h>
|
||||||
|
#include <sys/soundcard.h>
|
||||||
|
#include <sys/ioctl.h>
|
||||||
|
#include <unistd.h>
|
||||||
|
#include <stdlib.h>
|
||||||
|
|
||||||
|
#include <cassert>
|
||||||
|
#include <cstring>
|
||||||
|
#include <algorithm>
|
||||||
|
#include <cmath>
|
||||||
|
#include <cstdio>
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
/****************************************************************************
|
||||||
|
*
|
||||||
|
* Project Includes
|
||||||
|
*
|
||||||
|
****************************************************************************/
|
||||||
|
|
||||||
|
#include <AsyncFdWatch.h>
|
||||||
|
|
||||||
|
|
||||||
|
/****************************************************************************
|
||||||
|
*
|
||||||
|
* 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<int>(log2(size));
|
||||||
|
arg = static_cast<int>((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<int>(channels);
|
||||||
|
if(ioctl(fd, SNDCTL_DSP_CHANNELS, &arg) == -1)
|
||||||
|
{
|
||||||
|
perror("SNDCTL_DSP_CHANNELS ioctl failed");
|
||||||
|
close();
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
if(arg != static_cast<int>(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<size_t>(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<unsigned>(written) == frags_read * frag_size);
|
||||||
|
|
||||||
|
} while(frags_read == fragments);
|
||||||
|
|
||||||
|
watch->setEnabled(true);
|
||||||
|
|
||||||
|
} /* AudioDeviceOSS::writeSpaceAvailable */
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
/*
|
||||||
|
* This file has not been truncated
|
||||||
|
*/
|
||||||
|
|
||||||
@@ -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 <string>
|
||||||
|
|
||||||
|
|
||||||
|
/****************************************************************************
|
||||||
|
*
|
||||||
|
* 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
|
||||||
|
*/
|
||||||
|
|
||||||
@@ -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 <sys/ioctl.h>
|
||||||
|
|
||||||
|
#include <cassert>
|
||||||
|
#include <cstdio>
|
||||||
|
#include <cstring>
|
||||||
|
#include <cstdlib>
|
||||||
|
#include <vector>
|
||||||
|
#include <sstream>
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
/****************************************************************************
|
||||||
|
*
|
||||||
|
* Project Includes
|
||||||
|
*
|
||||||
|
****************************************************************************/
|
||||||
|
|
||||||
|
#include <AsyncUdpSocket.h>
|
||||||
|
#include <AsyncIpAddress.h>
|
||||||
|
|
||||||
|
|
||||||
|
/****************************************************************************
|
||||||
|
*
|
||||||
|
* 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
|
||||||
|
*/
|
||||||
|
|
||||||
@@ -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 <string>
|
||||||
|
|
||||||
|
|
||||||
|
/****************************************************************************
|
||||||
|
*
|
||||||
|
* Project Includes
|
||||||
|
*
|
||||||
|
****************************************************************************/
|
||||||
|
|
||||||
|
#include <AsyncIpAddress.h>
|
||||||
|
#include <AsyncTimer.h>
|
||||||
|
|
||||||
|
|
||||||
|
/****************************************************************************
|
||||||
|
*
|
||||||
|
* 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
|
||||||
|
*/
|
||||||
|
|
||||||
@@ -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
|
||||||
|
*/
|
||||||
|
|
||||||
@@ -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 <sigc++/sigc++.h>
|
||||||
|
#include <string>
|
||||||
|
|
||||||
|
|
||||||
|
/****************************************************************************
|
||||||
|
*
|
||||||
|
* Project Includes
|
||||||
|
*
|
||||||
|
****************************************************************************/
|
||||||
|
|
||||||
|
#include <AsyncAudioSink.h>
|
||||||
|
|
||||||
|
|
||||||
|
/****************************************************************************
|
||||||
|
*
|
||||||
|
* 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<void(const void*, int)> writeEncodedSamples;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @brief This signal is emitted when the source calls flushSamples
|
||||||
|
*/
|
||||||
|
sigc::signal<void()> flushEncodedSamples;
|
||||||
|
|
||||||
|
|
||||||
|
protected:
|
||||||
|
|
||||||
|
private:
|
||||||
|
AudioEncoder(const AudioEncoder&);
|
||||||
|
AudioEncoder& operator=(const AudioEncoder&);
|
||||||
|
|
||||||
|
}; /* class AudioEncoder */
|
||||||
|
|
||||||
|
|
||||||
|
} /* namespace */
|
||||||
|
|
||||||
|
#endif /* ASYNC_AUDIO_ENCODER_INCLUDED */
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
/*
|
||||||
|
* This file has not been truncated
|
||||||
|
*/
|
||||||
|
|
||||||
@@ -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 <AsyncAudioEncoder.h>
|
||||||
|
|
||||||
|
|
||||||
|
/****************************************************************************
|
||||||
|
*
|
||||||
|
* 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
|
||||||
|
*/
|
||||||
|
|
||||||
@@ -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 <stdint.h>
|
||||||
|
|
||||||
|
|
||||||
|
/****************************************************************************
|
||||||
|
*
|
||||||
|
* 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<count; ++i)
|
||||||
|
{
|
||||||
|
float sample = samples[i];
|
||||||
|
if (sample > 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<gsm_signal>(sample * 32767.0);
|
||||||
|
}
|
||||||
|
|
||||||
|
if (gsm_buf_len == GSM_BUF_SIZE)
|
||||||
|
{
|
||||||
|
gsm_buf_len = 0;
|
||||||
|
|
||||||
|
gsm_frame frame[FRAME_COUNT];
|
||||||
|
for (int frameno=0; frameno<FRAME_COUNT; ++frameno)
|
||||||
|
{
|
||||||
|
gsm_encode(gsmh, gsm_buf + frameno * FRAME_SAMPLE_CNT, frame[frameno]);
|
||||||
|
}
|
||||||
|
|
||||||
|
writeEncodedSamples(frame, FRAME_COUNT * sizeof(gsm_frame));
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
return count;
|
||||||
|
|
||||||
|
} /* AudioEncoderGsm::writeSamples */
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
/****************************************************************************
|
||||||
|
*
|
||||||
|
* Protected member functions
|
||||||
|
*
|
||||||
|
****************************************************************************/
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
/****************************************************************************
|
||||||
|
*
|
||||||
|
* Private member functions
|
||||||
|
*
|
||||||
|
****************************************************************************/
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
/*
|
||||||
|
* This file has not been truncated
|
||||||
|
*/
|
||||||
|
|
||||||
@@ -0,0 +1,182 @@
|
|||||||
|
/**
|
||||||
|
@file AsyncAudioEncoderGsm.h
|
||||||
|
@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
|
||||||
|
*/
|
||||||
|
|
||||||
|
#ifndef ASYNC_AUDIO_ENCODER_GSM_INCLUDED
|
||||||
|
#define ASYNC_AUDIO_ENCODER_GSM_INCLUDED
|
||||||
|
|
||||||
|
|
||||||
|
/****************************************************************************
|
||||||
|
*
|
||||||
|
* System Includes
|
||||||
|
*
|
||||||
|
****************************************************************************/
|
||||||
|
|
||||||
|
extern "C" {
|
||||||
|
#include <gsm.h>
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
/****************************************************************************
|
||||||
|
*
|
||||||
|
* Project Includes
|
||||||
|
*
|
||||||
|
****************************************************************************/
|
||||||
|
|
||||||
|
#include <AsyncAudioEncoder.h>
|
||||||
|
|
||||||
|
|
||||||
|
/****************************************************************************
|
||||||
|
*
|
||||||
|
* 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
|
||||||
|
*/
|
||||||
|
|
||||||
@@ -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 <limits>
|
||||||
|
#include <stdint.h>
|
||||||
|
|
||||||
|
|
||||||
|
/****************************************************************************
|
||||||
|
*
|
||||||
|
* Project Includes
|
||||||
|
*
|
||||||
|
****************************************************************************/
|
||||||
|
|
||||||
|
#include <AsyncAudioEncoder.h>
|
||||||
|
|
||||||
|
|
||||||
|
/****************************************************************************
|
||||||
|
*
|
||||||
|
* 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<uint16_t>::max())
|
||||||
|
{
|
||||||
|
count = std::numeric_limits<uint16_t>::max();
|
||||||
|
}
|
||||||
|
else if (count < 0)
|
||||||
|
{
|
||||||
|
return -1;
|
||||||
|
}
|
||||||
|
uint8_t buf[2];
|
||||||
|
buf[0] = static_cast<uint8_t>(count & 0xff);
|
||||||
|
buf[1] = static_cast<uint8_t>(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
|
||||||
|
*/
|
||||||
|
|
||||||
@@ -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 <iostream>
|
||||||
|
#include <cassert>
|
||||||
|
#include <cstdlib>
|
||||||
|
#include <sstream>
|
||||||
|
|
||||||
|
|
||||||
|
/****************************************************************************
|
||||||
|
*
|
||||||
|
* 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<int>(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<count; ++i)
|
||||||
|
{
|
||||||
|
sample_buf[buf_len++] = samples[i];
|
||||||
|
|
||||||
|
if (buf_len == frame_size)
|
||||||
|
{
|
||||||
|
buf_len = 0;
|
||||||
|
unsigned char output_buf[4000];
|
||||||
|
opus_int32 nbytes = opus_encode_float(enc, sample_buf, frame_size,
|
||||||
|
output_buf, sizeof(output_buf));
|
||||||
|
//cout << "### frame_size=" << frame_size << " nbytes=" << nbytes << endl;
|
||||||
|
if (nbytes > 0)
|
||||||
|
{
|
||||||
|
writeEncodedSamples(output_buf, nbytes);
|
||||||
|
}
|
||||||
|
else if (nbytes < 0)
|
||||||
|
{
|
||||||
|
cerr << "**** ERROR: Opus encoder error: " << opus_strerror(frame_size)
|
||||||
|
<< endl;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
return count;
|
||||||
|
|
||||||
|
} /* AudioEncoderOpus::writeSamples */
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
/****************************************************************************
|
||||||
|
*
|
||||||
|
* Protected member functions
|
||||||
|
*
|
||||||
|
****************************************************************************/
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
/****************************************************************************
|
||||||
|
*
|
||||||
|
* Private member functions
|
||||||
|
*
|
||||||
|
****************************************************************************/
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
/*
|
||||||
|
* This file has not been truncated
|
||||||
|
*/
|
||||||
|
|
||||||
@@ -0,0 +1,439 @@
|
|||||||
|
/**
|
||||||
|
@file AsyncAudioEncoderOpus.h
|
||||||
|
@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
|
||||||
|
*/
|
||||||
|
|
||||||
|
#ifndef ASYNC_AUDIO_ENCODER_OPUS_INCLUDED
|
||||||
|
#define ASYNC_AUDIO_ENCODER_OPUS_INCLUDED
|
||||||
|
|
||||||
|
|
||||||
|
/****************************************************************************
|
||||||
|
*
|
||||||
|
* System Includes
|
||||||
|
*
|
||||||
|
****************************************************************************/
|
||||||
|
|
||||||
|
#include <opus.h>
|
||||||
|
|
||||||
|
|
||||||
|
/****************************************************************************
|
||||||
|
*
|
||||||
|
* Project Includes
|
||||||
|
*
|
||||||
|
****************************************************************************/
|
||||||
|
|
||||||
|
#include <AsyncAudioEncoder.h>
|
||||||
|
|
||||||
|
|
||||||
|
/****************************************************************************
|
||||||
|
*
|
||||||
|
* 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
|
||||||
|
*/
|
||||||
|
|
||||||
@@ -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 <AsyncAudioEncoder.h>
|
||||||
|
|
||||||
|
|
||||||
|
/****************************************************************************
|
||||||
|
*
|
||||||
|
* 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
|
||||||
|
*/
|
||||||
|
|
||||||
@@ -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 <stdint.h>
|
||||||
|
|
||||||
|
|
||||||
|
/****************************************************************************
|
||||||
|
*
|
||||||
|
* 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<count; ++i)
|
||||||
|
{
|
||||||
|
float sample = samples[i];
|
||||||
|
if (sample > 1.0)
|
||||||
|
{
|
||||||
|
s16_samples[i] = 32767;
|
||||||
|
}
|
||||||
|
else if (sample < -1.0)
|
||||||
|
{
|
||||||
|
s16_samples[i] = -32767;
|
||||||
|
}
|
||||||
|
else
|
||||||
|
{
|
||||||
|
s16_samples[i] = static_cast<int16_t>(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
|
||||||
|
*/
|
||||||
|
|
||||||
@@ -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 <AsyncAudioEncoder.h>
|
||||||
|
|
||||||
|
|
||||||
|
/****************************************************************************
|
||||||
|
*
|
||||||
|
* 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
|
||||||
|
*/
|
||||||
|
|
||||||
@@ -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 <iostream>
|
||||||
|
#include <cassert>
|
||||||
|
#include <cstdlib>
|
||||||
|
|
||||||
|
|
||||||
|
/****************************************************************************
|
||||||
|
*
|
||||||
|
* 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<count; ++i)
|
||||||
|
{
|
||||||
|
sample_buf[buf_len++] = samples[i] * 32767.0;
|
||||||
|
|
||||||
|
if (buf_len == frame_size)
|
||||||
|
{
|
||||||
|
speex_encode(enc_state, sample_buf, &bits);
|
||||||
|
buf_len = 0;
|
||||||
|
|
||||||
|
if (++frame_cnt == frames_per_packet)
|
||||||
|
{
|
||||||
|
frame_cnt = 0;
|
||||||
|
speex_bits_insert_terminator(&bits);
|
||||||
|
int nbytes = speex_bits_nbytes(&bits);
|
||||||
|
char output_buf[nbytes];
|
||||||
|
nbytes = speex_bits_write(&bits, output_buf, nbytes);
|
||||||
|
speex_bits_reset(&bits);
|
||||||
|
//cout << "writing " << nbytes << " bytes\n";
|
||||||
|
writeEncodedSamples(output_buf, nbytes);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
return count;
|
||||||
|
|
||||||
|
} /* AudioEncoderSpeex::writeSamples */
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
/****************************************************************************
|
||||||
|
*
|
||||||
|
* Protected member functions
|
||||||
|
*
|
||||||
|
****************************************************************************/
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
/****************************************************************************
|
||||||
|
*
|
||||||
|
* Private member functions
|
||||||
|
*
|
||||||
|
****************************************************************************/
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
/*
|
||||||
|
* This file has not been truncated
|
||||||
|
*/
|
||||||
|
|
||||||
@@ -0,0 +1,290 @@
|
|||||||
|
/**
|
||||||
|
@file AsyncAudioEncoderSpeex.h
|
||||||
|
@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
|
||||||
|
*/
|
||||||
|
|
||||||
|
#ifndef ASYNC_AUDIO_ENCODER_SPEEX_INCLUDED
|
||||||
|
#define ASYNC_AUDIO_ENCODER_SPEEX_INCLUDED
|
||||||
|
|
||||||
|
|
||||||
|
/****************************************************************************
|
||||||
|
*
|
||||||
|
* System Includes
|
||||||
|
*
|
||||||
|
****************************************************************************/
|
||||||
|
|
||||||
|
#include <speex/speex.h>
|
||||||
|
|
||||||
|
|
||||||
|
/****************************************************************************
|
||||||
|
*
|
||||||
|
* Project Includes
|
||||||
|
*
|
||||||
|
****************************************************************************/
|
||||||
|
|
||||||
|
#include <AsyncAudioEncoder.h>
|
||||||
|
|
||||||
|
|
||||||
|
/****************************************************************************
|
||||||
|
*
|
||||||
|
* 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
|
||||||
|
*/
|
||||||
|
|
||||||
@@ -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 <cstdio>
|
||||||
|
#include <cstring>
|
||||||
|
#include <algorithm>
|
||||||
|
#include <cassert>
|
||||||
|
|
||||||
|
|
||||||
|
/****************************************************************************
|
||||||
|
*
|
||||||
|
* 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
|
||||||
|
*/
|
||||||
@@ -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 <AsyncAudioSink.h>
|
||||||
|
#include <AsyncAudioSource.h>
|
||||||
|
|
||||||
|
|
||||||
|
/****************************************************************************
|
||||||
|
*
|
||||||
|
* 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
|
||||||
|
*/
|
||||||
|
|
||||||
@@ -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 <iostream>
|
||||||
|
|
||||||
|
#include <cstring>
|
||||||
|
#include <cstdlib>
|
||||||
|
#include <cmath>
|
||||||
|
#include <locale>
|
||||||
|
|
||||||
|
|
||||||
|
/****************************************************************************
|
||||||
|
*
|
||||||
|
* 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; i<count; ++i)
|
||||||
|
{
|
||||||
|
dest[i] = output_gain * fv->func(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
|
||||||
|
*/
|
||||||
|
|
||||||
@@ -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 <string>
|
||||||
|
|
||||||
|
|
||||||
|
/****************************************************************************
|
||||||
|
*
|
||||||
|
* Project Includes
|
||||||
|
*
|
||||||
|
****************************************************************************/
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
/****************************************************************************
|
||||||
|
*
|
||||||
|
* Local Includes
|
||||||
|
*
|
||||||
|
****************************************************************************/
|
||||||
|
|
||||||
|
#include <AsyncAudioProcessor.h>
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
/****************************************************************************
|
||||||
|
*
|
||||||
|
* 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
|
||||||
|
*/
|
||||||
|
|
||||||
@@ -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 <cmath>
|
||||||
|
#include <cstring>
|
||||||
|
#include <cassert>
|
||||||
|
#include <iostream>
|
||||||
|
|
||||||
|
|
||||||
|
/****************************************************************************
|
||||||
|
*
|
||||||
|
* 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<Resonator*>::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; i<count; ++i)
|
||||||
|
{
|
||||||
|
float destN = m_combN->processSample(src[i]);
|
||||||
|
float dest2 = m_comb2->processSample(destN);
|
||||||
|
dest[i] = 0.0f;
|
||||||
|
for (std::vector<Resonator*>::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
|
||||||
|
*/
|
||||||
|
|
||||||
@@ -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 <vector>
|
||||||
|
|
||||||
|
|
||||||
|
/****************************************************************************
|
||||||
|
*
|
||||||
|
* Project Includes
|
||||||
|
*
|
||||||
|
****************************************************************************/
|
||||||
|
|
||||||
|
#include <AsyncAudioProcessor.h>
|
||||||
|
|
||||||
|
|
||||||
|
/****************************************************************************
|
||||||
|
*
|
||||||
|
* 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<Resonator*> m_resonators;
|
||||||
|
|
||||||
|
AudioFsf(const AudioFsf&);
|
||||||
|
AudioFsf& operator=(const AudioFsf&);
|
||||||
|
|
||||||
|
}; /* class AudioFsf */
|
||||||
|
|
||||||
|
|
||||||
|
} /* namespace */
|
||||||
|
|
||||||
|
#endif /* ASYNC_AUDIO_FSF_INCLUDED */
|
||||||
|
|
||||||
|
/*
|
||||||
|
* This file has not been truncated
|
||||||
|
*/
|
||||||
@@ -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 <cmath>
|
||||||
|
#include <cassert>
|
||||||
|
#include <iostream>
|
||||||
|
|
||||||
|
|
||||||
|
/****************************************************************************
|
||||||
|
*
|
||||||
|
* Project Includes
|
||||||
|
*
|
||||||
|
****************************************************************************/
|
||||||
|
|
||||||
|
#include <AsyncAudioSource.h>
|
||||||
|
|
||||||
|
|
||||||
|
/****************************************************************************
|
||||||
|
*
|
||||||
|
* 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<BLOCK_SIZE; ++i)
|
||||||
|
{
|
||||||
|
switch (m_waveform)
|
||||||
|
{
|
||||||
|
case SIN:
|
||||||
|
buf[i] = m_peak * sinf(arg);
|
||||||
|
break;
|
||||||
|
case SQUARE:
|
||||||
|
buf[i] = (arg < M_PI) ? m_peak : -m_peak;
|
||||||
|
break;
|
||||||
|
case TRIANGLE:
|
||||||
|
if (arg < M_PI / 2.0f)
|
||||||
|
{
|
||||||
|
buf[i] = m_peak * arg * 2.0f / M_PI;
|
||||||
|
}
|
||||||
|
else if (arg < M_PI)
|
||||||
|
{
|
||||||
|
buf[i] = m_peak * (2.0f - 2.0 * arg / M_PI);
|
||||||
|
}
|
||||||
|
else if (arg < 3.0f * M_PI / 2.0f)
|
||||||
|
{
|
||||||
|
buf[i] = -m_peak * (2.0f * arg / M_PI - 2.0f);
|
||||||
|
}
|
||||||
|
else
|
||||||
|
{
|
||||||
|
buf[i] = -m_peak * (4.0f - 2.0f * arg / M_PI);
|
||||||
|
}
|
||||||
|
break;
|
||||||
|
default:
|
||||||
|
buf[i] = 0;
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
arg += m_arginc;
|
||||||
|
if (arg >= 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
|
||||||
|
*/
|
||||||
@@ -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 <sigc++/sigc++.h>
|
||||||
|
#include <stdint.h>
|
||||||
|
|
||||||
|
#include <cstdio>
|
||||||
|
#include <cassert>
|
||||||
|
#include <cerrno>
|
||||||
|
#include <cmath>
|
||||||
|
|
||||||
|
|
||||||
|
/****************************************************************************
|
||||||
|
*
|
||||||
|
* 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; i<samples_read; ++i)
|
||||||
|
{
|
||||||
|
samples[i] = m_gain * samples[i];
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
return samples_read;
|
||||||
|
|
||||||
|
} /* AudioIO::readSamples */
|
||||||
|
|
||||||
|
|
||||||
|
bool AudioIO::doFlush(void) const
|
||||||
|
{
|
||||||
|
//printf("AudioIO::doFlush\n");
|
||||||
|
return input_fifo->doFlush();
|
||||||
|
} /* 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
|
||||||
|
*/
|
||||||
|
|
||||||
@@ -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 <cstdio>
|
||||||
|
#include <string>
|
||||||
|
|
||||||
|
|
||||||
|
/****************************************************************************
|
||||||
|
*
|
||||||
|
* Project Includes
|
||||||
|
*
|
||||||
|
****************************************************************************/
|
||||||
|
|
||||||
|
#include <AsyncFdWatch.h>
|
||||||
|
#include <AsyncTimer.h>
|
||||||
|
#include <AsyncAudioSink.h>
|
||||||
|
#include <AsyncAudioSource.h>
|
||||||
|
|
||||||
|
|
||||||
|
/****************************************************************************
|
||||||
|
*
|
||||||
|
* 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
|
||||||
|
*/
|
||||||
|
|
||||||
@@ -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 <cstring>
|
||||||
|
|
||||||
|
|
||||||
|
/****************************************************************************
|
||||||
|
*
|
||||||
|
* 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
|
||||||
|
*/
|
||||||
|
|
||||||
@@ -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 <AsyncAudioProcessor.h>
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
/****************************************************************************
|
||||||
|
*
|
||||||
|
* 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
|
||||||
|
*/
|
||||||
|
|
||||||
@@ -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 <cstdio>
|
||||||
|
#include <cstring>
|
||||||
|
#include <algorithm>
|
||||||
|
#include <cassert>
|
||||||
|
|
||||||
|
|
||||||
|
/****************************************************************************
|
||||||
|
*
|
||||||
|
* 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<MAX_WRITE_SIZE; i++)
|
||||||
|
{
|
||||||
|
silence[i] = 0;
|
||||||
|
}
|
||||||
|
unsigned timeout = (fifo_size << 4) / MAX_WRITE_SIZE;
|
||||||
|
do
|
||||||
|
{
|
||||||
|
samples_written = sinkWriteSamples(silence, MAX_WRITE_SIZE);
|
||||||
|
} while ((samples_written > 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
|
||||||
|
*/
|
||||||
@@ -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 <AsyncAudioSink.h>
|
||||||
|
#include <AsyncAudioSource.h>
|
||||||
|
|
||||||
|
|
||||||
|
/****************************************************************************
|
||||||
|
*
|
||||||
|
* 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
|
||||||
|
*/
|
||||||
|
|
||||||
@@ -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 <dirent.h>
|
||||||
|
#include <stdio.h>
|
||||||
|
#include <stdlib.h>
|
||||||
|
|
||||||
|
#include <dlfcn.h>
|
||||||
|
#include <cmath>
|
||||||
|
#include <cstring>
|
||||||
|
#include <iostream>
|
||||||
|
|
||||||
|
|
||||||
|
/****************************************************************************
|
||||||
|
*
|
||||||
|
* 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<InstanceInfo>();
|
||||||
|
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; i<m_desc->PortCount; ++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; i<m_desc->PortCount; ++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; i<m_desc->PortCount; ++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<LADSPA_Data*>(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
|
||||||
|
*/
|
||||||
@@ -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 <ladspa.h>
|
||||||
|
};
|
||||||
|
|
||||||
|
#include <string>
|
||||||
|
#include <map>
|
||||||
|
#include <memory>
|
||||||
|
#include <limits>
|
||||||
|
|
||||||
|
|
||||||
|
/****************************************************************************
|
||||||
|
*
|
||||||
|
* Project Includes
|
||||||
|
*
|
||||||
|
****************************************************************************/
|
||||||
|
|
||||||
|
#include <AsyncAudioProcessor.h>
|
||||||
|
|
||||||
|
|
||||||
|
/****************************************************************************
|
||||||
|
*
|
||||||
|
* 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<PortNumber>::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<InstanceInfo>;
|
||||||
|
using LabelMap = std::map<std::string, InstanceInfoP>;
|
||||||
|
using IDMap = std::map<UniqueID, InstanceInfoP>;
|
||||||
|
|
||||||
|
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
|
||||||
|
*/
|
||||||
@@ -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 <algorithm>
|
||||||
|
#include <cstring>
|
||||||
|
|
||||||
|
|
||||||
|
/****************************************************************************
|
||||||
|
*
|
||||||
|
* Project Includes
|
||||||
|
*
|
||||||
|
****************************************************************************/
|
||||||
|
|
||||||
|
#include <AsyncTimer.h>
|
||||||
|
|
||||||
|
|
||||||
|
/****************************************************************************
|
||||||
|
*
|
||||||
|
* 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<MixerSrc *>::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<MixerSrc *>::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<MixerSrc *>::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<samples_to_read; ++i)
|
||||||
|
{
|
||||||
|
outbuf[i] += tmp[i];
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
outbuf_pos = 0;
|
||||||
|
outbuf_cnt = samples_to_read;
|
||||||
|
}
|
||||||
|
} while (samples_written > 0);
|
||||||
|
|
||||||
|
output_stopped = (samples_written == 0);
|
||||||
|
|
||||||
|
} /* AudioMixer::outputHandler */
|
||||||
|
|
||||||
|
|
||||||
|
void AudioMixer::checkFlush(void)
|
||||||
|
{
|
||||||
|
if (is_flushed)
|
||||||
|
{
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
|
list<MixerSrc *>::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
|
||||||
|
*/
|
||||||
|
|
||||||
@@ -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 <list>
|
||||||
|
|
||||||
|
|
||||||
|
/****************************************************************************
|
||||||
|
*
|
||||||
|
* Project Includes
|
||||||
|
*
|
||||||
|
****************************************************************************/
|
||||||
|
|
||||||
|
#include <AsyncAudioSource.h>
|
||||||
|
#include <AsyncTimer.h>
|
||||||
|
|
||||||
|
|
||||||
|
/****************************************************************************
|
||||||
|
*
|
||||||
|
* 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<MixerSrc *> 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
|
||||||
|
*/
|
||||||
|
|
||||||
@@ -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 <iostream>
|
||||||
|
|
||||||
|
#include <cstring>
|
||||||
|
#include <cstdlib>
|
||||||
|
#include <cmath>
|
||||||
|
#include <locale>
|
||||||
|
#include <limits>
|
||||||
|
|
||||||
|
|
||||||
|
/****************************************************************************
|
||||||
|
*
|
||||||
|
* 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<count; ++i)
|
||||||
|
{
|
||||||
|
float noise = generateGaussianNoise();
|
||||||
|
dest[i] = src[i] + noise;
|
||||||
|
}
|
||||||
|
} /* AudioNoiseAdder::writeSamples */
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
/****************************************************************************
|
||||||
|
*
|
||||||
|
* Private member functions
|
||||||
|
*
|
||||||
|
****************************************************************************/
|
||||||
|
|
||||||
|
float AudioNoiseAdder::generateGaussianNoise(void)
|
||||||
|
{
|
||||||
|
static const float epsilon = std::numeric_limits<float>::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
|
||||||
|
*/
|
||||||
@@ -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 <string>
|
||||||
|
|
||||||
|
|
||||||
|
/****************************************************************************
|
||||||
|
*
|
||||||
|
* Project Includes
|
||||||
|
*
|
||||||
|
****************************************************************************/
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
/****************************************************************************
|
||||||
|
*
|
||||||
|
* Local Includes
|
||||||
|
*
|
||||||
|
****************************************************************************/
|
||||||
|
|
||||||
|
#include <AsyncAudioProcessor.h>
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
/****************************************************************************
|
||||||
|
*
|
||||||
|
* 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
|
||||||
|
*/
|
||||||
@@ -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 <stdio.h>
|
||||||
|
|
||||||
|
#include <algorithm>
|
||||||
|
#include <cstring>
|
||||||
|
#include <cassert>
|
||||||
|
|
||||||
|
|
||||||
|
/****************************************************************************
|
||||||
|
*
|
||||||
|
* Project Includes
|
||||||
|
*
|
||||||
|
****************************************************************************/
|
||||||
|
|
||||||
|
#include <AsyncTimer.h>
|
||||||
|
|
||||||
|
|
||||||
|
/****************************************************************************
|
||||||
|
*
|
||||||
|
* 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
|
||||||
|
*/
|
||||||
|
|
||||||
@@ -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 <sigc++/sigc++.h>
|
||||||
|
|
||||||
|
|
||||||
|
/****************************************************************************
|
||||||
|
*
|
||||||
|
* Project Includes
|
||||||
|
*
|
||||||
|
****************************************************************************/
|
||||||
|
|
||||||
|
#include <AsyncAudioSink.h>
|
||||||
|
#include <AsyncAudioSource.h>
|
||||||
|
|
||||||
|
|
||||||
|
/****************************************************************************
|
||||||
|
*
|
||||||
|
* 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
|
||||||
|
*/
|
||||||
|
|
||||||
@@ -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 <AsyncAudioSink.h>
|
||||||
|
#include <AsyncAudioSource.h>
|
||||||
|
|
||||||
|
|
||||||
|
/****************************************************************************
|
||||||
|
*
|
||||||
|
* 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
|
||||||
|
*/
|
||||||
|
|
||||||
@@ -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 <iostream>
|
||||||
|
#include <algorithm>
|
||||||
|
|
||||||
|
#include <cstring>
|
||||||
|
|
||||||
|
|
||||||
|
/****************************************************************************
|
||||||
|
*
|
||||||
|
* Project Includes
|
||||||
|
*
|
||||||
|
****************************************************************************/
|
||||||
|
|
||||||
|
#include <AsyncApplication.h>
|
||||||
|
|
||||||
|
|
||||||
|
/****************************************************************************
|
||||||
|
*
|
||||||
|
* 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
|
||||||
|
*/
|
||||||
|
|
||||||
@@ -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 <sigc++/sigc++.h>
|
||||||
|
#include <string>
|
||||||
|
|
||||||
|
|
||||||
|
/****************************************************************************
|
||||||
|
*
|
||||||
|
* Project Includes
|
||||||
|
*
|
||||||
|
****************************************************************************/
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
/****************************************************************************
|
||||||
|
*
|
||||||
|
* Local Includes
|
||||||
|
*
|
||||||
|
****************************************************************************/
|
||||||
|
|
||||||
|
#include <AsyncAudioSource.h>
|
||||||
|
#include <AsyncAudioSink.h>
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
/****************************************************************************
|
||||||
|
*
|
||||||
|
* 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
|
||||||
|
*/
|
||||||
|
|
||||||
@@ -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 <algorithm>
|
||||||
|
#include <cstring>
|
||||||
|
#include <cassert>
|
||||||
|
|
||||||
|
|
||||||
|
/****************************************************************************
|
||||||
|
*
|
||||||
|
* 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
|
||||||
|
*/
|
||||||
|
|
||||||
@@ -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 <AsyncAudioSink.h>
|
||||||
|
|
||||||
|
|
||||||
|
/****************************************************************************
|
||||||
|
*
|
||||||
|
* 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
|
||||||
|
*/
|
||||||
|
|
||||||
@@ -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 <cassert>
|
||||||
|
#include <cstdio>
|
||||||
|
#include <cstring>
|
||||||
|
#include <cerrno>
|
||||||
|
#include <algorithm>
|
||||||
|
#include <sstream>
|
||||||
|
#include <sys/time.h>
|
||||||
|
|
||||||
|
|
||||||
|
/****************************************************************************
|
||||||
|
*
|
||||||
|
* 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<unsigned>(count), max_samples - samples_written);
|
||||||
|
}
|
||||||
|
|
||||||
|
gettimeofday(&end_timestamp, NULL);
|
||||||
|
if (!timerisset(&begin_timestamp))
|
||||||
|
{
|
||||||
|
long usec = static_cast<long>(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<count; ++i)
|
||||||
|
{
|
||||||
|
float sample = samples[i];
|
||||||
|
if (sample > 1)
|
||||||
|
{
|
||||||
|
buf[i] = 32767;
|
||||||
|
}
|
||||||
|
else if (sample < -1)
|
||||||
|
{
|
||||||
|
buf[i] = -32767;
|
||||||
|
}
|
||||||
|
else
|
||||||
|
{
|
||||||
|
buf[i] = static_cast<short>(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
|
||||||
|
*/
|
||||||
|
|
||||||
@@ -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 <stdio.h>
|
||||||
|
#include <stdint.h>
|
||||||
|
#include <sigc++/sigc++.h>
|
||||||
|
#include <sys/time.h>
|
||||||
|
|
||||||
|
#include <string>
|
||||||
|
|
||||||
|
#include <AsyncAudioSink.h>
|
||||||
|
|
||||||
|
|
||||||
|
/****************************************************************************
|
||||||
|
*
|
||||||
|
* 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<void()> 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<void()> 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
|
||||||
|
*/
|
||||||
|
|
||||||
@@ -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 <AsyncAudioSink.h>
|
||||||
|
|
||||||
|
|
||||||
|
/****************************************************************************
|
||||||
|
*
|
||||||
|
* 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<AudioSource*>(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
|
||||||
|
*/
|
||||||
@@ -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 <map>
|
||||||
|
|
||||||
|
|
||||||
|
/****************************************************************************
|
||||||
|
*
|
||||||
|
* Project Includes
|
||||||
|
*
|
||||||
|
****************************************************************************/
|
||||||
|
|
||||||
|
#include <AsyncAudioSource.h>
|
||||||
|
|
||||||
|
|
||||||
|
/****************************************************************************
|
||||||
|
*
|
||||||
|
* 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<Async::AudioSource *, Branch *> 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
|
||||||
|
*/
|
||||||
|
|
||||||
@@ -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
|
||||||
|
*/
|
||||||
|
|
||||||
@@ -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 <cassert>
|
||||||
|
|
||||||
|
|
||||||
|
/****************************************************************************
|
||||||
|
*
|
||||||
|
* 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
|
||||||
|
*/
|
||||||
|
|
||||||
@@ -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
|
||||||
|
*/
|
||||||
|
|
||||||
Loaded 100 of 276 files, more files were not shown because too many files have changed in this diff.
Show more
Reference in new issue
Block a user