Skip to content

Commit

Permalink
updated CAToneFileGenerator
Browse files Browse the repository at this point in the history
Swift 3.0 support
  • Loading branch information
dimagimburg committed Sep 28, 2017
1 parent acdbc8e commit 77b5d98
Showing 1 changed file with 15 additions and 13 deletions.
28 changes: 15 additions & 13 deletions CH02_CAToneFileGenerator/CAToneFileGenerator/main.swift
Expand Up @@ -3,8 +3,8 @@
// CAToneFileGenerator
//
// Created by Ales Tsurko on 25.08.15.
// Edited by Dima Gimburg on 28.09.17.
//

import Foundation
import AudioToolbox

Expand All @@ -13,23 +13,23 @@ let DURATION = 5.0
let FILENAME_FORMAT = "%0.3f-sine.aif"

func main() {
let argc = Process.argc
let argv = Process.arguments
let argc = CommandLine.argc
let argv = CommandLine.arguments

guard argc > 1 else {
print("Usage: CAToneFileGenerator n\n(where n is tone in Hz)")
return
}

let hz = Double(argv[1])

assert(hz > 0)
assert(hz! > 0.0)

print("generating \(hz!) hz tone")

let fileName = String(format: FILENAME_FORMAT, hz!)
let filePath = (NSFileManager.defaultManager().currentDirectoryPath as NSString).stringByAppendingPathComponent(fileName)
let fileURL: CFURLRef = NSURL.fileURLWithPath(filePath)
let filePath = (FileManager.default.currentDirectoryPath as NSString).appendingPathComponent(fileName)
let fileURL: CFURL = NSURL.fileURL(withPath: filePath) as CFURL

// Prepare for format
var asbd = AudioStreamBasicDescription()
Expand All @@ -43,10 +43,12 @@ func main() {
asbd.mBytesPerPacket = asbd.mFramesPerPacket * asbd.mBytesPerFrame

// Set up the file
var audioFile: AudioFileID = nil
var _audioFile: AudioFileID? = nil
var audioErr = noErr

audioErr = AudioFileCreateWithURL(fileURL, kAudioFileAIFFType, &asbd, AudioFileFlags.EraseFile, &audioFile)
audioErr = AudioFileCreateWithURL(fileURL, kAudioFileAIFFType, &asbd, AudioFileFlags.eraseFile, &_audioFile)

guard let audioFile = _audioFile else { return }

assert(audioErr == noErr)

Expand All @@ -59,18 +61,18 @@ func main() {
while sampleCount < maxSampleCount {
for n in 0..<Int(wavelengthInSamples) {
// Square wave
// var sample = n < Int(wavelengthInSamples) / 2 ? (Int16.max).bigEndian : (Int16.min).bigEndian
// var sample = n < Int(wavelengthInSamples) / 2 ? (Int16.max).bigEndian : (Int16.min).bigEndian

// Saw wave
// var sample = Int16(((Double(n) / wavelengthInSamples) * Double(Int16.max) * 2) - Double(Int16.max)).bigEndian
// var sample = Int16(((Double(n) / wavelengthInSamples) * Double(Int16.max) * 2) - Double(Int16.max)).bigEndian

// Sine wave
var sample = Int16(Double(Int16.max) * sin(2 * M_PI * (Double(n) / wavelengthInSamples))).bigEndian
var sample = Int16(Double(Int16.max) * sin(2 * .pi * (Double(n) / wavelengthInSamples))).bigEndian

audioErr = AudioFileWriteBytes(audioFile, false, Int64(sampleCount*2), &bytesToWrite, &sample)

assert(audioErr == noErr)
sampleCount++
sampleCount += 1
}
}

Expand Down

0 comments on commit 77b5d98

Please sign in to comment.