From 5303930fc9f8f33bc5e8b314f0f03716c2ad8860 Mon Sep 17 00:00:00 2001 From: jsiegle <jsiegle@mit.edu> Date: Tue, 21 May 2013 21:33:10 -0400 Subject: [PATCH] Add source code for FileReader --- Source/Processors/FileReader.cpp | 201 +++++++++++++++++++++++++++++++ Source/Processors/FileReader.h | 98 +++++++++++++++ 2 files changed, 299 insertions(+) create mode 100644 Source/Processors/FileReader.cpp create mode 100644 Source/Processors/FileReader.h diff --git a/Source/Processors/FileReader.cpp b/Source/Processors/FileReader.cpp new file mode 100644 index 000000000..8b9939f30 --- /dev/null +++ b/Source/Processors/FileReader.cpp @@ -0,0 +1,201 @@ +/* + ------------------------------------------------------------------ + + This file is part of the Open Ephys GUI + Copyright (C) 2013 Open Ephys + + ------------------------------------------------------------------ + + 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 <http://www.gnu.org/licenses/>. + +*/ + +#include "FileReader.h" +#include "Editors/FileReaderEditor.h" +#include <stdio.h> + +FileReader::FileReader() + : GenericProcessor("File Reader") +{ + + input = 0; + timestamp = 0; + + enabledState(false); + +} + +FileReader::~FileReader() +{ + if (input) + fclose(input); +} + +AudioProcessorEditor* FileReader::createEditor() +{ + editor = new FileReaderEditor(this, true); + + return editor; + +} + + +float FileReader::getDefaultSampleRate() +{ + return 40000.0f; +} + +int FileReader::getDefaultNumOutputs() +{ + return 16; +} + +float FileReader::getDefaultBitVolts() +{ + return 0.0305f; +} + +void FileReader::enabledState(bool t) +{ + + isEnabled = t; + +} + + +void FileReader::setFile(String fullpath) +{ + + filePath = fullpath; + + const char* path = filePath.getCharPointer(); + + if (input) + fclose(input); + + input = fopen(path, "r"); + + // Avoid a segfault if file isn't found + if (!input) + { + std::cout << "Can't find data file " + << '"' << path << "\"" + << std::endl; + return; + } + + fseek(input, 0, SEEK_END); + lengthOfInputFile = ftell(input); + rewind(input); + +} + + +String FileReader::getFile() +{ + return filePath; +} + +void FileReader::updateSettings() +{ + +} + + + +void FileReader::process(AudioSampleBuffer& buffer, MidiBuffer& events, int& nSamples) +{ + + uint8 data[8]; + memcpy(data, ×tamp, 8); + + // generate timestamp + addEvent(events, // MidiBuffer + TIMESTAMP, // eventType + 0, // sampleNum + nodeId, // eventID + 0, // eventChannel + 8, // numBytes + data // data + ); + + // FIXME: needs to account for the fact that the ratio might not be an exact + // integer value + int samplesNeeded = (int) float(buffer.getNumSamples()) * (getDefaultSampleRate()/44100.0f); + + if (ftell(input) >= lengthOfInputFile - samplesNeeded) + { + rewind(input); + } + + size_t numRead = fread(readBuffer, 2, samplesNeeded*buffer.getNumChannels(), input); + + int chan = 0; + int samp = 0; + + for (size_t n = 0; n < numRead; n++) + { + + if (chan == buffer.getNumChannels()) + { + samp++; + timestamp++; + chan = 0; + } + + *buffer.getSampleData(chan++, samp) = float(-readBuffer[n]) * getDefaultBitVolts(); // previously 0.035 + + } + + nSamples = samplesNeeded; + +} + + +void FileReader::setParameter(int parameterIndex, float newValue) +{ + +} + + + +void FileReader::saveCustomParametersToXml(XmlElement* parentElement) +{ + + XmlElement* childNode = parentElement->createNewChildElement("FILENAME"); + childNode->setAttribute("path", getFile()); + +} + +void FileReader::loadCustomParametersFromXml() +{ + + + if (parametersAsXml != nullptr) + { + // use parametersAsXml to restore state + + forEachXmlChildElement(*parametersAsXml, xmlNode) + { + if (xmlNode->hasTagName("FILENAME")) + { + String filepath = xmlNode->getStringAttribute("path"); + FileReaderEditor* fre = (FileReaderEditor*) getEditor(); + fre->setFile(filepath); + + } + } + } + +} \ No newline at end of file diff --git a/Source/Processors/FileReader.h b/Source/Processors/FileReader.h new file mode 100644 index 000000000..38e5c6974 --- /dev/null +++ b/Source/Processors/FileReader.h @@ -0,0 +1,98 @@ +/* + ------------------------------------------------------------------ + + This file is part of the Open Ephys GUI + Copyright (C) 2013 Open Ephys + + ------------------------------------------------------------------ + + 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 <http://www.gnu.org/licenses/>. + +*/ + + +#ifndef __FILEREADER_H_B327D3D2__ +#define __FILEREADER_H_B327D3D2__ + + +#include "../../JuceLibraryCode/JuceHeader.h" + +#include "GenericProcessor.h" + +#define BUFFER_SIZE 102400 + +/** + + Reads data from a file. + + @see GenericProcessor + +*/ + +class FileReader : public GenericProcessor + +{ +public: + + FileReader(); + ~FileReader(); + + void process(AudioSampleBuffer& buffer, MidiBuffer& midiMessages, int& nSamples); + void setParameter(int parameterIndex, float newValue); + + AudioProcessorEditor* createEditor(); + + bool hasEditor() const + { + return true; + } + + void updateSettings(); + + bool isSource() + { + return true; + } + + void enabledState(bool t); + + float getDefaultSampleRate(); + int getDefaultNumOutputs(); + float getDefaultBitVolts(); + + void setFile(String fullpath); + String getFile(); + + void saveCustomParametersToXml(XmlElement* parentElement); + void loadCustomParametersFromXml(); + +private: + + uint64 timestamp; + + int lengthOfInputFile; + FILE* input; + + int16 readBuffer[BUFFER_SIZE]; + + int bufferSize; + + String filePath; + + JUCE_DECLARE_NON_COPYABLE_WITH_LEAK_DETECTOR(FileReader); + +}; + + +#endif // __FILEREADER_H_B327D3D2__ -- GitLab