Audacity 3.2.0
MultiFormatReader.cpp
Go to the documentation of this file.
1/**********************************************************************
2
3 Audacity: A Digital Audio Editor
4
5 MultiFormatReader.cpp
6
7 Philipp Sibler
8
9******************************************************************//*******************************************************************/
16
17#include "MultiFormatReader.h"
18
19#include <exception>
20#include <stdexcept>
21#include <cstring>
22#include <stdint.h>
23
24#include <wx/defs.h>
25
27{
28 if (wxBYTE_ORDER == wxLITTLE_ENDIAN)
29 {
31 }
32 else
33 {
35 }
36}
37
38
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}
49
51{
52 if (mpFid != NULL)
53 {
54 fclose(mpFid);
55 }
56}
57
59{
60 if (mpFid != NULL)
61 {
62 rewind(mpFid);
63 }
64}
65
66size_t MultiFormatReader::ReadSamples(void* buffer, size_t len,
69{
70 return ReadSamples(buffer, len, 1, format, end);
71}
72
73
74size_t MultiFormatReader::ReadSamples(void* buffer, size_t len, size_t stride,
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}
108
109size_t MultiFormatReader::Read(void* buffer, size_t size, size_t len, size_t stride)
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}
133
134
135void MultiFormatReader::SwapBytes(void* buffer, size_t size, size_t len)
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}
156
EndiannessT Which()
MultiFormatReader(const char *filename)
size_t ReadSamples(void *buffer, size_t len, MultiFormatReader::FormatT format, MachineEndianness::EndiannessT end)
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)
const char * end(const char *str) noexcept
Definition: StringUtils.h:106