From 40a1219dac5df9f314ac73f563cb1e77b9ea2782 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Micha=C5=82=20=C5=A0rajer?= Date: Tue, 29 Aug 2017 19:09:54 +0200 Subject: [PATCH] Support 24bit audio SND_PCM_FORMAT_S24_LE and similar are for 24bit ints packed in 4-bytes each. There is a similar family of formats for 3-bytes packed data (as stored in 24bit wave files). This commit: - adds S24_3LE, S24_3BE, U24_3LE, U24_3BE PCM formats to the alsaaudio.c - updates documentation - updates playwav.py to correctly play typical 24Bit PCM wave files Closes #38 --- alsaaudio.c | 4 ++++ doc/libalsaaudio.rst | 12 ++++++++---- playwav.py | 2 +- 3 files changed, 13 insertions(+), 5 deletions(-) diff --git a/alsaaudio.c b/alsaaudio.c index 76c0ffe..caadfdf 100644 --- a/alsaaudio.c +++ b/alsaaudio.c @@ -2478,6 +2478,10 @@ PyObject *PyInit_alsaaudio(void) _EXPORT_INT(m, "PCM_FORMAT_IMA_ADPCM",SND_PCM_FORMAT_IMA_ADPCM); _EXPORT_INT(m, "PCM_FORMAT_MPEG",SND_PCM_FORMAT_MPEG); _EXPORT_INT(m, "PCM_FORMAT_GSM",SND_PCM_FORMAT_GSM); + _EXPORT_INT(m, "PCM_FORMAT_S24_3LE",SND_PCM_FORMAT_S24_3LE); + _EXPORT_INT(m, "PCM_FORMAT_S24_3BE",SND_PCM_FORMAT_S24_3BE); + _EXPORT_INT(m, "PCM_FORMAT_U24_3LE",SND_PCM_FORMAT_U24_3LE); + _EXPORT_INT(m, "PCM_FORMAT_U24_3BE",SND_PCM_FORMAT_U24_3BE); /* DSD sample formats are included in ALSA 1.0.29 and higher * define OVERRIDE_DSD_COMPILE to include DSD sample support diff --git a/doc/libalsaaudio.rst b/doc/libalsaaudio.rst index ce08867..dc4527b 100644 --- a/doc/libalsaaudio.rst +++ b/doc/libalsaaudio.rst @@ -193,10 +193,10 @@ PCM objects have the following methods: ``PCM_FORMAT_S16_BE`` Signed 16 bit samples for each channel (Big Endian byte order) ``PCM_FORMAT_U16_LE`` Unsigned 16 bit samples for each channel (Little Endian byte order) ``PCM_FORMAT_U16_BE`` Unsigned 16 bit samples for each channel (Big Endian byte order) - ``PCM_FORMAT_S24_LE`` Signed 24 bit samples for each channel (Little Endian byte order) - ``PCM_FORMAT_S24_BE`` Signed 24 bit samples for each channel (Big Endian byte order)} - ``PCM_FORMAT_U24_LE`` Unsigned 24 bit samples for each channel (Little Endian byte order) - ``PCM_FORMAT_U24_BE`` Unsigned 24 bit samples for each channel (Big Endian byte order) + ``PCM_FORMAT_S24_LE`` Signed 24 bit samples for each channel (Little Endian byte order in 4 bytes) + ``PCM_FORMAT_S24_BE`` Signed 24 bit samples for each channel (Big Endian byte order in 4 bytes) + ``PCM_FORMAT_U24_LE`` Unsigned 24 bit samples for each channel (Little Endian byte order in 4 bytes) + ``PCM_FORMAT_U24_BE`` Unsigned 24 bit samples for each channel (Big Endian byte order in 4 bytes) ``PCM_FORMAT_S32_LE`` Signed 32 bit samples for each channel (Little Endian byte order) ``PCM_FORMAT_S32_BE`` Signed 32 bit samples for each channel (Big Endian byte order) ``PCM_FORMAT_U32_LE`` Unsigned 32 bit samples for each channel (Little Endian byte order) @@ -210,6 +210,10 @@ PCM objects have the following methods: ``PCM_FORMAT_IMA_ADPCM`` A 4:1 compressed format defined by the Interactive Multimedia Association. ``PCM_FORMAT_MPEG`` MPEG encoded audio? ``PCM_FORMAT_GSM`` 9600 bits/s constant rate encoding for speech + ``PCM_FORMAT_S24_3LE`` Signed 24 bit samples for each channel (Little Endian byte order in 3 bytes) + ``PCM_FORMAT_S24_3BE`` Signed 24 bit samples for each channel (Big Endian byte order in 3 bytes) + ``PCM_FORMAT_U24_3LE`` Unsigned 24 bit samples for each channel (Little Endian byte order in 3 bytes) + ``PCM_FORMAT_U24_3BE`` Unsigned 24 bit samples for each channel (Big Endian byte order in 3 bytes) ========================= =============== diff --git a/playwav.py b/playwav.py index 48a52ae..1940b40 100755 --- a/playwav.py +++ b/playwav.py @@ -24,7 +24,7 @@ def play(device, f): elif f.getsampwidth() == 2: device.setformat(alsaaudio.PCM_FORMAT_S16_LE) elif f.getsampwidth() == 3: - device.setformat(alsaaudio.PCM_FORMAT_S24_LE) + device.setformat(alsaaudio.PCM_FORMAT_S24_3LE) elif f.getsampwidth() == 4: device.setformat(alsaaudio.PCM_FORMAT_S32_LE) else: