Веб-сайт самохостера Lotigara

summaryrefslogtreecommitdiff
path: root/source/core/StarAudio.hpp
blob: f68cfe7ebb3e6d52cafd6cd9b7961085da0b5317 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
#pragma once

#include "StarIODevice.hpp"
//#define STAR_STREAM_AUDIO

namespace Star {

extern float const DefaultPerceptualRangeDb;
extern float const DefaultPerceptualBoostRangeDb;

float perceptualToAmplitude(float perceptual, float normalizedMax = 1.f,
  float range = DefaultPerceptualRangeDb, float boostRange = DefaultPerceptualBoostRangeDb);

float amplitudeToPerceptual(float amp, float normalizedMax = 1.f,
  float range = DefaultPerceptualRangeDb, float boostRange = DefaultPerceptualBoostRangeDb);

STAR_CLASS(CompressedAudioImpl);
STAR_CLASS(UncompressedAudioImpl);
STAR_CLASS(Audio);

STAR_EXCEPTION(AudioException, StarException);

// Simple class for reading audio files in ogg/vorbis and wav format.
// Reads and allows for decompression of a limited subset of ogg/vorbis.  Does
// not handle multiple bitstreams, sample rate or channel number changes.
// Entire stream is kept in memory, and is implicitly shared so copying Audio
// instances is not expensive.
class Audio {
public:
  explicit Audio(IODevicePtr device, String name = "");
  Audio(Audio const& audio);
  Audio(Audio&& audio);

  Audio& operator=(Audio const& audio);
  Audio& operator=(Audio&& audio);

  // This function returns the number of channels that this file has.  Channels
  // are static throughout file.
  unsigned channels() const;

  // This function returns the sample rate that this file has.  Sample rates
  // are static throughout file.
  unsigned sampleRate() const;

  // This function returns the playtime duration of the file.
  double totalTime() const;

  // This function returns total number of samples in this file.
  uint64_t totalSamples() const;

  // This function returns true when the datastream or file being read from is
  // a vorbis compressed file.  False otherwise.
  bool compressed() const;

  // If compressed, permanently uncompresses audio for faster reading.  The
  // uncompressed buffer is shared with all further copies of Audio, and this
  // is irreversible.
  void uncompress();

  // This function seeks the data stream to the given time in seconds.
  void seekTime(double time);

  // This function seeks the data stream to the given sample number
  void seekSample(uint64_t sample);

  // This function converts the current offset of the file to the time value of
  // that offset in seconds.
  double currentTime() const;

  // This function converts the current offset of the file to the current
  // sample number.
  uint64_t currentSample() const;

  // Reads into 16 bit signed buffer with channels interleaved.  Returns total
  // number of samples read (counting each channel individually).  0 indicates
  // end of stream.
  size_t readPartial(int16_t* buffer, size_t bufferSize);

  // Same as readPartial, but repeats read attempting to fill buffer as much as
  // possible
  size_t read(int16_t* buffer, size_t bufferSize);

  // Read into a given buffer, while also converting into the given number of
  // channels at the given sample rate and playback velocity.  If the number of
  // channels in the file is higher, only populates lower channels, if it is
  // lower, the last channel is copied to the remaining channels.  Attempts to
  // fill the buffer as much as possible up to end of stream.  May fail to fill
  // an entire buffer depending on the destinationSampleRate / velocity /
  // available samples.
  size_t resample(unsigned destinationChannels, unsigned destinationSampleRate,
      int16_t* destinationBuffer, size_t destinationBufferSize,
      double velocity = 1.0);

  String const& name() const;
  void setName(String name);

private:
  // If audio is uncompressed, this will be null.
  CompressedAudioImplPtr m_compressed;
  UncompressedAudioImplPtr m_uncompressed;

  ByteArray m_workingBuffer;
  String m_name;
};

}