Subversion Repositories DIN Is Noise

Rev

Rev 2250 | Details | Compare with Previous | Last modification | View Log | RSS feed

Rev Author Line No. Line
964 jag 1
/*
2
* scale_info.cc
2302 jag 3
* DIN Is Noise is copyright (c) 2006-2025 Jagannathan Sampath
1713 jag 4
* DIN Is Noise is released under GNU Public License 2.0
1479 jag 5
* For more information, please visit https://dinisnoise.org/
964 jag 6
*/
7
 
8
 
9
#include "scalelist.h"
10
#include "scale_info.h"
11
#include "tokenizer.h"
12
#include "utils.h"
13
#include "checkbutton.h"
14
#include "note.h"
15
#include "settings.h"
16
#include "ui_list.h"
17
#include "main.h"
2099 jag 18
#include "console.h"
19
#include "log.h"
964 jag 20
#include <vector>
21
#include <string>
22
 
23
extern string user_data_dir;
24
extern map<string, float> INTERVALS;
25
extern int NUM_INTERVALS;
26
extern vector<string> INTERVAL_NAMES;
27
extern string SCALE;
28
extern ui_list uis;
29
extern char BUFFER [];
30
 
31
int scale_info::set_tonic (float f) {
32
  nearest_note.freq = f;
33
  western = find_nearest_note (nearest_note.name, nearest_note.freq, nearest_note.distance);
2100 jag 34
 
35
  // snap note
36
  //
37
  /*float d2 = nearest_note.distance * nearest_note.distance;
2099 jag 38
  if (d2 > 1) {
39
    tonic = f;
40
  } else {
41
    if (nearest_note.freq != lastsnapfreq) {
42
      tonic = nearest_note.freq;
43
      lastsnapfreq = tonic;
44
    } else {
45
      tonic = f;
46
      lastsnapfreq = 0.0f;
47
    }
2100 jag 48
  }*/
49
 
50
  tonic = f;
964 jag 51
  lo_tonic = tonic / 2.0f;
52
  hi_tonic = tonic * 2.0f;
53
  if (scl) scl->tonic_changed ();
54
  return 1;
55
}
56
 
57
int scale_info::load_scale (const string& n) {
2250 jag 58
 
964 jag 59
  name = n;
60
 
61
  ifstream file ((user_data_dir + name).c_str(), ios::in);
62
  if (!file) return 0;
63
 
64
  string ignore;
65
  int nn;
66
  file >> ignore >> nn >> ignore;
67
  set_num_notes (nn);
68
  notes.resize (num_notes);
69
  for (int i = 0; i < num_notes; ++i) file >> notes[i];
70
 
71
  intervals = INTERVALS;
72
 
73
  if (scl) scl->scale_loaded ();
74
 
75
  return 1;
76
 
77
}
78
 
79
int scale_info::save_scale () {
80
 
81
  string fname (user_data_dir + name);
82
 
83
  ofstream file ((user_data_dir + name).c_str(), ios::out);
84
  if (!file) return 0;
85
 
86
  file << "num_notes " << num_notes << endl;
87
  file << "notes ";
1695 jag 88
  for (int i = 0; i < num_notes; ++i) file << notes[i] << spc;
964 jag 89
  file << endl;
90
 
91
  dlog << "+++ saved scale into " + fname + " +++" << endl;
92
 
93
  return 1;
94
 
95
}
96
 
97
void scale_info::update (checkbutton* cb_notes) {
98
  notes.clear ();
99
  name.clear ();
100
  for (int i = 0; i < NUM_INTERVALS; ++i) {
2077 jag 101
    if (cb_notes[i].state) {
964 jag 102
      const string& in = INTERVAL_NAMES[i];
103
      notes.push_back (in);
104
      name += in;
105
    }
106
  }
107
 
108
  set_num_notes (notes.size ());
109
  if (scl) scl->scale_changed ();
110
  SCALE = name;
111
}
112
 
113
void scale_info::update_settings () {
114
  sprintf (BUFFER, " nearest note = %s @ %0.3f Hz", nearest_note.name.c_str (), nearest_note.freq);
2112 jag 115
  uis.settings_scr.ol_nearest_note.option.set_text (BUFFER);
116
  uis.settings_scr.ol_nearest_note.set_apply_pos ();
1120 jag 117
  uis.settings_scr.sp_key.set_value (tonic);
964 jag 118
  uis.settings_scr.sn_scale_notes.refresh ();
119
}
120
 
121
void scale_info::set_num_notes (int n) {
122
  num_notes = n;
123
  last_note = num_notes - 1;
124
  second_last_note = last_note - 1;
125
  num_ranges = last_note;
126
  last_range = num_ranges - 1;
127
}