| 
									
										
										
										
											2014-11-16 16:51:45 -05:00
										 |  |  | #pragma once
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | #include <queue>
 | 
					
						
							|  |  |  | #include <vector>
 | 
					
						
							|  |  |  | #include "wx/wxprec.h"
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | #ifndef WX_PRECOMP
 | 
					
						
							|  |  |  | #include "wx/wx.h"
 | 
					
						
							|  |  |  | #endif
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | #include "wx/thread.h"
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | #include "liquid/liquid.h"
 | 
					
						
							| 
									
										
										
										
											2014-11-18 18:00:06 -05:00
										 |  |  | #include "AudioThread.h"
 | 
					
						
							| 
									
										
										
										
											2014-11-22 22:17:33 -05:00
										 |  |  | #include "ThreadQueue.h"
 | 
					
						
							|  |  |  | #include "CubicSDRDefs.h"
 | 
					
						
							| 
									
										
										
										
											2014-11-30 23:33:55 -05:00
										 |  |  | #include "DemodulatorWorkerThread.h"
 | 
					
						
							| 
									
										
										
										
											2014-11-16 16:51:45 -05:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2014-11-22 22:17:33 -05:00
										 |  |  | enum DemodulatorType { | 
					
						
							| 
									
										
										
										
											2014-11-26 21:05:19 -05:00
										 |  |  |     DEMOD_TYPE_NULL, DEMOD_TYPE_AM, DEMOD_TYPE_FM, DEMOD_TYPE_LSB, DEMOD_TYPE_USB | 
					
						
							| 
									
										
										
										
											2014-11-22 22:17:33 -05:00
										 |  |  | }; | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2014-11-30 17:11:29 -05:00
										 |  |  | class DemodulatorThreadCommand { | 
					
						
							| 
									
										
										
										
											2014-11-26 21:05:19 -05:00
										 |  |  | public: | 
					
						
							| 
									
										
										
										
											2014-11-30 17:11:29 -05:00
										 |  |  |     enum DemodulatorThreadCommandEnum { | 
					
						
							| 
									
										
										
										
											2014-11-30 23:33:55 -05:00
										 |  |  |         DEMOD_THREAD_CMD_NULL, DEMOD_THREAD_CMD_SET_BANDWIDTH, DEMOD_THREAD_CMD_SET_FREQUENCY | 
					
						
							| 
									
										
										
										
											2014-11-26 21:05:19 -05:00
										 |  |  |     }; | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2014-11-30 17:11:29 -05:00
										 |  |  |     DemodulatorThreadCommand() : | 
					
						
							| 
									
										
										
										
											2014-11-30 23:33:55 -05:00
										 |  |  |             cmd(DEMOD_THREAD_CMD_NULL), int_value(0) { | 
					
						
							| 
									
										
										
										
											2014-11-26 21:05:19 -05:00
										 |  |  | 
 | 
					
						
							|  |  |  |     } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2014-11-30 17:11:29 -05:00
										 |  |  |     DemodulatorThreadCommand(DemodulatorThreadCommandEnum cmd) : | 
					
						
							|  |  |  |             cmd(cmd), int_value(0) { | 
					
						
							| 
									
										
										
										
											2014-11-26 21:05:19 -05:00
										 |  |  | 
 | 
					
						
							|  |  |  |     } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     DemodulatorThreadCommandEnum cmd; | 
					
						
							|  |  |  |     int int_value; | 
					
						
							|  |  |  | }; | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2014-11-22 22:17:33 -05:00
										 |  |  | class DemodulatorThreadIQData { | 
					
						
							|  |  |  | public: | 
					
						
							|  |  |  |     unsigned int frequency; | 
					
						
							|  |  |  |     unsigned int bandwidth; | 
					
						
							|  |  |  |     std::vector<signed char> data; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     DemodulatorThreadIQData() : | 
					
						
							|  |  |  |             frequency(0), bandwidth(0) { | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     DemodulatorThreadIQData(unsigned int bandwidth, unsigned int frequency, std::vector<signed char> data) : | 
					
						
							|  |  |  |             data(data), frequency(frequency), bandwidth(bandwidth) { | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     ~DemodulatorThreadIQData() { | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     } | 
					
						
							|  |  |  | }; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | class DemodulatorThreadAudioData { | 
					
						
							|  |  |  | public: | 
					
						
							|  |  |  |     unsigned int frequency; | 
					
						
							|  |  |  |     unsigned int sampleRate; | 
					
						
							|  |  |  |     unsigned char channels; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     std::vector<float> data; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     DemodulatorThreadAudioData() : | 
					
						
							|  |  |  |             sampleRate(0), frequency(0), channels(0) { | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     DemodulatorThreadAudioData(unsigned int frequency, unsigned int sampleRate, std::vector<float> data) : | 
					
						
							|  |  |  |             data(data), sampleRate(sampleRate), frequency(frequency), channels(1) { | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     ~DemodulatorThreadAudioData() { | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     } | 
					
						
							|  |  |  | }; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | class DemodulatorThreadParameters { | 
					
						
							|  |  |  | public: | 
					
						
							| 
									
										
										
										
											2014-11-26 21:05:19 -05:00
										 |  |  |     unsigned int frequency; | 
					
						
							| 
									
										
										
										
											2014-11-22 22:17:33 -05:00
										 |  |  |     unsigned int inputRate; | 
					
						
							| 
									
										
										
										
											2014-11-26 21:05:19 -05:00
										 |  |  |     unsigned int bandwidth; // set equal to disable second stage re-sampling?
 | 
					
						
							| 
									
										
										
										
											2014-11-22 22:17:33 -05:00
										 |  |  |     unsigned int audioSampleRate; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     DemodulatorType demodType; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     DemodulatorThreadParameters() : | 
					
						
							| 
									
										
										
										
											2014-11-30 17:11:29 -05:00
										 |  |  |             frequency(0), inputRate(SRATE), bandwidth(200000), audioSampleRate(AUDIO_FREQUENCY), demodType(DEMOD_TYPE_FM) { | 
					
						
							| 
									
										
										
										
											2014-11-22 22:17:33 -05:00
										 |  |  | 
 | 
					
						
							|  |  |  |     } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     ~DemodulatorThreadParameters() { | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     } | 
					
						
							|  |  |  | }; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | typedef ThreadQueue<DemodulatorThreadIQData> DemodulatorThreadInputQueue; | 
					
						
							|  |  |  | typedef ThreadQueue<AudioThreadInput> DemodulatorThreadOutputQueue; | 
					
						
							| 
									
										
										
										
											2014-11-26 21:05:19 -05:00
										 |  |  | typedef ThreadQueue<DemodulatorThreadCommand> DemodulatorThreadCommandQueue; | 
					
						
							| 
									
										
										
										
											2014-11-22 22:17:33 -05:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2014-11-30 23:33:55 -05:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2014-11-22 22:17:33 -05:00
										 |  |  | class DemodulatorThread { | 
					
						
							| 
									
										
										
										
											2014-11-16 16:51:45 -05:00
										 |  |  | public: | 
					
						
							| 
									
										
										
										
											2014-11-17 22:58:56 -05:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2014-11-26 21:05:19 -05:00
										 |  |  |     DemodulatorThread(DemodulatorThreadInputQueue* pQueue); | 
					
						
							| 
									
										
										
										
											2014-11-16 16:51:45 -05:00
										 |  |  |     ~DemodulatorThread(); | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2014-11-22 22:17:33 -05:00
										 |  |  |     void threadMain(); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     void setVisualOutputQueue(DemodulatorThreadOutputQueue *tQueue) { | 
					
						
							|  |  |  |         visOutQueue = tQueue; | 
					
						
							|  |  |  |         visOutQueue->set_max_num_items(1); | 
					
						
							|  |  |  |     } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2014-11-26 21:05:19 -05:00
										 |  |  |     void setCommandQueue(DemodulatorThreadCommandQueue *tQueue) { | 
					
						
							|  |  |  |         commandQueue = tQueue; | 
					
						
							|  |  |  |     } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2014-11-30 17:11:29 -05:00
										 |  |  |     void setAudioInputQueue(AudioThreadInputQueue *tQueue) { | 
					
						
							|  |  |  |         audioInputQueue = tQueue; | 
					
						
							|  |  |  |     } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2014-11-26 21:05:19 -05:00
										 |  |  |     DemodulatorThreadParameters &getParams() { | 
					
						
							|  |  |  |         return params; | 
					
						
							|  |  |  |     } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     void initialize(); | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2014-11-23 19:39:27 -05:00
										 |  |  |     void terminate(); | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2014-11-16 16:51:45 -05:00
										 |  |  | protected: | 
					
						
							| 
									
										
										
										
											2014-11-22 22:33:32 -05:00
										 |  |  |     DemodulatorThreadInputQueue* inputQueue; | 
					
						
							| 
									
										
										
										
											2014-11-22 22:17:33 -05:00
										 |  |  |     DemodulatorThreadOutputQueue* visOutQueue; | 
					
						
							| 
									
										
										
										
											2014-11-26 21:05:19 -05:00
										 |  |  |     DemodulatorThreadCommandQueue* commandQueue; | 
					
						
							| 
									
										
										
										
											2014-11-30 17:11:29 -05:00
										 |  |  |     AudioThreadInputQueue *audioInputQueue; | 
					
						
							| 
									
										
										
										
											2014-11-16 16:51:45 -05:00
										 |  |  | 
 | 
					
						
							|  |  |  |     firfilt_crcf fir_filter; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     msresamp_crcf resampler; | 
					
						
							|  |  |  |     float resample_ratio; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     msresamp_crcf audio_resampler; | 
					
						
							|  |  |  |     float audio_resample_ratio; | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2014-11-17 22:58:56 -05:00
										 |  |  |     DemodulatorThreadParameters params; | 
					
						
							| 
									
										
										
										
											2014-11-26 22:29:23 -05:00
										 |  |  |     DemodulatorThreadParameters last_params; | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2014-11-16 16:51:45 -05:00
										 |  |  |     freqdem fdem; | 
					
						
							| 
									
										
										
										
											2014-11-27 22:13:21 -05:00
										 |  |  |     nco_crcf nco_shift; | 
					
						
							|  |  |  |     int shift_freq; | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2014-11-23 19:39:27 -05:00
										 |  |  |     std::atomic<bool> terminated; | 
					
						
							| 
									
										
										
										
											2014-11-26 21:05:19 -05:00
										 |  |  |     std::atomic<bool> initialized; | 
					
						
							| 
									
										
										
										
											2014-11-30 23:33:55 -05:00
										 |  |  | 
 | 
					
						
							|  |  |  |     DemodulatorWorkerThread *workerThread; | 
					
						
							|  |  |  |     std::thread *t_Worker; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     DemodulatorThreadWorkerCommandQueue *workerQueue; | 
					
						
							|  |  |  |     DemodulatorThreadWorkerResultQueue *workerResults; | 
					
						
							| 
									
										
										
										
											2014-11-16 16:51:45 -05:00
										 |  |  | }; |