如何使用音频队列服务从内存中录制和播放

发布于 2024-09-14 02:21:56 字数 1660 浏览 7 评论 0原文

我最近一直在尝试制作一款游戏,允许用户说话,然后用有趣的声音播放他所说的内容。

问题是只有当有人在 iPhone 附近说话时才会进行录音(我已经管理了这部分,我有一个录音机一直处于活动状态并获取其平均值和峰值)。

问题是当我想播放用户回复的内容时。 我一直在四处寻找,唯一的方法似乎是音频队列服务,因为它允许保存到内存中。从我的角度来看,由于文件分配,将声音保存到文件并播放会出现问题。

我还没有找到任何关于如何保存到内存的教程,所以如果有人有想法如何执行此操作或如何保存到文件并毫无问题地播放(现在声音被切断,前 0.5 秒听不到)而且 fps 也下降了 20)。

我当前的代码是:

void AudioInputCallback(
  void *inUserData, 
  AudioQueueRef inAQ, 
  AudioQueueBufferRef inBuffer, 
  const AudioTimeStamp *inStartTime, 
  UInt32 inNumberPacketDescriptions, 
  const AudioStreamPacketDescription *inPacketDescs)

  RecordState* recordState = (RecordState*)inUserData;
  if(!recordState->recording)
  {
    printf("Not recording, returning\n");
  }

  printf("Writing buffer %d\n", (int)recordState->currentPacket);

  //What to put here to save to memory?
  recordState->currentPacket += inNumberPacketDescriptions;

  AudioQueueEnqueueBuffer(recordState->queue, inBuffer, 0, NULL);
}

void AudioOutputCallback(
  void* inUserData,
  AudioQueueRef outAQ,
  AudioQueueBufferRef outBuffer)
{
  PlayState* playState = (PlayState*)inUserData; 
  if(!playState->playing)
  {
    printf("Not playing, returning\n");
    return;
  }

  printf("Queuing buffer %d for playback\n",(int) playState->currentPacket);

  AudioStreamPacketDescription* packetDescs;

  UInt32 bytesRead;
  UInt32 numPackets = 8000;
  OSStatus status;
  //What to put here to read from memory?
  status = AudioQueueEnqueueBuffer(
    playState->queue,
    outBuffer,
    0,
    packetDescs);

  playState->currentPacket += numPackets;

  AudioQueueFreeBuffer(playState->queue, outBuffer);
}

I've been trying lately to make a game that allows the user to talk and afterwards play what he talked back in a funny voice.

The catch is that the recording is done only when somebody talks near the iphone (I've managed this part I have a recorder active all the time and get it's average and peak values).

The problem is when I want to play what the user said back.
I've been searching around and the only way to do this seems to be Audio Queue Services due to the fact that it allows to save to memory. From my point of view saving the sound to a file and playing it back has issues due to the file allocation.

I haven't found any tutorial on how to save to memory so if anyone has an ideea how to do this or how to save to file and play back with no issues (now the sound is cut off the first 0.5 secs aren't heard and also the fps drops by 20).

My current code is:

void AudioInputCallback(
  void *inUserData, 
  AudioQueueRef inAQ, 
  AudioQueueBufferRef inBuffer, 
  const AudioTimeStamp *inStartTime, 
  UInt32 inNumberPacketDescriptions, 
  const AudioStreamPacketDescription *inPacketDescs)

  RecordState* recordState = (RecordState*)inUserData;
  if(!recordState->recording)
  {
    printf("Not recording, returning\n");
  }

  printf("Writing buffer %d\n", (int)recordState->currentPacket);

  //What to put here to save to memory?
  recordState->currentPacket += inNumberPacketDescriptions;

  AudioQueueEnqueueBuffer(recordState->queue, inBuffer, 0, NULL);
}

void AudioOutputCallback(
  void* inUserData,
  AudioQueueRef outAQ,
  AudioQueueBufferRef outBuffer)
{
  PlayState* playState = (PlayState*)inUserData; 
  if(!playState->playing)
  {
    printf("Not playing, returning\n");
    return;
  }

  printf("Queuing buffer %d for playback\n",(int) playState->currentPacket);

  AudioStreamPacketDescription* packetDescs;

  UInt32 bytesRead;
  UInt32 numPackets = 8000;
  OSStatus status;
  //What to put here to read from memory?
  status = AudioQueueEnqueueBuffer(
    playState->queue,
    outBuffer,
    0,
    packetDescs);

  playState->currentPacket += numPackets;

  AudioQueueFreeBuffer(playState->queue, outBuffer);
}

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。
列表为空,暂无数据
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文