/* * Copyright (C) 2015, Mike Walters * * This file is part of inspectrum. * * 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 3 of the License, 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. If not, see . */ #include "inputsource.h" #include #include #include #include #include #include InputSource::InputSource(const char *filename) { m_file = fopen(filename, "rb"); if (m_file == nullptr) throw std::runtime_error("Error opening file"); struct stat sb; if (fstat(fileno(m_file), &sb) != 0) throw std::runtime_error("Error fstating file"); m_file_size = sb.st_size; sampleCount = m_file_size / sizeof(std::complex); m_data = (std::complex*)mmap(NULL, m_file_size, PROT_READ, MAP_SHARED, fileno(m_file), 0); if (m_data == 0) throw std::runtime_error("Error mmapping file"); } InputSource::~InputSource() { munmap(m_data, m_file_size); fclose(m_file); } std::unique_ptr[]> InputSource::getSamples(off_t start, off_t length) { if(start < 0 || length < 0) return nullptr; if (start + length >= sampleCount) return nullptr; std::unique_ptr[]> dest(new std::complex[length]); memcpy(dest.get(), &m_data[start], length * sizeof(std::complex)); return dest; }