⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 fplay6.c

📁 用于TM1300/PNX1300系列DSP(主要用于视频处理)的外部设备的源码
💻 C
字号:
/*
 * Copyright (c) 1995-2000 by TriMedia Technologies .
 *
 * +------------------------------------------------------------------+
 * | This software is furnished under a license and may only be used  |
 * | and copied in accordance with the terms and conditions of  such  |
 * | a license and with the inclusion of this copyright notice. This  |
 * | software or any other copies of this software may not be provided|
 * | or otherwise made available to any other person.  The ownership  |
 * | and title of this software is not transferred.                   |
 * |                                                                  |
 * | The information in this software is subject  to change without   |
 * | any  prior notice and should not be construed as a commitment by |
 * | TriMedia Technologies .                                          |
 * |                                                                  |
 * | this code and information is provided "as is" without any        |
 * | warranty of any kind, either expressed or implied, including but |
 * | not limited to the implied warranties of merchantability and/or  |
 * | fitness for any particular purpose.                              |
 * +------------------------------------------------------------------+
 *
 *  Module name              : fplay6.c    1.21
 *
 *  Last update              : 17:14:14 - 00/11/09
 */
#include <tmlib/dprintf.h>      /* for debugging with DP(()) */
#include <stdio.h>

#include <tmlib/AppModel.h>
#include <ops/custom_defs.h>
#include <tm1/tmLibdevErr.h>
#include <tm1/tmAO.h>

#include <tm1/tmHelp.h>

#define  MAX_SAMPLE_SIZE  1000000    /* size in 32 bit words */
static int *samples;
static int  sample_pos = 0;
static int  sample_bytes = 0;

#define  BUF_SIZE  768
static int  buf1[BUF_SIZE + 16];
static int  buf2[BUF_SIZE + 16];
static int *pbuf1;
static int *pbuf2;

static void play6(char *waveFile, float srate);
static void play6ISR(void);

static tmLibdevErr_t err;
#define  LIBDEV(x)  err = (x); if (err != TMLIBDEV_OK) { printf("error 0x%08x: file %s, line %d.\n", err, __FILE__, __LINE__); exit(-1); }


main(int argc, char **argv)
{
    char        fileName[80];

    DPsize(1024 * 1024);
    DP((">entering six channel audio file player program, V1.0\n"));
    printf("fplay: six channel audio file player program, V1.0\n");

    DP(("Running on ")); tmHelpReportSystem(Null);
    printf("Running on "); tmHelpReportSystem(stdout);
    
    sprintf(fileName, "g.bin");    /* default */
    if (argc > 1)
        strncpy(fileName, argv[1], 80);
    play6(fileName, 44100.0);
}

static void 
play6(char *waveFile, float srate)
{
    aoInstanceSetup_t ao;
    FILE       *fp;
    Int         instance, i;
    char        ins[80];

    samples = (int *) malloc(MAX_SAMPLE_SIZE * 4);
    if (!samples) {
        printf("FATAL ERROR:  Error getting sample memory\n");
        exit(1);
    }

    printf("loading sound file %s...\n", waveFile);
    fp = fopen(waveFile, "rb");
    if (!fp) {
        printf("FATAL ERROR:  Failed to open sound file.\n");
        exit(2);
    }
    sample_bytes = fread(samples, 1, MAX_SAMPLE_SIZE, fp);
    printf("sample size is %d bytes.\n", sample_bytes);
    fclose(fp);

    pbuf1 = (int *) (((unsigned long) buf1 + 63) & ~63U);
    pbuf2 = (int *) (((unsigned long) buf2 + 63) & ~63U);

    memset(pbuf1, 0, BUF_SIZE * 4);
    _cache_copyback(pbuf1, BUF_SIZE * 4);
    memset(pbuf2, 0, BUF_SIZE * 4);
    _cache_copyback(pbuf2, BUF_SIZE * 4);

    ao.isr                  = play6ISR;
    ao.interruptPriority    = intPRIO_3;
    ao.audioTypeFormat      = atfLinearPCM;
    ao.audioSubtypeFormat   = apfFiveDotOne16;
    ao.sRate                = srate;
    ao.size                 = BUF_SIZE / 3;
    ao.base1                = pbuf1;
    ao.base2                = pbuf2;
    ao.underrunEnable       = True;
    ao.hbeEnable            = True;
    ao.buf1emptyEnable      = True;
    ao.buf2emptyEnable      = True;
    ao.output               = aaaNone;

    LIBDEV(aoOpen(&instance));
    LIBDEV(aoInstanceSetup(instance, &ao));

    /*
     * the expected data file is little endian, so force AO to be little
     * endian always (default endianess matches TriMedia CPU endianness).
     * 
     */
    aoEnableLITTLE_ENDIAN();

    LIBDEV(aoStart(instance));

    printf("wave file playing through six channel output:  "
           "Press return to stop.\n");
    gets(ins);

    LIBDEV(aoStop(instance));
    LIBDEV(aoClose(instance));

    exit(0);
}

static void 
_play6ISR(void)
{
    Int         i;
    UInt        stat = MMIO(AO_STATUS);

    if (aoUNDERRUN(stat)) {
        aoAckACK_UDR();
        DP((" UDR "));
    }
    if (aoHBE(stat)) {
        aoAckACK_HBE();
        DP((" HBE "));
    }
    if (aoBUF2_EMPTY(stat)) {
        for (i = 0; i < BUF_SIZE; i += 3) {
            pbuf2[i] = pbuf2[i + 1] = pbuf2[i + 2] = samples[sample_pos];
            if (sample_pos++ >= (sample_bytes >> 2)) {
                DP(("play6ISR 2"));
                sample_pos = 0;
            }
        }
        _cache_copyback(pbuf2, BUF_SIZE * 4);
        aoAckACK2();
    }
    if (aoBUF1_EMPTY(stat)) {
        for (i = 0; i < BUF_SIZE; i += 3) {
            pbuf1[i] = pbuf1[i + 1] = pbuf1[i + 2] = samples[sample_pos];
            if (sample_pos++ >= (sample_bytes >> 2)) {
                DP(("play6ISR 1"));
                sample_pos = 0;
            }
        }
        _cache_copyback(pbuf1, BUF_SIZE * 4);
        aoAckACK1();
    }
}

/* make the ISR run on the system stack, not the task stack */
static void 
play6ISR(void)
{
#pragma TCS_handler
    AppModel_suspend_scheduling();
    AppModel_run_on_sstack((AppModel_Fun)_play6ISR, Null);
    AppModel_resume_scheduling();
}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -