2013-09-19 16:04:30 +00:00
|
|
|
/*=============================================================================
|
2019-01-07 08:47:17 +00:00
|
|
|
Copyright (c) 2011-2019 Bolero MURAKAMI
|
2013-09-19 16:04:30 +00:00
|
|
|
https://github.com/bolero-MURAKAMI/Sprout
|
|
|
|
|
|
|
|
Distributed under the Boost Software License, Version 1.0. (See accompanying
|
|
|
|
file LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt)
|
|
|
|
=============================================================================*/
|
2014-04-30 07:30:26 +00:00
|
|
|
|
2013-09-19 16:04:30 +00:00
|
|
|
#include <climits>
|
|
|
|
#include <cstdint>
|
|
|
|
#include <cstdlib>
|
|
|
|
#include <string>
|
|
|
|
#include <iterator>
|
|
|
|
#include <type_traits>
|
|
|
|
#include <iostream>
|
|
|
|
#include <fstream>
|
2014-04-30 07:30:26 +00:00
|
|
|
#include <sprout/workaround/std/cstddef.hpp>
|
2013-09-19 16:04:30 +00:00
|
|
|
|
2013-09-21 04:56:23 +00:00
|
|
|
namespace toolspr {
|
|
|
|
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;
|
2013-09-19 16:04:30 +00:00
|
|
|
}
|
|
|
|
|
2013-09-21 04:56:23 +00:00
|
|
|
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;
|
2013-09-19 16:04:30 +00:00
|
|
|
}
|
2013-09-21 04:56:23 +00:00
|
|
|
} // namespace toolspr
|
2013-09-19 16:04:30 +00:00
|
|
|
|
|
|
|
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"
|
|
|
|
;
|
|
|
|
|
2013-09-21 04:56:23 +00:00
|
|
|
if (toolspr::read_chunk(it) != "RIFF") {
|
2013-09-19 16:04:30 +00:00
|
|
|
std::cerr
|
|
|
|
<< "#error not RIFF file.\n"
|
|
|
|
<< std::flush
|
|
|
|
;
|
|
|
|
return EXIT_FAILURE;
|
|
|
|
}
|
2013-09-21 04:56:23 +00:00
|
|
|
/*auto file_size = */toolspr::read_int<std::uint32_t>(it);
|
|
|
|
if (toolspr::read_chunk(it) != "WAVE") {
|
2013-09-19 16:04:30 +00:00
|
|
|
std::cerr
|
|
|
|
<< "#error not WAVE format.\n"
|
|
|
|
<< std::flush
|
|
|
|
;
|
|
|
|
return EXIT_FAILURE;
|
|
|
|
}
|
|
|
|
|
2013-09-21 04:56:23 +00:00
|
|
|
while (toolspr::read_chunk(it) != "fmt ") {
|
|
|
|
auto chunk_size = toolspr::read_int<std::uint32_t>(it);
|
2013-09-19 16:04:30 +00:00
|
|
|
std::advance(it, chunk_size);
|
|
|
|
}
|
2013-09-21 04:56:23 +00:00
|
|
|
auto fmt_size = toolspr::read_int<std::uint32_t>(it);
|
|
|
|
auto format_tag = toolspr::read_int<std::uint16_t>(it);
|
|
|
|
auto channels = toolspr::read_int<std::uint16_t>(it);
|
|
|
|
auto samples_per_sec = toolspr::read_int<std::uint32_t>(it);
|
|
|
|
auto bytes_per_sec = toolspr::read_int<std::uint32_t>(it);
|
|
|
|
auto block_size = toolspr::read_int<std::uint16_t>(it);
|
|
|
|
auto bits_per_sample = toolspr::read_int<std::uint16_t>(it);
|
2013-09-19 16:04:30 +00:00
|
|
|
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);
|
|
|
|
|
2013-09-21 04:56:23 +00:00
|
|
|
while (toolspr::read_chunk(it) != "data") {
|
|
|
|
auto chunk_size = toolspr::read_int<std::uint32_t>(it);
|
2013-09-19 16:04:30 +00:00
|
|
|
std::advance(it, chunk_size);
|
|
|
|
}
|
2013-09-21 04:56:23 +00:00
|
|
|
auto data_size = toolspr::read_int<std::uint32_t>(it);
|
2013-09-19 16:04:30 +00:00
|
|
|
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
|
2013-09-21 04:56:23 +00:00
|
|
|
<< toolspr::read_int<std::int16_t>(it) << ",\n"
|
2013-09-19 16:04:30 +00:00
|
|
|
;
|
|
|
|
}
|
|
|
|
std::cout
|
2013-09-21 04:56:23 +00:00
|
|
|
<< toolspr::read_int<std::int16_t>(it) << "\n"
|
2013-09-19 16:04:30 +00:00
|
|
|
;
|
|
|
|
} else if (bits_per_sample == 8) {
|
|
|
|
for (std::size_t i = 1; i != size; ++i) {
|
|
|
|
std::cout
|
2013-09-21 04:56:23 +00:00
|
|
|
<< toolspr::read_int<std::uint8_t>(it) << ",\n"
|
2013-09-19 16:04:30 +00:00
|
|
|
;
|
|
|
|
}
|
|
|
|
std::cout
|
2013-09-21 04:56:23 +00:00
|
|
|
<< toolspr::read_int<std::uint8_t>(it) << "\n"
|
2013-09-19 16:04:30 +00:00
|
|
|
;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
std::cout
|
|
|
|
<< "\n"
|
|
|
|
"#endif\n"
|
|
|
|
<< std::flush
|
|
|
|
;
|
|
|
|
}
|