yum-archive/yapBox

A black box for your yapping

git clone https://git.yummers.dev/yum-archive/yapBox

yumAdd optional transcription & curation componentsa4c1870

master
4.2 KiB156 linesraw
1#include <curses.h>
2#include <ncurses.h>
3#include <stdio.h>
4
5#include <filesystem>
6#include <fstream>
7#include <iostream>
8#include <map>
9#include <string>
10#include <utility>
11#include <vector>
12
13typedef std::pair<std::string, std::string> datapoint_t;
14
15const int PAGE_LINES = 40;
16const int TRANSCRIPT_CHARS = 120;
17
18void getData(
19    const std::filesystem::path& data_path,
20    std::vector<datapoint_t> &datapoints,
21    std::map<std::string, std::string> &transcripts) {
22  datapoints.clear();
23  transcripts.clear();
24  printw("Scanning for files at %s\n", data_path.string().c_str());
25  for (const auto& entry : std::filesystem::directory_iterator(data_path)) {
26    //printw("  Checking file %s\n", entry.path().string().c_str());
27    if (entry.is_regular_file()) {
28      std::filesystem::path filepath = entry.path();
29      std::string filename = filepath.stem().string();
30
31      if (filepath.extension() == ".wav") {
32        std::filesystem::path txt_file = filepath.replace_extension(".txt");
33        if (std::filesystem::exists(txt_file)) {
34          datapoints.emplace_back(filepath.string(), txt_file.string());
35          std::ifstream fileStream(txt_file);
36          std::stringstream buffer;
37          buffer << fileStream.rdbuf();
38          std::string contents = buffer.str();
39          contents.erase(std::remove(contents.begin(), contents.end(), '\n'), contents.cend());
40          contents.erase(std::remove(contents.begin(), contents.end(), '\r'), contents.cend());
41          contents = contents.substr(0, TRANSCRIPT_CHARS);
42          transcripts[txt_file.string()] = contents;
43        }
44      }
45    }
46  }
47}
48
49
50int main(int argc, char* argv[]) {
51	const std::filesystem::path cwd = std::filesystem::current_path();
52	std::filesystem::path data_path = std::filesystem::current_path();
53  if (argc == 2) {
54    data_path = std::filesystem::path(argv[1]);
55  }
56
57  // Initialize ncurses
58  initscr();
59  cbreak();
60  noecho();
61  keypad(stdscr, TRUE);
62
63  // Clear the screen and wait for 'q' or 'x'
64  bool run = true;
65  bool redraw = true;
66  int idx = 0;
67  int page_offset = 0;
68
69  std::vector<datapoint_t> datapoints;
70  std::map<std::string, std::string> transcripts;
71
72  std::string digits;
73  while (run) {
74    clear();
75    {
76      int cur_idx = 0;
77      getData(data_path, datapoints, transcripts);
78      for (const auto& [txt_path, transcript] : transcripts) {
79        if (cur_idx < page_offset * PAGE_LINES) {
80          ++cur_idx;
81          continue;
82        }
83
84        char selector = ((cur_idx % PAGE_LINES) == idx) ? '>' : ' ';
85        printw("%02d %c %s: %s\n", (cur_idx % PAGE_LINES), selector, txt_path.c_str(), transcript.c_str());
86        ++cur_idx;
87
88        if (cur_idx >= (page_offset + 1) * PAGE_LINES) {
89          break;
90        }
91      }
92    }
93    refresh();
94
95    int ch = getch();
96    if (ch == 'q') {
97      run = false;
98      continue;
99    } else if (ch == 'j') {
100      int step_sz = 1;
101      if (digits.size() > 0) {
102        step_sz = std::atoi(digits.c_str());
103        digits.clear();
104      }
105
106      idx += step_sz;
107      idx = std::min(PAGE_LINES - 1, idx);
108    } else if (ch == 'k') {
109      int step_sz = 1;
110      if (digits.size() > 0) {
111        step_sz = std::atoi(digits.c_str());
112        digits.clear();
113      }
114
115      idx -= step_sz;
116      idx = std::max(0, idx);
117    } else if (ch == KEY_NPAGE) {
118      ++page_offset;
119    } else if (ch == KEY_PPAGE) {
120      --page_offset;
121      page_offset = std::max(0, page_offset);
122    } else if (ch == 'x') {
123      int cur_idx = 0;
124      for (const auto& [txt_path, transcript] : transcripts) {
125        if (cur_idx != page_offset * PAGE_LINES + idx) {
126          ++cur_idx;
127          continue;
128        }
129        std::filesystem::path wav_file = std::filesystem::path(txt_path).replace_extension(".wav");
130        std::filesystem::remove(txt_path);
131        std::filesystem::remove(wav_file);
132        break;
133      }
134    } else if (ch >= '0' && ch <= '9') {
135      digits += ch;
136    } else if (ch == 'g') {
137      int target = idx;
138      if (digits.size() > 0) {
139        target = std::atoi(digits.c_str());
140        digits.clear();
141      }
142
143      idx = target;
144      idx = std::min(PAGE_LINES - 1, idx);
145      idx = std::max(0, idx);
146    } else if (ch == 27) {  // ASCII value of esc key
147      digits.clear();
148    }
149  }
150
151  // End ncurses mode
152  endwin();
153
154  return 0;
155}
156