/*
 * --- GSMP-COPYRIGHT-NOTE-BEGIN ---
 * 
 * This copyright note is auto-generated by ./scripts/Create-CopyPatch.
 * Please add additional copyright information _after_ the line containing
 * the GSMP-COPYRIGHT-NOTE-END tag. Otherwise it might get removed by
 * the ./scripts/Create-CopyPatch script. Do not edit this copyright text!
 * 
 * GSMP: pcm/tests/TestTone.cc
 * General Sound Manipulation Program is Copyright (C) 2000 - 2004
 *   Valentin Ziegler and René Rebe
 * 
 * This program is free software; you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation; version 2. A copy of the GNU General
 * Public License can be found in the file LICENSE.
 * 
 * This program is distributed in the hope that it will be useful, but
 * WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANT-
 * ABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General
 * Public License for more details.
 * 
 * --- GSMP-COPYRIGHT-NOTE-END ---
 */

#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>

// e.g. write
#include <unistd.h>

#include <math.h>

#include <iostream>

#include "Types.hh"

using namespace GSMP;

int main ()
{
  const channel_index channels = 2;
  const sample_rate rate = 44100;
  const int length = 10; // seconds
  
  int16_t* pcm_data = new int16_t [ channels * rate * length ];
  int16_t* pcm_data_ptr = pcm_data;

  const double f1 = 440.0;
  const double f2 = 8800.0;
  
  double f1d = f1 / (double) (rate);
  const double f2d = f2 / (double) (rate);
  
  const double fdd = (f2d - f1d) / (rate * length);
  
  
  std::cout << "f1d: " << f1d << std::endl;
  std::cout << "fdd: " << fdd << std::endl;
  
  double fx = 0;
  
  for (local_index i = 0; i < rate * length; ++i) {
    double x = sin ( 2.0 * M_PI * fx );
    
    fx += f1d;
    f1d += fdd;

    if (fx >= 1.0)
      fx -= 1.0;
    
    *(pcm_data_ptr++) = PcmConvert<double, int16_t>::Convert (x);
    *(pcm_data_ptr++) = PcmConvert<double, int16_t>::Convert (x);
    
  }
  
  int fd = open ("test.raw", O_RDWR | O_CREAT | O_TRUNC, 0664);
  if (fd < 0)
    std::cout << "Error creating 'tset.raw'!" << std::endl;
  else
    {
      write (fd, (char*) pcm_data,
	     channels * rate * length * (PcmTraits<int16_t>::Bits / 8) );
      close (fd);
    }
  
  
  delete [] pcm_data;
}
