-
-
Notifications
You must be signed in to change notification settings - Fork 620
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #1314 from shogo4405/feature/append-audio-buffer
Add stream.appendAudioBuffer interface.
- Loading branch information
Showing
21 changed files
with
244 additions
and
126 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,41 @@ | ||
import AVFoundation | ||
import Foundation | ||
import HaishinKit | ||
|
||
protocol AudioCaptureDelegate: AnyObject { | ||
func audioCapture(_ audioCapture: AudioCapture, buffer: AVAudioBuffer, time: AVAudioTime) | ||
} | ||
|
||
final class AudioCapture { | ||
var isRunning: Atomic<Bool> = .init(false) | ||
var delegate: (any AudioCaptureDelegate)? | ||
private let audioEngine = AVAudioEngine() | ||
} | ||
|
||
extension AudioCapture: Running { | ||
func startRunning() { | ||
guard !isRunning.value else { | ||
return | ||
} | ||
let input = audioEngine.inputNode | ||
let mixer = audioEngine.mainMixerNode | ||
audioEngine.connect(input, to: mixer, format: input.inputFormat(forBus: 0)) | ||
input.installTap(onBus: 0, bufferSize: 1024, format: input.inputFormat(forBus: 0)) { buffer, when in | ||
self.delegate?.audioCapture(self, buffer: buffer, time: when) | ||
} | ||
do { | ||
try audioEngine.start() | ||
isRunning.mutate { $0 = true } | ||
} catch { | ||
logger.error(error) | ||
} | ||
} | ||
|
||
func stopRunning() { | ||
guard isRunning.value else { | ||
return | ||
} | ||
audioEngine.stop() | ||
isRunning.mutate { $0 = false } | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,9 @@ | ||
import AVFoundation | ||
import CoreMedia | ||
import Foundation | ||
|
||
extension AVAudioTime { | ||
func makeTime() -> CMTime { | ||
return .init(value: CMTimeValue(sampleTime), timescale: CMTimeScale(sampleRate)) | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,8 @@ | ||
import AVFoundation | ||
import Foundation | ||
|
||
extension CMTime { | ||
func makeAudioTime() -> AVAudioTime { | ||
return .init(sampleTime: value, atRate: Double(timescale)) | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.