Skip to content

Commit c29a002

Browse files
authored
Merge pull request #153 from dotX12/dev
Minor code optimization
2 parents 8dba56a + fa03784 commit c29a002

File tree

4 files changed

+28
-24
lines changed

4 files changed

+28
-24
lines changed

src/fingerprinting/algorithm.rs

Lines changed: 14 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -119,7 +119,7 @@ impl SignatureGenerator {
119119
this.signature
120120
}
121121

122-
fn do_fft(self: &mut Self, s16_mono_16khz_buffer: &[i16]) {
122+
fn do_fft(&mut self, s16_mono_16khz_buffer: &[i16]) {
123123

124124
// Copy the 128 input s16le samples to the local ring buffer
125125

@@ -130,10 +130,10 @@ impl SignatureGenerator {
130130

131131
// Reorder the items (put the latest data at end) and apply Hanning window
132132

133-
for index in 0..=2047 {
133+
for (index, multiplier) in HANNING_WINDOW_2048_MULTIPLIERS.iter().enumerate() {
134134
self.reordered_ring_buffer_of_samples[index] =
135135
self.ring_buffer_of_samples[(index + self.ring_buffer_of_samples_index) & 2047] as f32 *
136-
HANNING_WINDOW_2048_MULTIPLIERS[index];
136+
multiplier;
137137
}
138138

139139
// Perform Fast Fourier transform
@@ -159,7 +159,7 @@ impl SignatureGenerator {
159159
self.fft_outputs_index &= 255;
160160
}
161161

162-
fn do_peak_spreading(self: &mut Self) {
162+
fn do_peak_spreading(&mut self) {
163163
let real_fft_results = &self.fft_outputs[((self.fft_outputs_index as i32 - 1) & 255) as usize];
164164

165165
let spread_fft_results = &mut self.spread_fft_outputs[self.spread_fft_outputs_index];
@@ -191,7 +191,7 @@ impl SignatureGenerator {
191191
self.spread_fft_outputs_index &= 255;
192192
}
193193

194-
fn do_peak_recognition(self: &mut Self) {
194+
fn do_peak_recognition(&mut self) {
195195

196196
// Note: when substracting an array index, casting to signed is needed
197197
// to avoid underflow panics at runtime.
@@ -265,15 +265,19 @@ impl SignatureGenerator {
265265
_ => { continue; }
266266
};
267267

268-
if !self.signature.frequency_band_to_sound_peaks.contains_key(&frequency_band) {
269-
self.signature.frequency_band_to_sound_peaks.insert(frequency_band, vec![]);
270-
}
268+
// In Rust, the entry method returns an Entry object,
269+
// which represents a cell in a HashMap that is either occupied or vacant.
270+
// You can use or_default to insert a value if the key is missing,
271+
// which avoids a double search of the key in the hash map.
272+
self.signature.frequency_band_to_sound_peaks
273+
.entry(frequency_band)
274+
.or_default();
271275

272276
self.signature.frequency_band_to_sound_peaks.get_mut(&frequency_band).unwrap().push(
273277
FrequencyPeak {
274-
fft_pass_number: fft_pass_number,
278+
fft_pass_number,
275279
peak_magnitude: peak_magnitude as u16,
276-
corrected_peak_frequency_bin: corrected_peak_frequency_bin,
280+
corrected_peak_frequency_bin,
277281
sample_rate_hz: 16000,
278282
}
279283
);

src/fingerprinting/signature_format.rs

Lines changed: 11 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -21,15 +21,15 @@ impl FrequencyPeak {
2121
/// rate, 1024 useful bins and the multiplication by 64 made before
2222
/// storing the information
2323
24-
pub fn get_frequency_hz(self: &Self) -> f32 {
24+
pub fn get_frequency_hz(&self) -> f32 {
2525

2626
self.corrected_peak_frequency_bin as f32 * (self.sample_rate_hz as f32 / 2.0 / 1024.0 / 64.0)
2727

2828
}
2929

3030
/// Not sure about this calculation but gives small enough numbers
3131
32-
pub fn get_amplitude_pcm(self: &Self) -> f32 {
32+
pub fn get_amplitude_pcm(&self) -> f32 {
3333

3434
(((self.peak_magnitude as f32 - 6144.0) / 1477.3).exp() * ((1 << 17) as f32) / 2.0).sqrt() / 1024.0
3535

@@ -38,7 +38,7 @@ impl FrequencyPeak {
3838
/// Assume that new FFT bins are emitted every 128 samples, on a
3939
/// standard 16 KHz sample rate basis.
4040
41-
pub fn get_seconds(self: &Self) -> f32 {
41+
pub fn get_seconds(&self) -> f32 {
4242

4343
(self.fft_pass_number as f32 * 128.0) / self.sample_rate_hz as f32
4444

@@ -174,13 +174,13 @@ impl DecodedSignature {
174174
if !frequency_band_to_sound_peaks.contains_key(&frequency_band) {
175175
frequency_band_to_sound_peaks.insert(frequency_band, vec![]);
176176
}
177-
177+
178178
frequency_band_to_sound_peaks.get_mut(&frequency_band).unwrap().push(
179179
FrequencyPeak {
180-
fft_pass_number: fft_pass_number,
180+
fft_pass_number,
181181
peak_magnitude: frequency_peaks_cursor.read_u16::<LittleEndian>()?,
182182
corrected_peak_frequency_bin: frequency_peaks_cursor.read_u16::<LittleEndian>()?,
183-
sample_rate_hz: sample_rate_hz
183+
sample_rate_hz
184184
}
185185
);
186186
}
@@ -210,7 +210,7 @@ impl DecodedSignature {
210210

211211
}
212212

213-
pub fn encode_to_binary(self: &Self) -> Result<Vec<u8>, Box<dyn Error>> {
213+
pub fn encode_to_binary(&self) -> Result<Vec<u8>, Box<dyn Error>> {
214214

215215
let mut cursor = Cursor::new(vec![]);
216216

@@ -242,7 +242,7 @@ impl DecodedSignature {
242242
cursor.write_u32::<LittleEndian>(0)?; // size_minus_header - Will write later
243243

244244
let mut sorted_iterator: Vec<_> = self.frequency_band_to_sound_peaks.iter().collect();
245-
sorted_iterator.sort_by(|x,y| x.0.cmp(&y.0));
245+
sorted_iterator.sort_by(|x, y| x.0.cmp(y.0));
246246

247247
for (frequency_band, frequency_peaks) in sorted_iterator {
248248

@@ -275,7 +275,7 @@ impl DecodedSignature {
275275

276276
cursor.write_u32::<LittleEndian>(0x60030040 + *frequency_band as u32)?;
277277
cursor.write_u32::<LittleEndian>(peaks_buffer.len() as u32)?;
278-
cursor.write(&peaks_buffer)?;
278+
cursor.write_all(&peaks_buffer)?;
279279
for _padding_index in 0..((4 - peaks_buffer.len() as u32 % 4) % 4) {
280280
cursor.write_u8(0)?;
281281
}
@@ -298,13 +298,13 @@ impl DecodedSignature {
298298
Ok(cursor.into_inner())
299299
}
300300

301-
pub fn encode_to_uri(self: &Self) -> Result<String, Box<dyn Error>> {
301+
pub fn encode_to_uri(&self) -> Result<String, Box<dyn Error>> {
302302

303303
Ok(format!("{}{}", DATA_URI_PREFIX, base64::encode(self.encode_to_binary()?)))
304304

305305
}
306306

307-
pub fn to_lure(self: &Self) -> Result<Vec<i16>, Box<dyn Error>> {
307+
pub fn to_lure(&self) -> Result<Vec<i16>, Box<dyn Error>> {
308308

309309
let mut buffer: Vec<i16> = [0].repeat((self.number_samples as f32 / self.sample_rate_hz as f32 * 16000.0) as usize);
310310

src/gui/song_history_interface.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -269,7 +269,7 @@ impl SongRecordInterface for FavoritesInterface {
269269
}
270270

271271
impl FavoritesInterface {
272-
pub fn get_is_favorite(self: &Self) -> &HashSet<Song> {
272+
pub fn get_is_favorite(&self) -> &HashSet<Song> {
273273
&self.is_favorite
274274
}
275275
}

src/utils/csv_song_history.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -35,11 +35,11 @@ pub struct Song {
3535
}
3636

3737
pub trait IsSong {
38-
fn get_song(self: Self) -> Song;
38+
fn get_song(self) -> Song;
3939
}
4040

4141
impl IsSong for SongHistoryRecord {
42-
fn get_song(self: Self) -> Song {
42+
fn get_song(self) -> Song {
4343
return Song {
4444
song_name: self.song_name,
4545
album: self.album,

0 commit comments

Comments
 (0)