mirror of
https://github.com/sle118/squeezelite-esp32.git
synced 2025-12-11 22:17:17 +03:00
move to new cspot
This commit is contained in:
@@ -0,0 +1,50 @@
|
||||
#include "AACContainer.h"
|
||||
#include "iostream"
|
||||
using namespace bell;
|
||||
|
||||
#define SYNC_WORLD_LEN 4
|
||||
|
||||
AACContainer::AACContainer(std::istream& istr) : bell::AudioContainer(istr) {}
|
||||
|
||||
bool AACContainer::fillBuffer() {
|
||||
if (this->bytesInBuffer < AAC_MAX_FRAME_SIZE * 2) {
|
||||
this->istr.read((char*)buffer.data() + bytesInBuffer,
|
||||
buffer.size() - bytesInBuffer);
|
||||
this->bytesInBuffer += istr.gcount();
|
||||
}
|
||||
return this->bytesInBuffer >= AAC_MAX_FRAME_SIZE * 2;
|
||||
}
|
||||
|
||||
std::byte* AACContainer::readSample(uint32_t& len) {
|
||||
if (!this->fillBuffer()) {
|
||||
len = 0;
|
||||
return nullptr;
|
||||
}
|
||||
|
||||
// Align the data if previous read was offseted
|
||||
if (toConsume > 0 && toConsume <= bytesInBuffer) {
|
||||
memmove(buffer.data(), buffer.data() + toConsume,
|
||||
buffer.size() - toConsume);
|
||||
bytesInBuffer = bytesInBuffer - toConsume;
|
||||
toConsume = 0;
|
||||
}
|
||||
|
||||
int startOffset =
|
||||
AACFindSyncWord((uint8_t*)this->buffer.data(), bytesInBuffer);
|
||||
|
||||
if (startOffset < 0) {
|
||||
// Discard word
|
||||
toConsume = AAC_MAX_FRAME_SIZE;
|
||||
return nullptr;
|
||||
}
|
||||
|
||||
len = bytesInBuffer - startOffset;
|
||||
|
||||
return this->buffer.data() + startOffset;
|
||||
}
|
||||
|
||||
void AACContainer::parseSetupData() {
|
||||
channels = 2;
|
||||
sampleRate = bell::SampleRate::SR_44100;
|
||||
bitWidth = bell::BitWidth::BW_16;
|
||||
}
|
||||
@@ -0,0 +1,24 @@
|
||||
#include "AudioContainers.h"
|
||||
|
||||
using namespace bell;
|
||||
|
||||
std::unique_ptr<bell::AudioContainer> AudioContainers::guessAudioContainer(
|
||||
std::istream& istr) {
|
||||
std::byte tmp[14];
|
||||
istr.read((char*)tmp, sizeof(tmp));
|
||||
|
||||
if (memcmp(tmp, "\xFF\xF1", 2) == 0 ||
|
||||
memcmp(tmp, "\xFF\xF9", 2) == 0) {
|
||||
// AAC found
|
||||
std::cout << "AAC" << std::endl;
|
||||
return std::make_unique<bell::AACContainer>(istr);
|
||||
} else if (memcmp(tmp, "\xFF\xFB", 2) == 0 ||
|
||||
memcmp(tmp, "\x49\x44\x33", 3) == 0) {
|
||||
// MP3 Found
|
||||
std::cout << "MP3" << std::endl;
|
||||
|
||||
return std::make_unique<bell::MP3Container>(istr);
|
||||
}
|
||||
|
||||
return nullptr;
|
||||
}
|
||||
@@ -0,0 +1,48 @@
|
||||
#include "MP3Container.h"
|
||||
|
||||
using namespace bell;
|
||||
|
||||
MP3Container::MP3Container(std::istream& istr) : bell::AudioContainer(istr) {}
|
||||
|
||||
bool MP3Container::fillBuffer() {
|
||||
if (this->bytesInBuffer < MP3_MAX_FRAME_SIZE * 2) {
|
||||
this->istr.read((char*)buffer.data() + bytesInBuffer,
|
||||
buffer.size() - bytesInBuffer);
|
||||
this->bytesInBuffer += istr.gcount();
|
||||
}
|
||||
return this->bytesInBuffer >= MP3_MAX_FRAME_SIZE * 2;
|
||||
}
|
||||
|
||||
std::byte* MP3Container::readSample(uint32_t& len) {
|
||||
if (!this->fillBuffer()) {
|
||||
len = 0;
|
||||
return nullptr;
|
||||
}
|
||||
|
||||
// Align the data if previous read was offseted
|
||||
if (toConsume > 0 && toConsume <= bytesInBuffer) {
|
||||
memmove(buffer.data(), buffer.data() + toConsume,
|
||||
buffer.size() - toConsume);
|
||||
bytesInBuffer = bytesInBuffer - toConsume;
|
||||
toConsume = 0;
|
||||
}
|
||||
|
||||
int startOffset =
|
||||
MP3FindSyncWord((uint8_t*)this->buffer.data(), bytesInBuffer);
|
||||
|
||||
if (startOffset < 0) {
|
||||
// Discard word
|
||||
toConsume = MP3_MAX_FRAME_SIZE;
|
||||
return nullptr;
|
||||
}
|
||||
|
||||
len = bytesInBuffer - startOffset;
|
||||
|
||||
return this->buffer.data() + startOffset;
|
||||
}
|
||||
|
||||
void MP3Container::parseSetupData() {
|
||||
channels = 2;
|
||||
sampleRate = bell::SampleRate::SR_44100;
|
||||
bitWidth = bell::BitWidth::BW_16;
|
||||
}
|
||||
@@ -0,0 +1,31 @@
|
||||
#pragma once
|
||||
|
||||
#include <cstring>
|
||||
#include <cstddef>
|
||||
#include <vector>
|
||||
#include "AudioContainer.h"
|
||||
#include "aacdec.h"
|
||||
|
||||
namespace bell {
|
||||
class AACContainer : public AudioContainer {
|
||||
public:
|
||||
~AACContainer(){};
|
||||
AACContainer(std::istream& istr);
|
||||
|
||||
std::byte* readSample(uint32_t& len) override;
|
||||
void parseSetupData() override;
|
||||
|
||||
bell::AudioCodec getCodec() override { return bell::AudioCodec::AAC; }
|
||||
|
||||
private:
|
||||
static constexpr auto AAC_MAX_FRAME_SIZE = 2100;
|
||||
static constexpr auto BUFFER_SIZE = 1024 * 10;
|
||||
|
||||
std::vector<std::byte> buffer = std::vector<std::byte>(BUFFER_SIZE);
|
||||
|
||||
size_t bytesInBuffer = 0;
|
||||
size_t dataOffset = 0;
|
||||
|
||||
bool fillBuffer();
|
||||
};
|
||||
} // namespace bell
|
||||
@@ -0,0 +1,27 @@
|
||||
#pragma once
|
||||
|
||||
#include <cstddef>
|
||||
#include <istream>
|
||||
#include <cstring>
|
||||
#include "CodecType.h"
|
||||
#include "StreamInfo.h"
|
||||
|
||||
namespace bell {
|
||||
class AudioContainer {
|
||||
protected:
|
||||
std::istream& istr;
|
||||
uint32_t toConsume = 0;
|
||||
|
||||
public:
|
||||
bell::SampleRate sampleRate;
|
||||
bell::BitWidth bitWidth;
|
||||
int channels;
|
||||
|
||||
AudioContainer(std::istream& istr) : istr(istr) {}
|
||||
|
||||
virtual std::byte* readSample(uint32_t& len) = 0;
|
||||
void consumeBytes(uint32_t bytes) { this->toConsume = bytes; }
|
||||
virtual void parseSetupData() = 0;
|
||||
virtual bell::AudioCodec getCodec() = 0;
|
||||
};
|
||||
} // namespace bell
|
||||
@@ -0,0 +1,11 @@
|
||||
#pragma once
|
||||
|
||||
#include <iostream>
|
||||
#include <memory>
|
||||
#include "AACContainer.h"
|
||||
#include "AudioContainer.h"
|
||||
#include "MP3Container.h"
|
||||
|
||||
namespace bell::AudioContainers {
|
||||
std::unique_ptr<bell::AudioContainer> guessAudioContainer(std::istream& istr);
|
||||
} // namespace bell::AudioContainers
|
||||
@@ -0,0 +1,30 @@
|
||||
#pragma once
|
||||
|
||||
#include <cstring>
|
||||
#include <cstddef>
|
||||
#include <vector>
|
||||
#include "AudioContainer.h"
|
||||
#include "mp3dec.h"
|
||||
|
||||
namespace bell {
|
||||
class MP3Container : public AudioContainer {
|
||||
public:
|
||||
~MP3Container(){};
|
||||
MP3Container(std::istream& istr);
|
||||
|
||||
std::byte* readSample(uint32_t& len) override;
|
||||
void parseSetupData() override;
|
||||
bell::AudioCodec getCodec() override { return bell::AudioCodec::MP3; }
|
||||
|
||||
private:
|
||||
static constexpr auto MP3_MAX_FRAME_SIZE = 2100;
|
||||
static constexpr auto BUFFER_SIZE = 1024 * 10;
|
||||
|
||||
std::vector<std::byte> buffer = std::vector<std::byte>(BUFFER_SIZE);
|
||||
|
||||
size_t bytesInBuffer = 0;
|
||||
size_t dataOffset = 0;
|
||||
|
||||
bool fillBuffer();
|
||||
};
|
||||
} // namespace bell
|
||||
Reference in New Issue
Block a user