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 <wx/intl.h>
#include <math.h>
#include <stdio.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 77 of file SampleFormat.cpp.

79{
80 auto size = SAMPLE_SIZE(format);
81 memset(dst + start*size, 0, len*size);
82}
int format
Definition: ExportPCM.cpp:56
#define SAMPLE_SIZE(SampleFormat)
Definition: SampleFormat.h:44

References format, SAMPLE_SIZE, and size.

Referenced by RingBuffer::Clear(), AudioIO::DrainRecordBuffers(), Sequence::Get(), WaveTrack::Get(), 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 111 of file SampleFormat.cpp.

116{
118 ditherType,
119 src, srcFormat, dst, dstFormat, len, srcStride, dstStride);
120}
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(), WaveClip::Append(), Sequence::ConvertToSampleFormat(), Sequence::DoAppend(), ExportPCM::Export(), RingBuffer::Get(), SqliteSampleBlock::GetBlob(), 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 60 of file SampleFormat.cpp.

61{
62 switch(format) {
63 case int16Sample:
64 /* i18n-hint: Audio data bit depth (precision): 16-bit integers */
65 return XO("16-bit PCM");
66 case int24Sample:
67 /* i18n-hint: Audio data bit depth (precision): 24-bit integers */
68 return XO("24-bit PCM");
69 case floatSample:
70 /* i18n-hint: Audio data bit depth (precision): 32-bit floating point */
71 return XO("32-bit float");
72 }
73 return XO("Unknown format"); // compiler food
74}
#define XO(s)
Definition: Internat.h:31
@ floatSample
Definition: SampleFormat.h:34
@ int16Sample
Definition: SampleFormat.h:32
@ int24Sample
Definition: SampleFormat.h:33

References floatSample, format, int16Sample, int24Sample, and XO.

Referenced by FormatMenuTable::OnFormatChange(), Sequence::Paste(), and anonymous_namespace{WaveTrackControls.cpp}::Status2DrawFunction().

Here is the caller graph for this function:

◆ InitDitherers()

void InitDitherers ( )

Definition at line 53 of file SampleFormat.cpp.

54{
55 // Read dither preferences
58}
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 84 of file SampleFormat.cpp.

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

References floatSample, format, SAMPLE_SIZE, and size.

Referenced by MixerSource::MixSameRate(), and MixerSource::MixVariableRates().

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 102 of file SampleFormat.cpp.

104{
105 CopySamples( src, srcFormat,
106 reinterpret_cast<samplePtr>(dst), floatSample, len,
108 srcStride, dstStride);
109}
@ 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(), DoSoftwarePlaythrough(), and WaveClipWaveformCache::GetWaveDisplay().

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 51 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 49 of file SampleFormat.cpp.

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