summaryrefslogtreecommitdiff
path: root/wk4/pset/volume/volume.c
diff options
context:
space:
mode:
authorFudgerboy <91767657+Fudgerboy@users.noreply.github.com>2024-04-04 06:44:49 +0000
committerFudgerboy <91767657+Fudgerboy@users.noreply.github.com>2024-04-04 06:44:49 +0000
commitab3fca1ae25c2ea2a95a8d93a1fc3e8bef0d0e69 (patch)
tree1e4beeb0d8163871786b0c5d2879f3f5f6e65cb9 /wk4/pset/volume/volume.c
parent1295585ad4f11c58deeba8888678ccabc40bc695 (diff)
Wed, Apr 3, 2024, 11:44 PM -07:00
Diffstat (limited to 'wk4/pset/volume/volume.c')
-rw-r--r--wk4/pset/volume/volume.c53
1 files changed, 53 insertions, 0 deletions
diff --git a/wk4/pset/volume/volume.c b/wk4/pset/volume/volume.c
new file mode 100644
index 0000000..0cde962
--- /dev/null
+++ b/wk4/pset/volume/volume.c
@@ -0,0 +1,53 @@
+// Modifies the volume of an audio file
+
+#include <stdint.h>
+#include <stdio.h>
+#include <stdlib.h>
+
+// Number of bytes in .wav header
+const int HEADER_SIZE = 44;
+
+int main(int argc, char *argv[])
+{
+ // Check command-line arguments
+ if (argc != 4)
+ {
+ printf("Usage: ./volume input.wav output.wav factor\n");
+ return 1;
+ }
+
+ // Open files and determine scaling factor
+ FILE *input = fopen(argv[1], "r");
+ if (input == NULL)
+ {
+ printf("Could not open file.\n");
+ return 1;
+ }
+
+ FILE *output = fopen(argv[2], "w");
+ if (output == NULL)
+ {
+ printf("Could not open file.\n");
+ return 1;
+ }
+
+ float factor = atof(argv[3]);
+
+ // TODO: Copy header from input file to output file
+ uint8_t header[HEADER_SIZE];
+ fread(header, HEADER_SIZE, 1, input);
+ fwrite(header, HEADER_SIZE, 1, output);
+
+ // TODO: Read samples from input file and write updated data to output file
+ int16_t buffer;
+
+ while (fread(&buffer, sizeof(int16_t), 1, input))
+ {
+ buffer *= factor;
+ fwrite(&buffer, sizeof(uint16_t), 1, output);
+ }
+
+ // Close files
+ fclose(input);
+ fclose(output);
+}