为什么我的音频功能一直循环播放而不是一直运行? [英] Why does my audio function keep looping instead of running all the way through?

查看:78
本文介绍了为什么我的音频功能一直循环播放而不是一直运行?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我的代码与我的播放"功能分开工作.我正在尝试使程序读取由记录"功能创建的.txt文件并播放注释.

My code is working apart from my "play" function. I'm trying to make the program read the .txt files created by the "record" function and play the notes back.

不幸的是,当用户按下 P 播放他们录制的音符时,它只会弹出带有相同菜单的菜单,而不是前进到程序的下一步.

Unfortunately, when the user presses P to play back the notes they have recorded, it just keeps popping up with the same menu, instead of advancing to the next step of the program.

#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", &note, &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;
}

推荐答案

我在您的程序中看到的问题

  1. 当用户输入Aa时,您将执行:

 textFilePointer = fopen("/Users/Luke/Desktop/midinotes1.txt", "w");
 *textFilePointer = record();
 counter = 0;

用于记录到"/Users/Luke/Desktop/midinotes1.txt"的代码已经在record()中进行了硬编码.此处无需在同一文件上使用fopen().

The code for recording to "/Users/Luke/Desktop/midinotes1.txt" is already hard coded in record(). There is no need to use fopen() on the same file here.

record()不需要返回FILE.顺便说一句,使用FILE作为返回类型似乎很奇怪.我不认为该标准甚至不支持使用FILE.我已经看到了FILE*作为参数和返回值的使用.

record() doesn't need to return a FILE. As an aside, use of FILE as return type seems strange. I don't think the standard even supports use of FILE. Use of FILE* as an argument and a return value are what I have seen.

record2()也是如此.

当用户输入pP时,您将执行:

When the user enters p or P, you execute:

 textFilePointer = fopen("/Users/Luke/Desktop/midinotes1.txt", "r");
 textFilePointer2 = fopen("/Users/Luke/Desktop/midinotes2.txt", "r");
 counter = 0;

此处没有呼叫play()的电话.可能是您在转录代码以将其发布到此处时迷路了.即使添加一行

There is no call to play() here. Perhaps that got lost while you were transcribing your code to post here. Even if add a line

 play();

那里,这里不需要使用fopen(). play()已经打开文件进行播放.另外,您要多次打开文件,而不使用它们或将其关闭.

there, there is no need to use fopen() here. play() already opens the file(s) for playing. Also, you are opening the file multiple times and not using them or closing them.

您要在main的所有三个if块中将counter设置为0. counter的值何时将成为16以满足do-while循环的退出标准?这是疏忽吗?

You are setting counter to 0 in all the three if blocks in main. When will value of counter be 16 to meet the exit criterion of the do-while loop? Is this an oversight?

play()的实现中,您具有:

file = fopen("/Users/Luke/Desktop/midinotes1.txt", "r");
file = fopen("/Users/Luke/Desktop/midinotes1.txt", "r");

那看起来不对.您在同一文件上两次调用fopen.该函数返回时,您将保留未使用的内容并打开FILE*.

That does not look right. You are calling fopen on the same file twice. You are leaving a unused and open FILE* when the function returns.

第二个文件永远不会播放.

The second file never gets played.

我将建议创建一个辅助功能playFile(),该功能确实需要播放给定文件的内容.然后,从play()两次调用playFile().这是伪代码:

I am going to suggest creation of a helper function, playFile(), which does the needful to play the contents of a given file. Then, call playFile() twice from play(). Here's the pseudo code:

void playFile(char const* file)
{
   FILE* file = fopen(file, "r);

   if ( file == NULL )
   {
      // deal with error.
   }
   else
   {
      // Your code to play the contents.

      // Close the file
      fclose(file);
   }
}

void play()
{
   playFile("/Users/Luke/Desktop/midinotes1.txt");
   playFile("/Users/Luke/Desktop/midinotes2.txt");
}

遵循 DRY原则

Follow the DRY principle

我建议创建一个具有通用代码record()record2()的函数recordToFile(),然后使用正确的参数从record()record2()调用它.

I am going to suggest creating a function recordToFile() that has the common code of record() and record2(), then calling it from record() and record2() with the right argument.

void recordToFile(char const* file)
{
   int counter;
   FILE* file = fopen(file, "w");
   if ( file == NULL )
   {
      // Deal with error
   }
   else
   {
      // Your code to record to the file.

      // Close the file
      fclose(file);
   }
}

void record(void)
{
   recordToFile("/Users/Luke/Desktop/midinotes1.txt");
}

void record2(void)
{
   recordToFile("/Users/Luke/Desktop/midinotes2.txt");
}

这篇关于为什么我的音频功能一直循环播放而不是一直运行?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆