zoukankan      html  css  js  c++  java
  • 音频队列-音频采集

    //
    //  AudioCapture.m
    //  live
    //
    //  Created by lujunjie on 2016/11/5.
    //  Copyright © 2016年 lujunjie. All rights reserved.
    //
    
    #import "AudioQueueCapture.h"
    #import <AudioToolbox/AudioToolbox.h>
    #define QUEUE_BUFFER_SIZE 10 //队列缓冲个数
    #define MIN_SIZE_PER_FRAME 1000 //每帧最小数据长度
    @interface AudioQueueCapture()
    {
        
        AudioQueueRef audioQueue;//音频播放队列
        AudioQueueBufferRef    audioQueueBuffer[QUEUE_BUFFER_SIZE];//音频缓存
        AudioStreamBasicDescription audioDescription;//音频参数
    }
    @end
    @implementation AudioQueueCapture
    
    - (instancetype)init
    {
        if (self=[super init]) {
            
            audioDescription.mSampleRate = 8000;//采样率
            audioDescription.mFormatID = kAudioFormatLinearPCM;
            audioDescription.mFormatFlags = kLinearPCMFormatFlagIsSignedInteger | kAudioFormatFlagIsPacked;
            audioDescription.mChannelsPerFrame = 1;///单声道 每一帧数据包含的通道数
            audioDescription.mFramesPerPacket = 1;//每个包数据的样本帧的数量
            audioDescription.mBitsPerChannel = 16;//每个采样点16bit量化 每一帧数据的每一个通道的采样位的数量
            audioDescription.mBytesPerPacket = 2; // 每个包数据的字节数量
            audioDescription.mBytesPerFrame = 2;//单帧包含的字节数据
            
        }
        return self;
    }
    
    
    - (void)stop
    {
        if (audioQueue) {
            AudioQueueStop(audioQueue, YES);
            AudioQueueDispose(audioQueue, YES);
            audioQueue = NULL;
        }
    }
    - (void)start
    {
        // 创建音频输入队列
        if (AudioQueueNewInput(&audioDescription, audioQueueInputCallback, (__bridge void * _Nullable)(self), NULL, NULL, 0, &audioQueue) != noErr){
            return;
        }
        for (int i = 0; i < QUEUE_BUFFER_SIZE; i++) {
             //请求音频队列对象来分配一个音频队列缓存。
            AudioQueueAllocateBuffer(audioQueue, MIN_SIZE_PER_FRAME, &audioQueueBuffer[i]);
             //给录音或者回放音频队列的缓存中添加一个缓存数据
            AudioQueueEnqueueBuffer(audioQueue, audioQueueBuffer[i], 0, NULL);
        }
        if (audioQueue) {
             //开始录音
            AudioQueueStart(audioQueue, NULL);
        }
    }
    
    
    void audioQueueInputCallback (void *inUserData,AudioQueueRef inoQueueRef, AudioQueueBufferRef inBuffer,const AudioTimeStamp *inStartTime, UInt32 inNumberPacketDescriptions, const AudioStreamPacketDescription *inPacketDescs) {
        
        AudioQueueCapture *self = (__bridge AudioQueueCapture *)inUserData;
        [self.delegate audioQueueCaptureData:inBuffer->mAudioData dataLength:inBuffer->mAudioDataBytesCapacity];
        AudioQueueEnqueueBuffer(inoQueueRef, inBuffer, 0, NULL);
    }
    - (void)dealloc {
        
        [self stop];
        self.delegate = nil;
        
    }
    @end
  • 相关阅读:
    【转】【Salesfoece】Salesforce 应用生命周期管理
    【Apex】【Salesfoece】Salesforce 的 package.xml 文件
    【转】【Salesfoece】Apex计划作业框架的实现--用于实现数据的定时自动处理。
    【转】【Salesfoece】Apex 的 Trigger 类简介
    【转】【Salesforce】提高 Visualforce 页面加载效率的小知识
    「这是啥」关于三维偏序
    Javaweb中PO BO VO DTO POJO DAO DO概念理解
    Python多进程、多线程及各自的适用场景
    基于LDA主题模型和SVM的文本分类
    理解accuracy/precision_score、micro/macro
  • 原文地址:https://www.cnblogs.com/-ljj/p/6032979.html
Copyright © 2011-2022 走看看