Esempio n. 1
0
int main(int argc, char *argv[]) {
	SID sid;
	sound_open();
	sid.reset();
	if (fcntl(0, F_SETFL, O_NONBLOCK) != 0)
		fatal("fcntl failed: %d", errno);
	int i = 0;
	while (1) {
		int n = read(0, &in_buf[i], sizeof(in_buf) - i);
		if (n < 0) {
			if (errno != EAGAIN && errno != EWOULDBLOCK)
				fatal("read failed: %d", errno);
			sound_emit(sid, SID_CLOCK / LOOP_FREQ);
		} else if (n > 0) {
			n += i;
			for (i = 0; i < n; i += 2) {
				if (n - i >= 2)
					sid.write(in_buf[i], in_buf[i + 1]);
			}
			if (n & 1)
				i = 1;
		} else {
			break;
		}
	}
	sound_close();
}
Esempio n. 2
0
static int sid_snapshot_read_module_simple(snapshot_t *s)
{
    BYTE major_version, minor_version;
    snapshot_module_t *m;
    BYTE tmp[34];

    m = snapshot_module_open(s, snap_module_name_simple,
                             &major_version, &minor_version);
    if (m == NULL) {
        goto fail;
    }

    if (major_version > SNAP_MAJOR_SIMPLE
        || minor_version > SNAP_MINOR_SIMPLE) {
        log_error(LOG_DEFAULT,
                  "SID: Snapshot module version (%d.%d) newer than %d.%d.\n",
                  major_version, minor_version,
                  SNAP_MAJOR_SIMPLE, SNAP_MINOR_SIMPLE);
        snapshot_module_close(m);
        goto fail;
    }

    /* If more than 32 bytes are present then the resource "Sound" and
       "SidEngine" come first! If there is only one byte present, then
       sound is disabled. */
    if (SMR_BA(m, tmp, 34) < 0) {
        if (SMR_BA(m, tmp, 32) < 0) {
            if (SMR_BA(m, tmp, 1) < 0) {
                snapshot_module_close(m);
                goto fail;
            } else {
                sound_close();
            }
        } else {
            memcpy(sid_get_siddata(0), &tmp[0], 32);
        }
    } else {
        int res_sound = (int)(tmp[0]);
        int res_engine = (int)(tmp[1]);

        screenshot_prepare_reopen();
        sound_close();
        screenshot_try_reopen();
        resources_set_int("Sound", res_sound);
        if (res_sound) {
            resources_set_int("SidEngine", res_engine);
            /* FIXME: Only data for first SID read. */
            memcpy(sid_get_siddata(0), &tmp[2], 32);
            sound_open();
        }
    }

    return snapshot_module_close(m);

fail:
    log_error(LOG_DEFAULT, "Failed reading SID snapshot");
    return -1;
}
Esempio n. 3
0
int main()
{
    FILE *fp_file;		/* ファイル書き込み用ファイルポインタ */
    int count;

    /* 音声入力 */
    sound_open();
    fprintf(stderr, ";; Start recording\n");

    count = sound_read((char *) buf, BUFSIZE);
    fprintf(stderr, ";; %d byte are recorded\n", count);

    sound_close();

  /****************************/
    /*  音声ファイルの書き出し  */
  /****************************/

    /* ファイルを書きだし専用でオープンする */
    fp_file = fopen("test.raw", "wb");
    if (fp_file == NULL) {	/* エラー処理 */
	perror("file open failed");
	return 1;
    }

    /* 音声データを書き込む */
    count = fwrite(buf, sizeof(short), BUFSIZE / sizeof(short), fp_file);
    if (count != BUFSIZE / sizeof(short)) {	/* エラー処理 */
	fprintf(stderr,
		"read count = %d, BUFSIZE = %d, sizeof(buf) = %d\n", count,
		BUFSIZE, BUFSIZE / sizeof(short));
    }

    /* ファイルをクローズする */
    fclose(fp_file);


    return 1;
}
Esempio n. 4
0
/*
 * Open a WAV file for reading: returns (WAVFILE *)
 *
 * The opened file is positioned at the first byte of WAV file data, or
 * NULL is returned if the open is unsuccessful.
 */
WAVFILE *WavOpenForRead(const char *Pathname) 
{
	WAVFILE *wfile;
	UInt32 offset;				/* File offset */
	Byte ubuf[4];				/* 4 byte buffer */
	UInt32 dbytes;				/* Data byte count */
						/* wavfile.c values : */
	int channels;				/* Channels recorded in this wav file */
	u_long samplerate;			/* Sampling rate */
	int sample_bits;			/* data bit size (8/12/16) */
	u_long samples;				/* The number of samples in this file */
	u_long datastart;			/* The offset to the wav data */

	if(g_wfile) WavClose(g_wfile); 

	wfile = wavfile_alloc(Pathname);

	if ( wfile == NULL )
		return NULL;			/* Insufficient memory (class B msg) */

	/*
	 * Open the file for reading:
	 */
//	printf("Opening WAV file %p\n", wfile->Pathname);
	if ( (wfile->fd = sound_open(wfile->Pathname,O_RDONLY)) < 0 ) 
	{
		printf("Opening WAV file %p failed\n", wfile->Pathname);
		goto errxit;
	}

	if ( sound_lseek(wfile->fd,0,SEEK_SET) != 0 ) 
	{
		printf("Rewinding WAV file %p\n",wfile->Pathname);
		goto errxit;		/* Wav file must be seekable device */
	}

	if ( WaveReadHeader(wfile->fd,&channels,&samplerate,&sample_bits,&samples,&datastart) != 0 ) 
	{
		printf("Reading WAV header from %p", wfile->Pathname);
		goto errxit;
	}

	/*
	 * Copy WAV data over to WAVFILE struct:
	 */
	if ( channels == 2 )
		wfile->wavinfo.Channels = Stereo;
	else	wfile->wavinfo.Channels = Mono;

	wfile->wavinfo.SamplingRate = (UInt32) samplerate;
	wfile->wavinfo.Samples = (UInt32) samples;
	wfile->wavinfo.DataBits = (UInt16) sample_bits;
	wfile->wavinfo.DataStart = (UInt32) datastart;
        wfile->num_samples = wfile->wavinfo.Samples;

	offset = wfile->wavinfo.DataStart - 4;

	/*
	 * Seek to byte count and read dbytes:
	 */
	if ( sound_lseek(wfile->fd,offset,SEEK_SET) != offset ) 
	{
		printf("Seeking to WAV data in %p",wfile->Pathname);
		goto errxit;			/* Seek failure */
	}

	if ( sound_read(wfile->fd,ubuf,4) != 4 ) 
	{
		printf("Reading dbytes from %s",wfile->Pathname);
		goto errxit;
	}

	/*
	 * Put little endian value into 32 bit value:
	 */
	dbytes = ubuf[3];
	dbytes = (dbytes << 8) | ubuf[2];
	dbytes = (dbytes << 8) | ubuf[1];
	dbytes = (dbytes << 8) | ubuf[0];

	wfile->wavinfo.DataBytes = dbytes;

	/*
	 * Open succeeded:
	 */
	return wfile;				/* Return open descriptor */

	/*
	 * Return error after failed open:
	  WavClose*/
errxit:	
	mem_free(wfile);				/* Dispose of WAVFILE struct */
	return NULL;				/* Return error indication */
}
Esempio n. 5
0
int main(int argc, char *argv[])
{
    int     wpm  = WPM;
    int     freq = FREQ;
    int     rate = RATE;
    int     ampl = AMPL;
    int     atta = ATTA;
    int     deca = DECA;

    /* Get any optional command line args (start with -) */
    while (argc > 1 && *argv[1] == '-') {
        if (!strcmp(argv[1], "-w")) { 
            wpm = atoi(argv[2]); 
        }
        if (!strcmp(argv[1], "-o")) {
            if (!strcmp(argv[2], "alsa"))
                outp = ALSA;
            else if (!strcmp(argv[2], "dsp"))
                outp = DSP;
            else if (!strcmp(argv[2], "alsa"))
                outp = STDOUT;
            else
                outp = 0;
        }
        if (!strcmp(argv[1], "-f")) { 
            freq = atoi(argv[2]); 
        }
        if (!strcmp(argv[1], "-r")) { 
            rate = atoi(argv[2]); 
        }
        if (!strcmp(argv[1], "-a")) { 
            ampl = atoi(argv[2]); 
        }
        if (!strcmp(argv[1], "-da")) { 
            atta = atoi(argv[2]); 
        }
        if (!strcmp(argv[1], "-dd")) { 
            deca = atoi(argv[2]); 
        }
        if (!strcmp(argv[1], "-h")) { 
            fprintf(stderr, usage, MAXTONES);
            return -1;
        }
        if (!strcmp(argv[1], "-v")) { 
            verbose = 1; 
            ++argc;     /* Offset for lack of value */
            --argv;     /* Needs to be last test!   */
        }
        argc -= 2;
        argv += 2;
    }

    /* Make sure we have more following */
    if (!(argc > 1)) {
        fprintf(stderr, "No text given\n");
        return -1;
    }

    /* Sanity check of values */
    if (wpm < MINWPM || wpm > MAXWPM) {
        fprintf(stderr, "Support %d to %d word per minute range\n",
                MINWPM, MAXWPM);
        return -1;
    }
    if (outp < 1) {
        fprintf(stderr, "Output format needs to be alsa, dsp, or stdout\n");
        return -1;
    }
    if (rate < 2 * freq) {
        fprintf(stderr, "Sample rate must be more then twice the frequency\n");
        return -1;
    }
    if (freq < MINFREQ || freq > MAXFREQ) {
        fprintf(stderr, "Support %d to %d Hz frequency range\n",
                MINFREQ, MAXFREQ);
        return -1;
    }
    if (rate < MINRATE || rate > MAXRATE) {
        fprintf(stderr, "Support %d to %d samples per second\n",
                MINRATE, MAXRATE);
        return -1;
    }
    if (ampl < MINAMPL || ampl > MAXAMPL) {
        fprintf(stderr, "Support %d to %d amplitude\n", MINAMPL, MAXAMPL);
        return -1;
    }

    /* Generate waveforms */
    if (mktones(wpm, freq, rate, ampl, atta, deca)) {
        return -1;
    }

    /* Setup DSP device */
    sound_open(outp);
    sound_setup(rate, outp);

    if (verbose) fprintf(stderr, "Morse code: "); 

    /* Disect remaining argv into letters */
    while (argc > 1) {
        text2code(argv[1]);
        argc--;
        argv++;
    }

    if (verbose) fprintf(stderr, "\n"); 

    /* Exit clean */
    free(ditbf);
    free(dahbf);
    free(sgapbf);
    free(lgapbf);
    sound_close(outp);
    return 0;
}