Audacity 3.2.0
Public Types | Public Member Functions | Private Member Functions | Private Attributes | List of all members
MultiFormatReader Class Reference

MultiFormatReader reads raw audio files in different formats and machine endianness representations. More...

#include <MultiFormatReader.h>

Collaboration diagram for MultiFormatReader:
[legend]

Public Types

enum  FormatT {
  Int8 = 0 , Int16 , Int32 , Uint8 ,
  Uint16 , Uint32 , Float , Double
}
 

Public Member Functions

 MultiFormatReader (const char *filename)
 
 ~MultiFormatReader ()
 
void Reset ()
 
size_t ReadSamples (void *buffer, size_t len, MultiFormatReader::FormatT format, MachineEndianness::EndiannessT end)
 
size_t ReadSamples (void *buffer, size_t len, size_t stride, MultiFormatReader::FormatT format, MachineEndianness::EndiannessT end)
 

Private Member Functions

size_t Read (void *buffer, size_t size, size_t len, size_t stride)
 
void SwapBytes (void *buffer, size_t size, size_t len)
 

Private Attributes

FILE * mpFid
 
MachineEndianness mEnd
 
uint8_t mSwapBuffer [8]
 

Detailed Description

MultiFormatReader reads raw audio files in different formats and machine endianness representations.

Definition at line 49 of file MultiFormatReader.h.

Member Enumeration Documentation

◆ FormatT

Enumerator
Int8 
Int16 
Int32 
Uint8 
Uint16 
Uint32 
Float 
Double 

Definition at line 56 of file MultiFormatReader.h.

Constructor & Destructor Documentation

◆ MultiFormatReader()

MultiFormatReader::MultiFormatReader ( const char *  filename)

Definition at line 39 of file MultiFormatReader.cpp.

40 : mpFid(NULL)
41{
42 mpFid = fopen(filename, "rb");
43
44 if (mpFid == NULL)
45 {
46 throw std::runtime_error("Error opening file");
47 }
48}

References mpFid.

◆ ~MultiFormatReader()

MultiFormatReader::~MultiFormatReader ( )

Definition at line 50 of file MultiFormatReader.cpp.

51{
52 if (mpFid != NULL)
53 {
54 fclose(mpFid);
55 }
56}

References mpFid.

Member Function Documentation

◆ Read()

size_t MultiFormatReader::Read ( void *  buffer,
size_t  size,
size_t  len,
size_t  stride 
)
private

Definition at line 109 of file MultiFormatReader.cpp.

110{
111 size_t actRead = 0;
112 uint8_t* pWork = (uint8_t*) buffer;
113
114 if (stride > 1)
115 {
116 // There are gaps between consecutive samples,
117 // so do a scattered read
118 for (size_t n = 0; n < len; n++)
119 {
120 actRead += fread(&(pWork[n*size]), size, 1, mpFid);
121 // FIXME: TRAP_ERR fseek return in MultiFormatReader unchecked.
122 fseek(mpFid, (stride - 1) * size, SEEK_CUR);
123 }
124 }
125 else
126 {
127 // Just do a linear read
128 actRead = fread(buffer, size, len, mpFid);
129 }
130
131 return actRead;
132}

References mpFid, and size.

Referenced by ReadSamples().

Here is the caller graph for this function:

◆ ReadSamples() [1/2]

size_t MultiFormatReader::ReadSamples ( void *  buffer,
size_t  len,
MultiFormatReader::FormatT  format,
MachineEndianness::EndiannessT  end 
)

Definition at line 66 of file MultiFormatReader.cpp.

69{
70 return ReadSamples(buffer, len, 1, format, end);
71}
size_t ReadSamples(void *buffer, size_t len, MultiFormatReader::FormatT format, MachineEndianness::EndiannessT end)
const char * end(const char *str) noexcept
Definition: StringUtils.h:106

References details::end(), anonymous_namespace{ExportPCM.cpp}::format, and ReadSamples().

Referenced by ReadSamples(), and FormatClassifier::ReadSignal().

Here is the call graph for this function:
Here is the caller graph for this function:

◆ ReadSamples() [2/2]

size_t MultiFormatReader::ReadSamples ( void *  buffer,
size_t  len,
size_t  stride,
MultiFormatReader::FormatT  format,
MachineEndianness::EndiannessT  end 
)

Definition at line 74 of file MultiFormatReader.cpp.

77{
78 bool swapflag = (mEnd.Which() != end);
79 size_t actRead=0;
80
81 switch(format)
82 {
83 case Int8:
84 case Uint8:
85 actRead = Read(buffer, 1, len, stride);
86 break;
87 case Int16:
88 case Uint16:
89 actRead = Read(buffer, 2, len, stride);
90 if(swapflag) SwapBytes(buffer, 2, len);
91 break;
92 case Int32:
93 case Uint32:
94 case Float:
95 actRead = Read(buffer, 4, len, stride);
96 if(swapflag) SwapBytes(buffer, 4, len);
97 break;
98 case Double:
99 actRead = Read(buffer, 8, len, stride);
100 if(swapflag) SwapBytes(buffer, 8, len);
101 break;
102 default:
103 break;
104 }
105
106 return actRead;
107}
EndiannessT Which()
MachineEndianness mEnd
void SwapBytes(void *buffer, size_t size, size_t len)
size_t Read(void *buffer, size_t size, size_t len, size_t stride)

References Double, details::end(), Float, anonymous_namespace{ExportPCM.cpp}::format, Int16, Int32, Int8, mEnd, Read(), SwapBytes(), Uint16, Uint32, Uint8, and MachineEndianness::Which().

Here is the call graph for this function:

◆ Reset()

void MultiFormatReader::Reset ( )

Definition at line 58 of file MultiFormatReader.cpp.

59{
60 if (mpFid != NULL)
61 {
62 rewind(mpFid);
63 }
64}

References mpFid.

Referenced by FormatClassifier::ReadSignal().

Here is the caller graph for this function:

◆ SwapBytes()

void MultiFormatReader::SwapBytes ( void *  buffer,
size_t  size,
size_t  len 
)
private

Definition at line 135 of file MultiFormatReader.cpp.

136{
137 uint8_t* pResBuffer = (uint8_t*) buffer;
138 uint8_t* pCurBuffer;
139
140 if (size > 8)
141 {
142 throw std::runtime_error("SwapBytes Exception: Format width exceeding 8 bytes.");
143 }
144
145 for (size_t i = 0; i < len; i++)
146 {
147 pCurBuffer = &(pResBuffer[i*size]);
148 memcpy(mSwapBuffer, &(pCurBuffer[0]), size);
149
150 for (size_t n = 0; n < size; n++)
151 {
152 pCurBuffer[n] = mSwapBuffer[size - n - 1];
153 }
154 }
155}

References mSwapBuffer, and size.

Referenced by ReadSamples().

Here is the caller graph for this function:

Member Data Documentation

◆ mEnd

MachineEndianness MultiFormatReader::mEnd
private

Definition at line 52 of file MultiFormatReader.h.

Referenced by ReadSamples().

◆ mpFid

FILE* MultiFormatReader::mpFid
private

Definition at line 51 of file MultiFormatReader.h.

Referenced by MultiFormatReader(), Read(), Reset(), and ~MultiFormatReader().

◆ mSwapBuffer

uint8_t MultiFormatReader::mSwapBuffer[8]
private

Definition at line 53 of file MultiFormatReader.h.

Referenced by SwapBytes().


The documentation for this class was generated from the following files: