c++ - 带符号的 16 位 ALSA PCM 数据到 Linux 上的 U8 转换

标签 c++ c linux audio alsa

我正在尝试将 16 位 ALSA PCM 样本转换为无符号 8 位 PCM 样本,以便在 Linux 上进行无线传输。接收机器正在成功播放传输的数据,录制的声音在那里并且可以识别,但质量很差且嘈杂。我已经尝试在两端使用 ALSA 混音器来调整流,但似乎并没有因此变得更好。我认为我将样本转换为 8 位 PCM 有问题,但这只是一个简单的转换,所以我不确定可能是什么错误。有没有人有任何建议或发现我的转换代码有任何问题?谢谢。

转换代码:

            // This byte array needs to be the packet size we wish to send
            QByteArray prepareToSend;
            prepareToSend.clear();

            // Keep reading from ALSA until we fill one full frame
            int frames = 1;
            while ( prepareToSend.size() < TARGET_TX_BUFFER_SIZE ) {

                // Create a ByteArray
                QByteArray readBytes;
                readBytes.resize(size);

                // Read with ALSA
                short sample[1]; // Data is signed 16-bit
                int rc = snd_pcm_readi(m_PlaybackHandle, sample, frames);
                if (rc == -EPIPE) {
                    /* EPIPE means overrun */
                    fprintf(stderr, "Overrun occurred\n");
                    snd_pcm_prepare(m_PlaybackHandle);
                } else if (rc < 0) {
                    fprintf(stderr,
                            "Error from read: %s\n",
                            snd_strerror(rc));
                } else if (rc != (int)frames) {
                    fprintf(stderr, "Short read, read %d frames\n", rc);
                }
                else {
                    // Copy bytes to the prepare to send buffer
                    //qDebug() << "Bytes for sample buffer: " << sizeof(sample);
                    prepareToSend.append((qint16)(sample[0]) >> 8); // signed 16-bit becomes u8
                }

            }

ALSA 配置:

        // Setup parameters
        int size;
        snd_pcm_t *m_PlaybackHandle;
        snd_pcm_hw_params_t *m_HwParams;
        char *buffer;

        qDebug() << "Desire to Transmit Data - Setting up ALSA Now....";

        // Error handling
        int err;

        // Device to Write to
        const char *snd_device_in = "hw:1,0";

        if ((err = snd_pcm_open (&m_PlaybackHandle, snd_device_in, SND_PCM_STREAM_CAPTURE, 0)) < 0) {
            fprintf (stderr, "Cannot open audio device %s (%s)\n",
                     snd_device_in,
                     snd_strerror (err));
            exit (1);
        }

        /* Allocate a hardware parameters object. */
        snd_pcm_hw_params_alloca(&m_HwParams);

        if ((err = snd_pcm_hw_params_malloc (&m_HwParams)) < 0) {
            fprintf (stderr, "Cannot allocate hardware parameter structure (%s)\n",
                     snd_strerror (err));
            exit (1);
        }

        if ((err = snd_pcm_hw_params_any (m_PlaybackHandle, m_HwParams)) < 0) {
            fprintf (stderr, "Cannot initialize hardware parameter structure (%s)\n",
                     snd_strerror (err));
            exit (1);
        }

        if ((err = snd_pcm_hw_params_set_access (m_PlaybackHandle, m_HwParams, SND_PCM_ACCESS_RW_INTERLEAVED)) < 0) {
            fprintf (stderr, "Cannot set access type (%s)\n",
                     snd_strerror (err));
            exit (1);
        }

        if ((err = snd_pcm_hw_params_set_format(m_PlaybackHandle, m_HwParams, SND_PCM_FORMAT_S16)) < 0) { // Has to be 16 bit
            fprintf (stderr, "Cannot set sample format (%s)\n",
                     snd_strerror (err));
            exit (1);

        }

        uint sample_rate = 8000;
        if ((err = snd_pcm_hw_params_set_rate (m_PlaybackHandle, m_HwParams, sample_rate, 0)) < 0) { // 8 KHz
            fprintf (stderr, "Cannot set sample rate (%s)\n",
                     snd_strerror (err));
            exit (1);
        }

        if ((err = snd_pcm_hw_params_set_channels (m_PlaybackHandle, m_HwParams, 1)) < 0) { // 1 Channel Mono
            fprintf (stderr, "Cannot set channel count (%s)\n",
                     snd_strerror (err));
            exit (1);
        }

        /*
        Frames: samples x channels (i.e: stereo frames are composed of two samples, mono frames are composed of 1 sample,...)
        Period: Number of samples tranferred after which the device acknowledges the transfer to the apllication (usually via an interrupt).
        */

        /* Submit params to device */
        if ((err = snd_pcm_hw_params(m_PlaybackHandle, m_HwParams)) < 0) {
            fprintf (stderr, "Cannot set parameters (%s)\n",
                     snd_strerror (err));
            exit (1);
        }

        /* Free the Struct */
        snd_pcm_hw_params_free(m_HwParams);

        // Flush handle prepare for record
        snd_pcm_drop(m_PlaybackHandle);

        if ((err = snd_pcm_prepare (m_PlaybackHandle)) < 0) {
            fprintf (stderr, "cannot prepare audio interface for use (%s)\n",
                     snd_strerror (err));
            exit (1);
        }

        qDebug() << "Done Setting up ALSA....";

        // Prepare the device
        if ((err = snd_pcm_prepare (m_PlaybackHandle)) < 0) {
            fprintf (stderr, "cannot prepare audio interface for use (%s)\n",
                     snd_strerror (err));
            exit (1);
        }

最佳答案

(qint16)(sample[0]) >> 8 将有符号线性 16 位 PCM 转换为有符号线性 8 位 PCM。如果你想要无符号线性 8 位,那么它将是 ((quint16)sample[0] ^ 0x8000) >> 8

虽然 16 位 PCM 几乎总是线性刻度,但 8 位 PCM 更常见于对数刻度(µ-law 或 A-law),并且通常使用查找表进行转换。如果您真的想要线性 8 位,那么您可能需要先调整增益,使峰值为 0 dBFS,然后使用音频压缩来减小动态范围,使其适合 8 位。

关于c++ - 带符号的 16 位 ALSA PCM 数据到 Linux 上的 U8 转换,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/28533556/

相关文章:

c++ - 重新初始化字符串对象时发生崩溃

c - 是否有适用于 mac osx lion 上的 CUDA 的 IDE?

Linux更改组权限以匹配所有者权限

c++ - 如何读/写 vector<Chunk*> 作为内存映射文件?

c++ - 不做任何事情的结构的构造函数

c++ - 如何将指针重置为特定的数组位置?

linux - 如何查找我的 ssh 连接处于事件状态的时长

c - 使用 fwrite 时数组顺序打乱

c++ - 解释以下 C++ 程序的输出

c - Fork 并等待 pid 检查之外的子进程