The Open Master Hearing Aid (openMHA)  openMHA
Open community platform for hearing aid algorithm research
plugins::hoertech::acrec::acwriter_t Class Reference

acwriter_t decouples signal processing from writing to disk. More...

Public Types

typedef double output_type
 The numeric data type used for outputting the data to disk. More...
 

Public Member Functions

 acwriter_t (bool active, unsigned fifosize, unsigned minwrite, const std::string &prefix, bool use_date, const std::string &varname)
 Constructor allocates fifo and disk output buffer. More...
 
 ~acwriter_t ()=default
 Deallocates memory but does not terminate the write_thread. More...
 
void process (MHA_AC::comm_var_t *)
 Place the data present in the algorithm communication variable into the fifo for output to disk. More...
 
void exit_request ()
 Terminate output thread. More...
 
const std::string & get_varname () const
 getter for ac variable name More...
 

Private Member Functions

void write_thread ()
 Main method of the disk writer thread. More...
 
void create_datafile (const std::string &prefix, bool use_date)
 Open data file for output. More...
 

Private Attributes

std::atomic< bool > close_session
 cross-thread-synchronization. More...
 
const bool active
 The writer thread and the output file will only be created when active is true. More...
 
std::unique_ptr< mha_fifo_lf_t< output_type > > fifo
 Fifo for decoupling signal processing thread from disk writer thread. More...
 
const unsigned int disk_write_threshold_min_num_samples
 Minimum number of samples that need to be waiting in the fifo before the disk writer thread writes them to disk. More...
 
std::thread writethread
 The thread that writes to disk. More...
 
std::unique_ptr< output_type[]> diskbuffer
 Intermediate buffer to receive data from fifo and store on disk. More...
 
std::fstream outfile
 Ouput file. More...
 
unsigned num_channels = 0U
 Number of channels of AC variable using stride. More...
 
bool is_num_channels_known = false
 The number of channels is determined during the first process callback. More...
 
bool is_complex = false
 If the AC variable is of complex valued type or not. More...
 
const std::string varname
 The name of the ac variable to publish. More...
 

Detailed Description

acwriter_t decouples signal processing from writing to disk.

Data arriving in numeric AC variables is converted to data type double, placed into a fifo pipeline to transport the data from the signal processing thread to the disk writing thread, and finally written to disk in chunks of at least minwrite numbers. All numbers are written to disk as binary doubles (8 bytes) in host byte order.

Member Typedef Documentation

◆ output_type

The numeric data type used for outputting the data to disk.

Constructor & Destructor Documentation

◆ acwriter_t()

acwriter_t::acwriter_t ( bool  active,
unsigned  fifosize,
unsigned  minwrite,
const std::string &  prefix,
bool  use_date,
const std::string &  varname 
)

Constructor allocates fifo and disk output buffer.

It spawns a new thread for writing data to disk when active==true. In order to terminate the thread, method exit_request must be called before this object is destroyed.

Parameters
activeOnly write data to disk when this is true.
fifosizeCapacity of both the fifo pipeline and of the disk buffer.
minwriteWait for a fifo fill count of at least minwrite doubles before flushing the contents of the fifo to disk. Fifo is also flushed before this object is destroyed.
prefixPath and start of output file name. Will be extended with file name extension ".dat".
use_dateWhen true, the current date and time will be appended to the output file name before the file name extension.
varnameName of AC variable to save into file. Can be accessed through getter method get_varname(). Stored here to avoid races between processing thread and configuration thread.

◆ ~acwriter_t()

plugins::hoertech::acrec::acwriter_t::~acwriter_t ( )
default

Deallocates memory but does not terminate the write_thread.

write_thread must be terminated before the destructor executes by calling exit_request.

Member Function Documentation

◆ process()

void acwriter_t::process ( MHA_AC::comm_var_t s)

Place the data present in the algorithm communication variable into the fifo for output to disk.

◆ exit_request()

void acwriter_t::exit_request ( )

Terminate output thread.

◆ get_varname()

const std::string& plugins::hoertech::acrec::acwriter_t::get_varname ( ) const
inline

getter for ac variable name

Returns
name as char* as needed by get_var

◆ write_thread()

void acwriter_t::write_thread ( )
private

Main method of the disk writer thread.

Periodically wakes up and checks if data needs to be written to disk.

◆ create_datafile()

void acwriter_t::create_datafile ( const std::string &  prefix,
bool  use_date 
)
private

Open data file for output.

Combine prefix, date, and file name extension

Parameters
prefixPath and start of output file name. Will be extended with file name extension ".dat".
use_dateWhen true, the current date and time will be appended to the output file name before the file name extension.

Member Data Documentation

◆ close_session

std::atomic<bool> plugins::hoertech::acrec::acwriter_t::close_session
private

cross-thread-synchronization.

write_thread() terminates after this is set to true by exit_request().

◆ active

const bool plugins::hoertech::acrec::acwriter_t::active
private

The writer thread and the output file will only be created when active is true.

◆ fifo

std::unique_ptr<mha_fifo_lf_t<output_type> > plugins::hoertech::acrec::acwriter_t::fifo
private

Fifo for decoupling signal processing thread from disk writer thread.

◆ disk_write_threshold_min_num_samples

const unsigned int plugins::hoertech::acrec::acwriter_t::disk_write_threshold_min_num_samples
private

Minimum number of samples that need to be waiting in the fifo before the disk writer thread writes them to disk.

◆ writethread

std::thread plugins::hoertech::acrec::acwriter_t::writethread
private

The thread that writes to disk.

◆ diskbuffer

std::unique_ptr<output_type[]> plugins::hoertech::acrec::acwriter_t::diskbuffer
private

Intermediate buffer to receive data from fifo and store on disk.

◆ outfile

std::fstream plugins::hoertech::acrec::acwriter_t::outfile
private

Ouput file.

◆ num_channels

unsigned plugins::hoertech::acrec::acwriter_t::num_channels = 0U
private

Number of channels of AC variable using stride.

If the number of channels changes during processing, an exception is thrown.

◆ is_num_channels_known

bool plugins::hoertech::acrec::acwriter_t::is_num_channels_known = false
private

The number of channels is determined during the first process callback.

is_num_channels_known is set to true after the first process callback.

◆ is_complex

bool plugins::hoertech::acrec::acwriter_t::is_complex = false
private

If the AC variable is of complex valued type or not.

If this changes during processing, then an exception is thrown.

◆ varname

const std::string plugins::hoertech::acrec::acwriter_t::varname
private

The name of the ac variable to publish.


The documentation for this class was generated from the following files: