1 // license:BSD-3-Clause
2 // copyright-holders:Nathan Woods
3 /***************************************************************************
4 
5     wave.c
6 
7     Code that interfaces
8     Functions to handle loading, creation, recording and playback
9     of wave samples for IO_CASSETTE
10 
11     2010-06-19 - Found that since 0.132, the right channel is badly out of
12     sync on a mono system, causing bad sound. Added code to disable
13     the second channel on a mono system.
14 
15 
16 ****************************************************************************/
17 
18 #include "emu.h"
19 #include "wave.h"
20 
21 #include "speaker.h"
22 
23 
24 #define ALWAYS_PLAY_SOUND   0
25 
26 
27 
28 DEFINE_DEVICE_TYPE(WAVE, wave_device, "wave", "Cassette Sound")
29 
wave_device(const machine_config & mconfig,const char * tag,device_t * owner,uint32_t clock)30 wave_device::wave_device(const machine_config &mconfig, const char *tag, device_t *owner, uint32_t clock)
31 	: device_t(mconfig, WAVE, tag, owner, clock)
32 	, device_sound_interface(mconfig, *this)
33 	, m_cass(*this, finder_base::DUMMY_TAG)
34 {
35 }
36 
37 //-------------------------------------------------
38 //  device_start - device-specific startup
39 //-------------------------------------------------
40 
device_start()41 void wave_device::device_start()
42 {
43 	stream_alloc(0, 2, machine().sample_rate());
44 }
45 
46 //-------------------------------------------------
47 //  sound_stream_update - handle a stream update
48 //-------------------------------------------------
49 
sound_stream_update(sound_stream & stream,std::vector<read_stream_view> const & inputs,std::vector<write_stream_view> & outputs)50 void wave_device::sound_stream_update(sound_stream &stream, std::vector<read_stream_view> const &inputs, std::vector<write_stream_view> &outputs)
51 {
52 	cassette_state state = m_cass->get_state() & (CASSETTE_MASK_UISTATE | CASSETTE_MASK_MOTOR | CASSETTE_MASK_SPEAKER);
53 
54 	if (m_cass->exists() && (ALWAYS_PLAY_SOUND || (state == (CASSETTE_PLAY | CASSETTE_MOTOR_ENABLED | CASSETTE_SPEAKER_ENABLED))))
55 	{
56 		cassette_image *cassette = m_cass->get_image();
57 		double time_index = m_cass->get_position();
58 		double duration = double(outputs[0].samples()) / outputs[0].sample_rate();
59 
60 		if (m_sample_buf.size() < outputs[0].samples())
61 			m_sample_buf.resize(outputs[0].samples());
62 
63 		for (int ch = 0; ch < 2; ch++)
64 		{
65 			cassette->get_samples(ch, time_index, duration, outputs[ch].samples(), 2, &m_sample_buf[0], cassette_image::WAVEFORM_16BIT);
66 			for (int sampindex = 0; sampindex < outputs[0].samples(); sampindex++)
67 				outputs[ch].put_int(sampindex, m_sample_buf[sampindex], 32768);
68 		}
69 	}
70 	else
71 	{
72 		outputs[0].fill(0);
73 		outputs[1].fill(0);
74 	}
75 }
76