summaryrefslogtreecommitdiff
path: root/src/airecord.cc
blob: e340832bed1d445280cd95401e44e6e1bad03776 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
/* -*- Mode: C++; tab-width: 2; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
/***************************************************************************
 *            audioin.h
 *
 *  Fri Apr  8 15:37:21 CEST 2011
 *  Copyright 2011 Bent Bisballe Nyeng
 *  deva@aasimon.org
 ****************************************************************************/

/*
 *  This file is part of libaudioin.
 *
 *  LibAudioIO is free software; you can redistribute it and/or
 *  modify it under the terms of the GNU Lesser General Public
 *  License as published by the Free Software Foundation; either
 *  version 2.1 of the License, or (at your option) any later version.
 *  
 *  LibAudioIO is distributed in the hope that it will be useful,
 *  but WITHOUT ANY WARRANTY; without even the implied warranty of
 *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
 *  Lesser General Public License for more details.
 *  
 *  You should have received a copy of the GNU Lesser General Public
 *  License along with LibAudioIO; if not, write to the Free Software
 *  Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
 */
#include <stdio.h>
#include <stdlib.h>
#include <string.h>

#include "audioin.h"

int main(int argc, char *argv[])
{
  int err = 0;

  if(argc < 4) {
    printf("Usage %s samplerate num_channels output_file\n", argv[0]);
    return 1;
  }

  int samplerate = atoi(argv[1]);
  int channels = atoi(argv[2]);
  char *fname = argv[3];

  struct ai_t *ai = ai_init(&err, "default", "Capture", samplerate, channels);
  if(ai == NULL || err) printf("Error: %d\n", err);

  if(!ai) {
    printf("Trying alternative mixer interface (not running on an InterCom)\n");
    //ai = ai_init(&err, "default", "H/W Multi", samplerate, channels);
    ai = ai_init(&err, "default", "Capture", samplerate, channels);
    if(ai == NULL || err) printf("Error: %d\n", err);
  }

  /** Chip: IDT 92HD83C1C5
Simple mixer control 'Master',0
Simple mixer control 'Headphone',0
Simple mixer control 'Speaker',0
Simple mixer control 'Front',0
Simple mixer control 'Front Mic Jack Mode',0
Simple mixer control 'Line',0
Simple mixer control 'Line Jack Mode',0
Simple mixer control 'Line',1
Simple mixer control 'Mic',0
Simple mixer control 'IEC958',0
Simple mixer control 'IEC958 Default PCM',0
Simple mixer control 'Capture',0
Simple mixer control 'Capture',1
Simple mixer control 'Input Source',0
Simple mixer control 'Input Source',1
Simple mixer control 'Internal Mic',0
   **/

  /** Chip: Realtek ALC662 rev1
Simple mixer control 'Master',0
Simple mixer control 'Headphone',0
Simple mixer control 'Front',0
Simple mixer control 'Front Mic',0
Simple mixer control 'Front Mic Boost',0
Simple mixer control 'Surround',0
Simple mixer control 'Center',0
Simple mixer control 'LFE',0
Simple mixer control 'Line',0
Simple mixer control 'IEC958',0
Simple mixer control 'IEC958 Default PCM',0
Simple mixer control 'Capture',0
Simple mixer control 'Capture',1
Simple mixer control 'Auto-Mute Mode',0
Simple mixer control 'Input Source',0
Simple mixer control 'Input Source',1
Simple mixer control 'Rear Mic',0
Simple mixer control 'Rear Mic Boost',0
   **/
  // 'Capture' record enabled, Input Source: 'Line'




  ai_set_mixer_level(&err, ai, 0, 1.0);
  ai_set_mixer_level(&err, ai, 1, 1.0);
    
  short pcm[4096 * 10];
  FILE *fp = fopen(fname, "w");
  if(!fp) {
    printf("Could not write %s\n", fname);
    return 1;
  }

  for(size_t i = 0; i < 500; i++) {
    memset(pcm, 0, sizeof(pcm));
    int r = ai_read(&err, ai, pcm, sizeof(pcm));
    if(r < 0 || err) printf("Error: %d\n", err);

    // for(int j = 0; j < 20; j++) pcm[j] = 0;

    int bufsz = r / (sizeof(short) * channels) ;
    int offset = 160 / (1024 / bufsz);
    int len = 16;
    short start[] = {
      pcm[ (bufsz - offset - 2) * 2],
      pcm[ (bufsz - offset - 2) * 2 + 1]
    };
    short stop[] = {
      pcm[ (bufsz - offset + len + 1) * 2],
      pcm[ (bufsz - offset + len + 1) * 2 + 1]
    };
    for(int j = 0; j < len; j++) {
      float d = (float)j / (float)len;
      pcm[(bufsz - offset + j - 1) * 2] = start[0] * (1 - d) + stop[0] * d;
      pcm[(bufsz - offset + j - 1) * 2 + 1] = start[1] * (1 - d) + stop[1] * d;
    }
    /*
    short m = 32000;
    pcm[(bufsz - offset) * 2] = m;
    pcm[(bufsz - offset) * 2 + 1] = m;
    pcm[(bufsz - offset + len) * 2] = m;
    pcm[(bufsz - offset + len) * 2 + 1] = m;
    */
    if(fwrite(pcm, r, 1, fp)) {}
  }
  fclose(fp);
  ai_close(&err, ai);

  return 0;
}