aboutsummaryrefslogtreecommitdiff
path: root/src/string.rs
blob: 26e562cdc5a7a57b7b68a04f791ed3281ff38c53 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
// This file is part of Guitar <https://github.com/Fuwn/guitar>.
//
// This program is free software: you can redistribute it and/or modify
// it under the terms of the GNU General Public License as published by
// the Free Software Foundation, version 3.
//
// This program is distributed in the hope that it will be useful, but
// WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
// General Public License for more details.
//
// You should have received a copy of the GNU General Public License
// along with this program. If not, see <http://www.gnu.org/licenses/>.
//
// Copyright (C) 2022-2022 Fuwn <[email protected]>
// SPDX-License-Identifier: GPL-3.0-only

use crate::{unit::Frequency, Pitch};

#[derive(Debug, Clone, PartialEq, PartialOrd)]
pub struct String {
  pitch: Pitch,
  frets: Vec<Pitch>,
  fret_count: usize,
  base_frequency: Frequency,
}

impl String {
  #[must_use]
  pub fn new(pitch: Pitch, fret_count: usize) -> Self {
    let mut frets = vec![];
    let mut next_pitch = pitch.clone();

    for _ in 0..fret_count {
      let semitones = next_pitch.semitones();

      frets.push(next_pitch.clone());
      next_pitch.set_semitones(semitones + 1);
    }

    Self {
      pitch,
      frets,
      fret_count,
      base_frequency: 440.,
    }
  }

  #[must_use]
  pub const fn pitch(&self) -> &Pitch { &self.pitch }

  #[must_use]
  pub const fn frets(&self) -> &Vec<Pitch> { &self.frets }

  #[must_use]
  pub const fn fret_count(&self) -> &usize { &self.fret_count }

  #[must_use]
  pub const fn base_frequency(&self) -> &Frequency { &self.base_frequency }

  pub fn set_pitch(&mut self, pitch: Pitch) {
    let mut next_pitch = pitch.clone();

    for fret in &mut self.frets {
      let semitones = next_pitch.semitones();

      *fret = next_pitch.clone();
      next_pitch.set_semitones(semitones + 1);
    }

    self.pitch = pitch;
  }

  pub fn set_fret_count(&mut self, fret_count: usize) {
    let mut frets = vec![];
    let mut next_pitch = self.pitch.clone();

    for _ in 0..fret_count {
      let semitones = next_pitch.semitones();

      frets.push(next_pitch.clone());
      next_pitch.set_semitones(semitones + 1);
    }

    self.frets = frets;
    self.fret_count = fret_count;
  }

  pub fn set_base_frequency(&mut self, base_frequency: Frequency) {
    self.base_frequency = base_frequency;

    for fret in &mut self.frets {
      fret.set_base_frequency(base_frequency);
    }
  }
}