【发布时间】:2015-03-23 15:03:33
【问题描述】:
该程序旨在能够记录和存储用户输入的旋律,然后播放用户录制的序列。我正在尝试添加一个节拍器,以便在录制时可以听到有节奏的脉搏。
我已经成功地制作了音调,并让函数与 int length 函数一起工作。我的问题是我只能让节拍器在录制序列之前或之后播放,这使得节拍器毫无意义,因为你在录制音符时听不到它。
函数包含一个while循环这一事实也阻止了程序继续执行程序的下一部分。我只希望节拍器功能在录音时处于活动状态。
有谁知道我如何让它在录音时播放节拍器,而不是让它在开始录音功能之前或之后播放节拍器?
#include "aservelibs/aservelib.h"
#include <stdio.h>
#include <math.h>
#include <string.h>
#include <stdlib.h>
//------------------------------------------------function declarations
float mtof(int note, float frequency);
FILE play(void);
FILE record(void);
FILE record2(void);
int length();
void metronome(void);
//------------------------------------------------main program
int main()
{
//------------------------------------------------variables
FILE *textFilePointer;
FILE *textFilePointer2;
int counter = 0;
char user;
//------------------------------------------------main menu
do
{
printf("Press A to Record 1st Melody (A), B to Record 2nd Melody (B)\nP to Play Melodies (P) X to Exit (X):");
scanf(" %c", &user);
//------------------------------------------------record 1st melody
if (user == 'a' || user == 'A')
{
textFilePointer = fopen("/Users/Luke/Desktop/midinotes1.txt", "w");
*textFilePointer = record();
metronome();
if(textFilePointer == NULL);
{
printf("Recording Complete\n");
aserveOscillator(3, 0, 0, 0);
}
counter = 0;
}
//------------------------------------------------record 2nd melody
else if (user == 'b' || user == 'B')
{
textFilePointer2 = fopen("/Users/Luke/Desktop/midinotes2.txt", "w");
*textFilePointer2 = record2();
metronome();
if(textFilePointer == NULL);
{
printf("Recording Complete\n");
aserveOscillator(3, 0, 0, 0);
}
counter = 0;
}
//------------------------------------------------plays the melodies back
else if (user == 'p' || user == 'P')
{
textFilePointer = fopen("/Users/Luke/Desktop/midinotes1.txt", "r");
*textFilePointer = play();
textFilePointer2 = fopen("/Users/Luke/Desktop/midinotes2.txt", "r");
*textFilePointer2 = play();
if(textFilePointer == NULL);
{
printf("Playback Complete\n");
aserveOscillator(0, 0, 0, 0);
aserveOscillator(1, 0, 0, 0);
}
counter = 0;
}
//-------------------------------------------------exits program
else if (user == 'x' || user == 'X')
{
exit(0);
}
}
while(counter < 16);
}
//--------------------------------------------------function declarations
//--------------------------------------------------converts MIDI number to frequency
float mtof(int note, float frequency)
{
frequency = 440.0 * pow(2, (note-69) / 12.0);
printf("Playing Note:%d\n", note);
return frequency;
}
//--------------------------------------------------changes tempo of sequence playback
int length()
{
return (aserveGetControl(7)/((127.0 - 0) / (1000 - 100))) + 100;
}
//--------------------------------------------------metronome function
void metronome(void)
{
while(true)
{
aserveOscillator(3, 1500, 0.8, 0);
aserveOscillator(3, 0, 0, 0);
aserveSleep(length()*2);
aserveOscillator(3, 0, 0, 0);
aserveOscillator(3, 1000, 0.8, 0);
aserveOscillator(3, 0, 0, 0);
aserveSleep(length()*2);
aserveOscillator(3, 0, 0, 0);
aserveOscillator(3, 1000, 0.8, 0);
aserveOscillator(3, 0, 0, 0);
aserveSleep(length()*2);
aserveOscillator(3, 0, 0, 0);
aserveOscillator(3, 1000, 0.8, 0);
aserveOscillator(3, 0, 0, 0);
aserveSleep(length()*2);
aserveOscillator(3, 0, 0, 0);
}
}
//--------------------------------------------------playback function
FILE play(void)
{
FILE*file;
file = fopen("/Users/Luke/Desktop/midinotes1.txt", "r");
file = fopen("/Users/Luke/Desktop/midinotes2.txt", "r");
do {
int note[2];
int velocity;
float freq[2];
int frequency;
fscanf(file, "%d, %d\n", ¬e[0], &velocity);
fscanf(file, "%d, %d\n", ¬e[1], &velocity);
freq[0] = mtof(note[0], frequency);
freq[1] = mtof(note[1], frequency);
aserveOscillator(0, freq[0], 1.0, 0);
aserveOscillator(1, freq[1], 1.0, 0);
aserveSleep(length()*2);
} while (feof(file) == 0);
return *file;
}
//--------------------------------------------------layer 1 record function
FILE record(void)
{
int counter;
FILE*file;
file = fopen("/Users/Luke/Desktop/midinotes1.txt", "w");
do
{
int note = aserveGetNote();
int velocity = aserveGetVelocity();
if (velocity > 0)
{
fprintf(file, "%d, %d\n", note, velocity);
printf("Note: %d, Velocity: %d\n", note, velocity);
counter++;
}
} while (counter < 16);
fclose(file);
return *file;
}
//--------------------------------------------------layer 2 record function
FILE record2(void)
{
int counter;
FILE*file;
file = fopen("/Users/Luke/Desktop/midinotes2.txt", "w");
do
{
int note = aserveGetNote();
int velocity = aserveGetVelocity();
if (velocity > 0)
{
fprintf(file, "%d, %d\n", note, velocity);
printf("Note: %d, Velocity: %d\n", note, velocity);
counter++;
}
} while (counter < 16);
fclose(file);
return *file;
}
【问题讨论】:
-
你应该研究线程这个话题。一个用于录制的线程,另一个用于播放的线程,恕我直言,要走的路。
-
@Henrik 我能在星期四之前解决吗?它的任务。我在网上看了一眼,看起来很混乱..你会说这是唯一的方法吗?感谢您的回复!
-
嘿嘿..好吧..事实上我不会说这是唯一的方法,你可以让record2中的循环通过检查时间定期播放节拍器的声音,但它如果您不想在录音中出现间隙,那么开始工作非常棘手..
标签: c multithreading function