File: module_cluster_instruments.cpp

package info (click to toggle)
satdump 1.2.2%2Bgb79af48-2
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 81,648 kB
  • sloc: cpp: 276,768; ansic: 164,598; lisp: 1,219; sh: 283; xml: 106; makefile: 7
file content (267 lines) | stat: -rw-r--r-- 11,010 bytes parent folder | download | duplicates (2)
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
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
#include "module_cluster_instruments.h"
#include <fstream>
#include "common/ccsds/ccsds_tm/vcdu.h"
#include "logger.h"
#include <filesystem>
#include "imgui/imgui.h"
#include "common/utils.h"
#include "common/ccsds/ccsds_tm/demuxer.h"
#include "products/products.h"
#include "products/dataset.h"
#include "common/simple_deframer.h"
#include "instruments/wbd_decoder.h"
#include "common/audio/audio_sink.h"
#include "common/dsp/io/wav_writer.h"
#include "core/config.h"


namespace cluster
{
    namespace instruments
    {
        CLUSTERInstrumentsDecoderModule::CLUSTERInstrumentsDecoderModule(std::string input_file, std::string output_file_hint, nlohmann::json parameters)
            : ProcessingModule(input_file, output_file_hint, parameters)
        {
            play_audio = satdump::config::main_cfg["user_interface"]["play_audio"]["value"].get<bool>();
        }

        void CLUSTERInstrumentsDecoderModule::process()
        {
            if (input_data_type == DATA_FILE)
                filesize = getFilesize(d_input_file);
            std::ifstream data_in;
            if (input_data_type == DATA_FILE)
                data_in = std::ifstream(d_input_file, std::ios::binary);

            logger->info("Using input frames " + d_input_file);

            time_t lastTime = 0;
            uint8_t cadu[1279];

            // Demuxers
            ccsds::ccsds_tm::Demuxer demuxer_vcid1(1101, false);

            std::ofstream output(d_output_file_hint + "_wbd.frm", std::ios::binary);
            std::ofstream output_cadu2(d_output_file_hint + "_cadu_bkp.cadu", std::ios::binary);

            def::SimpleDeframer wbddeframer(0xFAF334, 24, 8768, 0);
            WBDdecoder wbddecode;

            // Audio stuff
            int16_t audio_buffer[4352];
            std::shared_ptr<audio::AudioSink> audio_sink;
            if (input_data_type != DATA_FILE && audio::has_sink())
            {
                enable_audio = true;
                audio_sink = audio::get_default_sink();
                audio_sink->set_samplerate(27443);
                audio_sink->start();
            }

            // Buffers to wav...for all the antennas
            std::ofstream out_antenna_Ez(d_output_file_hint + "_Ez.wav", std::ios::binary);
            uint64_t final_data_size_ant_Ez = 0;
            dsp::WavWriter wave_writer_Ez(out_antenna_Ez);
            wave_writer_Ez.write_header(27443, 1);

            std::ofstream out_antenna_Bx(d_output_file_hint + "_Bx.wav", std::ios::binary);
            uint64_t final_data_size_ant_Bx = 0;
            dsp::WavWriter wave_writer_Bx(out_antenna_Bx);
            wave_writer_Bx.write_header(27443, 1);

            std::ofstream out_antenna_By(d_output_file_hint + "_By.wav", std::ios::binary);
            uint64_t final_data_size_ant_By = 0;
            dsp::WavWriter wave_writer_By(out_antenna_By);
            wave_writer_By.write_header(27443, 1);

            std::ofstream out_antenna_Ey(d_output_file_hint + "_Ey.wav", std::ios::binary);
            uint64_t final_data_size_ant_Ey = 0;
            dsp::WavWriter wave_writer_Ey(out_antenna_Ey);
            wave_writer_Ey.write_header(27443, 1);

            while (input_data_type == DATA_FILE ? !data_in.eof() : input_active.load())
            {
                // Read buffer
                if (input_data_type == DATA_FILE)
                    data_in.read((char *)&cadu, 1279);
                else
                    input_fifo->read((uint8_t *)&cadu, 1279);

                // Save (for now)
                output_cadu2.write((char *)cadu, 1279);

                // Parse this transport frame
                ccsds::ccsds_tm::VCDU vcdu = ccsds::ccsds_tm::parseVCDU(cadu);

                if (vcdu.vcid == 5) // Parse WBD VCID
                {
                    std::vector<ccsds::CCSDSPacket> ccsdsFrames = demuxer_vcid1.work(cadu);
                    for (ccsds::CCSDSPacket &pkt : ccsdsFrames)
                    {
                        std::vector<std::vector<uint8_t>> minorframes = wbddeframer.work(pkt.payload.data(), pkt.payload.size());
                        for (auto &frm : minorframes)
                        {
                            std::vector<MajorFrame> majorframes = wbddecode.work(frm.data());
                            for (auto &majorframe : majorframes)
                            {
                                for (int i = 0; i < 4352; i++)
                                    audio_buffer[i] = (int(majorframe.payload[i]) - 127) * 255;

                                if (enable_audio && play_audio)
                                    audio_sink->push_samples(audio_buffer, 4352);

                                if (majorframe.VCXO == 0)
                                {
                                    output.write((char *)majorframe.payload.data(), majorframe.payload.size());
                                    if (majorframe.antennaselect == 0)
                                    {
                                        out_antenna_Ez.write((char *)audio_buffer, 4352 * sizeof(int16_t));
                                        final_data_size_ant_Ez += 4352 * sizeof(int16_t);
                                    }
                                    else if (majorframe.antennaselect == 1)
                                    {
                                        out_antenna_Bx.write((char *)audio_buffer, 4352 * sizeof(int16_t));
                                        final_data_size_ant_Bx += 4352 * sizeof(int16_t);
                                    }
                                    else if (majorframe.antennaselect == 2)
                                    {
                                        out_antenna_By.write((char *)audio_buffer, 4352 * sizeof(int16_t));
                                        final_data_size_ant_By += 4352 * sizeof(int16_t);
                                    }
                                    else if (majorframe.antennaselect == 3)
                                    {
                                        out_antenna_Ey.write((char *)audio_buffer, 4352 * sizeof(int16_t));
                                        final_data_size_ant_Ey += 4352 * sizeof(int16_t);
                                    }
                                }

                                param_antenna = majorframe.antennaselect;
                                param_convfrq = majorframe.convfreq;
                                param_band = majorframe.outputmode;
                                param_VCXO = majorframe.VCXO;
                                param_OBDH = majorframe.OBDH;
                                param_commands = majorframe.commands;
                            }
                        }
                    }
                }

                progress = data_in.tellg();
                if (time(NULL) % 10 == 0 && lastTime != time(NULL))
                {
                    lastTime = time(NULL);
                    logger->info("Progress " + std::to_string(round(((double)progress / (double)filesize) * 1000.0) / 10.0) + "%%");
                }
            }

            if (enable_audio)
                audio_sink->stop();
                
            wave_writer_Ez.finish_header(final_data_size_ant_Ez);
            wave_writer_Bx.finish_header(final_data_size_ant_Bx);
            wave_writer_By.finish_header(final_data_size_ant_By);
            wave_writer_Ey.finish_header(final_data_size_ant_Ey);
            out_antenna_Ez.close();
            out_antenna_Bx.close();
            out_antenna_By.close();
            out_antenna_Ey.close();

            if (input_data_type == DATA_FILE)
                data_in.close();
            output_cadu2.close();
        }

        void CLUSTERInstrumentsDecoderModule::drawUI(bool window)
        {
            ImGui::Begin("Cluster Instruments Decoder", NULL, window ? 0 : NOWINDOW_FLAGS);

            ImGui::Text("Antenna :");
            ImGui::SameLine();
            if (param_antenna == 0)
                ImGui::Text("Ez");
            else if (param_antenna == 1)
                ImGui::Text("Bx");
            else if (param_antenna == 2)
                ImGui::Text("By");
            else if (param_antenna == 3)
                ImGui::Text("Ey");

            ImGui::Text("Conversion Freq : ");
            ImGui::SameLine();
            if (param_convfrq == 0)
                ImGui::Text("Baseband");
            else if (param_convfrq == 1)
                ImGui::Text("125.454 kHz");
            else if (param_convfrq == 2)
                ImGui::Text("250.908 kHz");
            else if (param_convfrq == 3)
                ImGui::Text("501.816 kHz");

            // ImGui::Text("Freq Band:");
            // ImGui::SameLine();
            // ImGui::Text("%d", param_band);

            ImGui::Text("VCXO : ");
            ImGui::SameLine();
            if (param_VCXO == 0)
                ImGui::Text("Locked");
            else
                ImGui::Text("Unlocked");

            ImGui::Text("OBDH : ");
            ImGui::SameLine();
            ImGui::Text("%d", param_OBDH);

            ImGui::Text("CMD : ");
            ImGui::SameLine();
            ImGui::Text("%d", param_commands);

            if (enable_audio)
            {
                ImGui::Spacing();
                const char* btn_icon, * label;
                ImU32 color;
                if (play_audio)
                {
                    color = style::theme.green;
                    btn_icon = u8"\uF028##wbdaudio";
                    label = "Audio Playing";
                }
                else
                {
                    color = style::theme.red;
                    btn_icon = u8"\uF026##wbdaudio";
                    label = "Audio Muted";
                }

                ImGui::PushStyleColor(ImGuiCol_Text, color);
                if (ImGui::Button(btn_icon))
                    play_audio = !play_audio;
                ImGui::PopStyleColor();
                ImGui::SameLine();
                ImGui::TextUnformatted(label);
            }

            if (input_data_type == DATA_FILE)
                ImGui::ProgressBar((double)progress / (double)filesize, ImVec2(ImGui::GetContentRegionAvail().x, 20 * ui_scale));

            ImGui::End();
        }

        std::string CLUSTERInstrumentsDecoderModule::getID()
        {
            return "cluster_instruments";
        }

        std::vector<std::string> CLUSTERInstrumentsDecoderModule::getParameters()
        {
            return {};
        }

        std::shared_ptr<ProcessingModule> CLUSTERInstrumentsDecoderModule::getInstance(std::string input_file, std::string output_file_hint, nlohmann::json parameters)
        {
            return std::make_shared<CLUSTERInstrumentsDecoderModule>(input_file, output_file_hint, parameters);
        }
    } // namespace amsu
} // namespace metop
// For Michal B. : This is what i am doing in your classes instead of boring python tasks :) -RS