4 * Copyright (C) 1997-2011 Adam Williams <broadcast at earthling dot net>
6 * This program is free software; you can redistribute it and/or modify
7 * it under the terms of the GNU General Public License as published by
8 * the Free Software Foundation; either version 2 of the License, or
9 * (at your option) any later version.
11 * This program is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 * GNU General Public License for more details.
16 * You should have received a copy of the GNU General Public License
17 * along with this program; if not, write to the Free Software
18 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
22 #include "bcdisplayinfo.h"
30 #include "spectrogram.h"
32 #include "transportque.inc"
41 REGISTER_PLUGIN(Spectrogram)
44 #define HALF_WINDOW (config.window_size / 2)
46 SpectrogramConfig::SpectrogramConfig()
49 window_size = MAX_WINDOW;
57 int SpectrogramConfig::equivalent(SpectrogramConfig &that)
59 return EQUIV(level, that.level) &&
60 xzoom == that.xzoom &&
61 frequency == that.frequency &&
62 window_size == that.window_size &&
63 normalize == that.normalize &&
65 history_size == that.history_size;
68 void SpectrogramConfig::copy_from(SpectrogramConfig &that)
72 frequency = that.frequency;
73 window_size = that.window_size;
74 normalize = that.normalize;
76 history_size = that.history_size;
78 CLAMP(history_size, MIN_HISTORY, MAX_HISTORY);
79 CLAMP(frequency, MIN_FREQ, MAX_FREQ);
80 CLAMP(xzoom, MIN_XZOOM, MAX_XZOOM);
83 void SpectrogramConfig::interpolate(SpectrogramConfig &prev,
84 SpectrogramConfig &next,
87 int64_t current_frame)
94 SpectrogramFrame::SpectrogramFrame(int data_size)
96 this->data_size = data_size;
97 data = new float[data_size];
101 SpectrogramFrame::~SpectrogramFrame()
108 SpectrogramLevel::SpectrogramLevel(Spectrogram *plugin, int x, int y)
109 : BC_FPot(x, y, plugin->config.level, INFINITYGAIN, 40)
111 this->plugin = plugin;
114 int SpectrogramLevel::handle_event()
116 plugin->config.level = get_value();
117 plugin->send_configure_change();
125 SpectrogramMode::SpectrogramMode(Spectrogram *plugin,
131 mode_to_text(plugin->config.mode))
133 this->plugin = plugin;
136 void SpectrogramMode::create_objects()
138 add_item(new BC_MenuItem(mode_to_text(VERTICAL)));
139 add_item(new BC_MenuItem(mode_to_text(HORIZONTAL)));
142 int SpectrogramMode::handle_event()
144 if(plugin->config.mode != text_to_mode(get_text()))
146 SpectrogramWindow *window = (SpectrogramWindow*)plugin->thread->window;
147 window->probe_x = -1;
148 window->probe_y = -1;
149 plugin->config.mode = text_to_mode(get_text());
150 window->canvas->clear_box(0, 0, window->canvas->get_w(), window->canvas->get_h());
151 plugin->send_configure_change();
156 const char* SpectrogramMode::mode_to_text(int mode)
161 return _("Vertical");
164 return _("Horizontal");
168 int SpectrogramMode::text_to_mode(const char *text)
170 if(!strcmp(mode_to_text(VERTICAL), text)) return VERTICAL;
177 SpectrogramHistory::SpectrogramHistory(Spectrogram *plugin,
180 : BC_IPot(x, y, plugin->config.history_size, MIN_HISTORY, MAX_HISTORY)
182 this->plugin = plugin;
185 int SpectrogramHistory::handle_event()
187 plugin->config.history_size = get_value();
188 plugin->send_configure_change();
197 SpectrogramWindowSize::SpectrogramWindowSize(Spectrogram *plugin,
206 this->plugin = plugin;
209 int SpectrogramWindowSize::handle_event()
211 plugin->config.window_size = atoi(get_text());
212 plugin->send_configure_change();
217 SpectrogramWindowSizeTumbler::SpectrogramWindowSizeTumbler(Spectrogram *plugin, int x, int y)
221 this->plugin = plugin;
224 int SpectrogramWindowSizeTumbler::handle_up_event()
226 plugin->config.window_size *= 2;
227 if(plugin->config.window_size > MAX_WINDOW)
228 plugin->config.window_size = MAX_WINDOW;
229 char string[BCTEXTLEN];
230 sprintf(string, "%d", plugin->config.window_size);
231 ((SpectrogramWindow*)plugin->get_thread()->get_window())->window_size->set_text(string);
232 plugin->send_configure_change();
236 int SpectrogramWindowSizeTumbler::handle_down_event()
238 plugin->config.window_size /= 2;
239 if(plugin->config.window_size < MIN_WINDOW)
240 plugin->config.window_size = MIN_WINDOW;
241 char string[BCTEXTLEN];
242 sprintf(string, "%d", plugin->config.window_size);
243 ((SpectrogramWindow*)plugin->get_thread()->get_window())->window_size->set_text(string);
244 plugin->send_configure_change();
252 SpectrogramNormalize::SpectrogramNormalize(Spectrogram *plugin, int x, int y)
253 : BC_CheckBox(x, y, plugin->config.normalize, _("Normalize"))
255 this->plugin = plugin;
258 int SpectrogramNormalize::handle_event()
260 plugin->config.normalize = get_value();
261 plugin->send_configure_change();
268 SpectrogramFreq::SpectrogramFreq(Spectrogram *plugin, int x, int y)
273 (int)plugin->config.frequency)
275 this->plugin = plugin;
278 int SpectrogramFreq::handle_event()
280 plugin->config.frequency = atoi(get_text());
281 CLAMP(plugin->config.frequency, MIN_FREQ, MAX_FREQ);
282 plugin->send_configure_change();
290 SpectrogramXZoom::SpectrogramXZoom(Spectrogram *plugin, int x, int y)
291 : BC_IPot(x, y, plugin->config.xzoom, MIN_XZOOM, MAX_XZOOM)
293 this->plugin = plugin;
296 int SpectrogramXZoom::handle_event()
298 plugin->config.xzoom = get_value();
299 plugin->send_configure_change();
305 SpectrogramCanvas::SpectrogramCanvas(Spectrogram *plugin, int x, int y, int w, int h)
306 : BC_SubWindow(x, y, w, h, BLACK)
308 this->plugin = plugin;
309 current_operation = NONE;
312 int SpectrogramCanvas::button_press_event()
314 if(is_event_win() && cursor_inside())
317 current_operation = DRAG;
318 plugin->send_configure_change();
324 int SpectrogramCanvas::button_release_event()
326 if(current_operation == DRAG)
328 current_operation = NONE;
334 int SpectrogramCanvas::cursor_motion_event()
336 if(current_operation == DRAG)
344 void SpectrogramCanvas::calculate_point()
346 int x = get_cursor_x();
347 int y = get_cursor_y();
348 CLAMP(x, 0, get_w() - 1);
349 CLAMP(y, 0, get_h() - 1);
351 ((SpectrogramWindow*)plugin->thread->window)->calculate_frequency(
356 //printf("SpectrogramCanvas::calculate_point %d %d\n", __LINE__, Freq::tofreq(freq_index));
359 void SpectrogramCanvas::draw_overlay()
361 SpectrogramWindow *window = (SpectrogramWindow*)plugin->thread->window;
362 if(window->probe_x >= 0 || window->probe_y >= 0)
366 if(plugin->config.mode == HORIZONTAL) draw_line(0, window->probe_y, get_w(), window->probe_y);
367 draw_line(window->probe_x, 0, window->probe_x, get_h());
379 SpectrogramWindow::SpectrogramWindow(Spectrogram *plugin)
380 : PluginClientWindow(plugin,
387 this->plugin = plugin;
388 probe_x = probe_y = -1;
391 SpectrogramWindow::~SpectrogramWindow()
395 void SpectrogramWindow::create_objects()
397 int x = plugin->get_theme()->widget_border;
401 char string[BCTEXTLEN];
405 add_subwindow(canvas = new SpectrogramCanvas(plugin,
410 BC_Pot::calculate_h() * 2 -
411 plugin->get_theme()->widget_border * 3));
412 canvas->set_cursor(CROSS_CURSOR, 0, 0);
414 x = plugin->get_theme()->widget_border;
415 y = canvas->get_y() + canvas->get_h() + plugin->get_theme()->widget_border;
419 add_subwindow(level_title = new BC_Title(x, y, _("Level:")));
420 x += level_title->get_w() + plugin->get_theme()->widget_border;
421 add_subwindow(level = new SpectrogramLevel(plugin, x, y));
422 x += level->get_w() + plugin->get_theme()->widget_border;
423 y += level->get_h() + plugin->get_theme()->widget_border;
425 add_subwindow(normalize = new SpectrogramNormalize(plugin, x1, y));
427 x = x1 + level_title->get_w() + level->get_w() + plugin->get_theme()->widget_border * 2;
431 sprintf(string, "%d", plugin->config.window_size);
432 add_subwindow(window_size_title = new BC_Title(x, y, _("Window size:")));
434 x += window_size_title->get_w() + plugin->get_theme()->widget_border;
435 add_subwindow(window_size = new SpectrogramWindowSize(plugin, x, y, string));
436 x += window_size->get_w();
437 add_subwindow(window_size_tumbler = new SpectrogramWindowSizeTumbler(plugin, x, y));
439 for(int i = MIN_WINDOW; i <= MAX_WINDOW; i *= 2)
441 sprintf(string, "%d", i);
442 window_size->add_item(new BC_MenuItem(string));
445 // x += window_size_tumbler->get_w() + plugin->get_theme()->widget_border;
447 y += window_size->get_h() + plugin->get_theme()->widget_border;
451 add_subwindow(mode_title = new BC_Title(x, y, _("Mode:")));
452 x += mode_title->get_w() + plugin->get_theme()->widget_border;
453 add_subwindow(mode = new SpectrogramMode(plugin,
456 mode->create_objects();
457 x += mode->get_w() + plugin->get_theme()->widget_border;
459 x = x1 = window_size_tumbler->get_x() +
460 window_size_tumbler->get_w() +
461 plugin->get_theme()->widget_border;
463 add_subwindow(history_title = new BC_Title(x, y, _("History:")));
464 x += history_title->get_w() + plugin->get_theme()->widget_border;
465 add_subwindow(history = new SpectrogramHistory(plugin,
470 y += history->get_h() + plugin->get_theme()->widget_border;
471 add_subwindow(xzoom_title = new BC_Title(x, y, _("X Zoom:")));
472 x += xzoom_title->get_w() + plugin->get_theme()->widget_border;
473 add_subwindow(xzoom = new SpectrogramXZoom(plugin, x, y));
474 x += xzoom->get_w() + plugin->get_theme()->widget_border;
478 add_subwindow(freq_title = new BC_Title(x1, y, _("Freq: 0 Hz")));
479 // x += freq_title->get_w() + plugin->get_theme()->widget_border;
480 y += freq_title->get_h() + plugin->get_theme()->widget_border;
481 // add_subwindow(freq = new SpectrogramFreq(plugin, x, y));
482 // y += freq->get_h() + plugin->get_theme()->widget_border;
484 add_subwindow(amplitude_title = new BC_Title(x, y, _("Amplitude: 0 dB")));
491 int SpectrogramWindow::resize_event(int w, int h)
493 int canvas_h = canvas->get_h();
494 int canvas_difference = get_h() - canvas_h;
496 canvas->reposition_window(0,
499 h - canvas_difference);
500 canvas->clear_box(0, 0, canvas->get_w(), canvas->get_h());
504 int y_diff = -canvas_h + canvas->get_h();
506 // Remove all columns which may be a different size.
507 plugin->frame_buffer.remove_all_objects();
508 plugin->frame_history.remove_all_objects();
510 level_title->reposition_window(
511 level_title->get_x(),
512 level_title->get_y() + y_diff);
513 level->reposition_window(level->get_x(),
514 level->get_y() + y_diff);
516 window_size_title->reposition_window(
517 window_size_title->get_x(),
518 window_size_title->get_y() + y_diff);
520 normalize->reposition_window(normalize->get_x(),
521 normalize->get_y() + y_diff);
522 window_size->reposition_window(window_size->get_x(),
523 window_size->get_y() + y_diff);
524 window_size_tumbler->reposition_window(window_size_tumbler->get_x(),
525 window_size_tumbler->get_y() + y_diff);
529 mode_title->reposition_window(mode_title->get_x(),
530 mode_title->get_y() + y_diff);
531 mode->reposition_window(mode->get_x(),
532 mode->get_y() + y_diff);
535 history_title->reposition_window(history_title->get_x(),
536 history_title->get_y() + y_diff);
537 history->reposition_window(history->get_x(),
538 history->get_y() + y_diff);
540 xzoom_title->reposition_window(xzoom_title->get_x(),
541 xzoom_title->get_y() + y_diff);
542 xzoom->reposition_window(xzoom->get_x(),
543 xzoom->get_y() + y_diff);
544 freq_title->reposition_window(freq_title->get_x(),
545 freq_title->get_y() + y_diff);
546 // freq->reposition_window(freq->get_x(),
547 // freq->get_y() + y_diff);
548 amplitude_title->reposition_window(amplitude_title->get_x(),
549 amplitude_title->get_y() + y_diff);
558 void SpectrogramWindow::calculate_frequency(int x, int y, int do_overlay)
560 if(x < 0 && y < 0) return;
562 // Clear previous overlay
563 if(do_overlay) canvas->draw_overlay();
565 // New probe position
569 // Convert to coordinates in frame history
570 int freq_pixel, time_pixel;
571 if(plugin->config.mode == VERTICAL)
573 freq_pixel = get_w() - x;
579 time_pixel = get_w() - x;
582 CLAMP(time_pixel, 0, plugin->frame_history.size() - 1);
583 if(plugin->frame_history.size())
585 SpectrogramFrame *ptr = plugin->frame_history.get(
586 plugin->frame_history.size() - time_pixel - 1);
590 if(plugin->config.mode == VERTICAL)
592 pixels = canvas->get_w();
593 freq_index = (pixels - freq_pixel) * TOTALFREQS / pixels;
597 pixels = canvas->get_h();
598 freq_index = (pixels - freq_pixel) * TOTALFREQS / pixels;
601 int freq = Freq::tofreq(freq_index);
604 CLAMP(freq_pixel, 0, ptr->data_size - 1);
605 double level = ptr->data[freq_pixel];
607 char string[BCTEXTLEN];
608 sprintf(string, _("Freq: %d Hz"), freq);
609 freq_title->update(string);
611 sprintf(string, _("Amplitude: %.2f dB"), level);
612 amplitude_title->update(string);
617 canvas->draw_overlay();
624 void SpectrogramWindow::update_gui()
626 char string[BCTEXTLEN];
627 level->update(plugin->config.level);
628 sprintf(string, "%d", plugin->config.window_size);
629 window_size->set_text(string);
631 mode->set_text(mode->mode_to_text(plugin->config.mode));
632 history->update(plugin->config.history_size);
634 // sprintf(string, "%d", plugin->config.window_fragment);
635 // window_fragment->set_text(string);
637 normalize->set_value(plugin->config.normalize);
664 Spectrogram::Spectrogram(PluginServer *server)
665 : PluginAClient(server)
673 Spectrogram::~Spectrogram()
681 frame_buffer.remove_all_objects();
682 frame_history.remove_all_objects();
686 void Spectrogram::reset()
693 audio_buffer_start = -MAX_WINDOW * 2;
698 bzero(&header, sizeof(data_header_t));
702 const char* Spectrogram::plugin_title() { return N_("Spectrogram"); }
703 int Spectrogram::is_realtime() { return 1; }
705 int Spectrogram::process_buffer(int64_t size,
707 int64_t start_position,
718 load_configuration();
720 // Reset audio buffer
721 if(window_size != config.window_size)
724 window_size = config.window_size;
738 data = new unsigned char[sizeof(data_header_t)];
744 freq_real = new double[MAX_WINDOW];
745 freq_imag = new double[MAX_WINDOW];
750 audio_buffer = new Samples(MAX_WINDOW);
755 int needed = buffer_size + size;
756 if(audio_buffer->get_allocated() < needed)
758 Samples *new_samples = new Samples(needed);
759 memcpy(new_samples->get_data(),
760 audio_buffer->get_data(),
761 sizeof(double) * buffer_size);
763 audio_buffer = new_samples;
766 double *audio_samples = audio_buffer->get_data();
767 memcpy(audio_samples + buffer_size,
769 sizeof(double) * size);
773 //printf("Spectrogram::process_buffer %d %d\n", __LINE__, buffer_size);
775 // Append a windows to end of GUI buffer
777 while(buffer_size >= window_size)
780 fft->do_fft(window_size, // must be a power of 2
781 0, // 0 = forward FFT, 1 = inverse
782 audio_samples, // array of input's real samples
783 0, // array of input's imag samples
784 freq_real, // array of output's reals
787 // Get peak in waveform
789 for(int i = 0; i < window_size; i++)
791 double sample = fabs(audio_samples[i]);
792 if(sample > max) max = sample;
795 // Append to end of data buffer
796 if(allocated_data < (total_windows + 1) * (HALF_WINDOW + 1))
798 int new_allocation = (total_windows + 1) * (HALF_WINDOW + 1);
799 unsigned char *new_data = new unsigned char[sizeof(data_header_t) +
800 sizeof(float) * new_allocation];
801 data_header_t *new_header = (data_header_t*)new_data;
802 data_header_t *old_header = (data_header_t*)data;
803 memcpy(new_header->samples,
805 sizeof(float) * allocated_data);
808 allocated_data = new_allocation;
811 data_header_t *header = (data_header_t*)data;
812 float *sample_output = header->samples + total_windows * (HALF_WINDOW + 1);
813 // 1st sample is maximum
814 sample_output[0] = max;
815 for(int i = 0; i < HALF_WINDOW; i++)
817 sample_output[i + 1] = sqrt(freq_real[i] * freq_real[i] +
818 freq_imag[i] * freq_imag[i]);
819 // sample_output[i + 1] = freq_real[i];
822 // Shift audio buffer out
823 memcpy(audio_samples,
824 audio_samples + window_size,
825 (buffer_size - window_size) * sizeof(double));
828 buffer_size -= window_size;
831 data_header_t *header = (data_header_t*)data;
832 header->window_size = window_size;
833 header->sample_rate = sample_rate;
834 header->total_windows = total_windows;
835 // Linear output level
836 header->level = DB::fromdb(config.level);
838 send_render_gui(data,
839 sizeof(data_header_t) +
840 sizeof(float) * total_windows * (HALF_WINDOW + 1));
845 void Spectrogram::render_stop()
848 audio_buffer_start = -MAX_WINDOW * 2;
849 frame_buffer.remove_all_objects();
850 frame_history.remove_all_objects();
856 NEW_WINDOW_MACRO(Spectrogram, SpectrogramWindow)
858 void Spectrogram::update_gui()
862 int result = load_configuration();
863 SpectrogramWindow *window = (SpectrogramWindow*)thread->get_window();
864 window->lock_window("Spectrogram::update_gui");
865 if(result) window->update_gui();
868 //printf("Spectrogram::update_gui %d\n", __LINE__);
869 // Shift in accumulated canvas columns
870 if(frame_buffer.size())
872 SpectrogramCanvas *canvas = (SpectrogramCanvas*)window->canvas;
873 canvas->draw_overlay();
874 // Z to draw in this iteration
875 int total_frames = timer->get_difference() *
880 //printf("Spectrogram::update_gui %d %d %ld\n", __LINE__, frame_buffer.size(), timer->get_difference());
881 if(total_frames) timer->subtract(total_frames *
886 // Add forced column drawing
887 for(int i = 0; i < frame_buffer.size(); i++)
888 if(frame_buffer.get(i)->force) total_frames++;
889 total_frames = MIN(frame_buffer.size(), total_frames);
891 // Limit to canvas width
892 if(config.mode == HORIZONTAL)
893 total_frames = MIN(canvas->get_w(), total_frames);
897 if(config.mode == HORIZONTAL)
900 int pixels = canvas->get_h();
901 canvas->copy_area(total_frames * config.xzoom,
905 canvas->get_w() - total_frames * config.xzoom,
911 frame < total_frames;
914 int x = canvas->get_w() - (total_frames - frame) * config.xzoom;
915 SpectrogramFrame *ptr = frame_buffer.get(0);
917 for(int i = 0; i < pixels; i++)
919 float db = ptr->data[
920 MIN(i, ptr->data_size - 1)];
922 float r_out, g_out, b_out;
925 #define DIVISION1 0.0
926 #define DIVISION2 -20.0
927 #define DIVISION3 INFINITYGAIN
930 h = 240 - (float)(db - DIVISION2) / (DIVISION1 - DIVISION2) *
935 HSV::hsv_to_rgb(r_out, g_out, b_out, h, s, v);
936 r = (int)(r_out * 0xff);
937 g = (int)(g_out * 0xff);
938 b = (int)(b_out * 0xff);
945 v = (float)(db - DIVISION3) / (DIVISION2 - DIVISION3);
946 HSV::hsv_to_rgb(r_out, g_out, b_out, h, s, v);
947 r = (int)(r_out * 0xff);
948 g = (int)(g_out * 0xff);
949 b = (int)(b_out * 0xff);
956 canvas->set_color((r << 16) |
959 if(config.xzoom == 1)
960 canvas->draw_pixel(x, i);
968 // Push frames onto history
969 for(int i = 0; i < config.xzoom; i++)
971 SpectrogramFrame *new_frame = new SpectrogramFrame(
973 frame_history.append(new_frame);
974 memcpy(new_frame->data, ptr->data, sizeof(float) * ptr->data_size);
977 // Clip history to canvas size
978 while(frame_history.size() > canvas->get_w())
979 frame_history.remove_object_number(0);
981 frame_buffer.remove_object_number(0);
987 // Shift frames into history
988 for(int frame = 0; frame < total_frames; frame++)
990 if(frame_history.size() >= config.history_size)
991 frame_history.remove_object_number(0);
993 frame_history.append(frame_buffer.get(0));
994 frame_buffer.remove_number(0);
997 // Reduce history size
998 while(frame_history.size() > config.history_size)
999 frame_history.remove_object_number(0);
1001 // Draw frames from history
1002 canvas->clear_box(0, 0, canvas->get_w(), canvas->get_h());
1003 for(int frame = 0; frame < frame_history.size(); frame++)
1005 SpectrogramFrame *ptr = frame_history.get(frame);
1006 //printf("%d %d\n", canvas->get_w(), ptr->data_size);
1008 int luma = (frame + 1) * 0x80 / frame_history.size();
1009 if(frame == frame_history.size() - 1)
1011 canvas->set_color(WHITE);
1012 canvas->set_line_width(2);
1015 canvas->set_color((luma << 16) |
1022 int w = canvas->get_w();
1023 int h = canvas->get_h();
1026 //printf("Spectrogram::update_gui %d ", __LINE__);
1028 for(int x2 = 0; x2 < w; x2++)
1030 float db = ptr->data[
1031 MIN((w - x2), ptr->data_size - 1)];
1032 //if(x2 > w - 10) printf("%.02f ", ptr->data[x2]);
1033 int y2 = h - 1 - (int)((db - INFINITYGAIN) /
1034 (0 - INFINITYGAIN) *
1036 CLAMP(y2, 0, h - 1);
1040 canvas->draw_line(x1, y1, x2, y2);
1051 canvas->set_line_width(1);
1058 // Recompute probe level
1059 window->calculate_frequency(window->probe_x, window->probe_y, 0);
1061 canvas->draw_overlay();
1065 while(frame_buffer.size() > MAX_COLUMNS)
1066 frame_buffer.remove_object_number(0);
1068 window->unlock_window();
1072 void Spectrogram::render_gui(void *data, int size)
1076 thread->get_window()->lock_window("Spectrogram::render_gui");
1077 data_header_t *header = (data_header_t*)data;
1078 memcpy(&this->header, header, sizeof(data_header_t));
1079 BC_SubWindow *canvas = ((SpectrogramWindow*)thread->get_window())->canvas;
1080 int pixels = canvas->get_w();
1081 if(config.mode == HORIZONTAL) pixels = canvas->get_h();
1083 // Set all previous columns to draw immediately
1084 for(int i = 0; i < frame_buffer.size(); i++)
1085 frame_buffer.get(i)->force = 1;
1088 for(int current_window = 0;
1089 current_window < header->total_windows;
1092 float *frame = header->samples +
1093 current_window * (header->window_size / 2 + 1);
1094 float frame_max = *frame;
1096 int niquist = get_project_samplerate() / 2;
1097 int total_slots = header->window_size / 2;
1098 int max_slot = total_slots - 1;
1099 // int slot1 = total_slots - 1;
1100 SpectrogramFrame *ptr =
1101 new SpectrogramFrame(
1104 // Scale slots to pixels
1105 for(int i = 0; i < pixels; i++)
1107 // Low frequency of row
1108 int freq_index1 = (int)((pixels - i) * TOTALFREQS / pixels);
1109 // High frequency of row
1110 int freq_index2 = (int)((pixels - i + 1) * TOTALFREQS / pixels);
1111 int freq1 = Freq::tofreq(freq_index1);
1112 int freq2 = Freq::tofreq(freq_index2);
1113 float slot1_f = (float)freq1 * max_slot / niquist;
1114 float slot2_f = (float)freq2 * max_slot / niquist;
1115 int slot1 = (int)(slot1_f);
1116 int slot2 = (int)(slot2_f);
1117 slot1 = MIN(slot1, max_slot);
1118 slot2 = MIN(slot2, max_slot);
1121 // Accumulate multiple slots in the same pixel
1122 if(slot2 > slot1 + 1)
1124 for(int j = slot1; j <= slot2; j++)
1127 sum /= slot2 - slot1 + 1;
1130 // Blend 2 slots to create pixel
1132 float weight = slot1_f - floor(slot1_f);
1133 int slot3 = MIN(slot1 + 1, max_slot);
1134 sum = frame[slot1] * (1.0 - weight) +
1135 frame[slot3] * weight;
1143 if(config.normalize)
1145 // Get the maximum level in the spectrogram
1147 for(int i = 0; i < pixels; i++)
1149 if(ptr->data[i] > max) max = ptr->data[i];
1153 for(int i = 0; i < pixels; i++)
1155 ptr->data[i] = header->level *
1162 // Get the maximum level in the spectrogram
1164 for(int i = 0; i < pixels; i++)
1166 if(ptr->data[i] > max) max = ptr->data[i];
1169 // Maximum level in spectrogram is the maximum waveform level
1170 for(int i = 0; i < pixels; i++)
1172 ptr->data[i] = header->level *
1179 // for(int i = 0; i < pixels; i++)
1181 // ptr->data[i] = header->level * ptr->data[i];
1186 //printf("Spectrogram::render_gui %d ", __LINE__);
1187 for(int i = 0; i < pixels; i++)
1189 ptr->data[i] = DB::todb(ptr->data[i]);
1190 //if(i > pixels - 10) printf("%.02f ", ptr->data[i]);
1196 frame_buffer.append(ptr);
1201 thread->get_window()->unlock_window();
1205 LOAD_CONFIGURATION_MACRO(Spectrogram, SpectrogramConfig)
1207 void Spectrogram::read_data(KeyFrame *keyframe)
1210 input.set_shared_input(keyframe->xbuf);
1215 result = input.read_tag();
1219 if(input.tag.title_is("SPECTROGRAM"))
1221 config.level = input.tag.get_property("LEVEL", config.level);
1222 config.normalize = input.tag.get_property("NORMALIZE", config.normalize);
1223 config.window_size = input.tag.get_property("WINDOW_SIZE", config.window_size);
1224 config.xzoom = input.tag.get_property("XZOOM", config.xzoom);
1225 config.mode = input.tag.get_property("MODE", config.mode);
1226 config.history_size = input.tag.get_property("HISTORY_SIZE", config.history_size);
1229 w = input.tag.get_property("W", w);
1230 h = input.tag.get_property("H", h);
1237 void Spectrogram::save_data(KeyFrame *keyframe)
1240 output.set_shared_output(keyframe->xbuf);
1242 output.tag.set_title("SPECTROGRAM");
1243 output.tag.set_property("LEVEL", (double)config.level);
1244 output.tag.set_property("NORMALIZE", (double)config.normalize);
1245 output.tag.set_property("WINDOW_SIZE", (int)config.window_size);
1246 output.tag.set_property("XZOOM", (int)config.xzoom);
1247 output.tag.set_property("MODE", (int)config.mode);
1248 output.tag.set_property("HISTORY_SIZE", (int)config.history_size);
1249 output.tag.set_property("W", (int)w);
1250 output.tag.set_property("H", (int)h);
1251 output.append_tag();
1252 output.tag.set_title("/SPECTROGRAM");
1253 output.append_tag();
1254 output.append_newline();
1255 output.terminate_string();