|
From: <man...@us...> - 2015-03-04 12:42:10
|
Revision: 4822
http://sourceforge.net/p/modplug/code/4822
Author: manxorist
Date: 2015-03-04 12:41:57 +0000 (Wed, 04 Mar 2015)
Log Message:
-----------
[Ref] Convert SampleIO::WriteSample to std::ostream and mpt::IO.
[Ref] Use mpt::oftsream to write wav and raw samples.
[Ref] Remove FILE* support in class WavWriter, which is now unsed. (fixes a 64bit warning)
Modified Paths:
--------------
trunk/OpenMPT/soundlib/SampleFormats.cpp
trunk/OpenMPT/soundlib/SampleIO.cpp
trunk/OpenMPT/soundlib/SampleIO.h
trunk/OpenMPT/soundlib/WAVTools.cpp
trunk/OpenMPT/soundlib/WAVTools.h
Modified: trunk/OpenMPT/soundlib/SampleFormats.cpp
===================================================================
--- trunk/OpenMPT/soundlib/SampleFormats.cpp 2015-03-04 12:19:20 UTC (rev 4821)
+++ trunk/OpenMPT/soundlib/SampleFormats.cpp 2015-03-04 12:41:57 UTC (rev 4822)
@@ -463,8 +463,14 @@
bool CSoundFile::SaveWAVSample(SAMPLEINDEX nSample, const mpt::PathString &filename) const
//----------------------------------------------------------------------------------------
{
- WAVWriter file(filename);
+ mpt::ofstream f(filename, std::ios::binary);
+ if(!f)
+ {
+ return false;
+ }
+ WAVWriter file(&f);
+
if(!file.IsValid())
{
return false;
@@ -480,7 +486,7 @@
sample.uFlags[CHN_STEREO] ? SampleIO::stereoInterleaved : SampleIO::mono,
SampleIO::littleEndian,
sample.uFlags[CHN_16BIT] ? SampleIO::signedPCM : SampleIO::unsignedPCM)
- .WriteSample(file.GetFile(), sample));
+ .WriteSample(f, sample));
file.WriteLoopInformation(sample);
file.WriteExtraInformation(sample, GetType());
@@ -504,8 +510,11 @@
bool CSoundFile::SaveRAWSample(SAMPLEINDEX nSample, const mpt::PathString &filename) const
//----------------------------------------------------------------------------------------
{
- FILE *f;
- if((f = mpt_fopen(filename, "wb")) == NULL) return false;
+ mpt::ofstream f(filename, std::ios::binary);
+ if(!f)
+ {
+ return false;
+ }
const ModSample &sample = Samples[nSample];
SampleIO(
@@ -515,7 +524,6 @@
SampleIO::signedPCM)
.WriteSample(f, sample);
- fclose(f);
return true;
}
Modified: trunk/OpenMPT/soundlib/SampleIO.cpp
===================================================================
--- trunk/OpenMPT/soundlib/SampleIO.cpp 2015-03-04 12:19:20 UTC (rev 4821)
+++ trunk/OpenMPT/soundlib/SampleIO.cpp 2015-03-04 12:41:57 UTC (rev 4822)
@@ -592,9 +592,10 @@
#ifndef MODPLUG_NO_FILESAVE
+
// Write a sample to file
-size_t SampleIO::WriteSample(FILE *f, const ModSample &sample, SmpLength maxSamples) const
-//----------------------------------------------------------------------------------------
+size_t SampleIO::WriteSample(std::ostream *f, const ModSample &sample, SmpLength maxSamples) const
+//------------------------------------------------------------------------------------------------
{
if(!sample.HasSampleData()) return 0;
@@ -640,11 +641,11 @@
bufcount++;
if(bufcount >= CountOf(buffer16))
{
- if(f) fwrite(buffer16, 1, bufcount * 2, f);
+ if(f) mpt::IO::WriteRaw(*f, buffer16, bufcount * 2);
bufcount = 0;
}
}
- if (bufcount) if(f) fwrite(buffer16, 1, bufcount * 2, f);
+ if (bufcount) if(f) mpt::IO::WriteRaw(*f, buffer16, bufcount * 2);
}
else if(GetBitDepth() == 8 && GetChannelFormat() == stereoSplit &&
@@ -672,11 +673,11 @@
}
if(bufcount >= CountOf(buffer8))
{
- if(f) fwrite(buffer8, 1, bufcount, f);
+ if(f) mpt::IO::WriteRaw(*f, buffer8, bufcount);
bufcount = 0;
}
}
- if (bufcount) if(f) fwrite(buffer8, 1, bufcount, f);
+ if (bufcount) if(f) mpt::IO::WriteRaw(*f, buffer8, bufcount);
}
len = numSamples * 2;
}
@@ -707,11 +708,11 @@
bufcount++;
if(bufcount >= CountOf(buffer16))
{
- if(f) fwrite(buffer16, 1, bufcount * 2, f);
+ if(f) mpt::IO::WriteRaw(*f, buffer16, bufcount * 2);
bufcount = 0;
}
}
- if (bufcount) if(f) fwrite(buffer16, 1, bufcount * 2, f);
+ if (bufcount) if(f) mpt::IO::WriteRaw(*f, buffer16, bufcount * 2);
}
len = numSamples * 4;
}
@@ -720,7 +721,7 @@
{
// Stereo signed interleaved
len = sample.GetSampleSizeInBytes();
- if(f) fwrite(pSampleVoid, 1, len, f);
+ if(f) mpt::IO::WriteRaw(*f, pSampleVoid, len);
}
else if(GetBitDepth() == 8 && GetChannelFormat() == stereoInterleaved && GetEncoding() == unsignedPCM)
@@ -733,18 +734,17 @@
bufcount++;
if(bufcount >= CountOf(buffer8))
{
- if(f) fwrite(buffer8, 1, bufcount, f);
+ if(f) mpt::IO::WriteRaw(*f, buffer8, bufcount);
bufcount = 0;
}
}
- if (bufcount) if(f) fwrite(buffer8, 1, bufcount, f);
+ if (bufcount) if(f) mpt::IO::WriteRaw(*f, buffer8, bufcount);
}
else if(GetEncoding() == IT214 || GetEncoding() == IT215)
{
// IT2.14-encoded samples
- mpt::FILE_ostream s(f);
- ITCompression its(sample, GetEncoding() == IT215, f ? &s : nullptr);
+ ITCompression its(sample, GetEncoding() == IT215, f);
len = its.GetCompressedSize();
}
@@ -777,15 +777,33 @@
}
if(bufcount >= CountOf(buffer8))
{
- if(f) fwrite(buffer8, 1, bufcount, f);
+ if(f) mpt::IO::WriteRaw(*f, buffer8, bufcount);
bufcount = 0;
}
}
- if (bufcount) if(f) fwrite(buffer8, 1, bufcount, f);
+ if (bufcount) if(f) mpt::IO::WriteRaw(*f, buffer8, bufcount);
}
return len;
}
+
+// Write a sample to file
+size_t SampleIO::WriteSample(std::ostream &f, const ModSample &sample, SmpLength maxSamples) const
+//------------------------------------------------------------------------------------------------
+{
+ return WriteSample(&f, sample, maxSamples);
+}
+
+
+// Write a sample to file
+size_t SampleIO::WriteSample(FILE *f, const ModSample &sample, SmpLength maxSamples) const
+//----------------------------------------------------------------------------------------
+{
+ mpt::FILE_ostream s(f);
+ return WriteSample(f ? &s : nullptr, sample, maxSamples);
+}
+
+
#endif // MODPLUG_NO_FILESAVE
Modified: trunk/OpenMPT/soundlib/SampleIO.h
===================================================================
--- trunk/OpenMPT/soundlib/SampleIO.h 2015-03-04 12:19:20 UTC (rev 4821)
+++ trunk/OpenMPT/soundlib/SampleIO.h 2015-03-04 12:41:57 UTC (rev 4822)
@@ -169,7 +169,11 @@
size_t ReadSample(ModSample &sample, FileReader &file) const;
#ifndef MODPLUG_NO_FILESAVE
+ // Optionally write a sample to file
+ size_t WriteSample(std::ostream *f, const ModSample &sample, SmpLength maxSamples = 0) const;
// Write a sample to file
+ size_t WriteSample(std::ostream &f, const ModSample &sample, SmpLength maxSamples = 0) const;
+ // Write a sample to file
size_t WriteSample(FILE *f, const ModSample &sample, SmpLength maxSamples = 0) const;
#endif // MODPLUG_NO_FILESAVE
};
Modified: trunk/OpenMPT/soundlib/WAVTools.cpp
===================================================================
--- trunk/OpenMPT/soundlib/WAVTools.cpp 2015-03-04 12:19:20 UTC (rev 4821)
+++ trunk/OpenMPT/soundlib/WAVTools.cpp 2015-03-04 12:41:57 UTC (rev 4822)
@@ -257,29 +257,9 @@
// WAV Writing
-// Output to file: Initialize with filename.
-WAVWriter::WAVWriter(const mpt::PathString &filename) : f(nullptr), fileOwned(false), s(nullptr), memory(nullptr), memSize(0)
-//---------------------------------------------------------------------------------------------------------------------------
-{
- f = mpt_fopen(filename, "w+b");
- fileOwned = true;
- Init();
-}
-
-
-// Output to file: Initialize with FILE*.
-WAVWriter::WAVWriter(FILE *file) : f(nullptr), fileOwned(false), s(nullptr), memory(nullptr), memSize(0)
-//------------------------------------------------------------------------------------------------------
-{
- f = file;
- fileOwned = false;
- Init();
-}
-
-
// Output to stream: Initialize with std::ostream*.
-WAVWriter::WAVWriter(std::ostream *stream) : f(nullptr), fileOwned(false), s(nullptr), memory(nullptr), memSize(0)
-//----------------------------------------------------------------------------------------------------------------
+WAVWriter::WAVWriter(std::ostream *stream) : s(nullptr), memory(nullptr), memSize(0)
+//----------------------------------------------------------------------------------
{
s = stream;
Init();
@@ -287,8 +267,8 @@
// Output to clipboard: Initialize with pointer to memory and size of reserved memory.
-WAVWriter::WAVWriter(void *mem, size_t size) : f(nullptr), fileOwned(false), s(nullptr), memory(static_cast<uint8 *>(mem)), memSize(size)
-//---------------------------------------------------------------------------------------------------------------------------------------
+WAVWriter::WAVWriter(void *mem, size_t size) : s(nullptr), memory(static_cast<uint8 *>(mem)), memSize(size)
+//----------------------------------------------------------------------------------------------------------
{
Init();
}
@@ -329,21 +309,6 @@
Seek(0);
Write(fileHeader);
- if(f != nullptr)
- {
-#ifdef _DEBUG
- fseek(f, 0, SEEK_END);
- size_t realSize = static_cast<size_t>(ftell(f));
- MPT_ASSERT(totalSize == realSize);
-#endif
- if(fileOwned)
- {
- fclose(f);
- fileOwned = false;
- }
- }
-
- f = nullptr;
s = nullptr;
memory = nullptr;
@@ -397,11 +362,8 @@
position = pos;
totalSize = std::max(totalSize, position);
- if(f != nullptr)
+ if(s != nullptr)
{
- fseek(f, position, SEEK_SET);
- } else if(s != nullptr)
- {
s->seekp(position);
}
}
@@ -411,11 +373,8 @@
void WAVWriter::Write(const void *data, size_t numBytes)
//------------------------------------------------------
{
- if(f != nullptr)
+ if(s != nullptr)
{
- fwrite(data, numBytes, 1, f);
- } else if(s != nullptr)
- {
s->write(static_cast<const char*>(data), numBytes);
} else if(memory != nullptr)
{
Modified: trunk/OpenMPT/soundlib/WAVTools.h
===================================================================
--- trunk/OpenMPT/soundlib/WAVTools.h 2015-03-04 12:19:20 UTC (rev 4821)
+++ trunk/OpenMPT/soundlib/WAVTools.h 2015-03-04 12:41:57 UTC (rev 4822)
@@ -386,9 +386,6 @@
//=============
{
protected:
- // When writing to file: File handle
- FILE *f;
- bool fileOwned;
// When writing to a stream: Stream pointer
std::ostream *s;
// When writing to memory: Memory address + length
@@ -405,10 +402,6 @@
RIFFChunk chunkHeader;
public:
- // Output to file: Initialize with filename. The created FILE* is owned by this instance.
- WAVWriter(const mpt::PathString &filename);
- // Output to file: Initialize with FILE*.
- WAVWriter(FILE *file);
// Output to stream: Initialize with std::ostream*.
WAVWriter(std::ostream *stream);
// Output to clipboard: Initialize with pointer to memory and size of reserved memory.
@@ -417,7 +410,7 @@
~WAVWriter();
// Check if anything can be written to the file.
- bool IsValid() const { return f != nullptr || s != nullptr || memory != nullptr; }
+ bool IsValid() const { return s != nullptr || memory != nullptr; }
// Finalize the file by closing the last open chunk and updating the file header. Returns total size of file.
size_t Finalize();
@@ -426,8 +419,6 @@
// Skip some bytes... For example after writing sample data.
void Skip(size_t numBytes) { Seek(position + numBytes); }
- // Get file handle
- FILE *GetFile() { return f; }
// Get position in file (not counting any changes done to the file from outside this class, i.e. through GetFile())
size_t GetPosition() const { return position; }
This was sent by the SourceForge.net collaborative development platform, the world's largest Open Source development site.
|