繁体   English   中英

如何让我的节拍器在我的节目录制的同时播放?

[英]How to make my metronome play at the same time as recording in my program?

该程序旨在能够记录和存储用户输入的旋律,然后播放用户录制的序列。 我正在尝试添加节拍器,以便在录音时您可以听到有节奏的脉搏。

我已经成功地制作了音调,并使函数与 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", &note[0], &velocity);
        fscanf(file, "%d, %d\n", &note[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;
}

下面是一些创建线程的代码:

#include <pthread.h>

void* metronome(void* param);

int playMetronome;

int main(){
    pthread_t tid1;

    playMetronome = 1;
    pthread_create(&tid1,NULL,metronome,0);

    //the code to record and play something

    playMetronome = 0; //the loop in metronome() stops at this point


    return 0;
}

void* metronome(void* param){
    while(playMetronome){
        //code to play metronome
        //this code will run simultaneous with the code to play melodies
   }

}

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM