简体   繁体   English

将未签名的char音频转换为short

[英]Convert unsigned char audio to short

Trying to convert an unsigned char to a short. 尝试将unsigned char转换为short。 The data is Audio. 数据是音频。 My system is Little Endian. 我的系统是Little Endian。 What I'm doing is this: 我在做什么是这样的:

    short buf1[audio_size / 2];
    for(size_t i = 0; i < audio_size; i += 2) {
        if(i > audio_size) {
            break;
        }

        if(i + 1 > audio_size) {
            buf1[i] = audio_data[i] << 8;
        } else {
            buf1[i] = audio_data[i] << 8|audio_data[i + 1];
        }
    }

The result is bad audio and EXC_BAD_ACCESS at buf1[i] = info.data[i] << 8|info.data[i + 1]; 结果是在buf1[i] = info.data[i] << 8|info.data[i + 1];音频和EXC_BAD_ACCESS不好buf1[i] = info.data[i] << 8|info.data[i + 1];

Update: 更新:

Audio sampling rate is 8000. 1 Channel. 音频采样率为8000。1通道。 16bit PCMU mulaw. 16位PCMU mulaw。

The buffer has a size audio_size / 2 , but you index into it up to the audio_size-1 index so you run into buffer overflow. 缓冲区的大小为audio_size / 2 ,但是您将其索引为audio_size-1索引,因此会遇到缓冲区溢出的情况。

You should use i/2 as index instead of i . 您应该使用i/2作为索引,而不是i

Also, another bug in this loop is that 另外,此循环中的另一个错误是

if(i + 1 > audio_size)

should be replaced by 应该由

if(i + 1 >= audio_size)

This is another reason for buffer overflow, but this time in the other (source) buffer 这是缓冲区溢出的另一个原因,但是这次在另一个(源)缓冲区中

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM