Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: backup and restore of samples using yaml file #3

Open
wants to merge 4 commits into
base: main
Choose a base branch
from

Conversation

reidevries
Copy link

@reidevries reidevries commented Feb 4, 2024

I've run basic tests on my own Volca Sample 2 to confirm the functionality is working, I will probably test more extensively soon.

See #1

@00nktk
Copy link
Owner

00nktk commented Feb 4, 2024

Thanks for the PR! Will give it a look today

@00nktk 00nktk self-requested a review February 4, 2024 08:44
Copy link
Owner

@00nktk 00nktk left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Great feature, thanks!

Got some minor comments. Also, please do a cargo clippy run and fix the issues from its output.

Comment on lines +2 to +3
use super::sample_slots::SampleSlots;
use serde::{Deserialize, Serialize};
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please regroup imports the way it's done in other files

// Std
use std::{...};

// External crate
use external_crate::{...};

// This crate
use crate::{...};

Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
use super::sample_slots::SampleSlots;
use serde::{Deserialize, Serialize};
use serde::{Deserialize, Serialize};
use super::sample_slots::SampleSlots;

Comment on lines +127 to +136
fn upload_sample_from_file(
&mut self,
input: PathBuf,
sample_no: Option<u8>,
mono_mode: MonoMode,
output: Option<PathBuf>,
dry_run: bool,
name: Option<&str>,
check_overwrite: bool,
) -> Result<()> {
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Too many arguments (clippy). I would split it into two functions: load and upload. Writing processed audio can be inside of "load" I think.

Comment on lines +9 to +13
// This is used instead of a HashMap to enforce a fixed set of keys and
// customize serialization behaviour for readability
pub struct SampleSlots {
slots: [Option<String>; 200],
}
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I believe there's an easier way to achieve this. Smth like

type SampleSlotsInner = BTreeMap<u8, String>; // No need to be public

#[derive(Default, Serialize, Deserialize)]
#[serde(try_from = "SampleSlotsInner")] 
pub struct SampleSlots(pub SampleSlotsInner);

// If you want to restrict sample index
impl TryFrom<SampleSlotsInner> for SampleSlots {
    type Error = anyhow::Error; // or String?

    fn try_from(map: SampleSlotsInner) -> Result<Self, Self::Error> {
        if value.last_key_value().map_or(false, |(&idx, _)| idx >= 200 /* make me const */ ) {
            bail!("Sample idx too big");
        }
    }
}

This way we don't need to implement (and don't have to support) [De-]Serialize, visitors and other stuff. Tough it lacks slot index restriction when the struct is constructed in a way other than through deserialization, but I doubt this is important at this point.


Also, this struct feels too big to be stored on stack (nothing critical though). And I can imagine String being replaced with something like this in the future.

struct SampleSlot {
    name: String,
    path: Option<PathBuf>,
    channel: Option<MonoMode>,
    ... // Some other metadata?
}

This is just an idea of how to extend this in the future, so one could have several "volca loadouts" of their sample collection without need to store multiple sample copies.

self.upload_sample_from_file(
file_name,
Some(i as u8),
MonoMode::Mid,
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Maybe a cli argument for default mode?

Comment on lines +302 to +304
} else {
self.delete_sample(i as u8, true)?;
}
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I would make this optional. Imagine swapping sample "slices" or "masks".


#[derive(Serialize, Deserialize)]
pub struct BackupData {
pub sample_slots: SampleSlots,
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

samples would be better, I believe.

If someday pattern-related stuff gets implemented backups could look like

samples:
    ... # sample data
patterns: # or sequences
    ... # pattern data

Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Remove this file please.

None => "",
};

if ext != "yaml" {
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I believe yml is also a valid extension. I think you can remove this check and let serde do its job.

Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I find this naming ("domain") a little confusing in this crate. While I realise it's more of a personal preference, I think calling this module a "library" and (maybe) reducing nesting (moving BackupData and SampleSlots up) will make it easier to read the code.

Comment on lines +2 to +7
use serde::de::{Deserialize, Deserializer, MapAccess, Visitor};
use serde::ser::{Serialize, SerializeMap, Serializer};
use std::fmt;
use std::ops::{Index, IndexMut};

extern crate serde_yaml;
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
use serde::de::{Deserialize, Deserializer, MapAccess, Visitor};
use serde::ser::{Serialize, SerializeMap, Serializer};
use std::fmt;
use std::ops::{Index, IndexMut};
extern crate serde_yaml;
use std::fmt;
use std::ops::{Index, IndexMut};
use serde::de::{Deserialize, Deserializer, MapAccess, Visitor};
use serde::ser::{Serialize, SerializeMap, Serializer};

extern not needed.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

2 participants