【发布时间】:2015-03-17 18:45:22
【问题描述】:
我的代码与我的“播放”功能不同。我试图让程序读取由“记录”功能创建的 .txt 文件并播放笔记。
不幸的是,当用户按下 P 来播放他们录制的音符时,它只是不断弹出相同的菜单,而不是前进到程序的下一步。
#include "aservelibs/aservelib.h"
#include <stdio.h>
#include <math.h>
#include <string.h>
float mtof(int note, float frequency);
FILE play(void);
FILE record(void);
FILE record2(void);
int main()
{
FILE *textFilePointer;
FILE *textFilePointer2;
int counter = 0;
char user;
do
{
printf("Press A to Record 1st Melody (A), B to Record 2nd Melody (B)\nP to Play Melodies (P):");
scanf(" %c", &user);
if (user == 'a' || user == 'A')
{
textFilePointer = fopen("/Users/Luke/Desktop/midinotes1.txt", "w");
*textFilePointer = record();
counter = 0;
}
else if (user == 'b' || user == 'B')
{
textFilePointer2 = fopen("/Users/Luke/Desktop/midinotes2.txt", "w");
*textFilePointer2 = record2();
counter = 0;
}
else if (user == 'p' || user == 'P')
{
textFilePointer = fopen("/Users/Luke/Desktop/midinotes1.txt", "r");
textFilePointer2 = fopen("/Users/Luke/Desktop/midinotes2.txt", "r");
counter = 0;
}
}
while(counter < 16);
}
float mtof(int note, float frequency)
{
frequency = 440.0 * pow(2, (note-69) / 12.0);
printf("%d\n", note);
return frequency;
}
FILE play(void)
{
FILE*file;
file = fopen("/Users/Luke/Desktop/midinotes1.txt", "r");
file = fopen("/Users/Luke/Desktop/midinotes1.txt", "r");
do {
int note = aserveGetNote();
int velocity = aserveGetVelocity();
fscanf(file, "%d, %d\n", ¬e, &velocity);
int frequency = mtof(note, frequency);
aserveOscillator(0, frequency, 1.0, 0);
aserveSleep(500);
} while (feof(file) == 0);
fclose(file);
return *file;
}
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);
counter++;
}
} while (counter < 16);
fclose(file);
return *file;
}
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);
counter++;
}
} while (counter < 16);
fclose(file);
return *file;
}
【问题讨论】: