C 使我的函数同时播放两个序列
该程序设计为能够记录和存储用户输入的旋律,然后回放用户录制的序列。我有两个记录函数,其中的函数扫描用户输入,然后将注释编号和速度存储到两个独立的C 使我的函数同时播放两个序列,c,function,C,Function,该程序设计为能够记录和存储用户输入的旋律,然后回放用户录制的序列。我有两个记录函数,其中的函数扫描用户输入,然后将注释编号和速度存储到两个独立的.txt文件中 该程序还具有节拍器功能(函数)和节拍(整数长度)功能 我的程序运行得很好,只是我刚刚意识到它不会同时播放录制的两个序列。事实上,它只播放存储在“midinotes2.txt”中的第二个序列,这让我感到困惑,我似乎无法找到一种方法让两个序列一起播放。有人能看到我的代码可能有什么问题,或者我可以添加什么使其工作 #include "aserv
.txt
文件中
该程序还具有节拍器功能(函数)和节拍(整数长度)功能
我的程序运行得很好,只是我刚刚意识到它不会同时播放录制的两个序列。事实上,它只播放存储在“midinotes2.txt”中的第二个序列,这让我感到困惑,我似乎无法找到一种方法让两个序列一起播放。有人能看到我的代码可能有什么问题,或者我可以添加什么使其工作
#include "aservelibs/aservelib.h"
#include <stdio.h>
#include <math.h>
#include <string.h>
#include <stdlib.h>
#include <pthread.h>
//------------------------------------------------function declarations
float mtof(int note, float frequency);
FILE play(void);
FILE record(void);
FILE record2(void);
int length();
void* metronome(void* param);
int playMetronome;
//------------------------------------------------main program
int main()
{
//------------------------------------------------variables
FILE *textFilePointer;
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')
{
playMetronome = 1;
pthread_t tid1;
pthread_create(&tid1,NULL,metronome,0);
textFilePointer = fopen("/Users/Luke/Desktop/midinotes1.txt", "w");
*textFilePointer = record();
if(textFilePointer == NULL);
{
printf("Recording Complete\n");
playMetronome = 0;
}
counter = 0;
}
//------------------------------------------------record 2nd melody
else if (user == 'b' || user == 'B')
{
playMetronome = 1;
pthread_t tid1;
pthread_create(&tid1,NULL,metronome,0);
textFilePointer = fopen("/Users/Luke/Desktop/midinotes2.txt", "w");
*textFilePointer = record2();
if(textFilePointer == NULL);
{
printf("Recording Complete\n");
playMetronome = 0;
}
counter = 0;
}
//------------------------------------------------plays the melodies back
else if (user == 'p' || user == 'P')
{
textFilePointer = fopen("/Users/Luke/Desktop/midinotes1.txt", "r");
*textFilePointer = play();
textFilePointer = fopen("/Users/Luke/Desktop/midinotes2.txt", "r");
*textFilePointer = 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* param)
{
while(playMetronome)
{
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);
}
return 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;
}
#包括“aservelibs/aservelib.h”
#包括
#包括
#包括
#包括
#包括
//------------------------------------------------函数声明
浮动mtof(整数注释,浮动频率);
文件播放(无效);
档案记录(作废);
档案记录2(作废);
int length();
无效*节拍器(无效*参数);
int播放节拍器;
//------------------------------------------------主程序
int main()
{
//------------------------------------------------变数
文件*textFilePointer;
int计数器=0;
字符用户;
//------------------------------------------------主菜单
做
{
printf(“按A录制第一首旋律(A),按B录制第二首旋律(B)\nP播放旋律(P)X退出(X):”;
scanf(“%c”和“用户”);
//------------------------------------------------录制第一首旋律
如果(用户==“a”| |用户==“a”)
{
播放节拍器=1;
pthread_t tid1;
pthread_create(&tid1,NULL,metronome,0);
textFilePointer=fopen(“/Users/Luke/Desktop/midinotes1.txt”,“w”);
*textFilePointer=record();
if(textFilePointer==NULL);
{
printf(“录制完成\n”);
播放节拍器=0;
}
计数器=0;
}
//------------------------------------------------录制第二旋律
else if(user='b'| | user=='b')
{
播放节拍器=1;
pthread_t tid1;
pthread_create(&tid1,NULL,metronome,0);
textFilePointer=fopen(“/Users/Luke/Desktop/midinotes2.txt”,“w”);
*textFilePointer=record2();
if(textFilePointer==NULL);
{
printf(“录制完成\n”);
播放节拍器=0;
}
计数器=0;
}
//------------------------------------------------把旋律放回去
else if(user='p'| | user='p')
{
textFilePointer=fopen(“/Users/Luke/Desktop/midinotes1.txt”,“r”);
*textFilePointer=play();
textFilePointer=fopen(“/Users/Luke/Desktop/midinotes2.txt”,“r”);
*textFilePointer=play();
if(textFilePointer==NULL);
{
printf(“播放完成\n”);
一个振荡器(0,0,0,0);
一个振荡器(1,0,0,0);
}
计数器=0;
}
//-------------------------------------------------退出程序
else if(user='x'| | user=='x')
{
出口(0);
}
}
而(计数器<16);
}
//--------------------------------------------------函数声明
//--------------------------------------------------将MIDI数字转换为频率
浮动mtof(整数注释,浮动频率)
{
频率=440.0*pow(2,(注-69)/12.0);
printf(“播放注释:%d\n”,注释);
返回频率;
}
//--------------------------------------------------更改序列播放的速度
int-length()
{
返回(aserveGetControl(7)/(127.0-0)/(1000-100))+100;
}
//--------------------------------------------------节拍器功能
无效*节拍器(无效*参数)
{
while(游戏节拍器)
{
阿瑟夫振荡器(3,1500,0.8,0);
a振荡器(3,0,0,0);
AServeSeep(长度()*2);
a振荡器(3,0,0,0);
阿瑟夫振荡器(31000,0.8,0);
a振荡器(3,0,0,0);
AServeSeep(长度()*2);
a振荡器(3,0,0,0);
阿瑟夫振荡器(31000,0.8,0);
a振荡器(3,0,0,0);
AServeSeep(长度()*2);
a振荡器(3,0,0,0);
阿瑟夫振荡器(31000,0.8,0);
a振荡器(3,0,0,0);
AServeSeep(长度()*2);
a振荡器(3,0,0,0);
}
返回0;
}
//--------------------------------------------------回放功能
文件播放(无效)
{
文件*文件;
file=fopen(“/Users/Luke/Desktop/midinotes1.txt”,“r”);
file=fopen(“/Users/Luke/Desktop/midinotes2.txt”,“r”);
做{
国际注释[2];
内速度;
浮动频率[2];
整数频率;
fscanf(文件“%d,%d\n”&注释[0],&velocity);
fscanf(文件“%d,%d\n”、&注释[1]、&速度);
频率[0]=mtof(注[0],频率);
频率[1]=mtof(注[1],频率);
阿瑟夫振荡器(0,频率[0],1.0,0);
一个振荡器(1,频率[1],1.0,0);
AServeSeep(长度()*2);
}而(feof(file)==0);
返回*文件;
}
//--------------------------------------------------第1层记录功能
文件记录(作废)
{
整数计数器;
文件*文件;
file=fopen(“/Users/Luke/Desktop/midinotes1.txt”,“w”);
做
{
int note=aserveGetNote();
int-velocity=aserveGetVelocity();
如果(速度>0)
{
fprintf(文件,“%d,%d\n”,注释,速度);
printf(“注释:%d,速度:%d\n”,注释,速度);
计数器++;
}
}而(计数器<16);
fclose(文件);
返回*文件;
}
//--------------------------------------------------第二层记录功能
文件记录2(作废)
{
在里面
file = fopen("/Users/Luke/Desktop/midinotes1.txt", "r");
file = fopen("/Users/Luke/Desktop/midinotes2.txt", "r");