Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

chore: switch to Apple swift-format #38

Merged
merged 10 commits into from
Jun 15, 2024
26 changes: 5 additions & 21 deletions .github/workflows/swift.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -19,38 +19,22 @@ on:
- "Sources/Copus"

jobs:
swiftformat:
name: SwiftFormat
format:
name: Format
runs-on: ubuntu-latest
steps:
- name: Checkout repo
uses: actions/checkout@v4
with:
submodules: recursive

- name: Install SwiftFormat
- name: Install swift-format
uses: Cyberbeni/install-swift-tool@v2
with:
url: https://github.com/nicklockwood/SwiftFormat
url: https://github.com/apple/swift-format

- name: Format Swift code
run: swiftformat --verbose .
run: swift-format -r -i ./

- name: Verify formatted code is unchanged
run: git diff --exit-code HEAD -w -G'(^[^# /])|(^#\w)|(^\s+[^#/])' # Ignore whitespace and comments

test:
name: Test
runs-on: macos-latest
timeout-minutes: 10
steps:
- name: Checkout repo
uses: actions/checkout@v4
with:
submodules: recursive

- name: Test
run: swift test

- name: Generate release build
run: swift build -c release
36 changes: 36 additions & 0 deletions .github/workflows/test.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
name: Test

on:
push:
branches:
- main
paths:
- ".github/workflows/swift.yaml"
- ".gitmodules"
- "**.swift"
- "Package.resolved"
- "Sources/Copus"
pull_request:
paths:
- ".github/workflows/swift.yaml"
- ".gitmodules"
- "**.swift"
- "Package.resolved"
- "Sources/Copus"

jobs:
test:
name: Test
runs-on: macos-latest
timeout-minutes: 10
steps:
- name: Checkout repo
uses: actions/checkout@v4
with:
submodules: recursive

- name: Test
run: swift test

- name: Generate release build
run: swift build -c release
13 changes: 13 additions & 0 deletions .swift-format
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
{
"version": 1,
"lineLength": 2048,
"indentation": {
"tabs": 1
},
"tabWidth": 4,
"maximumBlankLines": 1,
"respectsExistingLineBreaks": true,
"lineBreakBeforeControlFlowKeywords": true,
"lineBreakBeforeEachArgument": false,
"prioritizeKeepingFunctionOutputTogether": true
}
4 changes: 0 additions & 4 deletions .swiftformat

This file was deleted.

2 changes: 1 addition & 1 deletion .vscode/extensions.json
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@
"foxundermoon.shell-format",
"redhat.vscode-yaml",
"stkb.rewrap",
"vknabel.vscode-swiftformat",
"vknabel.vscode-apple-swift-format",
"xaver.clang-format",
],
// List of extensions recommended by VS Code that should not be recommended for users of this workspace.
Expand Down
24 changes: 17 additions & 7 deletions .vscode/settings.json
Original file line number Diff line number Diff line change
@@ -1,12 +1,12 @@
{
// Editor
"editor.codeActionsOnSave": {
"source.fixAll.eslint": true
"source.fixAll.eslint": "explicit"
},
"editor.detectIndentation": false,
"editor.formatOnSave": true,
"editor.insertSpaces": false,
"editor.tabSize": 8,
"editor.tabSize": 4,
"files.eol": "\n",
//
// Files
Expand All @@ -26,7 +26,6 @@
// HTML
"html.format.contentUnformatted": "pre,code,textarea,script",
"html.format.unformatted": "pre,code,textarea,script,wbr",
"html.format.endWithNewline": true,
//
// JSON
"[json]": {
Expand All @@ -38,7 +37,7 @@
//
// Make
"[makefile]": {
"editor.tabSize": 8
"editor.tabSize": 4
},
//
// Markdown
Expand All @@ -47,7 +46,7 @@
80
],
"editor.codeActionsOnSave": {
"source.fixAll.markdownlint": true
"source.fixAll.markdownlint": "explicit"
}
},
"markdown.preview.breaks": true,
Expand All @@ -61,7 +60,7 @@
// Property Lists
"[plist]": {
"editor.formatOnSave": true,
"editor.tabSize": 8,
"editor.tabSize": 4,
},
//
// Shell
Expand All @@ -73,7 +72,7 @@
"[swift]": {
"editor.detectIndentation": true,
"editor.insertSpaces": false,
"editor.tabSize": 8,
"editor.tabSize": 4,
},
//
// YAML
Expand All @@ -88,4 +87,15 @@
"strings": true
}
},
"[github-actions-workflow]": {
"editor.autoIndent": "keep",
"editor.detectIndentation": true,
"editor.insertSpaces": true,
"editor.tabSize": 2,
"editor.quickSuggestions": {
"other": true,
"comments": false,
"strings": true
}
}
}
2 changes: 1 addition & 1 deletion Package.swift
Original file line number Diff line number Diff line change
Expand Up @@ -89,7 +89,7 @@ let package = Package(

.define("OPUS_BUILD"),
.define("VAR_ARRAYS", to: "1"),
.define("FLOATING_POINT"), // Enable Opus floating-point mode
.define("FLOATING_POINT"), // Enable Opus floating-point mode

.define("HAVE_DLFCN_H", to: "1"),
.define("HAVE_INTTYPES_H", to: "1"),
Expand Down
8 changes: 4 additions & 4 deletions Sources/Opus/AVAudioFormat+Extensions.swift
Original file line number Diff line number Diff line change
@@ -1,12 +1,12 @@
import AVFoundation

public extension AVAudioFormat {
enum OpusPCMFormat {
extension AVAudioFormat {
public enum OpusPCMFormat {
case int16
case float32
}

convenience init?(opusPCMFormat: OpusPCMFormat, sampleRate: Double, channels: AVAudioChannelCount) {
public convenience init?(opusPCMFormat: OpusPCMFormat, sampleRate: Double, channels: AVAudioChannelCount) {
switch opusPCMFormat {
case .int16:
self.init(commonFormat: .pcmFormatInt16, sampleRate: sampleRate, channels: channels, interleaved: channels != 1)
Expand All @@ -18,7 +18,7 @@ public extension AVAudioFormat {
}
}

var isValidOpusPCMFormat: Bool {
public var isValidOpusPCMFormat: Bool {
switch sampleRate {
case .opus8khz, .opus12khz, .opus16khz, .opus24khz, .opus48khz:
break
Expand Down
6 changes: 3 additions & 3 deletions Sources/Opus/AVAudioFrameCount+Extensions.swift
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
import AVFoundation

public extension AVAudioFrameCount {
extension AVAudioFrameCount {
// Opus can encode packets as small as 2.5ms at 8khz (20 samples)
static let opusMin: Self = 20
public static let opusMin: Self = 20

// Opus can encode packets as large as to 120ms at 48khz (5760 samples)
static let opusMax: Self = 5760
public static let opusMax: Self = 5760
}
12 changes: 6 additions & 6 deletions Sources/Opus/Double+Extensions.swift
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
public extension Double {
static let opus8khz: Self = 8000
static let opus12khz: Self = 12000
static let opus16khz: Self = 16000
static let opus24khz: Self = 24000
static let opus48khz: Self = 48000
extension Double {
public static let opus8khz: Self = 8000
public static let opus12khz: Self = 12000
public static let opus16khz: Self = 16000
public static let opus24khz: Self = 24000
public static let opus48khz: Self = 48000
}
4 changes: 2 additions & 2 deletions Sources/Opus/Opus.Application.swift
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
public extension Opus {
struct Application: Equatable, RawRepresentable, ExpressibleByIntegerLiteral {
extension Opus {
public struct Application: Equatable, RawRepresentable, ExpressibleByIntegerLiteral {
public typealias IntegerLiteralType = Int32
public var rawValue: IntegerLiteralType

Expand Down
10 changes: 5 additions & 5 deletions Sources/Opus/Opus.Decoder.swift
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
import AVFoundation
import Copus

public extension Opus {
class Decoder {
extension Opus {
public class Decoder {
let format: AVAudioFormat
let decoder: OpaquePointer

Expand Down Expand Up @@ -37,8 +37,8 @@ public extension Opus {

// MARK: Public decode methods

public extension Opus.Decoder {
func decode(_ input: Data) throws -> AVAudioPCMBuffer {
extension Opus.Decoder {
public func decode(_ input: Data) throws -> AVAudioPCMBuffer {
try input.withUnsafeBytes {
let input = $0.bindMemory(to: UInt8.self)
let sampleCount = opus_decoder_get_nb_samples(decoder, input.baseAddress!, Int32($0.count))
Expand All @@ -51,7 +51,7 @@ public extension Opus.Decoder {
}
}

func decode(_ input: UnsafeBufferPointer<UInt8>, to output: AVAudioPCMBuffer) throws {
public func decode(_ input: UnsafeBufferPointer<UInt8>, to output: AVAudioPCMBuffer) throws {
let decodedCount: Int
switch output.format.commonFormat {
case .pcmFormatInt16:
Expand Down
14 changes: 7 additions & 7 deletions Sources/Opus/Opus.Encoder.swift
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
import AVFoundation
import Copus

public extension Opus {
class Encoder {
extension Opus {
public class Encoder {
let format: AVAudioFormat
let application: Application
let encoder: OpaquePointer
Expand Down Expand Up @@ -39,26 +39,26 @@ public extension Opus {

// MARK: Public encode methods

public extension Opus.Encoder {
func encode(_ input: AVAudioPCMBuffer, to output: inout Data) throws -> Int {
extension Opus.Encoder {
public func encode(_ input: AVAudioPCMBuffer, to output: inout Data) throws -> Int {
output.count = try output.withUnsafeMutableBytes {
try encode(input, to: $0)
}
return output.count
}

func encode(_ input: AVAudioPCMBuffer, to output: inout [UInt8]) throws -> Int {
public func encode(_ input: AVAudioPCMBuffer, to output: inout [UInt8]) throws -> Int {
try output.withUnsafeMutableBufferPointer {
try encode(input, to: $0)
}
}

func encode(_ input: AVAudioPCMBuffer, to output: UnsafeMutableRawBufferPointer) throws -> Int {
public func encode(_ input: AVAudioPCMBuffer, to output: UnsafeMutableRawBufferPointer) throws -> Int {
let output = UnsafeMutableBufferPointer(start: output.baseAddress!.bindMemory(to: UInt8.self, capacity: output.count), count: output.count)
return try encode(input, to: output)
}

func encode(_ input: AVAudioPCMBuffer, to output: UnsafeMutableBufferPointer<UInt8>) throws -> Int {
public func encode(_ input: AVAudioPCMBuffer, to output: UnsafeMutableBufferPointer<UInt8>) throws -> Int {
guard input.format.sampleRate == format.sampleRate, input.format.channelCount == format.channelCount else {
throw Opus.Error.badArgument
}
Expand Down
4 changes: 2 additions & 2 deletions Sources/Opus/Opus.Error.swift
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
public extension Opus {
struct Error: Swift.Error, Equatable, RawRepresentable, ExpressibleByIntegerLiteral {
extension Opus {
public struct Error: Swift.Error, Equatable, RawRepresentable, ExpressibleByIntegerLiteral {
public typealias IntegerLiteralType = Int32
public var rawValue: IntegerLiteralType

Expand Down
3 changes: 1 addition & 2 deletions Sources/Opus/Opus.swift
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
import AVFoundation
import CoreAudio

@_exported import Copus
import CoreAudio

public enum Opus: CaseIterable {}
2 changes: 1 addition & 1 deletion Tests/OpusTests/OpusRoundTripTests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ final class OpusRoundTripTests: XCTestCase {
func testSilence() throws {
let format = AVAudioFormat(opusPCMFormat: .float32, sampleRate: .opus48khz, channels: 1)!
let input = AVAudioPCMBuffer(pcmFormat: format, frameCapacity: .opusMax)!
input.frameLength = input.frameCapacity // Silence
input.frameLength = input.frameCapacity // Silence
_ = try encodeAndDecode(input)
}

Expand Down