summaryrefslogtreecommitdiff
path: root/Radio/Utils/GenSin/main.swift
diff options
context:
space:
mode:
Diffstat (limited to 'Radio/Utils/GenSin/main.swift')
-rw-r--r--Radio/Utils/GenSin/main.swift142
1 files changed, 142 insertions, 0 deletions
diff --git a/Radio/Utils/GenSin/main.swift b/Radio/Utils/GenSin/main.swift
new file mode 100644
index 0000000..b247dc2
--- /dev/null
+++ b/Radio/Utils/GenSin/main.swift
@@ -0,0 +1,142 @@
+//
+// main.swift
+// GenSin
+//
+// Created by Jacky Jack on 21/02/2025.
+//
+
+import Foundation
+import ArgumentParser
+
+/*
+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
+}
+
+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.listFormats {
+ print("Supported output:")
+ print(" s8,u8: [DONE]")
+ print(" s16,u16: not")
+ print(" s16q11: not")
+ print(" f32: 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","u8","s16","u16"]
+ if supportedExtensionList.contains(fileExtension) {
+ if ["s8"].contains(fileExtension) {
+ outputFormat = OutputFormat.FMT_S8
+ } else if ["u8"].contains(fileExtension) {
+ outputFormat = OutputFormat.FMT_U8
+ } else if ["s16"].contains(fileExtension) {
+ outputFormat = OutputFormat.FMT_S16
+ } else if ["u16"].contains(fileExtension) {
+ outputFormat = OutputFormat.FMT_U16
+ }// 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] = []
+
+//generate signal
+outdata_f32 = genSin(args.samplerate, args.freq, args.amplitude, 0.0, args.num)
+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 {
+ print("Unimplemented 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 {
+ 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()
+
+
+