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
1
Help us understand the problem. What is going on with this article?
@Kyome

Swift:OpenALでcaf音源を鳴らす方法

はじめに

ゲームなど速いレスポンスが必要な場合OpenALで音を鳴らすのが良いらしいことを知ったので、OpenALでcaf音源を鳴らすクラスを作成した。

ソースコード

OpenALUtil.swift
import OpenAL
import AudioToolbox

class OpenALUtil {
    var buffer: ALuint = 0
    var source: ALuint = 0

    init(fileName name: String) {
        let device = alcOpenDevice(nil)
        let context = alcCreateContext(device, nil)
        alcMakeContextCurrent(context)

        alGenBuffers(1, &buffer)
        let error: ALenum = alGetError()
        if error != AL_NO_ERROR {
            Swift.print(error.description)
            return
        }

        //open audio-file
        var fileId: AudioFileID? = nil
        let url: CFURL = URL(fileURLWithPath: Bundle.main.path(forResource: name, ofType: "caf")!) as CFURL
        let _: OSStatus = AudioFileOpenURL(url, .readPermission, kAudioFileCAFType, &fileId)

        //get byte-size of audio-file
        var byteCount: UInt64 = 0
        var propertySize = UInt32(MemoryLayout.size(ofValue: byteCount))
        let _: OSStatus = AudioFileGetProperty(fileId!, kAudioFilePropertyAudioDataByteCount, &propertySize, &byteCount)

        //read audio-file
        var bytesRead = UInt32(byteCount)
        let audioData = malloc(Int(bytesRead))!
        let _: OSStatus = AudioFileReadBytes(fileId!, false, 0, &bytesRead, audioData)

        //close audio-file
        let _: OSStatus = AudioFileClose(fileId!)

        //data into buffer
        alGenBuffers(1, &buffer)
        alBufferData(buffer, AL_FORMAT_MONO16, audioData, ALsizei(bytesRead), 44100)

        //write buffer to source
        alGenSources(1, &source)
        alSourcei(source, AL_LOOPING, AL_TRUE)
        alSourcei(source, AL_BUFFER, ALint(buffer))
    }

    public func play() {
        alSourcePlay(source)
    }

    public func pause() {
        alSourcePause(source)
    }

    public func stop() {
        alSourceStop(source)
    }
}

このClassの使い方

ViewController.swift
let sound = OpenALUtil(fileName: "click") //click.cafを指定
sound.play() //再生
sound.pause() //一時停止
sound.stop() //停止
1
Help us understand the problem. What is going on with this article?
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
Kyome
火星に住む犬です。 趣味でmacOS アプリを開発しています。

Comments

No comments
Sign up for free and join this conversation.
Sign Up
If you already have a Qiita account Login
1
Help us understand the problem. What is going on with this article?