【问题标题】:How to transform byte array in short array with Objective-C?如何使用 Objective-C 将字节数组转换为短数组?
【发布时间】:2014-02-10 15:43:07
【问题描述】:

我正在为 iOS 开发一个与录音相关的移动应用程序。

因此,我正在开发一些不同的音效来修改录制的声音,但我在实现其中一些时遇到了问题。

我正在尝试创建回显/延迟效果,我需要将一个字节数组转换为一个短数组,但我不知道如何在 Objective-C 中做到这一点。

谢谢。

这是我目前实现它的源代码,但就像 byte 是一个非常短的类型一样,当我应用衰减(必须返回一个浮点值)时,我的音频会产生可怕的噪音。

- (NSURL *)echo:(NSURL *)input output:(NSURL *)output{

    int delay = 50000;
    float attenuation = 0.5f;

    NSMutableData *audioData = [NSMutableData dataWithContentsOfURL:input];
    NSUInteger dataSize = [audioData length] - 44;
    NSUInteger audioLength = [audioData length];

    NSUInteger newAudioLength = audioLength + delay;


    // Copy bytes
    Byte *byteData = (Byte*)malloc(audioLength);
    memcpy(byteData, [audioData bytes], audioLength);

    short *shortData = (short*)malloc(audioLength/2);

    // create a new array to store new modify data
    Byte *newByteData = (Byte*)malloc(newAudioLength);
    newByteData = byteData;

    for (int i = 44; i < audioLength - delay; i++)
    {
        newByteData[i + delay] += byteData[i] * attenuation;
    }

    // Copy bytes in a new NSMutableData
    NSMutableData *newAudioData = [NSMutableData dataWithBytes:newByteData length:newAudioLength];

    // Store in a file
    [newAudioData writeToFile:[output path] atomically:YES];

    // Set WAV size
    [[AudioUtils alloc] setAudioFileSize:output];

    return output;
}

【问题讨论】:

  • 您的音频文件格式是什么?看起来是 WAV 吗?

标签: ios objective-c audio effects voice


【解决方案1】:

最后,我可以完成实现这四种方法的回声效果。我希望它们对你有用。

字节转短数组

- (short *) byte2short:(Byte *)bytes size:(int)size resultSize:(int)resultSize{
    short *shorts = (short *)malloc(sizeof(short)*resultSize);
    for (int i=0; i < size/2; i++){
        shorts[i] = (bytes[i*2+1] << 8) | bytes[i*2];
    }
    return shorts;
}

短字节数组

- (Byte *) short2byte:(short *)shorts size:(int)size resultSize:(int)resultSize{
    Byte *bytes = (Byte *)malloc(sizeof(Byte)*resultSize);
    for (int i = 0; i < size; i++)
    {
        bytes[i * 2] = (Byte) (shorts[i] & 0x00FF);
        bytes[(i * 2) + 1] = (Byte) (shorts[i] >> 8);
        shorts[i] = 0;
    }
    return bytes;
}

效果

- (NSMutableData *) effect:(NSMutableData *)data delay:(int)delay attenuation:(float)attenuation{
    NSUInteger audioLength = [data length];

    // Copy original data in a byte array
    Byte *byteData = (Byte*)malloc(sizeof(Byte)*audioLength);
    memcpy(byteData, [data bytes], audioLength);

    short *shortData = (short*)malloc(sizeof(short)*(audioLength/2 + delay));
    shortData = [self byte2short:byteData size:(int)audioLength resultSize:(int)audioLength/2 + delay];

    // Array to store shorts
    short *newShortData = shortData;

    for (int i = 44; i < audioLength/2; i++)
    {
        newShortData[i + delay] += (short)((float)shortData[i] * attenuation);
    }

    Byte *newByteData = [self short2byte:newShortData size:(int)(audioLength/2 + delay) resultSize:(int)(audioLength + delay*2)];
    // Copy bytes to a NSMutableData in order to create new file
    NSMutableData *newAudioData = [NSMutableData dataWithBytes:newByteData length:(int)(audioLength + delay*2)];

    return newAudioData;
}

回声效果

- (NSURL *)echo:(NSURL *)input output:(NSURL *)output{

    NSMutableData *audioData = [NSMutableData dataWithContentsOfURL:input];

    // we call effect method that returns a NSMutableData and create a new file
    [[self effect:audioData delay:6000 attenuation:0.5f] writeToFile:[output path] atomically:YES];

    // We set file's size (is a method I have implemented)
    [[AudioUtils alloc] setAudioFileSize:output];

    return output;
}

【讨论】:

  • 除非我将衰减保持为 0,否则仍然会发出可怕的噪音。
【解决方案2】:

没有预定义的函数可以从字节数组创建短数组,但使用 for 循环应该相当简单

// create a short array
short *shortData = malloc(sizeof(short)*audioLength);
for (i=0; i<bytearray.length, i++)
{
    shortData[i] = byteData[i];
}

代码不是严格正确的(意思是我没有编译它,只是在这里写的),但它应该让你知道如何去做。

另外请注意,使用两个字节而不是一个字节保存音频数据在播放时会产生非常不同的结果,但我假设您知道如何处理用于特定目的的音频数据。

【讨论】:

  • 感谢您的回答。但是仅仅将字节转换为一个短数组是不够的,因为一个短对象是一个字节的双倍。因此,正如您在我的最终答案中看到的那样,解决方案包括将两个字节短接在一起:)
  • 现在我明白了您将两个字节合并为一个短字节是什么意思。你原来的问题不清楚,但我很高兴你把它整理出来!干杯。
猜你喜欢
  • 2012-06-04
  • 1970-01-01
  • 1970-01-01
  • 2015-02-16
  • 1970-01-01
  • 2017-01-06
  • 1970-01-01
  • 1970-01-01
  • 2018-05-27
相关资源
最近更新 更多