Created
December 4, 2012 15:01
-
-
Save bolero-MURAKAMI/4204899 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#include <climits> | |
#include <cstddef> | |
#include <cstdint> | |
#include <cstdlib> | |
#include <string> | |
#include <iterator> | |
#include <type_traits> | |
#include <iostream> | |
#include <fstream> | |
template<std::size_t N = 4, typename InputIterator> | |
std::string read_chunk(InputIterator& it) { | |
std::string s; | |
for (std::size_t i = 0; i != N; ++i, ++it) { | |
s.push_back(*it); | |
} | |
return s; | |
} | |
template<typename IntType, typename InputIterator> | |
IntType read_int(InputIterator& it) { | |
IntType n = 0; | |
for (std::size_t i = 0; i != sizeof(IntType); ++i, ++it) { | |
n |= static_cast<IntType>(static_cast<unsigned char>(*it)) << (i * CHAR_BIT); | |
} | |
return n; | |
} | |
int main(int argc, char* argv[]) { | |
if (argc < 2) { | |
std::cerr | |
<< "#error missing parameter.\n" | |
<< std::flush | |
; | |
return 0; | |
} | |
std::ifstream ifs(argv[1], std::ios_base::in | std::ios_base::binary); | |
std::istreambuf_iterator<char> it(ifs); | |
std::istreambuf_iterator<char> last; | |
std::cout | |
<< "#if defined(COMPOST_LOADING_SOURCE_VERSION)\n" | |
"\n" | |
"COMPOST_SRC_VERSION(0)\n" | |
"\n" | |
"#elif defined(COMPOST_LOADING_SOURCE_INFO)\n" | |
"\n" | |
; | |
if (read_chunk(it) != "RIFF") { | |
std::cerr | |
<< "#error not RIFF file.\n" | |
<< std::flush | |
; | |
return EXIT_FAILURE; | |
} | |
/*auto file_size = */read_int<std::uint32_t>(it); | |
if (read_chunk(it) != "WAVE") { | |
std::cerr | |
<< "#error not WAVE format.\n" | |
<< std::flush | |
; | |
return EXIT_FAILURE; | |
} | |
while (read_chunk(it) != "fmt ") { | |
auto chunk_size = read_int<std::uint32_t>(it); | |
std::advance(it, chunk_size); | |
} | |
auto fmt_size = read_int<std::uint32_t>(it); | |
auto format_tag = read_int<std::uint16_t>(it); // フォーマットID | |
auto channels = read_int<std::uint16_t>(it); // チャンネル数 | |
auto samples_per_sec = read_int<std::uint32_t>(it); // サンプリングレート | |
auto bytes_per_sec = read_int<std::uint32_t>(it); // データ速度 (Byte/sec) | |
auto block_size = read_int<std::uint16_t>(it); // ブロックサイズ (Byte/sample*チャンネル数) | |
auto bits_per_sample = read_int<std::uint16_t>(it); // サンプルあたりのビット数 (bit/sample) | |
std::cout | |
<< format_tag << ",\n" | |
<< channels << ",\n" | |
<< samples_per_sec << ",\n" | |
<< bytes_per_sec << ",\n" | |
<< block_size << ",\n" | |
<< bits_per_sample << ",\n" | |
; | |
std::advance(it, fmt_size - 16); | |
while (read_chunk(it) != "data") { | |
auto chunk_size = read_int<std::uint32_t>(it); | |
std::advance(it, chunk_size); | |
} | |
auto data_size = read_int<std::uint32_t>(it); | |
std::size_t size = data_size / (bits_per_sample / CHAR_BIT); | |
std::cout | |
<< size << "\n" | |
<< "\n" | |
"#elif defined(COMPOST_LOADING_SOURCE_DATA)\n" | |
"\n" | |
; | |
if (size > 0) { | |
if (bits_per_sample == 16) { | |
for (std::size_t i = 1; i != size; ++i) { | |
std::cout | |
<< read_int<std::int16_t>(it) << ",\n" | |
; | |
} | |
std::cout | |
<< read_int<std::int16_t>(it) << "\n" | |
; | |
} else if (bits_per_sample == 8) { | |
for (std::size_t i = 1; i != size; ++i) { | |
std::cout | |
<< read_int<std::uint8_t>(it) << ",\n" | |
; | |
} | |
std::cout | |
<< read_int<std::uint8_t>(it) << "\n" | |
; | |
} | |
} | |
std::cout | |
<< "\n" | |
"#endif\n" | |
<< std::flush | |
; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment