Lua: Don't crash if input.[gs]et{,a} is called outside on_input
[lsnes.git] / include / video / sox.hpp
blobff7a36689fe4f02de263f84b0e636a7dea4a07b1
1 #ifndef _sox__hpp__included__
2 #define _sox__hpp__included__
4 #include <cstdint>
5 #include <fstream>
6 #include <stdexcept>
7 #include <vector>
9 /**
10 * .sox sound dumper.
12 class sox_dumper
14 public:
15 /**
16 * Create new dumper with specified sound sampling rate and channel count.
18 * parameter filename: The name of file to dump to.
19 * parameter samplerate: The sampling rate (must be positive)
20 * parameter channels: The channel count.
22 * throws std::bad_alloc: Not enough memory
23 * throws std::runtime_error: Error opening .sox file
25 sox_dumper(const std::string& filename, double samplerate, uint32_t channels) throw(std::bad_alloc,
26 std::runtime_error);
28 /**
29 * Destructor.
31 ~sox_dumper() throw();
33 /**
34 * Close the dump.
36 * throws std::bad_alloc: Not enough memory
37 * throws std::runtime_error: Error fixing and closing .sox file
39 void close() throw(std::bad_alloc, std::runtime_error);
41 /**
42 * Dump a sample
44 * parameters a: Sample channel values.
46 template<typename... args>
47 void sample(args... a)
49 sample2<0>(a...);
51 private:
52 template<size_t o>
53 void sample2()
55 for(size_t i = o; i < samplebuffer.size(); ++i)
56 samplebuffer[i] = 0;
57 internal_dump_sample();
59 template<size_t o, typename itype, typename... args>
60 void sample2(itype v, args... a)
62 if(o < samplebuffer.size())
63 place_sample(o, v);
64 sample2<o + 1>(a...);
67 void place_sample(size_t index, int8_t v)
69 samplebuffer[index] = static_cast<int32_t>(v) << 24;
71 void place_sample(size_t index, uint8_t v)
73 place_sample(index, static_cast<uint32_t>(v) << 24);
75 void place_sample(size_t index, int16_t v)
77 samplebuffer[index] = static_cast<int32_t>(v) << 16;
79 void place_sample(size_t index, uint16_t v)
81 place_sample(index, static_cast<uint32_t>(v) << 16);
83 void place_sample(size_t index, int32_t v)
85 samplebuffer[index] = v;
87 void place_sample(size_t index, uint32_t v)
89 place_sample(index, static_cast<int32_t>(v + 0x80000000U));
92 void internal_dump_sample();
93 std::vector<char> databuf;
94 std::vector<int32_t> samplebuffer;
95 std::ofstream sox_file;
96 uint64_t samples_dumped;
99 #endif