Jpp 19.3.0-rc.2
the software that should make you happy
Loading...
Searching...
No Matches
KM3NeT_Audio_Channel.cc
Go to the documentation of this file.
2/**
3 * \author fsimeone
4 */
5
6
7#ifdef DEBUG_KM3NET_AUDIO_CHANNEL
8#include <stdlib.h>
9#include <iostream>
10#include <boost/date_time/posix_time/posix_time.hpp>
11#include <Tools/KM3NeT_Debug.h>
12#define DEBUG_MSG(str,lev) do { if (((uint8_t)lev)>=KM3NeT_Audio_Channel::DEBUG_LEVEL) { DBG_MSG << boost::posix_time::microsec_clock::local_time() << " DEBUG_KM3NET_AUDIO_CHANNEL(" << (void*)this << "): "<< str << '\n'; } }while( false )
13#else
14#define DEBUG_MSG(str,lev) do { } while ( false )
15#endif
16
17KM3NeT_Audio_Channel::KM3NeT_Audio_Channel( boost::asio::io_service& io_service,\
18 callback_send_toa callback,\
19 uint32_t id,\
20 std::string& debug_path,\
21 uint32_t window,\
22 uint32_t overlap\
23 ): strand_(io_service),\
24 finder_(window),\
25 data_(window,overlap)
26{
27 DEBUG_MSG("Constructor",0);
28 id_=id;
29 send_toa_=callback;
30 test_=0;
31 overlap_=overlap;
32 test_file_=0;
33 debug_files_path_=debug_path;
34}
35
37{
38 DEBUG_MSG("Destructor",0);
39 if ((test_)&&(test_file_)) std::fclose(test_file_);
40}
41
42void KM3NeT_Audio_Channel::Add_probe(pProbe probe, std::size_t id, std::size_t threshold)
43{
44 thresholds_.insert(threshold_entry(id,threshold));
45 probes_id_.push_back(id);
46 finder_.addProbeSignal(probe);
47}
48
49void KM3NeT_Audio_Channel::Append(real_type sample)
50{
51 if(data_.Append(sample))
52 {
53 ToA_Packet l_toa;
54 l_toa.Time.Channel=1;
55 real_type* buffer=data_.Get_window(&l_toa);
56 strand_.post(boost::bind(&KM3NeT_Audio_Channel::Analysis,this,l_toa,buffer,data_.Get_size()));
57 }
58}
59
60void KM3NeT_Audio_Channel::Analysis(ToA_Packet packet,real_type* buffer,uint32_t length)
61{
62 DEBUG_MSG("Analysis",2);
63 if (test_)
64 {
66
67 if (std::ftell(test_file_)>MAX_DUMP_SIZE)
68 {
69 DEBUG_MSG("Test file DOM="<<packet.Header.DOM_Identifier<<" ch="<<packet.Time.Channel<<" closed",2);
70 std::fclose(test_file_);
72 }
73
74 if (test_file_)
75 {
76 std::fwrite( (char*) &packet.Header.Time_Stamp_seconds, sizeof(uint32_t), 1, test_file_);
77 std::fwrite( (char*) &packet.Header.Time_Stamp_ticks, sizeof(uint32_t), 1, test_file_);
78 std::fwrite( (char*) &length, sizeof(uint32_t), 1, test_file_);
79 std::fwrite( ((char*)buffer)+sizeof(real_type)*overlap_,sizeof(real_type), length-overlap_, test_file_);
80 }
81 }
83 finder_.copyFromBuffer(buffer,length);
84 finder_.findTOAs(toaList);
85 free(buffer);
86
87 packet.Header.Number_of_Items=1;
88 packet.Header.Frame_Length=sizeof(ToA_Packet);
89
91 packet.Header.DOM_Identifier=htonl(packet.Header.DOM_Identifier);
92 packet.Header.DQ_Status=htonl(packet.Header.DQ_Status);
93 packet.Header.DOM_Status1=htonl(packet.Header.DOM_Status1);
94 packet.Header.DOM_Status2=htonl(packet.Header.DOM_Status2);
95 packet.Header.DOM_Status3=htonl(packet.Header.DOM_Status3);
96 packet.Header.DOM_Status4=htonl(packet.Header.DOM_Status4);
97 packet.Header.Data_Type=htonl(packet.Header.Data_Type);
98 packet.Header.Frame_Length=htonl(packet.Header.Frame_Length);
99 packet.Header.Number_of_Items=htonl(packet.Header.Number_of_Items);
100 packet.Header.Run_Number=htonl(packet.Header.Run_Number);
101 packet.Header.Sequence_Number=htonl(packet.Header.Sequence_Number);
103 packet.Header.Time_Stamp_ticks=htonl(packet.Header.Time_Stamp_ticks);
104
105 double sampling=128./packet.Time.Sampling_Rate;
106 for (uint8_t i=0; i<toaList.size(); i++)
107 {
108 ToA_Packet l_toa;
109 l_toa=packet;
110 l_toa.Time.Reserved=0;
111
112 if (toaList[i].getGoodness()>=thresholds_.find(probes_id_[i])->second)
113 {
114 l_toa.Time.Emitter_ID=probes_id_[i];
115 l_toa.Time.Quality_Factor=toaList[i].getGoodness();
116 l_toa.Time.ToA+=sampling * toaList[i].getTime();
117
118 l_toa.Time.Emitter_ID=htonl(l_toa.Time.Emitter_ID);
119 l_toa.Time.Quality_Factor=htonl(l_toa.Time.Quality_Factor);
120 l_toa.Time.ToA=htonl(l_toa.Time.ToA);
121
122 send_toa_(l_toa);
123 }
124 }
125}
126
127void KM3NeT_Audio_Channel::Open_test_file(uint32_t id,uint8_t ch)
128{
129 std::string l_file=debug_files_path_;
130 std::time_t seconds = std::time(0);
131 l_file+="DOM_"+std::to_string((long long unsigned int)id)+"_CH"+std::to_string((long long unsigned int)ch)+"_"+std::to_string((long long unsigned int)seconds)+".bin";
132 test_file_= std::fopen(l_file.c_str(),"wb");
133 DEBUG_MSG("Test file opened at "<<seconds,2);
134}
#define DEBUG_MSG(str, lev)
boost::shared_ptr< KM3::TOALIB::CTOAFinder::probeSignalT > pProbe
#define MAX_DUMP_SIZE
boost::function< void(ToA_Packet packet)> callback_send_toa
std::pair< std::size_t, std::size_t > threshold_entry
void Add_probe(pProbe probe, std::size_t id, std::size_t threshold)
callback_send_toa send_toa_
void Append(real_type sample)
void Analysis(ToA_Packet packet, real_type *buffer, uint32_t length)
boost::asio::io_service::strand strand_
void Open_test_file(uint32_t id, uint8_t ch)
std::map< std::size_t, std::size_t > thresholds_
std::vector< std::size_t > probes_id_
KM3::TOALIB::CTOAFinder finder_
KM3NeT_Audio_Channel(boost::asio::io_service &io_service, callback_send_toa callback, uint32_t id, std::string &debug_path, uint32_t window=STD_WINDOW, uint32_t overlap=STD_OVERLAP)
real_type * Get_window(ToA_Packet *packet)
bool Append(real_type sample)
DAQ_Common_Header Header
Acoustic_Time_of_Arrival Time