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
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
|
/*
* audio.cpp: A program for the Video Disk Recorder
*
* See the README file for copyright information and how to reach the author.
*
* $Id$
*/
#include "audio.h"
cMarkAdAudio::cMarkAdAudio(MarkAdContext *maContext)
{
macontext=maContext;
mark.Comment=NULL;
mark.Position=0;
mark.Type=0;
channels=0;
#if 0
lastiframe_gain=-ANALYZEFRAMES;
#endif
lastiframe_silence=-1;
}
cMarkAdAudio::~cMarkAdAudio()
{
ResetMark();
}
void cMarkAdAudio::ResetMark()
{
if (mark.Comment) free(mark.Comment);
mark.Comment=NULL;
mark.Position=0;
mark.Type=0;
}
bool cMarkAdAudio::AddMark(int Type, int Position, const char *Comment)
{
if (!Comment) return false;
if (mark.Comment)
{
int oldlen=strlen(mark.Comment);
mark.Comment=(char *) realloc(mark.Comment,oldlen+10+strlen(Comment));
if (!mark.Comment)
{
mark.Position=0;
return false;
}
strcat(mark.Comment," [");
strcat(mark.Comment,Comment);
strcat(mark.Comment,"]");
}
else
{
mark.Comment=strdup(Comment);
}
mark.Position=Position;
mark.Type=Type;
return true;
}
bool cMarkAdAudio::SilenceDetection()
{
if (!macontext->Audio.Data.Valid) return false;
if (lastiframe_silence==lastiframe) return false; // we already detected silence for this frame
int samples=macontext->Audio.Data.SampleBufLen/
sizeof(*macontext->Audio.Data.SampleBuf)/
macontext->Audio.Info.Channels;
short left,right;
int lowvalcount=0;
for (int i=0; i<samples; i++)
{
left=macontext->Audio.Data.SampleBuf[0+(i*2)];
right=macontext->Audio.Data.SampleBuf[1+(i*2)];
if ((abs(left)+abs(right))<CUT_VAL)
{
lowvalcount++;
if (lowvalcount>MIN_LOWVALS)
{
lastiframe_silence=lastiframe;
return true;
}
}
else
{
lowvalcount=0;
}
}
return false;
}
#if 0
bool cMarkAdAudio::AnalyzeGain()
{
if (!macontext->Audio.Data.Valid) return false;
int samples=macontext->Audio.Data.SampleBufLen/
sizeof(*macontext->Audio.Data.SampleBuf)/
macontext->Audio.Info.Channels;
double left[samples];
double right[samples];
for (int i=0; i<samples; i++)
{
left[i]=macontext->Audio.Data.SampleBuf[0+(i*2)];
right[i]=macontext->Audio.Data.SampleBuf[1+(i*2)];
}
if ((lastiframe-lastiframe_gain)>ANALYZEFRAMES)
{
if (lastiframe_gain>0)
{
double dgain,gain = audiogain.GetGain();
dgain=gain-lastgain;
printf("%05i %+.2f db %+.2f db\n",lastiframe_gain,gain,lastgain);
lastgain=gain;
}
audiogain.Init(macontext->Audio.Info.SampleRate);
lastiframe_gain=lastiframe;
}
if (audiogain.AnalyzeSamples(left,right,samples,2)!=GAIN_ANALYSIS_OK)
{
lastiframe_gain=-ANALYZEFRAMES;
}
return true;
}
#endif
bool cMarkAdAudio::ChannelChange(int a, int b)
{
if ((a==0) || (b==0)) return false;
if (a!=b) return true;
return false;
}
MarkAdMark *cMarkAdAudio::Process(int LastIFrame)
{
ResetMark();
if (!LastIFrame) return NULL;
lastiframe=LastIFrame;
#if 0
AnalyzeGain();
#endif
if (macontext->Audio.Options.AudioSilenceDetection)
{
if (SilenceDetection())
{
char *buf=NULL;
if (asprintf(&buf,"audio channel silence detecion (%i)",lastiframe)!=-1)
{
AddMark(MT_SILENCECHANGE,lastiframe,buf);
free(buf);
}
}
}
if (ChannelChange(macontext->Audio.Info.Channels,channels))
{
char *buf=NULL;
if (asprintf(&buf,"audio channel change from %i to %i (%i)", channels,
macontext->Audio.Info.Channels,lastiframe)!=-1)
{
if (macontext->Audio.Info.Channels>2)
{
AddMark(MT_CHANNELSTART,lastiframe,buf);
}
else
{
AddMark(MT_CHANNELSTOP,lastiframe,buf);
}
free(buf);
}
}
channels=macontext->Audio.Info.Channels;
return &mark;
}
|