-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.cpp
77 lines (59 loc) · 1.96 KB
/
main.cpp
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
#include <iostream>
#include <stdio.h>
#include <vector>
#include "WavADPCM.h"
/**
* Reads the data of the given file into a std::vector
* @param path File to load
* @param target Target vector, which will be filled with the data
*/
void readFile(const std::string& path, std::vector<uint8_t>& target)
{
FILE* f = fopen(path.c_str(), "rb");
if(!f)
throw std::runtime_error("Failed to open file: " + path);
fseek(f, 0, SEEK_END);
std::size_t s = ftell(f);
fseek(f, 0, SEEK_SET);
if(s == 0)
throw std::runtime_error("File '" + path + "' is empty!");
target.resize(s);
fread(&target[0], s, 1, f);
fclose(f);
}
int main(int argc, char** argv)
{
if(argc < 3)
{
std::cout << "Usage: ms_adpcm <in-file> <out-file>";
return 0;
}
try {
std::vector<uint8_t> data;
readFile(argv[1], data);
std::cout << "File size: " << data.size() / 1024 << " kb" << std::endl;
ms_adpcm::WAVEFORMATEX fmt;
const uint8_t* rawData = nullptr;
std::size_t rawSize;
ms_adpcm::parse(data.data(), data.size(), fmt, rawData, rawSize);
std::size_t numBlocks = rawSize / fmt.nBlockAlign;
std::size_t samplesPerBlock = ms_adpcm::calculateNumSamplesInBlock(fmt.nBlockAlign);
std::size_t numSamples = numBlocks * samplesPerBlock;
std::size_t sampleIdx = 0;
std::vector<int16_t> samples;
samples.resize(numSamples);
for(std::size_t i=0; i < numBlocks; i++)
{
std::size_t offset = i * fmt.nBlockAlign;
ms_adpcm::decodeBlock(rawData + offset, fmt.nBlockAlign, &samples[sampleIdx]);
sampleIdx += samplesPerBlock;
}
std::cout << fmt;
ms_adpcm::writeWav(argv[2], samples.data(), samples.size(), fmt.nSamplesPerSec);
}catch(const std::runtime_error& e)
{
std::cout << "Conversion failed: " << e.what() << std::endl;
return 1;
}
return 0;
}