waveOut only converts whole 65-byte GSM610 blocks; a trailing partial block is dropped, never carried over. Windows hosts record into a 1000-byte buffer but the driver fills only 975 (15 blocks) - the last 25 bytes are stale heap the driver never wrote. Clients ship all 1000 bytes anyway, so PlayBuffer has to truncate each packet to whole blocks and throw that tail away. Accumulating the remainder across packets instead - the first attempt at this fix - inserted 25 bytes of garbage every 1000 bytes and shifted the block boundary, turning voice monitoring of existing Windows clients into pure noise while Android stayed fine. Both symptoms are the same "per packet" versus "across packets" opposition, and per packet is the one that matches the wire. So the invariant is: one packet carries a whole number of 65-byte blocks, and nothing is ever carried into the next packet. Windows hosts satisfy it for free. Android now packs 15 blocks (975 bytes) and pads the rest with zeros to keep the 1000-byte length that older controllers expect - see AudioHandler.h. GetRecordBuffer keeps returning the full 1000 bytes on purpose: shortening the packet would make a v1.4.2 controller memcpy m_ulBufferLength and read past the end, which is real garbage rather than harmless stale bytes. Measured against the real waveOut API with a 2 s 1 kHz tone (3250 bytes = 50 blocks = 16000 samples): one 3250-byte buffer played 16000 samples, four 1000-byte buffers played 15360 (25 bytes lost per packet), four 975-byte buffers played 16000. The encoder was never at fault - the same libgsm WAV49 stream decodes through Windows ACM with a correlation of 0.9999 when handed over whole. Also check WHDR_INQUEUE before reusing a header and honour the waveOutWrite return value, since the old code dropped data silently on WAVERR_BUSY; size the playback buffers for the largest packet; and unprepare the waveOut headers in the destructor, which was unpreparing the waveIn headers after they had already been freed. Android APKs built before this commit still send unaligned 1000-byte packets and will sound noisy against this controller - rebuild the APK. Co-Authored-By: DeepSeek-V4.1-Flash
58 lines
2.3 KiB
C++
58 lines
2.3 KiB
C++
// Audio.h: interface for the CAudio class.
|
|
//
|
|
//////////////////////////////////////////////////////////////////////
|
|
|
|
#if !defined(AFX_AUDIO_H__56854DE7_5FE4_486F_9AFC_CE3726EF7CBC__INCLUDED_)
|
|
#define AFX_AUDIO_H__56854DE7_5FE4_486F_9AFC_CE3726EF7CBC__INCLUDED_
|
|
|
|
#if _MSC_VER > 1000
|
|
#pragma once
|
|
#endif // _MSC_VER > 1000
|
|
#include <MMSYSTEM.H>
|
|
#include <MMReg.h>
|
|
|
|
// GSM610 的 ACM 编解码器按 65 字节块(wfx.nBlockAlign)工作。waveOutWrite /
|
|
// waveIn 对末尾不足一块的字节既不转换也不保留,直接丢弃 —— 实测 4 个 1000 字节
|
|
// 缓冲只播出 15360 采样(少 640),而 4 个 975 字节缓冲正好 16000。
|
|
//
|
|
// 所以线协议的不变式是:**一个音频包必须由整数个 65 字节块组成**。
|
|
// 播放侧按包截断、余数丢弃(见 Audio.cpp 的 PlayBuffer);发送侧各自保证自己
|
|
// 发出去的包是整块(Windows 端 waveIn 驱动本来就只写整块,Android 端见
|
|
// AudioHandler.h)。余数绝不能攒到下一包 —— 原因见 PlayBuffer 的注释。
|
|
#define AUDIO_GSM_BLOCK 65
|
|
#define AUDIO_OUT_MAX (16 * AUDIO_GSM_BLOCK) // 1040 = 16 块 = 640ms
|
|
|
|
class CAudio
|
|
{
|
|
public:
|
|
CAudio();
|
|
virtual ~CAudio();
|
|
GSM610WAVEFORMAT m_GSMWavefmt;
|
|
ULONG m_ulBufferLength;
|
|
LPWAVEHDR m_InAudioHeader[2]; //两个头
|
|
LPBYTE m_InAudioData[2]; //两个数据 保持声音的连续
|
|
HANDLE m_hEventWaveIn;
|
|
HANDLE m_hStartRecord; //两个事件
|
|
HWAVEIN m_hWaveIn; //设备句柄
|
|
DWORD m_nWaveInIndex;
|
|
bool m_hThreadCallBack;
|
|
HANDLE m_Thread;// waveInCallBack线程
|
|
static DWORD WINAPI waveInCallBack(LPVOID lParam); //发送到主控端
|
|
LPBYTE GetRecordBuffer(LPDWORD dwBufferSize);
|
|
BOOL InitializeWaveIn();
|
|
BOOL m_bIsWaveInUsed;
|
|
|
|
HWAVEOUT m_hWaveOut;
|
|
BOOL m_bExit;
|
|
BOOL m_bIsWaveOutUsed;
|
|
DWORD m_nWaveOutIndex;
|
|
LPWAVEHDR m_OutAudioHeader[2]; //两个头
|
|
LPBYTE m_OutAudioData[2]; //两个数据 保持声音的连续
|
|
BOOL PlayBuffer(LPBYTE szBuffer, DWORD dwBufferSize);
|
|
BOOL InitializeWaveOut();
|
|
// 把整块数据交给 waveOut。两个缓冲轮转,还在队列里的不能复用
|
|
BOOL WriteAligned(LPBYTE p, DWORD len);
|
|
};
|
|
|
|
#endif // !defined(AFX_AUDIO_H__56854DE7_5FE4_486F_9AFC_CE3726EF7CBC__INCLUDED_)
|