summaryrefslogtreecommitdiff
path: root/Radio/Utils/iqconvert/main.swift
blob: b8b5f3bd2c923c9ed2de07c76f99da291f2a0329 (plain) (blame)
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
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
//
//  main.swift
//  iqconvert
//
//  Created by Jacky Jack on 17/01/2025.
//

import Foundation
import ArgumentParser

enum SDRInputFormat {
    case FMT_NONE
    case FMT_U8
    case FMT_I16
    case FMT_U16
    case FMT_SC16Q11
    case FMT_F32
    case FMT_WAV
}

//set the command line arguments
struct CommandLineArgs: ParsableCommand {
    @Argument var inputFile:String = ""
    @Argument var outputFile:String = ""
    @Flag(help:"Version \(software_version)") var version: Bool = false
    @Flag(name: .shortAndLong) var verbose: Bool = false
    @Flag(name: .shortAndLong) var listFormats: Bool = false
}

let args = CommandLineArgs.parseOrExit()

if args.listFormats {
    print("Supported input:")
    print(" u8:      RTLSDR   ")
    print(" u16:     AirSpyHF ")
    print(" u16r:    AirSpy   [not supported]")
    print(" i16:     AirSpy   ")
    print(" i16r:    AirSpy   [not supported]") //int16 real 1 * 16bit per sample
    print(" sc16q11: BladeRF  ")
    print(" fc32r:   AirSpy   [not supported]") //float32 real 1 * 32bit per sample
    print(" fc32:    AirSpy   [not supported]")
    print("Supported output:")
    print(" fc32:    inspectrum ")
    print(" wav:     SDR#,sdr++              [not suppoprted]")
}

//var inputFilePath:String
//check if input file exists as parameter is given
if !checkIfFileExists(args.inputFile) {
    print("Error: Input file \(args.inputFile) does not exist")
    exit(1)
}

let fileReader = FileReader()
do {
    try fileReader.open(filename: args.inputFile)
} catch {
    print("ERROR: \(error) - Cant open file \(args.inputFile)")
    exit(1)
}

var inputFormat = SDRInputFormat.FMT_NONE
//file is open lets detect extension
if let dotIndex = args.inputFile.lastIndex(of: ".") {
    let index = args.inputFile.index(dotIndex, offsetBy: 1)
    let fileExtension = String(args.inputFile[index..<args.inputFile.endIndex])
    print("\(args.inputFile) extension \(fileExtension)")
    let supportedExtensionList = ["u8","cu8","i16","ci16","u16","cu16","sc16q11"]
    if supportedExtensionList.contains(fileExtension) {
        if ["u8","cu8"].contains(fileExtension) {
            inputFormat = SDRInputFormat.FMT_U8
        } else if ["i16","ci16"].contains(fileExtension) {
            inputFormat = SDRInputFormat.FMT_I16
        } else if ["u16","cu16"].contains(fileExtension) {
            inputFormat = SDRInputFormat.FMT_U16
        } else if ["sc16q11"].contains(fileExtension) {
            inputFormat = SDRInputFormat.FMT_SC16Q11
        }
    }
}

//outputfile Format
var outputFormat = SDRInputFormat.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 = ["f32","fc32"]
    if supportedExtensionList.contains(fileExtension) {
        if ["f32","fc32"].contains(fileExtension) {
            outputFormat = SDRInputFormat.FMT_F32
        } else if ["wav"].contains(fileExtension) {
            outputFormat = SDRInputFormat.FMT_WAV
        }
    }
}

print("Input file format:\(inputFormat) output file format:\(outputFormat)")

//read from file and convert to file format



var data_u8:[UInt8] = []
var data_i16:[Int16] = []
var data_u16:[UInt16] = []

var outdata_f32:[Float32] = []

switch inputFormat {
case .FMT_U8:
    do {
        data_u8 = try fileReader.readAllUInt8()
    } catch {
        print("Reading file: \(error)")
        exit(1)
    }
    break
case .FMT_U16:
    do {
        data_u16 = try fileReader.readAllUInt16()
    } catch {
        print("Reading file: \(error)")
        exit(1)
    }
    break
case .FMT_I16:
    do {
        data_i16 = try fileReader.readAllInt16()
    } catch {
        print("Reading file: \(error)")
        exit(1)
    }
    break
case .FMT_SC16Q11:
    do {
        print("FMT_SC16Q11")
        data_i16 = try fileReader.readAllInt16()
    } catch {
        print("Reading file: \(error)")
        exit(1)
    }
    break
default:
    print("Unknow input file format")
}

//convert to output format
switch (inputFormat) {
case .FMT_U8:
    if data_u8.count > 0 {
        outdata_f32 = cnvU8toFloat32(data_u8)
    } else {
        print("No data in buffer")
    }
    break
case .FMT_U16:
    if data_u16.count > 0 {
        outdata_f32 = cnvU16toFloat32(data_u16)
    } else {
        print("No data in buffer")
    }
    break
case .FMT_I16:
    if data_i16.count > 0 {
        outdata_f32 = cnvI16toFloat32(data_i16)
    } else {
        print("No data in buffer")
    }
    break
case .FMT_SC16Q11:
    if data_i16.count > 0 {
        print("convert to FMT_SC16Q11")
        outdata_f32 = cnvSC16Q11toFloat32(data_i16)
    } else {
        print("No data in buffer")
    }
    break
default:
    print("Cant convert to unknown format")
}

// 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 {
    try fileWrite.writeAll(outdata_f32)
} catch {
    print("Cant write to file \(error)")
    exit(1)
}


fileReader.close()
fileWrite.close()