Chris@175: /* -*- c-basic-offset: 4 indent-tabs-mode: nil -*- vi:set ts=8 sts=4 sw=4: */ Chris@175: Chris@175: /* Chris@175: Sonic Visualiser Chris@175: An audio file viewer and annotation editor. Chris@175: Centre for Digital Music, Queen Mary, University of London. Chris@202: This file copyright 2006 QMUL. Chris@175: Chris@175: This program is free software; you can redistribute it and/or Chris@175: modify it under the terms of the GNU General Public License as Chris@175: published by the Free Software Foundation; either version 2 of the Chris@175: License, or (at your option) any later version. See the file Chris@175: COPYING included with this distribution for more information. Chris@175: */ Chris@175: Chris@1122: #ifndef WRITABLE_WAVE_FILE_MODEL_H Chris@1122: #define WRITABLE_WAVE_FILE_MODEL_H Chris@175: Chris@175: #include "WaveFileModel.h" Chris@1122: #include "ReadOnlyWaveFileModel.h" Chris@1122: #include "PowerOfSqrtTwoZoomConstraint.h" Chris@175: Chris@175: class WavFileWriter; Chris@175: class WavFileReader; Chris@175: Chris@1122: class WritableWaveFileModel : public WaveFileModel Chris@175: { Chris@175: Q_OBJECT Chris@175: Chris@175: public: Chris@1520: enum class Normalisation { None, Peak }; Chris@1520: Chris@1520: /** Chris@1520: * Create a WritableWaveFileModel of the given sample rate and Chris@1520: * channel count, storing data in a new float-type extended WAV Chris@1520: * file with the given path. If path is the empty string, the data Chris@1520: * will be stored in a newly-created temporary file. Chris@1520: * Chris@1520: * If normalisation == None, sample values will be written Chris@1520: * verbatim, and will be ready to read as soon as they have been Chris@1520: * written. Otherwise samples will be normalised on writing; this Chris@1520: * will require an additional pass and temporary file, and no Chris@1520: * samples will be available to read until after writeComplete() Chris@1520: * has returned. Chris@1520: */ Chris@1520: WritableWaveFileModel(QString path, Chris@1520: sv_samplerate_t sampleRate, Chris@1520: int channels, Chris@1520: Normalisation normalisation); Chris@1520: Chris@1520: /** Chris@1520: * Create a WritableWaveFileModel of the given sample rate and Chris@1520: * channel count, storing data in a new float-type extended WAV Chris@1520: * file in a temporary location. This is equivalent to passing an Chris@1520: * empty path to the constructor above. Chris@1520: * Chris@1520: * If normalisation == None, sample values will be written Chris@1520: * verbatim, and will be ready to read as soon as they have been Chris@1520: * written. Otherwise samples will be normalised on writing; this Chris@1520: * will require an additional pass and temporary file, and no Chris@1520: * samples will be available to read until after writeComplete() Chris@1520: * has returned. Chris@1520: */ Chris@1517: WritableWaveFileModel(sv_samplerate_t sampleRate, Chris@1517: int channels, Chris@1520: Normalisation normalisation); Chris@1520: Chris@1520: /** Chris@1520: * Create a WritableWaveFileModel of the given sample rate and Chris@1520: * channel count, storing data in a new float-type extended WAV Chris@1520: * file in a temporary location, and applying no normalisation. Chris@1520: * Chris@1520: * This is equivalent to passing an empty path and Chris@1520: * Normalisation::None to the first constructor above. Chris@1520: */ Chris@1520: WritableWaveFileModel(sv_samplerate_t sampleRate, Chris@1520: int channels); Chris@1520: Chris@175: ~WritableWaveFileModel(); Chris@175: Chris@188: /** Chris@188: * Call addSamples to append a block of samples to the end of the Chris@1337: * file. Chris@1337: * Chris@1337: * This function only appends the samples to the file being Chris@1337: * written; it does not update the model's view of the samples in Chris@1337: * that file. That is, it updates the file on disc but the model Chris@1337: * itself does not change its content. This is because re-reading Chris@1337: * the file to update the model may be more expensive than adding Chris@1337: * the samples in the first place. If you are writing small Chris@1337: * numbers of samples repeatedly, you probably only want the model Chris@1337: * to update periodically rather than after every write. Chris@1337: * Chris@1337: * Call updateModel() periodically to tell the model to update its Chris@1337: * own view of the samples in the file being written. Chris@1337: * Chris@1337: * Call setWriteProportion() periodically if the file being Chris@1337: * written has known duration and you want the model to be able to Chris@1337: * report the write progress as a percentage. Chris@1337: * Chris@1337: * Call writeComplete() when the file has been completely written. Chris@188: */ Chris@1325: virtual bool addSamples(const float *const *samples, sv_frame_t count); Chris@1133: Chris@1133: /** Chris@1337: * Tell the model to update its own (read) view of the (written) Chris@1337: * file. May cause modelChanged() and modelChangedWithin() to be Chris@1337: * emitted. See the comment to addSamples above for rationale. Chris@1337: */ Chris@1337: void updateModel(); Chris@1337: Chris@1337: /** Chris@1133: * Set the proportion of the file which has been written so far, Chris@1133: * as a percentage. This may be used to indicate progress. Chris@1133: * Chris@1133: * Note that this differs from the "completion" percentage Chris@1133: * reported through isReady()/getCompletion(). That percentage is Chris@1133: * updated when "internal processing has advanced... but the model Chris@1133: * has not changed externally", i.e. it reports progress in Chris@1133: * calculating the initial state of a model. In contrast, an Chris@1133: * update to setWriteProportion corresponds to a change in the Chris@1133: * externally visible state of the model (i.e. it contains more Chris@1133: * data than before). Chris@1133: */ Chris@1133: void setWriteProportion(int proportion); Chris@1133: Chris@1133: /** Chris@1133: * Indicate that writing is complete. You should call this even if Chris@1337: * you have never called setWriteProportion() or updateModel(). Chris@1133: */ Chris@1133: void writeComplete(); Chris@1133: Chris@1133: static const int PROPORTION_UNKNOWN; Chris@1133: Chris@1133: /** Chris@1133: * Get the proportion of the file which has been written so far, Chris@1133: * as a percentage. Return PROPORTION_UNKNOWN if unknown. Chris@1133: */ Chris@1133: int getWriteProportion() const; Chris@175: Chris@1580: bool isOK() const override; Chris@1580: bool isReady(int *) const override; Chris@1133: Chris@1133: /** Chris@1133: * Return the generation completion percentage of this model. This Chris@1133: * is always 100, because the model is always in a complete state Chris@1133: * -- it just contains varying amounts of data depending on how Chris@1133: * much has been written. Chris@1133: */ Chris@1133: virtual int getCompletion() const { return 100; } Chris@188: Chris@1580: const ZoomConstraint *getZoomConstraint() const override { Chris@179: static PowerOfSqrtTwoZoomConstraint zc; Chris@179: return &zc; Chris@179: } Chris@179: Chris@1580: sv_frame_t getFrameCount() const override; Chris@1580: int getChannelCount() const override { return m_channels; } Chris@1580: sv_samplerate_t getSampleRate() const override { return m_sampleRate; } Chris@1580: sv_samplerate_t getNativeRate() const override { return m_sampleRate; } Chris@1122: Chris@1580: QString getTitle() const override { Chris@1122: if (m_model) return m_model->getTitle(); Chris@1122: else return ""; Chris@1122: } Chris@1580: QString getMaker() const override { Chris@1122: if (m_model) return m_model->getMaker(); Chris@1122: else return ""; Chris@1122: } Chris@1580: QString getLocation() const override { Chris@1122: if (m_model) return m_model->getLocation(); Chris@1122: else return ""; Chris@1122: } Chris@175: Chris@1580: float getValueMinimum() const override { return -1.0f; } Chris@1580: float getValueMaximum() const override { return 1.0f; } Chris@175: Chris@1580: sv_frame_t getStartFrame() const override { return m_startFrame; } Chris@1580: sv_frame_t getEndFrame() const override { return m_startFrame + getFrameCount(); } Chris@175: Chris@1580: void setStartFrame(sv_frame_t startFrame) override; Chris@175: Chris@1580: floatvec_t getData(int channel, sv_frame_t start, sv_frame_t count) const override; Chris@175: Chris@1580: std::vector getMultiChannelData(int fromchannel, int tochannel, sv_frame_t start, sv_frame_t count) const override; Chris@363: Chris@1580: int getSummaryBlockSize(int desired) const override; Chris@377: Chris@1580: void getSummaries(int channel, sv_frame_t start, sv_frame_t count, Chris@1580: RangeBlock &ranges, int &blockSize) const override; Chris@300: Chris@1580: Range getSummary(int channel, sv_frame_t start, sv_frame_t count) const override; Chris@175: Chris@1580: QString getTypeName() const override { return tr("Writable Wave File"); } Chris@345: Chris@1580: void toXml(QTextStream &out, Chris@175: QString indent = "", Chris@1580: QString extraAttributes = "") const override; Chris@175: Chris@175: protected: Chris@1122: ReadOnlyWaveFileModel *m_model; Chris@1520: Chris@1520: /** When normalising, this writer is used to write verbatim Chris@1520: * samples to the temporary file prior to Chris@1520: * normalisation. Otherwise it's null Chris@1520: */ Chris@1520: WavFileWriter *m_temporaryWriter; Chris@1520: QString m_temporaryPath; Chris@1520: Chris@1520: /** When not normalising, this writer is used to write verbatim Chris@1520: * samples direct to the target file. When normalising, it is Chris@1520: * used to write normalised samples to the target after the Chris@1520: * temporary file has been completed. But it is still created on Chris@1520: * initialisation, so that there is a file header ready for the Chris@1520: * reader to address. Chris@1520: */ Chris@1520: WavFileWriter *m_targetWriter; Chris@1520: QString m_targetPath; Chris@1520: Chris@175: WavFileReader *m_reader; Chris@1520: Normalisation m_normalisation; Chris@1040: sv_samplerate_t m_sampleRate; Chris@929: int m_channels; Chris@1038: sv_frame_t m_frameCount; Chris@1038: sv_frame_t m_startFrame; Chris@1133: int m_proportion; Chris@1520: Chris@1520: private: Chris@1520: void init(QString path = ""); Chris@1520: void normaliseToTarget(); Chris@175: }; Chris@175: Chris@175: #endif Chris@175: