2016-12-07 14:49:42 -05:00
|
|
|
/*
|
|
|
|
* Copyright (C) 2016 Jared Boone, ShareBrained Technology, Inc.
|
|
|
|
* Copyright (C) 2016 Furrtek
|
2024-02-10 10:56:50 -05:00
|
|
|
* Copyright (C) 2024 Mark Thompson
|
2016-12-07 14:49:42 -05:00
|
|
|
*
|
|
|
|
* This file is part of PortaPack.
|
|
|
|
*
|
|
|
|
* This program is free software; you can redistribute it and/or modify
|
|
|
|
* it under the terms of the GNU General Public License as published by
|
|
|
|
* the Free Software Foundation; either version 2, or (at your option)
|
|
|
|
* any later version.
|
|
|
|
*
|
|
|
|
* This program is distributed in the hope that it will be useful,
|
|
|
|
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
|
|
* GNU General Public License for more details.
|
|
|
|
*
|
|
|
|
* You should have received a copy of the GNU General Public License
|
|
|
|
* along with this program; see the file COPYING. If not, write to
|
|
|
|
* the Free Software Foundation, Inc., 51 Franklin Street,
|
|
|
|
* Boston, MA 02110-1301, USA.
|
|
|
|
*/
|
|
|
|
|
2016-10-04 13:12:10 -04:00
|
|
|
#include "io_wave.hpp"
|
2023-06-17 16:01:46 -04:00
|
|
|
#include "utility.hpp"
|
2016-12-07 14:49:42 -05:00
|
|
|
|
2017-03-10 19:59:04 -05:00
|
|
|
bool WAVFileReader::open(const std::filesystem::path& path) {
|
2023-05-18 16:16:05 -04:00
|
|
|
size_t i = 0;
|
|
|
|
char ch;
|
|
|
|
const uint8_t tag_INAM[4] = {'I', 'N', 'A', 'M'};
|
2024-02-07 15:37:49 -05:00
|
|
|
const uint8_t tag_data[4] = {'d', 'a', 't', 'a'};
|
|
|
|
struct data_t data_header;
|
2024-02-06 05:33:00 -05:00
|
|
|
char title_buffer[32]{0};
|
2024-02-07 15:37:49 -05:00
|
|
|
uint32_t riff_size, inam_search_start, title_size;
|
2023-05-18 16:16:05 -04:00
|
|
|
size_t search_limit = 0;
|
|
|
|
|
|
|
|
// Already open ?
|
|
|
|
if (path.string() == last_path.string()) {
|
|
|
|
rewind();
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2024-02-06 05:33:00 -05:00
|
|
|
// Reinitialize to avoid old data when switching files
|
|
|
|
title_string = "";
|
|
|
|
sample_rate_ = 0;
|
|
|
|
bytes_per_sample = 0;
|
|
|
|
|
2023-06-17 16:01:46 -04:00
|
|
|
auto error = file_.open(path);
|
2023-05-18 16:16:05 -04:00
|
|
|
|
|
|
|
if (!error.is_valid()) {
|
2024-02-07 15:37:49 -05:00
|
|
|
if (!file_.read((void*)&header, sizeof(header)).is_ok()) // Read header (RIFF and WAVE)
|
|
|
|
return false;
|
2023-05-18 16:16:05 -04:00
|
|
|
|
2024-02-07 15:37:49 -05:00
|
|
|
// Assuming here that RIFF & WAV & fmt chunk ID's are all correct...
|
|
|
|
riff_size = 8 + header.cksize;
|
|
|
|
|
|
|
|
// check for the "data" chunk ID
|
|
|
|
if (memcmp(header.data.ckID, tag_data, 4) == 0) {
|
|
|
|
data_start = 20 + header.fmt.cksize + 8;
|
|
|
|
data_size_ = header.data.cksize;
|
|
|
|
inam_search_start = data_start + data_size_;
|
|
|
|
} else {
|
|
|
|
// data header wasn't where we guessed; skip over one unexpected chunk (perhaps LIST/INFO/INAM)
|
|
|
|
inam_search_start = 20 + header.fmt.cksize;
|
|
|
|
file_.seek(20 + header.fmt.cksize + 8 + header.data.cksize);
|
|
|
|
if (!file_.read((void*)&data_header, sizeof(data_header)).is_ok())
|
|
|
|
return false;
|
|
|
|
|
|
|
|
if (memcmp(data_header.ckID, tag_data, 4) == 0) {
|
|
|
|
data_start = 20 + header.fmt.cksize + 8 + header.data.cksize + 8;
|
|
|
|
data_size_ = data_header.cksize;
|
|
|
|
} else
|
|
|
|
return false;
|
|
|
|
}
|
2023-05-18 16:16:05 -04:00
|
|
|
|
|
|
|
// Look for INAM (title) tag
|
2024-02-07 15:37:49 -05:00
|
|
|
if (inam_search_start < riff_size) {
|
|
|
|
file_.seek(inam_search_start);
|
2023-06-17 16:01:46 -04:00
|
|
|
while (file_.read((void*)&ch, 1).is_ok()) {
|
2023-05-18 16:16:05 -04:00
|
|
|
if (ch == tag_INAM[i++]) {
|
|
|
|
if (i == 4) {
|
|
|
|
// Tag found, copy title
|
2023-06-17 16:01:46 -04:00
|
|
|
file_.read((void*)&title_size, sizeof(uint32_t));
|
2023-05-18 16:16:05 -04:00
|
|
|
if (title_size > 32) title_size = 32;
|
2023-06-17 16:01:46 -04:00
|
|
|
file_.read((void*)&title_buffer, title_size);
|
2023-05-18 16:16:05 -04:00
|
|
|
title_string = title_buffer;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
if (ch == tag_INAM[0])
|
|
|
|
i = 1;
|
|
|
|
else
|
|
|
|
i = 0;
|
|
|
|
}
|
|
|
|
if (search_limit == 256)
|
|
|
|
break;
|
|
|
|
else
|
|
|
|
search_limit++;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
sample_rate_ = header.fmt.nSamplesPerSec;
|
|
|
|
bytes_per_sample = header.fmt.wBitsPerSample / 8;
|
|
|
|
|
|
|
|
rewind();
|
|
|
|
|
|
|
|
last_path = path;
|
|
|
|
|
|
|
|
return true;
|
|
|
|
} else {
|
|
|
|
return false;
|
|
|
|
}
|
2016-12-07 14:49:42 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
void WAVFileReader::rewind() {
|
2023-06-17 16:01:46 -04:00
|
|
|
file_.seek(data_start);
|
2016-12-07 14:49:42 -05:00
|
|
|
}
|
|
|
|
|
2017-03-10 19:59:04 -05:00
|
|
|
std::string WAVFileReader::title() {
|
2023-05-18 16:16:05 -04:00
|
|
|
return title_string;
|
2017-03-10 19:59:04 -05:00
|
|
|
}
|
|
|
|
|
2016-12-07 14:49:42 -05:00
|
|
|
uint32_t WAVFileReader::ms_duration() {
|
2023-06-17 16:01:46 -04:00
|
|
|
return ::ms_duration(data_size_, sample_rate_, bytes_per_sample);
|
2016-12-07 14:49:42 -05:00
|
|
|
}
|
|
|
|
|
2017-10-15 10:53:40 -04:00
|
|
|
void WAVFileReader::data_seek(const uint64_t Offset) {
|
2023-06-17 16:01:46 -04:00
|
|
|
file_.seek(data_start + (Offset * bytes_per_sample));
|
2017-10-15 10:53:40 -04:00
|
|
|
}
|
2023-05-18 16:16:05 -04:00
|
|
|
|
2017-03-05 10:37:56 -05:00
|
|
|
/*int WAVFileReader::seek_mss(const uint16_t minutes, const uint8_t seconds, const uint32_t samples) {
|
2023-06-17 16:01:46 -04:00
|
|
|
const auto result = file_.seek(data_start + ((((minutes * 60) + seconds) * sample_rate_) + samples) * bytes_per_sample);
|
2023-05-18 16:16:05 -04:00
|
|
|
|
|
|
|
if (result.is_error())
|
|
|
|
return 0;
|
2016-12-07 14:49:42 -05:00
|
|
|
|
2023-05-18 16:16:05 -04:00
|
|
|
return 1;
|
2017-03-05 10:37:56 -05:00
|
|
|
}*/
|
2016-12-07 14:49:42 -05:00
|
|
|
|
|
|
|
uint16_t WAVFileReader::channels() {
|
2023-05-18 16:16:05 -04:00
|
|
|
return header.fmt.nChannels;
|
2016-12-07 14:49:42 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
uint32_t WAVFileReader::sample_rate() {
|
2023-05-18 16:16:05 -04:00
|
|
|
return sample_rate_;
|
2016-12-07 14:49:42 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
uint32_t WAVFileReader::data_size() {
|
2023-05-18 16:16:05 -04:00
|
|
|
return data_size_;
|
2016-12-07 14:49:42 -05:00
|
|
|
}
|
|
|
|
|
2017-10-15 10:53:40 -04:00
|
|
|
uint32_t WAVFileReader::sample_count() {
|
2023-05-18 16:16:05 -04:00
|
|
|
return data_size_ / bytes_per_sample;
|
2017-10-15 10:53:40 -04:00
|
|
|
}
|
|
|
|
|
2016-12-07 14:49:42 -05:00
|
|
|
uint16_t WAVFileReader::bits_per_sample() {
|
2023-05-18 16:16:05 -04:00
|
|
|
return header.fmt.wBitsPerSample;
|
2016-12-07 14:49:42 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
Optional<File::Error> WAVFileWriter::create(
|
2023-05-18 16:16:05 -04:00
|
|
|
const std::filesystem::path& filename,
|
|
|
|
size_t sampling_rate_set,
|
|
|
|
const std::string& title_set) {
|
|
|
|
sampling_rate = sampling_rate_set;
|
|
|
|
title = title_set;
|
|
|
|
const auto create_error = FileWriter::create(filename);
|
|
|
|
if (create_error.is_valid()) {
|
|
|
|
return create_error;
|
|
|
|
} else {
|
|
|
|
return update_header();
|
|
|
|
}
|
2016-12-07 14:49:42 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
Optional<File::Error> WAVFileWriter::update_header() {
|
2023-06-17 16:01:46 -04:00
|
|
|
header_t header{sampling_rate, (uint32_t)bytes_written_ - sizeof(header_t), info_chunk_size};
|
2023-05-18 16:16:05 -04:00
|
|
|
|
2023-06-17 16:01:46 -04:00
|
|
|
const auto seek_0_result = file_.seek(0);
|
2023-05-18 16:16:05 -04:00
|
|
|
if (seek_0_result.is_error()) {
|
|
|
|
return seek_0_result.error();
|
|
|
|
}
|
|
|
|
|
|
|
|
const auto old_position = seek_0_result.value();
|
|
|
|
|
2023-06-17 16:01:46 -04:00
|
|
|
const auto write_result = file_.write(&header, sizeof(header));
|
2023-05-18 16:16:05 -04:00
|
|
|
if (write_result.is_error()) {
|
|
|
|
return write_result.error();
|
|
|
|
}
|
|
|
|
|
2023-06-17 16:01:46 -04:00
|
|
|
const auto seek_old_result = file_.seek(old_position);
|
2023-05-18 16:16:05 -04:00
|
|
|
if (seek_old_result.is_error()) {
|
|
|
|
return seek_old_result.error();
|
|
|
|
}
|
|
|
|
|
|
|
|
return {};
|
2016-12-07 14:49:42 -05:00
|
|
|
}
|
2017-08-12 09:29:54 -04:00
|
|
|
|
|
|
|
Optional<File::Error> WAVFileWriter::write_tags() {
|
2023-05-18 16:16:05 -04:00
|
|
|
tags_t tags{title};
|
|
|
|
|
2023-06-17 16:01:46 -04:00
|
|
|
const auto write_result = file_.write(&tags, sizeof(tags));
|
2023-05-18 16:16:05 -04:00
|
|
|
if (write_result.is_error()) {
|
|
|
|
return write_result.error();
|
|
|
|
}
|
|
|
|
|
|
|
|
info_chunk_size = sizeof(tags);
|
|
|
|
|
|
|
|
return {};
|
2017-08-12 09:29:54 -04:00
|
|
|
}
|