Wxwidgets: More menu twiddling
[lsnes.git] / avi / sdmp.cpp
blob3e8e0c30fa084312022485808e44331f140eccb5
1 #include "lsnes.hpp"
2 #include <snes/snes.hpp>
4 #include "sdmp.hpp"
6 #include <sstream>
7 #include <iomanip>
8 #include <stdexcept>
10 #define CUTOFF 2100000000
12 sdump_dumper::sdump_dumper(const std::string& prefix, bool ss)
14 oprefix = prefix;
15 sdump_ss = ss;
16 ssize = 0;
17 next_seq = 0;
18 sdump_iopen = false;
21 sdump_dumper::~sdump_dumper() throw()
23 try {
24 end();
25 } catch(...) {
29 void sdump_dumper::frame(const uint32_t* buffer, unsigned flags)
31 flags &= 0xF;
32 unsigned char tbuffer[2049];
33 if(!sdump_iopen || (ssize > CUTOFF && !sdump_ss)) {
34 std::cerr << "Starting new segment" << std::endl;
35 if(sdump_iopen)
36 out.close();
37 std::ostringstream str;
38 if(sdump_ss)
39 str << oprefix;
40 else
41 str << oprefix << "_" << std::setw(4) << std::setfill('0') << (next_seq++) << ".sdmp";
42 std::string str2 = str.str();
43 out.open(str2.c_str(), std::ios::out | std::ios::binary);
44 if(!out)
45 throw std::runtime_error("Failed to open '" + str2 + "'");
46 sdump_iopen = true;
47 tbuffer[0] = 'S';
48 tbuffer[1] = 'D';
49 tbuffer[2] = 'M';
50 tbuffer[3] = 'P';
51 uint32_t apufreq = SNES::system.apu_frequency();
52 uint32_t cpufreq = SNES::system.cpu_frequency();
53 tbuffer[4] = cpufreq >> 24;
54 tbuffer[5] = cpufreq >> 16;
55 tbuffer[6] = cpufreq >> 8;
56 tbuffer[7] = cpufreq;
57 tbuffer[8] = apufreq >> 24;
58 tbuffer[9] = apufreq >> 16;
59 tbuffer[10] = apufreq >> 8;
60 tbuffer[11] = apufreq;
61 out.write(reinterpret_cast<char*>(tbuffer), 12);
62 if(!out)
63 throw std::runtime_error("Failed to write header to '" + str2 + "'");
64 ssize = 12;
66 tbuffer[0] = flags;
67 for(unsigned i = 0; i < 512; i++) {
68 for(unsigned j = 0; j < 512; j++) {
69 tbuffer[4 * j + 1] = buffer[512 * i + j] >> 24;
70 tbuffer[4 * j + 2] = buffer[512 * i + j] >> 16;
71 tbuffer[4 * j + 3] = buffer[512 * i + j] >> 8;
72 tbuffer[4 * j + 4] = buffer[512 * i + j];
74 out.write(reinterpret_cast<char*>(tbuffer + (i ? 1 : 0)), i ? 2048 : 2049);
76 if(!out)
77 throw std::runtime_error("Failed to write frame");
78 ssize += 1048577;
81 void sdump_dumper::sample(short left, short right)
83 if(!sdump_iopen)
84 return;
85 unsigned char pkt[5];
86 pkt[0] = 16;
87 pkt[1] = static_cast<unsigned short>(left) >> 8;
88 pkt[2] = static_cast<unsigned short>(left);
89 pkt[3] = static_cast<unsigned short>(right) >> 8;
90 pkt[4] = static_cast<unsigned short>(right);
91 out.write(reinterpret_cast<char*>(pkt), 5);
92 if(!out)
93 throw std::runtime_error("Failed to write sample");
94 ssize += 5;
97 void sdump_dumper::end()
99 if(sdump_iopen)
100 out.close();