Audacity 3.2.0
Functions | Variables
SampleFormat.cpp File Reference

Functions that work with Dither and initialise it. More...

#include "SampleFormat.h"
#include "Dither.h"
#include <math.h>
#include <stdlib.h>
#include <string.h>
#include "Prefs.h"
#include "Internat.h"
Include dependency graph for SampleFormat.cpp:

Go to the source code of this file.

Functions

void InitDitherers ()
 
TranslatableString GetSampleFormatStr (sampleFormat format)
 
void ClearSamples (samplePtr dst, sampleFormat format, size_t start, size_t len)
 
void ReverseSamples (samplePtr dst, sampleFormat format, int start, int len)
 
void SamplesToFloats (constSamplePtr src, sampleFormat srcFormat, float *dst, size_t len, size_t srcStride, size_t dstStride)
 Copy samples from any format into the widest format, which is 32 bit float, with no dithering. More...
 
void CopySamples (constSamplePtr src, sampleFormat srcFormat, samplePtr dst, sampleFormat dstFormat, size_t len, DitherType ditherType, unsigned int srcStride, unsigned int dstStride)
 Copy samples from any format to any other format; apply dithering only if narrowing the format. More...
 

Variables

DitherType gLowQualityDither = DitherType::none
 These global variables are assigned at application startup or after change of preferences. More...
 
DitherType gHighQualityDither = DitherType::shaped
 
static Dither gDitherAlgorithm
 

Detailed Description

Functions that work with Dither and initialise it.

This file handles converting between all of the different sample formats that Audacity supports, such as 16-bit, 24-bit (packed into a 32-bit int), and 32-bit float.

Floating-point samples use the range -1.0...1.0, inclusive. Integer formats use the full signed range of their data type, for example 16-bit samples use the range -32768...32767. This means that reading in a wav file and writing it out again ('round tripping'), via floats, is lossless; -32768 equates to -1.0f and 32767 equates to +1.0f - (a little bit). It also means (unfortunately) that writing out +1.0f leads to clipping by 1 LSB. This creates some distortion, but I (MJS) have not been able to measure it, it's so small. Zero is preserved.

http://limpet.net/audacity/bugzilla/show_bug.cgi?id=200 leads to some of the discussions that were held about this.

Note: These things are now handled by the Dither class, which also replaces the CopySamples() method (msmeyer)

Definition in file SampleFormat.cpp.

Function Documentation

◆ ClearSamples()

void ClearSamples ( samplePtr  dst,
sampleFormat  format,
size_t  start,
size_t  len 
)

Definition at line 75 of file SampleFormat.cpp.

77{
78 auto size = SAMPLE_SIZE(format);
79 memset(dst + start*size, 0, len*size);
80}
#define SAMPLE_SIZE(SampleFormat)
Definition: SampleFormat.h:52

References anonymous_namespace{ExportPCM.cpp}::format, SAMPLE_SIZE, and size.

Referenced by RingBuffer::Clear(), AudioIO::DrainRecordBuffers(), Sequence::Get(), WideSampleSequence::GetFloats(), WaveTrack::GetOne(), SampleBlock::GetSamples(), RingBuffer::Put(), and Sequence::SetSamples().

Here is the caller graph for this function:

◆ CopySamples()

void CopySamples ( constSamplePtr  src,
sampleFormat  srcFormat,
samplePtr  dst,
sampleFormat  dstFormat,
size_t  len,
DitherType  ditherType = gHighQualityDither,
unsigned int  srcStride = 1,
unsigned int  dstStride = 1 
)

Copy samples from any format to any other format; apply dithering only if narrowing the format.

Parameters
srcaddress of source samples
srcFormatformat of source samples, determines sizeof each one
dstaddress of floating-point numbers
lencount of samples to copy
srcStridehow many samples to advance src after copying each one
dstStringhow many samples to advance dst after copying each one
dstFormatformat of destination samples, determines sizeof each one
ditherTypechoice of dithering algorithm to use if narrowing the format
Parameters
ditherTypedefault is loaded from a global variable

Definition at line 109 of file SampleFormat.cpp.

114{
116 ditherType,
117 src, srcFormat, dst, dstFormat, len, srcStride, dstStride);
118}
static Dither gDitherAlgorithm
void Apply(DitherType ditherType, constSamplePtr source, sampleFormat sourceFormat, samplePtr dest, sampleFormat destFormat, unsigned int len, unsigned int sourceStride=1, unsigned int destStride=1)
Definition: Dither.cpp:216

References Dither::Apply(), and gDitherAlgorithm.

Referenced by AUPImportFileHandle::AddSamples(), Sequence::Append(), Sequence::ConvertToSampleFormat(), Sequence::DoAppend(), RingBuffer::Get(), SqliteSampleBlock::GetBlob(), PCMExportProcessor::Process(), Mixer::Process(), RingBuffer::Put(), SamplesToFloats(), and Sequence::SetSamples().

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

◆ GetSampleFormatStr()

TranslatableString GetSampleFormatStr ( sampleFormat  format)

Definition at line 58 of file SampleFormat.cpp.

59{
60 switch(format) {
61 case int16Sample:
62 /* i18n-hint: Audio data bit depth (precision): 16-bit integers */
63 return XO("16-bit PCM");
64 case int24Sample:
65 /* i18n-hint: Audio data bit depth (precision): 24-bit integers */
66 return XO("24-bit PCM");
67 case floatSample:
68 /* i18n-hint: Audio data bit depth (precision): 32-bit floating point */
69 return XO("32-bit float");
70 }
71 return XO("Unknown format"); // compiler food
72}
XO("Cut/Copy/Paste")
constexpr sampleFormat int16Sample
Definition: SampleFormat.h:43
constexpr sampleFormat floatSample
Definition: SampleFormat.h:45
constexpr sampleFormat int24Sample
Definition: SampleFormat.h:44

References floatSample, anonymous_namespace{ExportPCM.cpp}::format, int16Sample, int24Sample, and XO().

Referenced by FormatMenuTable::OnFormatChange(), and Sequence::Paste().

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

◆ InitDitherers()

void InitDitherers ( )

Definition at line 51 of file SampleFormat.cpp.

52{
53 // Read dither preferences
56}
DitherType gLowQualityDither
These global variables are assigned at application startup or after change of preferences.
DitherType gHighQualityDither
static DitherType FastDitherChoice()
Definition: Dither.cpp:410
static DitherType BestDitherChoice()
Definition: Dither.cpp:415

References Dither::BestDitherChoice(), Dither::FastDitherChoice(), gHighQualityDither, and gLowQualityDither.

Referenced by QualityPrefs::Commit(), and AudacityApp::InitPart2().

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

◆ ReverseSamples()

void ReverseSamples ( samplePtr  dst,
sampleFormat  format,
int  start,
int  len 
)

Definition at line 82 of file SampleFormat.cpp.

84{
85 auto size = SAMPLE_SIZE(format);
86 samplePtr first = dst + start * size;
87 samplePtr last = dst + (start + len - 1) * size;
88 enum : size_t { fixedSize = SAMPLE_SIZE(floatSample) };
89 wxASSERT(static_cast<size_t>(size) <= fixedSize);
90 char temp[fixedSize];
91 while (first < last) {
92 memcpy(temp, first, size);
93 memcpy(first, last, size);
94 memcpy(last, temp, size);
95 first += size;
96 last -= size;
97 }
98}
char * samplePtr
Definition: SampleFormat.h:57

References floatSample, anonymous_namespace{ExportPCM.cpp}::format, SAMPLE_SIZE, and size.

Referenced by WaveTrack::GetOne(), and ClipTimeAndPitchSource::Pull().

Here is the caller graph for this function:

◆ SamplesToFloats()

void SamplesToFloats ( constSamplePtr  src,
sampleFormat  srcFormat,
float *  dst,
size_t  len,
size_t  srcStride = 1,
size_t  dstStride = 1 
)

Copy samples from any format into the widest format, which is 32 bit float, with no dithering.

Parameters
srcaddress of source samples
srcFormatformat of source samples, determines sizeof each one
dstaddress of floating-point numbers
lencount of samples to copy
srcStridehow many samples to advance src after copying each one
dstStringhow many samples to advance dst after copying each one

Definition at line 100 of file SampleFormat.cpp.

102{
103 CopySamples( src, srcFormat,
104 reinterpret_cast<samplePtr>(dst), floatSample, len,
106 srcStride, dstStride);
107}
@ none
Definition: Dither.h:20
void CopySamples(constSamplePtr src, sampleFormat srcFormat, samplePtr dst, sampleFormat dstFormat, size_t len, DitherType ditherType, unsigned int srcStride, unsigned int dstStride)
Copy samples from any format to any other format; apply dithering only if narrowing the format.

References CopySamples(), floatSample, and none.

Referenced by AudioIoCallback::AudioCallback(), SqliteSampleBlock::CalcSummary(), and DoSoftwarePlaythrough().

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

Variable Documentation

◆ gDitherAlgorithm

Dither gDitherAlgorithm
static

Definition at line 49 of file SampleFormat.cpp.

Referenced by CopySamples().

◆ gHighQualityDither

DitherType gHighQualityDither = DitherType::shaped

◆ gLowQualityDither

DitherType gLowQualityDither = DitherType::none

These global variables are assigned at application startup or after change of preferences.

Definition at line 47 of file SampleFormat.cpp.

Referenced by InitDitherers(), and Mixer::Process().