Volume: 課題
予め用意された音楽ファイル(.wav)を入力ファイルとし、そのファイルの音量をどれだけ調整するかを入力し、音量を調節した後のファイルに出力するプログラムを作るのが課題。
https://cs50.harvard.edu/x/2021/labs/4/
Volume: 回答例
// 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];
// There is only one header, so parse it just once
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(int16_t), 1, output);
}
// Close files
fclose(input);
fclose(output);
}
未希 諒
香港在住の34歳サラリーマン。戦略家、写真家、旅人、サウナー。 2025年までに季節を選びながら世界中を飛び回る仕事をするために準備中。その道中で発見した気づきや気になる話題を中心に発信しています。2010年より旅をスタートし通算500箇所の宿に宿泊。年間の3分の1をホテルで暮らす。
Related posts
About media
Ramps Blog は、ノマドによるノマドのためのブログです。日本、香港、シンガポール、タイ、台湾のノマド生活を通じて見えた景色、特に食、カルチャー、テクノロジー、ビジネス、アート、デザインなどについて発信しています。
Recent Posts
Twitter feed is not available at the moment.