1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
|
//
// main.swift
// GenSin
//
// Created by Jacky Jack on 21/02/2025.
//
import Foundation
import ArgumentParser
import ComplexModule
/*
Takes as input
*/
enum OutputFormat {
case FMT_NONE
case FMT_U8
case FMT_S8
case FMT_U16
case FMT_S16
case FMT_SC16Q11
case FMT_F32
case FMT_WAV_INT16
case FMT_WAV_F32
}
var complex_type:Bool = false
print("gen sin")
//set the command line arguments
struct CommandLineArgs: ParsableCommand {
@Argument var outputFile:String = ""
@Flag(help:"Version \(software_version)") var version: Bool = false
@Flag(name: .shortAndLong) var verbose: Bool = false
@Option(name:.shortAndLong) var samplerate: Int = 1000
@Option(name:.shortAndLong) var amplitude: Float = 1.0
@Option(name:.shortAndLong) var freq: Int = 10
@Option(name:.shortAndLong) var num: Int = 100000
//@Option(name:.shortAndLong) var type: String = "sine"
@Flag(name: .shortAndLong) var listFormats: Bool = false
}
let args = CommandLineArgs.parseOrExit()
if args.version {
print("Software version \(software_version)")
exit(0)
}
if args.listFormats {
print("Supported output:")
print(" s8,u8 : [DONE]")
print(" cs8,cu8 : [TEST]")
print(" s16,u16 : [DONE]")
print(" cs16,cu16: [TEST]")
print(" s16q11 : [TEST]")
print(" cs16q11 : [TEST]")
print(" f32 : not ")
print(" cf32 : not ")
print(" wav i16, fc32: not")
}
//outputfile Format
var outputFormat = OutputFormat.FMT_NONE
if let dotIndex = args.outputFile.lastIndex(of: ".") {
let index = args.outputFile.index(dotIndex, offsetBy: 1)
let fileExtension = String(args.outputFile[index..<args.outputFile.endIndex])
print("\(args.outputFile) extension \(fileExtension)")
let supportedExtensionList = ["s8","cs8","u8","cu8","s16","cs16","u16","cu16","s16q11","cs16q11"]
if supportedExtensionList.contains(fileExtension) {
if ["s8"].contains(fileExtension) {
outputFormat = OutputFormat.FMT_S8
} else if ["cs8"].contains(fileExtension){
outputFormat = OutputFormat.FMT_S8
complex_type = true
} else if ["u8"].contains(fileExtension) {
outputFormat = OutputFormat.FMT_U8
} else if ["cu8"].contains(fileExtension) {
outputFormat = OutputFormat.FMT_U8
complex_type = true
} else if ["s16"].contains(fileExtension) {
outputFormat = OutputFormat.FMT_S16
} else if ["cs16"].contains(fileExtension) {
outputFormat = OutputFormat.FMT_S16
complex_type = true
} else if ["u16"].contains(fileExtension) {
outputFormat = OutputFormat.FMT_U16
} else if ["cu16"].contains(fileExtension) {
outputFormat = OutputFormat.FMT_U16
complex_type = true
} else if ["s16q11"].contains(fileExtension) {
outputFormat = OutputFormat.FMT_SC16Q11
} else if ["cs16q11"].contains(fileExtension) {
outputFormat = OutputFormat.FMT_SC16Q11
complex_type = true
} else if ["wav"].contains(fileExtension) {
outputFormat = OutputFormat.FMT_WAV_F32
}
else {
print("Unknown output format")
}
}
}
var outdata_s8:[Int8] = []
var outdata_u8:[UInt8] = []
var outdata_s16:[Int16] = []
var outdata_u16:[UInt16] = []
var outdata_f32:[Float32] = []
var outdata_cf32:[Complex<Float32>] = []
//var outdata_cs8:[Complex<Int8>] = []
//var outdata_cu8:[UInt8] = []
//var outdata_cs16:[Int16] = []
//var outdata_cu16:[UInt16] = []
//var outdata_cs16q11:[Complex<Int16>] = []
//var outdata_cf32:[Float32] = []
//generate signal
outdata_f32 = genSin(args.samplerate, args.freq, args.amplitude, 0.0, args.num)
outdata_cf32 = genSin(args.samplerate, args.freq, args.amplitude, 0.0, args.num)
if complex_type == false {
if outputFormat == OutputFormat.FMT_S8 {
outdata_s8 = cnvFloat32ToInt8(outdata_f32)
} else if outputFormat == OutputFormat.FMT_U8 {
outdata_u8 = cnvFloat32ToUInt8(outdata_f32)
} else if outputFormat == OutputFormat.FMT_S16 {
outdata_s16 = cnvFloat32ToInt16(outdata_f32)
} else if outputFormat == OutputFormat.FMT_U16 {
outdata_u16 = cnvFloat32ToUInt16(outdata_f32)
} else if outputFormat == OutputFormat.FMT_SC16Q11 {
outdata_s16 = cnvFloat32ToS16Q11(outdata_f32)
} else {
print("Unimplemented output format converter")
}
//complex numbers
} else {
if outputFormat == OutputFormat.FMT_S8 {
outdata_s8 = cnvFloat32ToCInt8(outdata_cf32)
} else if outputFormat == OutputFormat.FMT_U8 {
outdata_u8 = cnvFloat32ToCUInt8(outdata_cf32)
} else if outputFormat == OutputFormat.FMT_S16 {
outdata_s16 = cnvFloat32ToCInt16(outdata_cf32)
} else if outputFormat == OutputFormat.FMT_U16 {
outdata_u16 = cnvFloat32ToCUInt16(outdata_cf32)
} else if outputFormat == OutputFormat.FMT_SC16Q11 {
outdata_s16 = cnvFloat32ToS16Q11(outdata_cf32)
} else {
print("Unimplemented complex output format converter")
}
}
// write to file
let fileWrite = FileWriter()
var file_ok = false
if (checkIfFileExists(args.outputFile)) {
do {
print("try to delet file for write")
try fileWrite.deleteForWrite(filename: args.outputFile)
} catch {
print("cant delete file \(error)")
exit(1)
}
} else {
do {
print("try to creating file")
try fileWrite.createForWrite(filename: args.outputFile)
} catch {
print("cant create file \(error)")
exit(1)
}
}
do {
if outputFormat == OutputFormat.FMT_S8 {
try fileWrite.writeAll(outdata_s8)
} else if outputFormat == OutputFormat.FMT_U8 {
try fileWrite.writeAll(outdata_u8)
} else if (outputFormat == OutputFormat.FMT_S16)
|| (outputFormat == OutputFormat.FMT_SC16Q11) {
try fileWrite.writeAll(outdata_s16)
} else if outputFormat == OutputFormat.FMT_U16 {
try fileWrite.writeAll(outdata_u16)
} else {
print("Not supported write type")
}
} catch {
print("Cant write to file \(error)")
exit(1)
}
fileWrite.close()
|