/*============================================================================= Copyright (c) 2011-2014 Bolero MURAKAMI 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) =============================================================================*/ #ifndef TOOLS_COMPOST_WAVE_IO_HPP #define TOOLS_COMPOST_WAVE_IO_HPP #include #include #include #include #include #include #include #include #include #include #include #include #include #include namespace toolspr { // // output_plot // // output for gnuplot // template std::basic_ostream& output_plot(std::basic_ostream& os, InputRange const& range) { os << std::fixed; unsigned x = 0; for (auto const& e : range) { os << x++ << ',' << e << '\n'; } return os; } template std::basic_ostream& output_plot(std::string const& filename, InputRange const& range) { std::ofstream os(filename); output_plot(os, range); } // // output_plot_real // // output for gnuplot (only real part) // template std::basic_ostream& output_plot_real(std::basic_ostream& os, InputRange const& range) { os << std::fixed; unsigned x = 0; for (auto const& e : range) { os << x++ << ',' << real(e) << '\n'; } return os; } template std::basic_ostream& output_plot_real(std::string const& filename, InputRange const& range) { std::ofstream os(filename); output_plot_real(os, range); } // // write_chunk // write_int // write_wav // // write data for wav format // template void write_chunk(OutputIterator& it, std::string const& s) { it = std::copy(s.begin(), s.end(), it); } template void write_int(OutputIterator& it, IntType const& n) { for (std::size_t i = 0; i != sizeof(IntType); ++i, ++it) { *it = static_cast((static_cast::type>(n) >> (i * CHAR_BIT)) & 0xFF); } } template void write_wav(OutputIterator& it, sprout::compost::sources::info_type const& info, InputRange const& data) { std::size_t data_size = info.size * info.bits_per_sample / CHAR_BIT; write_chunk(it, "RIFF"); write_int(it, 36 + data_size); write_chunk(it, "WAVE"); write_chunk(it, "fmt "); write_int(it, 16); write_int(it, info.format_tag); write_int(it, info.channels); write_int(it, info.samples_per_sec); write_int(it, info.bytes_per_sec); write_int(it, info.block_size); write_int(it, info.bits_per_sample); write_chunk(it, "data"); write_int(it, data_size); if (info.bits_per_sample == 16) { for (auto e : data) { write_int(it, e); } } else if (info.bits_per_sample == 8) { for (auto e : data) { write_int(it, e); } } } // // output_wav // // output for wav format // template void output_wav(std::basic_ostream& os, sprout::compost::sources::info_type const& info, InputRange const& data) { std::ostreambuf_iterator it(os); write_wav(it, info, data); } template void output_wav(std::string const& filename, sprout::compost::sources::info_type const& info, InputRange const& data) { std::ofstream os(filename, std::ios_base::out | std::ios_base::binary); output_wav(os, info, data); } // // setup_dev_dsp // // Setting up dsp device // int setup_dev_dsp(sprout::compost::sources::info_type const& info) { /* open of dsp device */ int fd = open("/dev/dsp", O_RDWR); if (fd < 0) { std::cerr << "open of /dev/dsp failed" << std::endl; return -1; } /* initializations of dsp device */ int arg = 0; int status = 0; /* sampling bit rate */ arg = info.bits_per_sample; status = ioctl(fd, SOUND_PCM_WRITE_BITS, &arg); if (status == -1 || arg != info.bits_per_sample) { return -1; } /* stereo or monoral */ arg = info.channels; status = ioctl(fd, SOUND_PCM_WRITE_CHANNELS, &arg); if (status == -1 || arg != info.channels) { return -1; } /* sampling frequency */ arg = info.samples_per_sec; status = ioctl(fd, SOUND_PCM_WRITE_RATE, &arg); if (status == -1 || arg != static_cast(info.samples_per_sec)) { return -1; } return fd; } // // write_dev_dsp // // write to dsp device // ssize_t write_dev_dsp(int fd, void const* buf, std::size_t n) { return write(fd, buf, n); } } // namespace toolspr #endif // #ifndef TOOLS_COMPOST_WAVE_IO_HPP