octavia

octobanana's customizable text-based audio visualization interactive application.


octavia

/

src

/

app

/

record.hh

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 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154
/*
                                    88888888
                                  888888888888
                                 88888888888888
                                8888888888888888
                               888888888888888888
                              888888  8888  888888
                              88888    88    88888
                              888888  8888  888888
                              88888888888888888888
                              88888888888888888888
                             8888888888888888888888
                          8888888888888888888888888888
                        88888888888888888888888888888888
                              88888888888888888888
                            888888888888888888888888
                           888888  8888888888  888888
                           888     8888  8888     888
                                   888    888

                                   OCTOBANANA

Licensed under the MIT License

Copyright (c) 2020 Brett Robinson <https://octobanana.com/>

Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in all
copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
SOFTWARE.
*/

#ifndef APP_RECORD_HH
#define APP_RECORD_HH

#include "ob/fft.hh"

#include "app/filter.hh"

#include <SFML/Audio.hpp>
#include <SFML/System.hpp>

#include <cmath>
#include <cstddef>

#include <mutex>
#include <atomic>
#include <vector>
#include <complex>

class Record : public sf::SoundRecorder {
public:
  using value_type = double;
  using FFT = OB::FFT<value_type>;
  using complex_type = std::complex<value_type>;

  struct Band {
    double min {0};
    double max {0};
    double val {0};
  };

  struct Bands {
    Band sub_bass {20, 60, 0};
    Band bass {60, 250, 0};
    Band low_midrange {250, 500, 0};
    Band midrange {500, 2000, 0};
    Band upper_midrange {2000, 4000, 0};
    Band presence {4000, 6000, 0};
    Band brilliance {6000, 20000, 0};
  };

  struct Channel {
    Bands bands;
    std::vector<value_type> samples;
    std::vector<value_type> fmtbuf;
    Filter::Low_Pass low_pass_filter;
    Filter::High_Pass high_pass_filter;
    Filter::High_Shelf high_shelf_filter;
  };

  Record(std::size_t size = 1024);
  ~Record();

  void process_interval(sf::Time interval);
  unsigned int sample_rate() const;
  void sample_rate(unsigned int rate);
  std::string const& device() const;
  bool device(std::string const& name);
  unsigned int channels() const;
  void channels(unsigned int count);
  static std::vector<std::string> devices();
  static std::string device_default();
  static bool available();
  bool start();
  void stop();

  bool recording() const;
  Bands const& bands_left() const;
  Bands const& bands_right() const;
  std::vector<double> const& buffer_left() const;
  std::vector<double> const& buffer_right() const;
  std::vector<double> samples_left();
  std::vector<double> samples_right();
  std::size_t size() const;
  std::size_t low_pass() const;
  void low_pass(std::size_t const hz);
  std::size_t high_pass() const;
  void high_pass(std::size_t const hz);
  void process();
  void process_left();
  void process_right();

private:
  void process_impl(Channel& channel);
  bool onStart() override;
  void onStop() override;
  bool onProcessSamples(sf::Int16 const* samples, std::size_t size) override;

  void filter_init();
  void filter(Channel& channel, std::size_t const pos);

  std::mutex _mutex;
  std::atomic<bool> _silence {true};
  std::atomic<bool> _update {false};
  std::atomic<bool> _recording {false};
  double const _hann_constant {0.5};
  std::size_t const _size {2048};
  std::size_t _sample_rate {48000};
  std::size_t _low_pass {20000};
  std::size_t _high_pass {20};
  bool _trim_bins {true};
  FFT _fft;
  Channel _left;
  Channel _right;
  std::vector<complex_type> _inbuf;
  std::vector<complex_type> _outbuf;
  std::vector<value_type> _hann;
};

#endif // APP_RECORD_HH
Back to Top