Skip to content

Commit

Permalink
🐞 Fix buffer method, layout and update README.md
Browse files Browse the repository at this point in the history
🐞 Bugs

The controlProminence method is now deprecated. Therefore, I removed the deprecated code and updated the button style to be borderedProminent instead of bordered.

The match button does not have padding and touches the bottom edge of the screen. Therefore, I added vertical padding to it.

The old buffer method would make the session fail to find a match for the audio. Therefore, I updated the buffer and signature methods per the developer documentation (https://developer.apple.com/documentation/shazamkit/shsignaturegenerator/generating_a_signature_from_an_audio_buffer).

📄 Documentation

iOS 15.0 and Xcode 13.0 are officially out of beta. Therefore, I removed any mention of the word "beta" in the README.md file.

The README.md file does not contain a screenshot similar to other repositories from the organisation. Therefore, I added a screenshot.
  • Loading branch information
Yaacoub committed Nov 7, 2021
1 parent c0f55ce commit 3e33143
Show file tree
Hide file tree
Showing 3 changed files with 39 additions and 13 deletions.
8 changes: 6 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -4,9 +4,9 @@ This demo project is associated with my article [ShazamKit: A first look](https:

### Availability

iOS 15.0+ (Beta)
iOS 15.0+
<br>
Xcode 13.0+ (Beta)
Xcode 13.0+

### Frameworks

Expand All @@ -15,3 +15,7 @@ Foundation
ShazamKit
<br>
SwiftUI

<br>

<img src="https://user-images.githubusercontent.com/34966652/140640986-a54461da-7a9c-4c09-b57e-9d3f6c39a5a3.jpeg" height="640" alt="Preview"/>
40 changes: 31 additions & 9 deletions Shared/ViewModels/ContentViewModel.swift
Original file line number Diff line number Diff line change
Expand Up @@ -42,18 +42,40 @@ class ContentViewModel: NSObject, ObservableObject {

//MARK:- Private Methods

private func buffer() -> AVAudioPCMBuffer? {
guard let audioFileURL = audioFileURL,
let audioFile = try? AVAudioFile(forReading: audioFileURL) else { return nil }
let pcmFormat = audioFile.processingFormat
let frameCapacity = AVAudioFrameCount(12 * audioFile.fileFormat.sampleRate)
guard let buffer = AVAudioPCMBuffer(pcmFormat: pcmFormat, frameCapacity: frameCapacity) else { return nil }
try? audioFile.read(into: buffer)
return buffer
private func buffer(audioFile: AVAudioFile, outputFormat: AVAudioFormat) -> AVAudioPCMBuffer? {
let frameCount = AVAudioFrameCount((1024 * 64) / (audioFile.processingFormat.streamDescription.pointee.mBytesPerFrame))
let outputFrameCapacity = AVAudioFrameCount(12 * audioFile.fileFormat.sampleRate)
guard let inputBuffer = AVAudioPCMBuffer(pcmFormat: audioFile.processingFormat, frameCapacity: frameCount),
let outputBuffer = AVAudioPCMBuffer(pcmFormat: outputFormat, frameCapacity: outputFrameCapacity),
let converter = AVAudioConverter(from: audioFile.processingFormat, to: outputFormat) else { return nil }
while true {
let status = converter.convert(to: outputBuffer, error: nil) { inNumPackets, outStatus in
do {
try audioFile.read(into: inputBuffer)
outStatus.pointee = .haveData
return inputBuffer
} catch {
if audioFile.framePosition >= audioFile.length {
outStatus.pointee = .endOfStream
return nil
} else {
outStatus.pointee = .noDataNow
return nil
}
}
}
switch status {
case .endOfStream, .error: return nil
default: return outputBuffer
}
}
}

private func signature() -> SHSignature? {
guard let buffer = buffer() else { return nil }
guard let audioFileURL = audioFileURL,
let audioFile = try? AVAudioFile(forReading: audioFileURL),
let audioFormat = AVAudioFormat(standardFormatWithSampleRate: 44100, channels: 1),
let buffer = buffer(audioFile: audioFile, outputFormat: audioFormat) else { return nil }
let signatureGenerator = SHSignatureGenerator()
try? signatureGenerator.append(buffer, at: nil)
return signatureGenerator.signature()
Expand Down
4 changes: 2 additions & 2 deletions Shared/Views/ContentView.swift
Original file line number Diff line number Diff line change
Expand Up @@ -56,9 +56,9 @@ struct ContentView: View {
.padding(.vertical)
Button(viewModel.isMatching ? "Matching…" : "Match preexisting audio", action: viewModel.startMatching)
.disabled(viewModel.isMatching)
.padding(.vertical)
}
.buttonStyle(.bordered)
.controlProminence(.increased)
.buttonStyle(.borderedProminent)
.controlSize(.large)
.tint(.blue)
}
Expand Down

0 comments on commit 3e33143

Please sign in to comment.