File size: 1,711 Bytes
2e0d354
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
#pragma once
#include <algorithm>
#include <array>
#include <chrono>
#include <cmath>
#include <complex>
#include <cstdint>
#include <cstring>
#include <filesystem>
#include <fstream>
#include <iostream>
#include <memory>
#include <numeric>
#include <stdexcept>
#include <string>
#include <vector>

namespace fs = std::filesystem;
using Floats = std::vector<float>;
using Row = std::array<int32_t, 10>;
struct View { const float* data; size_t size; };
inline View view(const Floats& v) { return {v.data(), v.size()}; }
inline void require(bool ok, const std::string& reason) {
    if (!ok) throw std::runtime_error(reason);
}
template<class T> std::vector<T> read_file(const fs::path& p, size_t expected = 0) {
    auto bytes = fs::file_size(p);
    require(bytes % sizeof(T) == 0 && (!expected || bytes == expected * sizeof(T)), "bad size: " + p.string());
    std::vector<T> data(bytes / sizeof(T));
    std::ifstream f(p, std::ios::binary);
    require(bool(f.read(reinterpret_cast<char*>(data.data()), bytes)), "read: " + p.string());
    return data;
}
template<class T> void write_file(const fs::path& p, const std::vector<T>& data) {
    std::ofstream f(p, std::ios::binary);
    f.write(reinterpret_cast<const char*>(data.data()), data.size()*sizeof(T));
    f.close();
    require(bool(f), "write: " + p.string());
}
using Outputs = std::vector<Floats*>;
class Graph {
public:
    virtual void run(const std::vector<View>& inputs, const Outputs& outputs) = 0;
    virtual ~Graph() = default;
};
std::unique_ptr<Graph> open_graph(const fs::path& model, bool decoder);
Floats decode_audio(const Floats& head, int frames, const fs::path& shared);
void save_audio(const fs::path& dest, const Floats& wav);