Skip to content

Commit

Permalink
CopyChannels
Browse files Browse the repository at this point in the history
  • Loading branch information
pschatzmann committed Nov 17, 2023
1 parent 054455d commit e089655
Show file tree
Hide file tree
Showing 2 changed files with 47 additions and 11 deletions.
15 changes: 4 additions & 11 deletions src/AudioHttp/AudioServer.h
Original file line number Diff line number Diff line change
Expand Up @@ -285,7 +285,6 @@ class AudioEncoderServer : public AudioServer {
~AudioEncoderServer() {
}


/**
* @brief Start the server. You need to be connected to WiFI before calling this method
*
Expand All @@ -296,10 +295,10 @@ class AudioEncoderServer : public AudioServer {
void begin(Stream &in, int sample_rate, int channels, int bits_per_sample=16, BaseConverter *converter=nullptr) {
TRACED();
this->in = ∈
setConverter(converter);
audio_info.sample_rate = sample_rate;
audio_info.channels = channels;
audio_info.bits_per_sample = bits_per_sample;
this->converter_ptr = converter;
encoder->setAudioInfo(audio_info);
//encoded_stream.begin(&client_obj, encoder);
encoded_stream.setInput(&client_obj);
Expand All @@ -319,7 +318,7 @@ class AudioEncoderServer : public AudioServer {
TRACED();
this->in = ∈
this->audio_info = info;
this->converter_ptr = converter;
setConverter(converter);
encoder->setAudioInfo(audio_info);
//encoded_stream.begin(&client_obj, encoder);
encoded_stream.setInput(&client_obj);
Expand Down Expand Up @@ -389,8 +388,6 @@ class AudioEncoderServer : public AudioServer {
copier.begin(encoded_stream, *in);
}
}


};

/**
Expand All @@ -407,17 +404,15 @@ class AudioWAVServer : public AudioEncoderServer {
* @brief Construct a new Audio W A V Server object
* We assume that the WiFi is already connected
*/
AudioWAVServer(int port=80) : AudioEncoderServer(new WAVEncoder(), port){
}
AudioWAVServer(int port=80) : AudioEncoderServer(new WAVEncoder(), port){}

/**
* @brief Construct a new Audio W A V Server object
*
* @param network
* @param password
*/
AudioWAVServer(const char* network, const char *password, int port=80) : AudioEncoderServer(new WAVEncoder(), network, password, port) {
}
AudioWAVServer(const char* network, const char *password, int port=80) : AudioEncoderServer(new WAVEncoder(), network, password, port) {}

/// Destructor: release the allocated encoder
~AudioWAVServer() {
Expand All @@ -431,8 +426,6 @@ class AudioWAVServer : public AudioEncoderServer {
WAVEncoder &wavEncoder(){
return *static_cast<WAVEncoder*>(encoder);
}


};

}
Expand Down
43 changes: 43 additions & 0 deletions src/AudioTools/BaseConverter.h
Original file line number Diff line number Diff line change
Expand Up @@ -1165,6 +1165,49 @@ class SmoothTransition : public BaseConverter {
}
};

/**
* @brief Copy channel Cx value of type T shifted by S bits to all Cn channels
* @ingroup convert
* @tparam T, Cn, Cx, S
*/
template<typename T, size_t Cn, size_t Cx, size_t S>
class CopyChannels : public BaseConverter {
public:
CopyChannels() : _max_val(0), _counter(0), _prev_ms(0) {}

size_t convert(uint8_t *src, size_t size) {
T *chan = (T*)src;
size_t samples = (size / Cn) / sizeof(T);
for( size_t s=0; s<samples; s++ ) {
chan[s*Cn+Cx] = (Cx < Cn) ? chan[s*Cn+Cx] << S : 0;

for( size_t c=0; c<Cn; c++ ) {
if( c != Cx ) {
chan[s*Cn+c] = chan[s*Cn+Cx];
}
}

if( _max_val < chan[s*Cn] ) {
_max_val = chan[s*Cn];
}

_counter++;
uint32_t now = millis();
if( now - _prev_ms > 1000 ) {
_prev_ms = now;
LOGI("CopyChannels samples: %u, amplitude: %d", _counter, _max_val);
_max_val = 0;
}
}
return samples * Cn * sizeof(T);
}

private:
T _max_val;
uint32_t _counter;
uint32_t _prev_ms;
};



}

0 comments on commit e089655

Please sign in to comment.