foobar2000 SDK  2015-08-03
dsp.cpp
Go to the documentation of this file.
1 #include "stdafx.h"
2 #include "resource.h"
3 
4 static void RunDSPConfigPopup(const dsp_preset & p_data,HWND p_parent,dsp_preset_edit_callback & p_callback);
5 
6 class dsp_sample : public dsp_impl_base
7 {
8 public:
9  dsp_sample(dsp_preset const & in) : m_gain(0) {
10  parse_preset(m_gain, in);
11  }
12 
13  static GUID g_get_guid() {
14  //This is our GUID. Generate your own one when reusing this code.
15  static const GUID guid = { 0x890827b, 0x67df, 0x4c27, { 0xba, 0x1a, 0x4f, 0x95, 0x8d, 0xf, 0xb5, 0xd0 } };
16  return guid;
17  }
18 
19  static void g_get_name(pfc::string_base & p_out) { p_out = "Sample DSP";}
20 
22  // Perform any operations on the chunk here.
23  // The most simple DSPs can just alter the chunk in-place here and skip the following functions.
24 
25 
26  // trivial DSP code: apply our gain to the audio data.
27  chunk->scale( audio_math::gain_to_scale( m_gain ) );
28 
29  // To retrieve the currently processed track, use get_cur_file().
30  // Warning: the track is not always known - it's up to the calling component to provide this data and in some situations we'll be working with data that doesn't originate from an audio file.
31  // If you rely on get_cur_file(), you should change need_track_change_mark() to return true to get accurate information when advancing between tracks.
32 
33  return true; //Return true to keep the chunk or false to drop it from the chain.
34  }
35 
37  // The end of playlist has been reached, we've already received the last decoded audio chunk.
38  // We need to finish any pending processing and output any buffered data through insert_chunk().
39  }
41  // Should do nothing except for special cases where your DSP performs special operations when changing tracks.
42  // If this function does anything, you must change need_track_change_mark() to return true.
43  // If you have pending audio data that you wish to output, you can use insert_chunk() to do so.
44  }
45 
46  void flush() {
47  // If you have any audio data buffered, you should drop it immediately and reset the DSP to a freshly initialized state.
48  // Called after a seek etc.
49  }
50 
51  double get_latency() {
52  // If the DSP buffers some amount of audio data, it should return the duration of buffered data (in seconds) here.
53  return 0;
54  }
55 
57  // Return true if you need on_endoftrack() or need to accurately know which track we're currently processing
58  // WARNING: If you return true, the DSP manager will fire on_endofplayback() at DSPs that are before us in the chain on track change to ensure that we get an accurate mark, so use it only when needed.
59  return false;
60  }
61  static bool g_get_default_preset(dsp_preset & p_out) {
62  make_preset(0, p_out);
63  return true;
64  }
65  static void g_show_config_popup(const dsp_preset & p_data,HWND p_parent,dsp_preset_edit_callback & p_callback) {
66  ::RunDSPConfigPopup(p_data, p_parent, p_callback);
67  }
68  static bool g_have_config_popup() {return true;}
69  static void make_preset(float gain, dsp_preset & out) {
70  dsp_preset_builder builder; builder << gain; builder.finish(g_get_guid(), out);
71  }
72  static void parse_preset(float & gain, const dsp_preset & in) {
73  try {
74  dsp_preset_parser parser(in); parser >> gain;
75  } catch(exception_io_data) {gain = 0;}
76  }
77 private:
78  float m_gain;
79 };
80 
81 // Use dsp_factory_nopreset_t<> instead of dsp_factory_t<> if your DSP does not provide preset/configuration functionality.
83 
84 
85 class CMyDSPPopup : public CDialogImpl<CMyDSPPopup> {
86 public:
87  CMyDSPPopup(const dsp_preset & initData, dsp_preset_edit_callback & callback) : m_initData(initData), m_callback(callback) {}
88 
89  enum { IDD = IDD_DSP };
90 
91  enum {
92  RangeMin = -20,
93  RangeMax = 20,
94 
95  RangeTotal = RangeMax - RangeMin
96  };
97 
98  BEGIN_MSG_MAP(CMyDSPPopup)
99  MSG_WM_INITDIALOG(OnInitDialog)
100  COMMAND_HANDLER_EX(IDOK, BN_CLICKED, OnButton)
101  COMMAND_HANDLER_EX(IDCANCEL, BN_CLICKED, OnButton)
102  MSG_WM_HSCROLL(OnHScroll)
103  END_MSG_MAP()
104 
105 private:
106 
107  BOOL OnInitDialog(CWindow, LPARAM) {
108  m_slider = GetDlgItem(IDC_SLIDER);
109  m_slider.SetRange(0, RangeTotal);
110 
111  {
112  float val;
113  dsp_sample::parse_preset(val, m_initData);
114  m_slider.SetPos( pfc::clip_t<t_int32>( pfc::rint32(val), RangeMin, RangeMax ) - RangeMin );
115  RefreshLabel(val);
116  }
117  return TRUE;
118  }
119 
120  void OnButton(UINT, int id, CWindow) {
121  EndDialog(id);
122  }
123 
124  void OnHScroll(UINT nSBCode, UINT nPos, CScrollBar pScrollBar) {
125  float val;
126  val = (float) ( m_slider.GetPos() + RangeMin );
127 
128  {
129  dsp_preset_impl preset;
130  dsp_sample::make_preset(val, preset);
131  m_callback.on_preset_changed(preset);
132  }
133  RefreshLabel(val);
134  }
135 
136  void RefreshLabel(float val) {
137  pfc::string_formatter msg; msg << pfc::format_float(val) << " dB";
138  ::uSetDlgItemText(*this, IDC_SLIDER_LABEL, msg);
139  }
140 
141  const dsp_preset & m_initData; // modal dialog so we can reference this caller-owned object.
143 
144  CTrackBarCtrl m_slider;
145 };
146 
147 static void RunDSPConfigPopup(const dsp_preset & p_data,HWND p_parent,dsp_preset_edit_callback & p_callback) {
148  CMyDSPPopup popup(p_data, p_callback);
149  if (popup.DoModal(p_parent) != IDOK) {
150  // If the dialog exited with something else than IDOK,k
151  // tell host that the editing has been cancelled by sending it old preset data that we got initialized with
152  p_callback.on_preset_changed(p_data);
153  }
154 }
static void make_preset(float gain, dsp_preset &out)
Definition: dsp.cpp:69
virtual void on_preset_changed(const dsp_preset &)=0
BOOL SHARED_EXPORT uSetDlgItemText(HWND wnd, UINT id, const char *p_text)
CMyDSPPopup(const dsp_preset &initData, dsp_preset_edit_callback &callback)
Definition: dsp.cpp:87
Definition: pfc.h:71
bool need_track_change_mark()
To be overridden by a DSP implementation. Returns true if DSP needs to know exact track change point ...
Definition: dsp.cpp:56
static bool g_get_default_preset(dsp_preset &p_out)
Definition: dsp.cpp:61
dsp_preset_edit_callback & m_callback
Definition: dsp.cpp:142
CTrackBarCtrl m_slider
Definition: dsp.cpp:144
void RefreshLabel(float val)
Definition: dsp.cpp:136
void flush()
To be overridden by a DSP implementation. Flushes the DSP (drops any buffered data). The implementation should reset the DSP to the same state it was in before receiving any audio data. Called after seeking, etc.
Definition: dsp.cpp:46
typedef BOOL(WINAPI *pPowerSetRequest_t)(__in HANDLE PowerRequest
static void RunDSPConfigPopup(const dsp_preset &p_data, HWND p_parent, dsp_preset_edit_callback &p_callback)
Definition: dsp.cpp:147
void on_endofplayback(abort_callback &)
To be overridden by a DSP implementation. Called at the end of played stream, typically at the end of...
Definition: dsp.cpp:36
Interface to container of a chunk of audio data. See audio_chunk_impl for an implementation.
Definition: audio_chunk.h:5
void OnHScroll(UINT nSBCode, UINT nPos, CScrollBar pScrollBar)
Definition: dsp.cpp:124
static void g_show_config_popup(const dsp_preset &p_data, HWND p_parent, dsp_preset_edit_callback &p_callback)
Definition: dsp.cpp:65
static GUID g_get_guid()
Definition: dsp.cpp:13
string8_fastalloc string_formatter
Definition: string_base.h:615
void scale(audio_sample p_value)
Helper function; scales entire chunk content by specified value.
Helper.
Definition: dsp.h:466
Helper.
Definition: dsp.h:485
t_int32 rint32(double p_val)
Definition: other.cpp:209
double get_latency()
To be overridden by a DSP implementation. Retrieves amount of data buffered by the DSP...
Definition: dsp.cpp:51
bool on_chunk(audio_chunk *chunk, abort_callback &)
To be overridden by a DSP implementation. Processes a chunk of audio data. You can call insert_chunk(...
Definition: dsp.cpp:21
void finish(const GUID &id, dsp_preset &out)
Definition: dsp.h:488
static bool g_have_config_popup()
Definition: dsp.cpp:68
void OnButton(UINT, int id, CWindow)
Definition: dsp.cpp:120
float m_gain
Definition: dsp.cpp:78
void on_endoftrack(abort_callback &)
To be overridden by a DSP implementation. Called on track change. You can use insert_chunk() to dump ...
Definition: dsp.cpp:40
dsp_sample(dsp_preset const &in)
Definition: dsp.cpp:9
static void parse_preset(float &gain, const dsp_preset &in)
Definition: dsp.cpp:72
const dsp_preset & m_initData
Definition: dsp.cpp:141
static void g_get_name(pfc::string_base &p_out)
Definition: dsp.cpp:19
static dsp_factory_t< dsp_sample > g_dsp_sample_factory
Definition: dsp.cpp:82
Helper class for implementing dsps. You should derive from dsp_impl_base instead of from dsp directly...
Definition: dsp.h:100