Qiita Teams that are logged in
You are not logged in to any team

Log in to Qiita Team
Community
OrganizationAdvent CalendarQiitadon (β)
Service
Qiita JobsQiita ZineQiita Blog
Help us understand the problem. What is going on with this article?

iOSで特定の周波数の音を鳴らす方法

More than 5 years have passed since last update.

指定した周波数の音を鳴らすためのコードです。
ご参考までに。
AudioToolBox.frameworkを追加する必要があります。

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

@interface ViewController : UIViewController{
    AudioUnit aU;
    UInt32 bitRate;
}

@property (nonatomic) double phase;
@property (nonatomic) Float64 sampleRate;
@property (nonatomic) Float64 frequency;

static OSStatus renderer(void *inRef,
                         AudioUnitRenderActionFlags *ioActionFlags,
                         const AudioTimeStamp* inTimeStamp,
                         UInt32 inBusNumber,
                         UInt32 inNumberFrames,
                         AudioBufferList *ioData);

@end

ViewController.m
-(void)playSound{

    //Sampling rate
    _sampleRate = 44100.0f;

    //Bit rate
    bitRate = 8;  // 8bit

    //周波数(音程)
    _frequency = 261.62;

    //AudioComponentDescription
    AudioComponentDescription aCD;
    aCD.componentType = kAudioUnitType_Output;
    aCD.componentSubType = kAudioUnitSubType_RemoteIO;
    aCD.componentManufacturer = kAudioUnitManufacturer_Apple;
    aCD.componentFlags = 0;
    aCD.componentFlagsMask = 0;

    //AudioComponent
    AudioComponent aC = AudioComponentFindNext(NULL, &aCD);
    AudioComponentInstanceNew(aC, &aU);
    AudioUnitInitialize(aU);

    //コールバック
    AURenderCallbackStruct callbackStruct;
    callbackStruct.inputProc = renderer;
    callbackStruct.inputProcRefCon = (__bridge void*)self;
    AudioUnitSetProperty(aU,
                         kAudioUnitProperty_SetRenderCallback,
                         kAudioUnitScope_Input,
                         0,
                         &callbackStruct,
                         sizeof(AURenderCallbackStruct));

    //AudioStreamBasicDescription
    AudioStreamBasicDescription aSBD;
    aSBD.mSampleRate = _sampleRate;
    aSBD.mFormatID = kAudioFormatLinearPCM;
    aSBD.mFormatFlags = kAudioFormatFlagsAudioUnitCanonical;
    aSBD.mChannelsPerFrame = 2;
    aSBD.mBytesPerPacket = sizeof(AudioUnitSampleType);
    aSBD.mBytesPerFrame = sizeof(AudioUnitSampleType);
    aSBD.mFramesPerPacket = 1;
    aSBD.mBitsPerChannel = bitRate * sizeof(AudioUnitSampleType);
    aSBD.mReserved = 0;

    //AudioUnit
    AudioUnitSetProperty(aU,
                         kAudioUnitProperty_StreamFormat,
                         kAudioUnitScope_Input,
                         0,
                         &aSBD,
                         sizeof(aSBD));

    //再生
    AudioOutputUnitStart(aU);

}

static OSStatus renderer(void *inRef,
                         AudioUnitRenderActionFlags *ioActionFlags,
                         const AudioTimeStamp* inTimeStamp,
                         UInt32 inBusNumber,
                         UInt32 inNumberFrames,
                         AudioBufferList *ioData) {

    //キャスト
    ViewController* def = (__bridge ViewController*)inRef;

    //サイン波
    float freq = def.frequency*2.0*M_PI/def.sampleRate;

    //値を書き込むポインタ
    AudioUnitSampleType *outL = ioData->mBuffers[0].mData;
    AudioUnitSampleType *outR = ioData->mBuffers[1].mData;

    for (int i = 0; i < inNumberFrames; i++) {
        // 周波数を計算
        float wave = sin(def.phase);
        AudioUnitSampleType sample = wave * (1 << kAudioUnitSampleFractionBits);
        *outL++ = sample;
        *outR++ = sample;
        def.phase += freq;
    }

    return noErr;

};
yuky_az
「ヒトとAIの共生」がミッションの会社、SAI-Lab株式会社の代表取締役。 東北大学大学院理学研究科修了。理学博士(物理学)。 著書に「はじめてのディープラーニング」、「No.1スクール講師陣による 世界一受けたいiPhoneアプリ開発の授業」。オンライン教育プラットフォームUdemyで、2万人近くを指導する人気講師。
https://sai-lab.co.jp
Why not register and get more from Qiita?
  1. We will deliver articles that match you
    By following users and tags, you can catch up information on technical fields that you are interested in as a whole
  2. you can read useful information later efficiently
    By "stocking" the articles you like, you can search right away