-
AVAudioFile과 AVAudioBufferiOS 2022. 6. 30. 18:31
AVAudioFile
An object that represents an audio file that the system can open for reading or writing.
시스템이 읽고 쓰기위해 열수 있는 오디오 파일을 나타내는 객체우린 파일의 format에 상관없이 AVAudioPCMBuffer 객체를 통해 읽거나 쓸 수 있다. 이러한 객체들에는 프레임워크에서 file processing format 으로 'AVAudioCommonFormat'이라는 샘플이 포함되어 있다.
AVAudioFile을 통해 실제 파일 format으로 변환 하거나 변환 시킨다.
즉, AVAudioFile을 통해 실제 오디오 파일을 읽거나 쓸수 있게 만드는 것이다.
- AVAudioFile의 초기화
let file_Int32 = try AVAudioFile(forReading: url, commonFormat: .pcmFormatInt32, interleaved: true) let file = try AVAudioFile(forReading: url) let fileFormat_Int = file_Int32.processingFormat let fileFormat = file.processingFormat print("fileFormat: ",fileFormat_Int) print("fileFormat: ",fileFormat) //fileFormat: <AVAudioFormat 0x60000249eee0: 2 ch, 44100 Hz, Int32, inter> //fileFormat: <AVAudioFormat 0x600002484780: 2 ch, 44100 Hz, Float32, non-inter>
초기화 시 포맷을 지정 해 줄수 있다.
AVAudioBuffer
An object that represents a buffer of audio data with a format.
포맷을 가지고있는 오디오 데이터의 버퍼를 나타내는 객체AudioNode에는 각 노드의 연결 지점을 잇는 버스가 존재하는데, 노드가 읽고 쓰는 오디오 데이터는 AudioBuffer의 클래스 인스턴스로 감싼 오디오 데이터이다.
Node를 CD로 예를 들었다면 buffer는 CD안에 들어있는 원본 데이터를 원하는 포맷으로 감싼 데이터이다.AVAudioBuffer를 상속 받는 클래스는 두가지 클래스가 존재한다.
- AVAudioCompressedBuffer: 압축 오디오 형식에 사용하는 오디오 버퍼 객체
- AVAudioPCMBuffer: PCM 오디오 형식과 함께 사용하는 오디오 버퍼를 나타내는 객체
이 글에서 다루어 볼 주제는 AVAudioPCMBuffer이다.
AVAudioPCMBuffer
An object that represents an audio buffer you use with PCM audio formats.
- 여기서 PCM 포맷이란?
간단히 말해서 아날로그의 소리를 PCM(펄스 코드 변조)를 통해 디지털화 한 포맷이다.
PCMBuffer 클래스는 PCM 형식의 오디오 버퍼를 조작하는데 유용한 메서드를 제공한다.
즉, 우린 아날로그 오디오 데이터를 처리하기 위해 오디오 데이터를 AVAudioFile로 감싸고, PCM format을 조작하기 위해 AVAudioPCMBuffer로 초기화하여 다루는 것이다.
AudioPCMBuffer 초기화
init?(pcmFormat: AVAudioFormat, frameCapacity: AVAudioFrameCount) //Creates a PCM audio buffer instance for PCM audio data. //PCM 오디오 데이터를 위해 PCM audio buffer의 인스턴스를 생성한다. // AVAudioFrameCOunt frame의 용량을 결정한다. frame - channel의 단위 init?(pcmFormat: AVAudioFormat, bufferListNoCopy: UnsafePointer<AudioBufferList>, deallocator: ((UnsafePointer<AudioBufferList>) -> Void)?) //Creates a PCM audio buffer instance without copying samples, for PCM audio data, with a specified buffer list and a deallocator closure. //샘플을 복사하지 않고 지정된 버퍼 리스트와 할당 해체되는 클로저와를 가지고 있는 PCM 오디오 버퍼 인스턴스를 생성한다.
let file = try AVAudioFile(forReading: url) let fileFormat = file.fileFormat guard let buffer = AVAudioPCMBuffer(pcmFormat: fileFormat, frameCapacity: .max) else { return }
'iOS' 카테고리의 다른 글
CollectionView Cell 이동 시키기 (2) - UICollectionView Drag and Drop Delegate (0) 2022.06.10 CollectionView Cell 이동 시키기 (1) - 기존 방식과 custom InteractiveMovement (0) 2022.06.09 WWDC) Animation hitch와 render loop (0) 2022.04.05 Cocoapods) custom 라이브러리에 이미지 파일 embedded 하기 (0) 2021.08.04 iOS) Dynamic TextView의 Underline - TextView에 밑줄 추가하기 (0) 2021.03.05