|
| 1 | +# consonaR Agent Instructions |
| 2 | + |
| 3 | +## Package Overview |
| 4 | + |
| 5 | +consonaR is an R package implementing consonance-based algorithms for algorithmic composition, based on William Sethares' _Tuning, Timbre, Spectrum, Scale_. It provides functions for musical scale calculations including frequency/MIDI conversion, cents/ratio math, and Scala `.scl` file parsing. |
| 6 | + |
| 7 | +## Build Commands |
| 8 | + |
| 9 | +```bash |
| 10 | +# Install dependencies and load package in development mode |
| 11 | +devtools::load_all() |
| 12 | + |
| 13 | +# Check package (linting, documentation, etc.) |
| 14 | +R CMD check |
| 15 | + |
| 16 | +# Run specific checks only: |
| 17 | +devtools::check() # Full check |
| 18 | +devtools::document() # Generate documentation from Roxygen2 |
| 19 | +devtools::spell_check() # Spelling |
| 20 | +devtools::test() # Tests (none currently) |
| 21 | +packageDependencies::pbd_report() # Check package dependencies |
| 22 | + |
| 23 | +# Build vignettes |
| 24 | +Rscript -e "devtools::build_vignettes()" |
| 25 | + |
| 26 | +# Build complete package |
| 27 | +R CMD build . |
| 28 | + |
| 29 | +# Install from source |
| 30 | +R CMD INSTALL . |
| 31 | +``` |
| 32 | + |
| 33 | +## Linting and Style |
| 34 | + |
| 35 | +```bash |
| 36 | +# Run styler for automatic code formatting |
| 37 | +styler::style_pkg() |
| 38 | + |
| 39 | +# Use lintr for linting issues (no standard lint command exists yet) |
| 40 | +lintr::lint_package() |
| 41 | +``` |
| 42 | + |
| 43 | +## Tests |
| 44 | + |
| 45 | +No tests currently exist. When adding tests, use the `testthat` framework: |
| 46 | + |
| 47 | +```bash |
| 48 | +# Create test file structure |
| 49 | +devtools::use_testthat() |
| 50 | + |
| 51 | +# Run all tests |
| 52 | +devtools::test() |
| 53 | + |
| 54 | +# Run a single test file |
| 55 | +testthat::test_file("tests/testthat/test-scales.R") |
| 56 | + |
| 57 | +# Run a single test |
| 58 | +testthat::test_local(filter = "test_name") |
| 59 | +``` |
| 60 | + |
| 61 | +## Code Style Guidelines |
| 62 | + |
| 63 | +### Imports and Dependencies |
| 64 | + |
| 65 | +- Use `@importFrom` in Roxygen2 comments for all external function imports |
| 66 | +- Group related imports: data.table operations, fractional package, numbers package |
| 67 | +- Never use `importFrom(utils, globalVariables)` except to declare `.I`, `:=`, etc. from data.table |
| 68 | +- Example: |
| 69 | + ```r |
| 70 | + #' @importFrom data.table data.table setkey shift ":=" ".I" |
| 71 | + ``` |
| 72 | + |
| 73 | +### Function Documentation (Roxygen2) |
| 74 | + |
| 75 | +All exported functions require complete Roxygen2 documentation with: |
| 76 | +- `@title` - Brief function name/description |
| 77 | +- `@name` - Function name |
| 78 | +- `@description` - Detailed description in markdown |
| 79 | +- `@param` - Each parameter with type and description |
| 80 | +- `@returns` - Return value type and meaning |
| 81 | +- `@examples` - At least one runnable example (must work without modification) |
| 82 | +- `@export` - For all exported functions |
| 83 | +- Use `\itemize{}` for lists in documentation |
| 84 | +- Use `\insertCite{key}{consonaR}` with Rdpack for citations from inst/REFERENCES.bib |
| 85 | + |
| 86 | +### Type Safety and Parameter Handling |
| 87 | + |
| 88 | +- Explicitly type all parameters as `numeric`, `character`, or `list` |
| 89 | +- Use default parameter values liberally when sensible |
| 90 | +- Always return explicit values; avoid implicit returns where possible |
| 91 | +- For file I/O, set `warn = 2` to treat warnings as errors during file operations |
| 92 | +- Check for try-error results from file operations: |
| 93 | + ```r |
| 94 | + result <- try(file(path, open = "rt"), silent = TRUE) |
| 95 | + if (inherits(result, "try-error")) { |
| 96 | + return(list(status = paste0("error message"))) |
| 97 | + } |
| 98 | + ``` |
| 99 | + |
| 100 | +### Error Handling |
| 101 | + |
| 102 | +- Return named lists with `status` field for error conditions: |
| 103 | + - `"Oll Korrect"` for success |
| 104 | + - Error messages as character strings otherwise |
| 105 | +- Include relevant data in errors (e.g., `file_contents` on parse failures) |
| 106 | +- Validate input early and return status before proceeding |
| 107 | + |
| 108 | +### Naming Conventions |
| 109 | + |
| 110 | +- Function names: snake_case with descriptive, self-documenting names |
| 111 | + - Conversion functions use `X2Y` pattern: `freq2MIDI`, `cents2ratio`, `ratio2factors` |
| 112 | + - Table-generating functions end in `_scale_table`: `et_scale_table`, `sclfile_scale_table`, `prodset_scale_table` |
| 113 | +- Variable names: snake_case, descriptive (e.g., `degree`, `ratio_cents`, `file_contents`) |
| 114 | +- Use consistent parameter naming: `root_freq`, `period`, `divisions`, `sclfile_path`, `freq1`, `freq2` |
| 115 | +- Numeric constants use decimal points for float types: `2.0`, `B1 <- -3.5` |
| 116 | + |
| 117 | +### Data Processing with data.table |
| 118 | + |
| 119 | +- Create tables with named columns immediately |
| 120 | +- Use backticks for special operators: `` `:=`() ``, `".I"`, `"shift"` |
| 121 | +- Set keys after table creation: `data.table::setkey(table, column)` |
| 122 | +- Append computed columns using shift operations and `:=`: |
| 123 | + ```r |
| 124 | + result <- result[, `:=`(new_col = col - data.table::shift(col))] |
| 125 | + ``` |
| 126 | + |
| 127 | +### Vectorized Operations |
| 128 | + |
| 129 | +- Use `vapply()` over `lapply()` when return type is known (numeric vectors) |
| 130 | +- Prefer `sapply()`, `mapply()` for simple vectorization |
| 131 | +- Use `lapply()` for complex list-processing tasks |
| 132 | + |
| 133 | +### Mathematical Conventions |
| 134 | + |
| 135 | +- Always use explicit decimal notation in math: `2 ^ (cents / 1200)` not `2^(cents/1200)` |
| 136 | +- Define algorithm constants locally with uppercase names: `B1`, `X_STAR`, `S1` |
| 137 | +- Return numeric values for all mathematical functions |
| 138 | + |
| 139 | +### Code Layout |
| 140 | + |
| 141 | +- Place empty lines between logical sections of a function |
| 142 | +- Keep lines under 80 characters when readable |
| 143 | +- Use parentheses around return expressions consistently: `return(expression)` |
| 144 | +- Align continuation lines with meaningful indentation |
| 145 | + |
| 146 | +### File I/O Best Practices |
| 147 | + |
| 148 | +- Store test files in `inst/test_scl_files/` |
| 149 | +- Access package files via `system.file(...)` never using absolute paths |
| 150 | +- Close file connections after use (`close(connection)`) |
| 151 | +- Handle three error types: file not found, insufficient content, invalid format |
| 152 | + |
| 153 | +### Example Documentation |
| 154 | + |
| 155 | +```r |
| 156 | +#' @examples |
| 157 | +#' |
| 158 | +#' # Standard example |
| 159 | +#' print(result <- et_scale_table()) |
| 160 | +#' |
| 161 | +#' # With conditional output based on status |
| 162 | +#' result <- sclfile_scale_table(system.file( |
| 163 | +#' "test_scl_files/example.scl", |
| 164 | +#' package = "consonaR" |
| 165 | +#' )) |
| 166 | +#' if (result$status == "Oll Korrect") { |
| 167 | +#' print(result$scale_table) |
| 168 | +#' } else { |
| 169 | +#' print(result$status) |
| 170 | +#' } |
| 171 | +``` |
| 172 | + |
| 173 | +## Package Structure |
| 174 | + |
| 175 | +``` |
| 176 | +R/ |
| 177 | + scales.R # Main source file - all exported functions |
| 178 | +man/ # Generated .Rd documentation files |
| 179 | +vignettes/ # Rmarkdown vignettes |
| 180 | +docs/ # pkgdown-generated website output |
| 181 | +inst/ |
| 182 | + REFERENCES.bib # Citation database for Rdpack |
| 183 | + test_scl_files/ # Sample Scala scale files for examples |
| 184 | +``` |
| 185 | + |
| 186 | +## Vignettes |
| 187 | + |
| 188 | +All vignettes use knitr with markdown: |
| 189 | +- Set `knitr::opts_chunk$set(echo = TRUE)` in setup |
| 190 | +- Include `bibliography` for citations from inst/REFERENCES.bib |
| 191 | +- Use parameterized code chunks consistent with source |
0 commit comments