日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 > 编程资源 > 编程问答 >内容正文

编程问答

Iphone 音频

發布時間:2025/4/5 编程问答 23 豆豆
生活随笔 收集整理的這篇文章主要介紹了 Iphone 音频 小編覺得挺不錯的,現在分享給大家,幫大家做個參考.

iPhone microphone輸入事件捕獲

目的:
利用麥克風做為一種事件的輸入方式
核心:
通過AudioSession與AudioQueue實現麥克風輸入的數據捕捉.

開啟AudioSession:
1.? ? AudioSessionInitialize
2.? ? AudioSessionSetProperty(kAudioSessionProperty_AudioCategory)
3.? ? AudioSessionSetActive

建立聲音格式:
1.? ? 聲音格式的數據結構AudioStreamBasicDescription
2.? ? 使用kAudioFormatLinearPCM來做為聲音格式

建立AudioQueue:
1.? ? AudioQueueNewInput
2.? ? AudioQueueStart
3.? ? AudioQueueSetProperty(kAudioQueueProperty_EnableLevelMetering)

獲取聲音峰值數據:
1.? ? 記錄峰值的數據結構AudioQueueLevelMeterState
2.? ? AudioQueueGetProperty(kAudioQueueProperty_CurrentLevelMeterDB)

關閉AudioQueue:
1.? ? AudioQueueStop
2.? ? AudioQueueDispose

代碼:


#import <UIKit/UIKit.h>
#include <AudioToolbox/AudioToolbox.h>

@interface MicrophoneTestViewController : UIViewController {

? ? IBOutlet UILabel*? ? _averagePower;
? ? IBOutlet UILabel*? ? _peakPower;

? ? AudioQueueRef? ?? ?? ?? ?? ? mQueue;
? ? AudioStreamBasicDescription? ? mFormat;
? ? AudioQueueLevelMeterState? ? *_chan_lvls;
? ? NSArray? ?? ?? ?? ?? ?? ?? ?? ?*_channelNumbers;
}

-(void)setChannelNumbers:(NSArray *)v;
-(void)initAudioSession;

- (IBAction)startstop: (id) sender;

@end
[/code]

[code]
#import "MicrophoneTestViewController.h"

static void MyInputBufferHandler(void *? ?? ?? ?? ?? ?? ?? ?? ?? ?? ?? ?? ?inUserData,
? ?? ?? ?? ?? ?? ?? ?? ?? ?? ?? ?AudioQueueRef? ?? ?? ?? ?? ?? ?? ?? ?? ? inAQ,
? ?? ?? ?? ?? ?? ?? ?? ?? ?? ?? ?AudioQueueBufferRef? ?? ?? ?? ?? ?? ???inBuffer,
? ?? ?? ?? ?? ?? ?? ?? ?? ?? ?? ?const AudioTimeStamp *? ?? ?? ?? ?? ?? ???inStartTime,
? ?? ?? ?? ?? ?? ?? ?? ?? ?? ?? ?UInt32? ?? ?? ?? ?? ?? ?? ?? ?? ?? ?? ?? ?inNumPackets,
? ?? ?? ?? ?? ?? ?? ?? ?? ?? ?? ?const AudioStreamPacketDescription*? ? inPacketDesc)
{
? ? // 如果要記錄聲音,可以在這里做記錄處理.
? ? // 如果要分析聲音數據,可以在這里做記錄處理.
}

static void interruptionListener(void *? ? inClientData,
? ?? ?? ?? ?? ?? ?? ?? ?? ?? ?? ?UInt32? ? inInterruptionState)
{
? ? // 聲音中斷通知(BEGIN,END)
}

@implementation MicrophoneTestViewController

// Implement viewDidLoad to do additional setup after loading the view, typically from a nib.
- (void)viewDidLoad {
? ? [super viewDidLoad];

? ? _averagePower.text = @"0";
? ? _peakPower.text = @"0";
? ? mQueue = NULL;
? ? _channelNumbers = [[NSArray alloc] initWithObjects:[NSNumber numberWithInt:0], nil];
? ? _chan_lvls = (AudioQueueLevelMeterState*)malloc(sizeof(AudioQueueLevelMeterState) * [_channelNumbers count]);

? ? [self initAudioSession];

? ? [NSTimer
? ???scheduledTimerWithTimeInterval:1.f/30.f
? ???target:self
? ???selector:@selector(_refresh)
? ???userInfo:nil
? ???repeats:YES
? ???];
}

- (void)didReceiveMemoryWarning {
? ? // Releases the view if it doesn't have a superview.
? ? [super didReceiveMemoryWarning];

? ? // Release any cached data, p_w_picpaths, etc that aren't in use.
}

- (void)viewDidUnload {
? ? // Release any retained subviews of the main view.
? ? // e.g. self.myOutlet = nil;
? ? [_channelNumbers release];
? ? free(_chan_lvls);
}


- (void)dealloc {
? ? [super dealloc];
}

-(void)initAudioSession
{
? ? OSStatus error = AudioSessionInitialize(NULL, NULL, interruptionListener, self);
? ? if (error) printf("ERROR INITIALIZING AUDIO SESSION! %d\n", (int)error);
? ? else
? ? {
? ?? ???UInt32 category = kAudioSessionCategory_PlayAndRecord;? ?
? ?? ???error = AudioSessionSetProperty(kAudioSessionProperty_AudioCategory, sizeof(category), &category);
? ?? ???if (error) printf("couldn't set audio category!");

? ?? ???error = AudioSessionSetActive(true);
? ?? ???if (error) printf("AudioSessionSetActive (true) failed");
? ? }
}

-(void)setupAudioFormat:(UInt32)inFormatID
{
? ? memset(&mFormat, 0, sizeof(mFormat));

? ? UInt32 size = sizeof(mFormat.mSampleRate);
? ? OSStatus result = AudioSessionGetProperty(kAudioSessionProperty_CurrentHardwareSampleRate,
? ?? ?? ?? ?? ?? ?? ?? ?? ? &size,
? ?? ?? ?? ?? ?? ?? ?? ?? ? &mFormat.mSampleRate);

? ? size = sizeof(mFormat.mChannelsPerFrame);
? ? result = AudioSessionGetProperty(kAudioSessionProperty_CurrentHardwareInputNumberChannels,
? ?? ?? ?? ?? ?? ?? ?? ?? ? &size,
? ?? ?? ?? ?? ?? ?? ?? ?? ? &mFormat.mChannelsPerFrame);

? ? mFormat.mFormatID = inFormatID;
? ? if (inFormatID == kAudioFormatLinearPCM)
? ? {
? ?? ???// if we want pcm, default to signed 16-bit little-endian
? ?? ???mFormat.mFormatFlags = kLinearPCMFormatFlagIsSignedInteger | kLinearPCMFormatFlagIsPacked;
? ?? ???mFormat.mBitsPerChannel = 16;
? ?? ???mFormat.mBytesPerPacket = mFormat.mBytesPerFrame = (mFormat.mBitsPerChannel / 8) * mFormat.mChannelsPerFrame;
? ?? ???mFormat.mFramesPerPacket = 1;
? ? }
}

-(void)startMicrophone
{
? ? [self setupAudioFormat:kAudioFormatLinearPCM];
? ? OSStatus result = AudioQueueNewInput(&mFormat, MyInputBufferHandler, NULL, NULL, NULL, 0, &mQueue);
? ? if (result == noErr) {
? ?? ???result = AudioQueueStart(mQueue, NULL);
? ?? ???if (result == noErr) {
? ?? ?? ?? ?UInt32 val = 1;
? ?? ?? ?? ?AudioQueueSetProperty(mQueue, kAudioQueueProperty_EnableLevelMetering, &val, sizeof(UInt32));

? ?? ?? ?? ?if (mFormat.mChannelsPerFrame != [_channelNumbers count])
? ?? ?? ?? ?{
? ?? ?? ?? ?? ? NSArray *chan_array;
? ?? ?? ?? ?? ? if (mFormat.mChannelsPerFrame < 2)
? ?? ?? ?? ?? ?? ???chan_array = [[NSArray alloc] initWithObjects:[NSNumber numberWithInt:0], nil];
? ?? ?? ?? ?? ? else
? ?? ?? ?? ?? ?? ???chan_array = [[NSArray alloc] initWithObjects:[NSNumber numberWithInt:0], [NSNumber numberWithInt:1], nil];

? ?? ?? ?? ?? ? [self setChannelNumbers:chan_array];
? ?? ?? ?? ?? ? [chan_array release];

? ?? ?? ?? ?? ? _chan_lvls = (AudioQueueLevelMeterState*)realloc(_chan_lvls, mFormat.mChannelsPerFrame * sizeof(AudioQueueLevelMeterState));
? ?? ?? ?? ?}

? ?? ?? ?? ?return;
? ?? ???}
? ? }

? ? // 失敗
? ? mQueue = NULL;
? ? NSLog(@"startMicrophone:失敗.");
? ? return;
}

-(void)stopMicrophone
{
? ? if (mQueue) {
? ?? ???AudioQueueStop(mQueue, true);
? ?? ???AudioQueueDispose(mQueue, true);
? ?? ???mQueue = NULL;
? ? }
}

-(void)_refresh
{
? ? if (mQueue) {
? ?? ???UInt32 data_sz = sizeof(AudioQueueLevelMeterState) * [_channelNumbers count];
? ?? ???OSErr status = AudioQueueGetProperty(mQueue, kAudioQueueProperty_CurrentLevelMeterDB, _chan_lvls, &data_sz);
? ?? ???if (status == noErr)
? ?? ???{
? ?? ?? ?? ?// 這里沒有去處理多個通道的數據顯示,直接就顯示最后一個通道的結果了
? ?? ?? ?? ?// 這里的值就是我們打算用來做為一些觸發機制的值了,需要用到的時候直接訪問_chan_lvls這個數組
? ?? ?? ?? ?for (int i=0; i<[_channelNumbers count]; i++)
? ?? ?? ?? ?{
? ?? ?? ?? ?? ? NSInteger channelIdx = [(NSNumber *)[_channelNumbers objectAtIndex:i] intValue];
? ?? ?? ?? ?? ? if (channelIdx < [_channelNumbers count] && channelIdx <= 127)
? ?? ?? ?? ?? ? {
? ?? ?? ?? ?? ?? ???_averagePower.text = [NSString stringWithFormat:@"%f", _chan_lvls[channelIdx].mAveragePower];
? ?? ?? ?? ?? ?? ???_peakPower.text = [NSString stringWithFormat:@"%f", _chan_lvls[channelIdx].mPeakPower];
? ?? ?? ?? ?? ? }
? ?? ?? ?? ?}
? ?? ???}
? ? }
}

-(void)setChannelNumbers:(NSArray *)v
{
? ? [v retain];
? ? [_channelNumbers release];
? ? _channelNumbers = v;
}

- (IBAction)startstop: (id) sender
{
? ? if (mQueue) {
? ?? ???[self stopMicrophone];
? ? } else {
? ?? ???[self startMicrophone];
? ? }
}

@end

轉載于:https://blog.51cto.com/3426724/717015

總結

以上是生活随笔為你收集整理的Iphone 音频的全部內容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。

主站蜘蛛池模板: 国产做受高潮动漫 | 二区影院 | 国产欧美一区二区三区在线老狼 | 麻豆一区二区在线观看 | 欧洲久久久久 | 网红日批视频 | 永久免费黄色 | 国产视频福利在线观看 | 成熟人妻av无码专区 | 久人人| jizz欧美大片 | 久久免费在线观看 | wwwxxx欧美 | 亚洲国产精品自拍 | 日韩成人午夜电影 | 久久久久99精品成人片直播 | 天堂中文网在线 | 精品无码人妻一区二区三区 | xxxxx国产 | 怡红院成人av | 黄色国产一级片 | 一级片在线观看免费 | 激烈的性高湖波多野结衣 | 狠狠躁18三区二区一区视频 | 99精品久久久久久久婷婷 | 欧美性猛交性大交 | 亚洲精品99久久久久中文字幕 | 波多野吉衣中文字幕 | 美女的奶胸大爽爽大片 | 日批视频免费看 | 欧美成人激情视频 | 中国av在线播放 | 欧美黑人性生活 | wwwxxx黄色片| 日韩欧美久久久 | www.欧美com| 丁香婷婷六月 | 日本精品99 | 伊人天天干 | 成人激情开心 | 亚洲天堂手机在线 | 亚洲免费观看高清在线观看 | 欧美成人精品激情在线观看 | 中文字幕乱码一二三区 | 91官网在线 | 久久精品—区二区三区舞蹈 | 日本电影大尺度免费观看 | 97视频资源 | 伊人影视久久 | 日韩午夜激情视频 | 在线免费看黄色 | videosex抽搐痉挛高潮 | 日本一级淫片1000部 | 午夜精品99 | 成人免费看高清电影在线观看 | 成人精品免费在线观看 | 免费欧美视频 | 夜夜摸夜夜爽 | 四虎福利视频 | 在线视频区 | 天天干夜夜爽 | 亚洲精品国产a | 国产一区二区三区乱码 | 日日干天天干 | 亚洲精品日韩丝袜精品 | 成年人视频在线播放 | 免费一级suv好看的国产网站 | 色屁屁ts人妖系列二区 | 尹人久久 | 日韩大片免费观看视频播放 | 成人国产一区二区三区 | 午夜黄色福利 | 性xxxx18| www黄色com | 五月婷婷激情网 | 久久男人视频 | 无码人妻久久一区二区三区不卡 | 伊人性视频 | 国产伦精品一区三区精东 | 伊人影院av| 久久99精品久久久久久噜噜 | 日韩成人在线一区 | hs视频在线观看 | 免费成人深夜夜行网站视频 | 靠逼网站在线观看 | 日韩色在线观看 | 爱蜜臀av| 狠狠躁18三区二区一区 | 手机在线观看毛片 | 玖玖爱这里只有精品 | 青草视频在线观看视频 | 成人一区二区三区 | 亚洲欧美日韩国产精品 | 双女主黄文 | 黄色片日韩 | 伊人久久青青 | 国产伦精品一区二区三区视频网站 | 蜜桃一区二区 | 在线看你懂得 |